net/mlx5: fix mark action with drop action
[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->drop && parser->mark)
790                 parser->mark = 0;
791         if (!parser->queues_n && !parser->drop) {
792                 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
793                                    NULL, "no valid action");
794                 return -rte_errno;
795         }
796         return 0;
797 exit_action_not_supported:
798         rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
799                            actions, "action not supported");
800         return -rte_errno;
801 }
802
803 /**
804  * Validate items.
805  *
806  * @param priv
807  *   Pointer to private structure.
808  * @param[in] items
809  *   Pattern specification (list terminated by the END pattern item).
810  * @param[out] error
811  *   Perform verbose error reporting if not NULL.
812  * @param[in, out] parser
813  *   Internal parser structure.
814  *
815  * @return
816  *   0 on success, a negative errno value otherwise and rte_errno is set.
817  */
818 static int
819 priv_flow_convert_items_validate(struct priv *priv,
820                                  const struct rte_flow_item items[],
821                                  struct rte_flow_error *error,
822                                  struct mlx5_flow_parse *parser)
823 {
824         const struct mlx5_flow_items *cur_item = mlx5_flow_items;
825         unsigned int i;
826
827         (void)priv;
828         /* Initialise the offsets to start after verbs attribute. */
829         if (parser->drop) {
830                 parser->drop_q.offset = sizeof(struct ibv_flow_attr);
831         } else {
832                 for (i = 0; i != hash_rxq_init_n; ++i)
833                         parser->queue[i].offset = sizeof(struct ibv_flow_attr);
834         }
835         for (; items->type != RTE_FLOW_ITEM_TYPE_END; ++items) {
836                 const struct mlx5_flow_items *token = NULL;
837                 unsigned int n;
838                 int err;
839
840                 if (items->type == RTE_FLOW_ITEM_TYPE_VOID)
841                         continue;
842                 for (i = 0;
843                      cur_item->items &&
844                      cur_item->items[i] != RTE_FLOW_ITEM_TYPE_END;
845                      ++i) {
846                         if (cur_item->items[i] == items->type) {
847                                 token = &mlx5_flow_items[items->type];
848                                 break;
849                         }
850                 }
851                 if (!token)
852                         goto exit_item_not_supported;
853                 cur_item = token;
854                 err = mlx5_flow_item_validate(items,
855                                               (const uint8_t *)cur_item->mask,
856                                               cur_item->mask_sz);
857                 if (err)
858                         goto exit_item_not_supported;
859                 if (items->type == RTE_FLOW_ITEM_TYPE_VXLAN) {
860                         if (parser->inner) {
861                                 rte_flow_error_set(error, ENOTSUP,
862                                                    RTE_FLOW_ERROR_TYPE_ITEM,
863                                                    items,
864                                                    "cannot recognize multiple"
865                                                    " VXLAN encapsulations");
866                                 return -rte_errno;
867                         }
868                         parser->inner = 1;
869                 }
870                 if (parser->drop) {
871                         parser->drop_q.offset += cur_item->dst_sz;
872                 } else if (parser->queues_n == 1) {
873                         parser->queue[HASH_RXQ_ETH].offset += cur_item->dst_sz;
874                 } else {
875                         for (n = 0; n != hash_rxq_init_n; ++n)
876                                 parser->queue[n].offset += cur_item->dst_sz;
877                 }
878         }
879         if (parser->mark) {
880                 for (i = 0; i != hash_rxq_init_n; ++i)
881                         parser->queue[i].offset +=
882                                 sizeof(struct ibv_flow_spec_action_tag);
883         }
884         if (parser->count) {
885                 unsigned int size = sizeof(struct ibv_flow_spec_counter_action);
886
887                 if (parser->drop) {
888                         parser->drop_q.offset += size;
889                 } else {
890                         for (i = 0; i != hash_rxq_init_n; ++i)
891                                 parser->queue[i].offset += size;
892                 }
893         }
894         return 0;
895 exit_item_not_supported:
896         rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
897                            items, "item not supported");
898         return -rte_errno;
899 }
900
901 /**
902  * Allocate memory space to store verbs flow attributes.
903  *
904  * @param priv
905  *   Pointer to private structure.
906  * @param[in] priority
907  *   Flow priority.
908  * @param[in] size
909  *   Amount of byte to allocate.
910  * @param[out] error
911  *   Perform verbose error reporting if not NULL.
912  *
913  * @return
914  *   A verbs flow attribute on success, NULL otherwise.
915  */
916 static struct ibv_flow_attr*
917 priv_flow_convert_allocate(struct priv *priv,
918                            unsigned int priority,
919                            unsigned int size,
920                            struct rte_flow_error *error)
921 {
922         struct ibv_flow_attr *ibv_attr;
923
924         (void)priv;
925         ibv_attr = rte_calloc(__func__, 1, size, 0);
926         if (!ibv_attr) {
927                 rte_flow_error_set(error, ENOMEM,
928                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
929                                    NULL,
930                                    "cannot allocate verbs spec attributes.");
931                 return NULL;
932         }
933         ibv_attr->priority = priority;
934         return ibv_attr;
935 }
936
937 /**
938  * Finalise verbs flow attributes.
939  *
940  * @param priv
941  *   Pointer to private structure.
942  * @param[in, out] parser
943  *   Internal parser structure.
944  */
945 static void
946 priv_flow_convert_finalise(struct priv *priv, struct mlx5_flow_parse *parser)
947 {
948         const unsigned int ipv4 =
949                 hash_rxq_init[parser->layer].ip_version == MLX5_IPV4;
950         const enum hash_rxq_type hmin = ipv4 ? HASH_RXQ_TCPV4 : HASH_RXQ_TCPV6;
951         const enum hash_rxq_type hmax = ipv4 ? HASH_RXQ_IPV4 : HASH_RXQ_IPV6;
952         const enum hash_rxq_type ohmin = ipv4 ? HASH_RXQ_TCPV6 : HASH_RXQ_TCPV4;
953         const enum hash_rxq_type ohmax = ipv4 ? HASH_RXQ_IPV6 : HASH_RXQ_IPV4;
954         const enum hash_rxq_type ip = ipv4 ? HASH_RXQ_IPV4 : HASH_RXQ_IPV6;
955         unsigned int i;
956
957         (void)priv;
958         if (parser->layer == HASH_RXQ_ETH) {
959                 goto fill;
960         } else {
961                 /*
962                  * This layer becomes useless as the pattern define under
963                  * layers.
964                  */
965                 rte_free(parser->queue[HASH_RXQ_ETH].ibv_attr);
966                 parser->queue[HASH_RXQ_ETH].ibv_attr = NULL;
967         }
968         /* Remove opposite kind of layer e.g. IPv6 if the pattern is IPv4. */
969         for (i = ohmin; i != (ohmax + 1); ++i) {
970                 if (!parser->queue[i].ibv_attr)
971                         continue;
972                 rte_free(parser->queue[i].ibv_attr);
973                 parser->queue[i].ibv_attr = NULL;
974         }
975         /* Remove impossible flow according to the RSS configuration. */
976         if (hash_rxq_init[parser->layer].dpdk_rss_hf &
977             parser->rss_conf.rss_hf) {
978                 /* Remove any other flow. */
979                 for (i = hmin; i != (hmax + 1); ++i) {
980                         if ((i == parser->layer) ||
981                              (!parser->queue[i].ibv_attr))
982                                 continue;
983                         rte_free(parser->queue[i].ibv_attr);
984                         parser->queue[i].ibv_attr = NULL;
985                 }
986         } else  if (!parser->queue[ip].ibv_attr) {
987                 /* no RSS possible with the current configuration. */
988                 parser->queues_n = 1;
989                 return;
990         }
991 fill:
992         /*
993          * Fill missing layers in verbs specifications, or compute the correct
994          * offset to allocate the memory space for the attributes and
995          * specifications.
996          */
997         for (i = 0; i != hash_rxq_init_n - 1; ++i) {
998                 union {
999                         struct ibv_flow_spec_ipv4_ext ipv4;
1000                         struct ibv_flow_spec_ipv6 ipv6;
1001                         struct ibv_flow_spec_tcp_udp udp_tcp;
1002                 } specs;
1003                 void *dst;
1004                 uint16_t size;
1005
1006                 if (i == parser->layer)
1007                         continue;
1008                 if (parser->layer == HASH_RXQ_ETH) {
1009                         if (hash_rxq_init[i].ip_version == MLX5_IPV4) {
1010                                 size = sizeof(struct ibv_flow_spec_ipv4_ext);
1011                                 specs.ipv4 = (struct ibv_flow_spec_ipv4_ext){
1012                                         .type = IBV_FLOW_SPEC_IPV4_EXT |
1013                                                 parser->inner,
1014                                         .size = size,
1015                                 };
1016                         } else {
1017                                 size = sizeof(struct ibv_flow_spec_ipv6);
1018                                 specs.ipv6 = (struct ibv_flow_spec_ipv6){
1019                                         .type = IBV_FLOW_SPEC_IPV6 |
1020                                                 parser->inner,
1021                                         .size = size,
1022                                 };
1023                         }
1024                         if (parser->queue[i].ibv_attr) {
1025                                 dst = (void *)((uintptr_t)
1026                                                parser->queue[i].ibv_attr +
1027                                                parser->queue[i].offset);
1028                                 memcpy(dst, &specs, size);
1029                                 ++parser->queue[i].ibv_attr->num_of_specs;
1030                         }
1031                         parser->queue[i].offset += size;
1032                 }
1033                 if ((i == HASH_RXQ_UDPV4) || (i == HASH_RXQ_TCPV4) ||
1034                     (i == HASH_RXQ_UDPV6) || (i == HASH_RXQ_TCPV6)) {
1035                         size = sizeof(struct ibv_flow_spec_tcp_udp);
1036                         specs.udp_tcp = (struct ibv_flow_spec_tcp_udp) {
1037                                 .type = ((i == HASH_RXQ_UDPV4 ||
1038                                           i == HASH_RXQ_UDPV6) ?
1039                                          IBV_FLOW_SPEC_UDP :
1040                                          IBV_FLOW_SPEC_TCP) |
1041                                         parser->inner,
1042                                 .size = size,
1043                         };
1044                         if (parser->queue[i].ibv_attr) {
1045                                 dst = (void *)((uintptr_t)
1046                                                parser->queue[i].ibv_attr +
1047                                                parser->queue[i].offset);
1048                                 memcpy(dst, &specs, size);
1049                                 ++parser->queue[i].ibv_attr->num_of_specs;
1050                         }
1051                         parser->queue[i].offset += size;
1052                 }
1053         }
1054 }
1055
1056 /**
1057  * Validate and convert a flow supported by the NIC.
1058  *
1059  * @param priv
1060  *   Pointer to private structure.
1061  * @param[in] attr
1062  *   Flow rule attributes.
1063  * @param[in] pattern
1064  *   Pattern specification (list terminated by the END pattern item).
1065  * @param[in] actions
1066  *   Associated actions (list terminated by the END action).
1067  * @param[out] error
1068  *   Perform verbose error reporting if not NULL.
1069  * @param[in, out] parser
1070  *   Internal parser structure.
1071  *
1072  * @return
1073  *   0 on success, a negative errno value otherwise and rte_errno is set.
1074  */
1075 static int
1076 priv_flow_convert(struct priv *priv,
1077                   const struct rte_flow_attr *attr,
1078                   const struct rte_flow_item items[],
1079                   const struct rte_flow_action actions[],
1080                   struct rte_flow_error *error,
1081                   struct mlx5_flow_parse *parser)
1082 {
1083         const struct mlx5_flow_items *cur_item = mlx5_flow_items;
1084         unsigned int i;
1085         int ret;
1086
1087         /* First step. Validate the attributes, items and actions. */
1088         *parser = (struct mlx5_flow_parse){
1089                 .create = parser->create,
1090                 .layer = HASH_RXQ_ETH,
1091                 .mark_id = MLX5_FLOW_MARK_DEFAULT,
1092         };
1093         ret = priv_flow_convert_attributes(priv, attr, error, parser);
1094         if (ret)
1095                 return ret;
1096         ret = priv_flow_convert_actions(priv, actions, error, parser);
1097         if (ret)
1098                 return ret;
1099         ret = priv_flow_convert_items_validate(priv, items, error, parser);
1100         if (ret)
1101                 return ret;
1102         priv_flow_convert_finalise(priv, parser);
1103         /*
1104          * Second step.
1105          * Allocate the memory space to store verbs specifications.
1106          */
1107         if (parser->drop) {
1108                 parser->drop_q.ibv_attr =
1109                         priv_flow_convert_allocate(priv, attr->priority,
1110                                                    parser->drop_q.offset,
1111                                                    error);
1112                 if (!parser->drop_q.ibv_attr)
1113                         return ENOMEM;
1114                 parser->drop_q.offset = sizeof(struct ibv_flow_attr);
1115         } else if (parser->queues_n == 1) {
1116                 unsigned int priority =
1117                         attr->priority +
1118                         hash_rxq_init[HASH_RXQ_ETH].flow_priority;
1119                 unsigned int offset = parser->queue[HASH_RXQ_ETH].offset;
1120
1121                 parser->queue[HASH_RXQ_ETH].ibv_attr =
1122                         priv_flow_convert_allocate(priv, priority,
1123                                                    offset, error);
1124                 if (!parser->queue[HASH_RXQ_ETH].ibv_attr)
1125                         return ENOMEM;
1126                 parser->queue[HASH_RXQ_ETH].offset =
1127                         sizeof(struct ibv_flow_attr);
1128         } else {
1129                 for (i = 0; i != hash_rxq_init_n; ++i) {
1130                         unsigned int priority =
1131                                 attr->priority +
1132                                 hash_rxq_init[i].flow_priority;
1133                         unsigned int offset;
1134
1135                         if (!(parser->rss_conf.rss_hf &
1136                               hash_rxq_init[i].dpdk_rss_hf) &&
1137                             (i != HASH_RXQ_ETH))
1138                                 continue;
1139                         offset = parser->queue[i].offset;
1140                         parser->queue[i].ibv_attr =
1141                                 priv_flow_convert_allocate(priv, priority,
1142                                                            offset, error);
1143                         if (!parser->queue[i].ibv_attr)
1144                                 goto exit_enomem;
1145                         parser->queue[i].offset = sizeof(struct ibv_flow_attr);
1146                 }
1147         }
1148         /* Third step. Conversion parse, fill the specifications. */
1149         parser->inner = 0;
1150         for (; items->type != RTE_FLOW_ITEM_TYPE_END; ++items) {
1151                 if (items->type == RTE_FLOW_ITEM_TYPE_VOID)
1152                         continue;
1153                 cur_item = &mlx5_flow_items[items->type];
1154                 ret = cur_item->convert(items,
1155                                         (cur_item->default_mask ?
1156                                          cur_item->default_mask :
1157                                          cur_item->mask),
1158                                         parser);
1159                 if (ret) {
1160                         rte_flow_error_set(error, ENOTSUP,
1161                                            RTE_FLOW_ERROR_TYPE_ITEM,
1162                                            items, "item not supported");
1163                         goto exit_free;
1164                 }
1165         }
1166         if (parser->mark)
1167                 mlx5_flow_create_flag_mark(parser, parser->mark_id);
1168         if (parser->count && parser->create) {
1169                 mlx5_flow_create_count(priv, parser);
1170                 if (!parser->cs)
1171                         goto exit_count_error;
1172         }
1173         /*
1174          * Last step. Complete missing specification to reach the RSS
1175          * configuration.
1176          */
1177         if (parser->queues_n > 1) {
1178                 priv_flow_convert_finalise(priv, parser);
1179         } else if (!parser->drop) {
1180                 /*
1181                  * Action queue have their priority overridden with
1182                  * Ethernet priority, this priority needs to be adjusted to
1183                  * their most specific layer priority.
1184                  */
1185                 parser->queue[HASH_RXQ_ETH].ibv_attr->priority =
1186                         attr->priority +
1187                         hash_rxq_init[parser->layer].flow_priority;
1188         }
1189 exit_free:
1190         /* Only verification is expected, all resources should be released. */
1191         if (!parser->create) {
1192                 if (parser->drop) {
1193                         rte_free(parser->drop_q.ibv_attr);
1194                         parser->drop_q.ibv_attr = NULL;
1195                 }
1196                 for (i = 0; i != hash_rxq_init_n; ++i) {
1197                         if (parser->queue[i].ibv_attr) {
1198                                 rte_free(parser->queue[i].ibv_attr);
1199                                 parser->queue[i].ibv_attr = NULL;
1200                         }
1201                 }
1202         }
1203         return ret;
1204 exit_enomem:
1205         for (i = 0; i != hash_rxq_init_n; ++i) {
1206                 if (parser->queue[i].ibv_attr) {
1207                         rte_free(parser->queue[i].ibv_attr);
1208                         parser->queue[i].ibv_attr = NULL;
1209                 }
1210         }
1211         rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1212                            NULL, "cannot allocate verbs spec attributes.");
1213         return ret;
1214 exit_count_error:
1215         rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1216                            NULL, "cannot create counter.");
1217         return rte_errno;
1218 }
1219
1220 /**
1221  * Copy the specification created into the flow.
1222  *
1223  * @param parser
1224  *   Internal parser structure.
1225  * @param src
1226  *   Create specification.
1227  * @param size
1228  *   Size in bytes of the specification to copy.
1229  */
1230 static void
1231 mlx5_flow_create_copy(struct mlx5_flow_parse *parser, void *src,
1232                       unsigned int size)
1233 {
1234         unsigned int i;
1235         void *dst;
1236
1237         if (parser->drop) {
1238                 dst = (void *)((uintptr_t)parser->drop_q.ibv_attr +
1239                                 parser->drop_q.offset);
1240                 memcpy(dst, src, size);
1241                 ++parser->drop_q.ibv_attr->num_of_specs;
1242                 parser->drop_q.offset += size;
1243                 return;
1244         }
1245         for (i = 0; i != hash_rxq_init_n; ++i) {
1246                 if (!parser->queue[i].ibv_attr)
1247                         continue;
1248                 /* Specification must be the same l3 type or none. */
1249                 if (parser->layer == HASH_RXQ_ETH ||
1250                     (hash_rxq_init[parser->layer].ip_version ==
1251                      hash_rxq_init[i].ip_version) ||
1252                     (hash_rxq_init[i].ip_version == 0)) {
1253                         dst = (void *)((uintptr_t)parser->queue[i].ibv_attr +
1254                                         parser->queue[i].offset);
1255                         memcpy(dst, src, size);
1256                         ++parser->queue[i].ibv_attr->num_of_specs;
1257                         parser->queue[i].offset += size;
1258                 }
1259         }
1260 }
1261
1262 /**
1263  * Convert Ethernet item to Verbs specification.
1264  *
1265  * @param item[in]
1266  *   Item specification.
1267  * @param default_mask[in]
1268  *   Default bit-masks to use when item->mask is not provided.
1269  * @param data[in, out]
1270  *   User structure.
1271  */
1272 static int
1273 mlx5_flow_create_eth(const struct rte_flow_item *item,
1274                      const void *default_mask,
1275                      void *data)
1276 {
1277         const struct rte_flow_item_eth *spec = item->spec;
1278         const struct rte_flow_item_eth *mask = item->mask;
1279         struct mlx5_flow_parse *parser = (struct mlx5_flow_parse *)data;
1280         const unsigned int eth_size = sizeof(struct ibv_flow_spec_eth);
1281         struct ibv_flow_spec_eth eth = {
1282                 .type = parser->inner | IBV_FLOW_SPEC_ETH,
1283                 .size = eth_size,
1284         };
1285
1286         parser->layer = HASH_RXQ_ETH;
1287         if (spec) {
1288                 unsigned int i;
1289
1290                 if (!mask)
1291                         mask = default_mask;
1292                 memcpy(&eth.val.dst_mac, spec->dst.addr_bytes, ETHER_ADDR_LEN);
1293                 memcpy(&eth.val.src_mac, spec->src.addr_bytes, ETHER_ADDR_LEN);
1294                 eth.val.ether_type = spec->type;
1295                 memcpy(&eth.mask.dst_mac, mask->dst.addr_bytes, ETHER_ADDR_LEN);
1296                 memcpy(&eth.mask.src_mac, mask->src.addr_bytes, ETHER_ADDR_LEN);
1297                 eth.mask.ether_type = mask->type;
1298                 /* Remove unwanted bits from values. */
1299                 for (i = 0; i < ETHER_ADDR_LEN; ++i) {
1300                         eth.val.dst_mac[i] &= eth.mask.dst_mac[i];
1301                         eth.val.src_mac[i] &= eth.mask.src_mac[i];
1302                 }
1303                 eth.val.ether_type &= eth.mask.ether_type;
1304         }
1305         mlx5_flow_create_copy(parser, &eth, eth_size);
1306         return 0;
1307 }
1308
1309 /**
1310  * Convert VLAN item to Verbs specification.
1311  *
1312  * @param item[in]
1313  *   Item specification.
1314  * @param default_mask[in]
1315  *   Default bit-masks to use when item->mask is not provided.
1316  * @param data[in, out]
1317  *   User structure.
1318  */
1319 static int
1320 mlx5_flow_create_vlan(const struct rte_flow_item *item,
1321                       const void *default_mask,
1322                       void *data)
1323 {
1324         const struct rte_flow_item_vlan *spec = item->spec;
1325         const struct rte_flow_item_vlan *mask = item->mask;
1326         struct mlx5_flow_parse *parser = (struct mlx5_flow_parse *)data;
1327         struct ibv_flow_spec_eth *eth;
1328         const unsigned int eth_size = sizeof(struct ibv_flow_spec_eth);
1329
1330         if (spec) {
1331                 unsigned int i;
1332                 if (!mask)
1333                         mask = default_mask;
1334
1335                 if (parser->drop) {
1336                         eth = (void *)((uintptr_t)parser->drop_q.ibv_attr +
1337                                        parser->drop_q.offset - eth_size);
1338                         eth->val.vlan_tag = spec->tci;
1339                         eth->mask.vlan_tag = mask->tci;
1340                         eth->val.vlan_tag &= eth->mask.vlan_tag;
1341                         return 0;
1342                 }
1343                 for (i = 0; i != hash_rxq_init_n; ++i) {
1344                         if (!parser->queue[i].ibv_attr)
1345                                 continue;
1346
1347                         eth = (void *)((uintptr_t)parser->queue[i].ibv_attr +
1348                                        parser->queue[i].offset - eth_size);
1349                         eth->val.vlan_tag = spec->tci;
1350                         eth->mask.vlan_tag = mask->tci;
1351                         eth->val.vlan_tag &= eth->mask.vlan_tag;
1352                 }
1353         }
1354         return 0;
1355 }
1356
1357 /**
1358  * Convert IPv4 item to Verbs specification.
1359  *
1360  * @param item[in]
1361  *   Item specification.
1362  * @param default_mask[in]
1363  *   Default bit-masks to use when item->mask is not provided.
1364  * @param data[in, out]
1365  *   User structure.
1366  */
1367 static int
1368 mlx5_flow_create_ipv4(const struct rte_flow_item *item,
1369                       const void *default_mask,
1370                       void *data)
1371 {
1372         const struct rte_flow_item_ipv4 *spec = item->spec;
1373         const struct rte_flow_item_ipv4 *mask = item->mask;
1374         struct mlx5_flow_parse *parser = (struct mlx5_flow_parse *)data;
1375         unsigned int ipv4_size = sizeof(struct ibv_flow_spec_ipv4_ext);
1376         struct ibv_flow_spec_ipv4_ext ipv4 = {
1377                 .type = parser->inner | IBV_FLOW_SPEC_IPV4_EXT,
1378                 .size = ipv4_size,
1379         };
1380
1381         parser->layer = HASH_RXQ_IPV4;
1382         if (spec) {
1383                 if (!mask)
1384                         mask = default_mask;
1385                 ipv4.val = (struct ibv_flow_ipv4_ext_filter){
1386                         .src_ip = spec->hdr.src_addr,
1387                         .dst_ip = spec->hdr.dst_addr,
1388                         .proto = spec->hdr.next_proto_id,
1389                         .tos = spec->hdr.type_of_service,
1390                 };
1391                 ipv4.mask = (struct ibv_flow_ipv4_ext_filter){
1392                         .src_ip = mask->hdr.src_addr,
1393                         .dst_ip = mask->hdr.dst_addr,
1394                         .proto = mask->hdr.next_proto_id,
1395                         .tos = mask->hdr.type_of_service,
1396                 };
1397                 /* Remove unwanted bits from values. */
1398                 ipv4.val.src_ip &= ipv4.mask.src_ip;
1399                 ipv4.val.dst_ip &= ipv4.mask.dst_ip;
1400                 ipv4.val.proto &= ipv4.mask.proto;
1401                 ipv4.val.tos &= ipv4.mask.tos;
1402         }
1403         mlx5_flow_create_copy(parser, &ipv4, ipv4_size);
1404         return 0;
1405 }
1406
1407 /**
1408  * Convert IPv6 item to Verbs specification.
1409  *
1410  * @param item[in]
1411  *   Item specification.
1412  * @param default_mask[in]
1413  *   Default bit-masks to use when item->mask is not provided.
1414  * @param data[in, out]
1415  *   User structure.
1416  */
1417 static int
1418 mlx5_flow_create_ipv6(const struct rte_flow_item *item,
1419                       const void *default_mask,
1420                       void *data)
1421 {
1422         const struct rte_flow_item_ipv6 *spec = item->spec;
1423         const struct rte_flow_item_ipv6 *mask = item->mask;
1424         struct mlx5_flow_parse *parser = (struct mlx5_flow_parse *)data;
1425         unsigned int ipv6_size = sizeof(struct ibv_flow_spec_ipv6);
1426         struct ibv_flow_spec_ipv6 ipv6 = {
1427                 .type = parser->inner | IBV_FLOW_SPEC_IPV6,
1428                 .size = ipv6_size,
1429         };
1430
1431         parser->layer = HASH_RXQ_IPV6;
1432         if (spec) {
1433                 unsigned int i;
1434
1435                 if (!mask)
1436                         mask = default_mask;
1437                 memcpy(&ipv6.val.src_ip, spec->hdr.src_addr,
1438                        RTE_DIM(ipv6.val.src_ip));
1439                 memcpy(&ipv6.val.dst_ip, spec->hdr.dst_addr,
1440                        RTE_DIM(ipv6.val.dst_ip));
1441                 memcpy(&ipv6.mask.src_ip, mask->hdr.src_addr,
1442                        RTE_DIM(ipv6.mask.src_ip));
1443                 memcpy(&ipv6.mask.dst_ip, mask->hdr.dst_addr,
1444                        RTE_DIM(ipv6.mask.dst_ip));
1445                 ipv6.mask.flow_label = mask->hdr.vtc_flow;
1446                 ipv6.mask.next_hdr = mask->hdr.proto;
1447                 ipv6.mask.hop_limit = mask->hdr.hop_limits;
1448                 /* Remove unwanted bits from values. */
1449                 for (i = 0; i < RTE_DIM(ipv6.val.src_ip); ++i) {
1450                         ipv6.val.src_ip[i] &= ipv6.mask.src_ip[i];
1451                         ipv6.val.dst_ip[i] &= ipv6.mask.dst_ip[i];
1452                 }
1453                 ipv6.val.flow_label &= ipv6.mask.flow_label;
1454                 ipv6.val.next_hdr &= ipv6.mask.next_hdr;
1455                 ipv6.val.hop_limit &= ipv6.mask.hop_limit;
1456         }
1457         mlx5_flow_create_copy(parser, &ipv6, ipv6_size);
1458         return 0;
1459 }
1460
1461 /**
1462  * Convert UDP item to Verbs specification.
1463  *
1464  * @param item[in]
1465  *   Item specification.
1466  * @param default_mask[in]
1467  *   Default bit-masks to use when item->mask is not provided.
1468  * @param data[in, out]
1469  *   User structure.
1470  */
1471 static int
1472 mlx5_flow_create_udp(const struct rte_flow_item *item,
1473                      const void *default_mask,
1474                      void *data)
1475 {
1476         const struct rte_flow_item_udp *spec = item->spec;
1477         const struct rte_flow_item_udp *mask = item->mask;
1478         struct mlx5_flow_parse *parser = (struct mlx5_flow_parse *)data;
1479         unsigned int udp_size = sizeof(struct ibv_flow_spec_tcp_udp);
1480         struct ibv_flow_spec_tcp_udp udp = {
1481                 .type = parser->inner | IBV_FLOW_SPEC_UDP,
1482                 .size = udp_size,
1483         };
1484
1485         if (parser->layer == HASH_RXQ_IPV4)
1486                 parser->layer = HASH_RXQ_UDPV4;
1487         else
1488                 parser->layer = HASH_RXQ_UDPV6;
1489         if (spec) {
1490                 if (!mask)
1491                         mask = default_mask;
1492                 udp.val.dst_port = spec->hdr.dst_port;
1493                 udp.val.src_port = spec->hdr.src_port;
1494                 udp.mask.dst_port = mask->hdr.dst_port;
1495                 udp.mask.src_port = mask->hdr.src_port;
1496                 /* Remove unwanted bits from values. */
1497                 udp.val.src_port &= udp.mask.src_port;
1498                 udp.val.dst_port &= udp.mask.dst_port;
1499         }
1500         mlx5_flow_create_copy(parser, &udp, udp_size);
1501         return 0;
1502 }
1503
1504 /**
1505  * Convert TCP item to Verbs specification.
1506  *
1507  * @param item[in]
1508  *   Item specification.
1509  * @param default_mask[in]
1510  *   Default bit-masks to use when item->mask is not provided.
1511  * @param data[in, out]
1512  *   User structure.
1513  */
1514 static int
1515 mlx5_flow_create_tcp(const struct rte_flow_item *item,
1516                      const void *default_mask,
1517                      void *data)
1518 {
1519         const struct rte_flow_item_tcp *spec = item->spec;
1520         const struct rte_flow_item_tcp *mask = item->mask;
1521         struct mlx5_flow_parse *parser = (struct mlx5_flow_parse *)data;
1522         unsigned int tcp_size = sizeof(struct ibv_flow_spec_tcp_udp);
1523         struct ibv_flow_spec_tcp_udp tcp = {
1524                 .type = parser->inner | IBV_FLOW_SPEC_TCP,
1525                 .size = tcp_size,
1526         };
1527
1528         if (parser->layer == HASH_RXQ_IPV4)
1529                 parser->layer = HASH_RXQ_TCPV4;
1530         else
1531                 parser->layer = HASH_RXQ_TCPV6;
1532         if (spec) {
1533                 if (!mask)
1534                         mask = default_mask;
1535                 tcp.val.dst_port = spec->hdr.dst_port;
1536                 tcp.val.src_port = spec->hdr.src_port;
1537                 tcp.mask.dst_port = mask->hdr.dst_port;
1538                 tcp.mask.src_port = mask->hdr.src_port;
1539                 /* Remove unwanted bits from values. */
1540                 tcp.val.src_port &= tcp.mask.src_port;
1541                 tcp.val.dst_port &= tcp.mask.dst_port;
1542         }
1543         mlx5_flow_create_copy(parser, &tcp, tcp_size);
1544         return 0;
1545 }
1546
1547 /**
1548  * Convert VXLAN item to Verbs specification.
1549  *
1550  * @param item[in]
1551  *   Item specification.
1552  * @param default_mask[in]
1553  *   Default bit-masks to use when item->mask is not provided.
1554  * @param data[in, out]
1555  *   User structure.
1556  */
1557 static int
1558 mlx5_flow_create_vxlan(const struct rte_flow_item *item,
1559                        const void *default_mask,
1560                        void *data)
1561 {
1562         const struct rte_flow_item_vxlan *spec = item->spec;
1563         const struct rte_flow_item_vxlan *mask = item->mask;
1564         struct mlx5_flow_parse *parser = (struct mlx5_flow_parse *)data;
1565         unsigned int size = sizeof(struct ibv_flow_spec_tunnel);
1566         struct ibv_flow_spec_tunnel vxlan = {
1567                 .type = parser->inner | IBV_FLOW_SPEC_VXLAN_TUNNEL,
1568                 .size = size,
1569         };
1570         union vni {
1571                 uint32_t vlan_id;
1572                 uint8_t vni[4];
1573         } id;
1574
1575         id.vni[0] = 0;
1576         parser->inner = IBV_FLOW_SPEC_INNER;
1577         if (spec) {
1578                 if (!mask)
1579                         mask = default_mask;
1580                 memcpy(&id.vni[1], spec->vni, 3);
1581                 vxlan.val.tunnel_id = id.vlan_id;
1582                 memcpy(&id.vni[1], mask->vni, 3);
1583                 vxlan.mask.tunnel_id = id.vlan_id;
1584                 /* Remove unwanted bits from values. */
1585                 vxlan.val.tunnel_id &= vxlan.mask.tunnel_id;
1586         }
1587         mlx5_flow_create_copy(parser, &vxlan, size);
1588         return 0;
1589 }
1590
1591 /**
1592  * Convert mark/flag action to Verbs specification.
1593  *
1594  * @param parser
1595  *   Internal parser structure.
1596  * @param mark_id
1597  *   Mark identifier.
1598  */
1599 static int
1600 mlx5_flow_create_flag_mark(struct mlx5_flow_parse *parser, uint32_t mark_id)
1601 {
1602         unsigned int size = sizeof(struct ibv_flow_spec_action_tag);
1603         struct ibv_flow_spec_action_tag tag = {
1604                 .type = IBV_FLOW_SPEC_ACTION_TAG,
1605                 .size = size,
1606                 .tag_id = mlx5_flow_mark_set(mark_id),
1607         };
1608
1609         assert(parser->mark);
1610         mlx5_flow_create_copy(parser, &tag, size);
1611         return 0;
1612 }
1613
1614 /**
1615  * Convert count action to Verbs specification.
1616  *
1617  * @param priv
1618  *   Pointer to private structure.
1619  * @param parser
1620  *   Pointer to MLX5 flow parser structure.
1621  *
1622  * @return
1623  *   0 on success, errno value on failure.
1624  */
1625 static int
1626 mlx5_flow_create_count(struct priv *priv __rte_unused,
1627                        struct mlx5_flow_parse *parser __rte_unused)
1628 {
1629 #ifdef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
1630         unsigned int size = sizeof(struct ibv_flow_spec_counter_action);
1631         struct ibv_counter_set_init_attr init_attr = {0};
1632         struct ibv_flow_spec_counter_action counter = {
1633                 .type = IBV_FLOW_SPEC_ACTION_COUNT,
1634                 .size = size,
1635                 .counter_set_handle = 0,
1636         };
1637
1638         init_attr.counter_set_id = 0;
1639         parser->cs = ibv_create_counter_set(priv->ctx, &init_attr);
1640         if (!parser->cs)
1641                 return EINVAL;
1642         counter.counter_set_handle = parser->cs->handle;
1643         mlx5_flow_create_copy(parser, &counter, size);
1644 #endif
1645         return 0;
1646 }
1647
1648 /**
1649  * Complete flow rule creation with a drop queue.
1650  *
1651  * @param priv
1652  *   Pointer to private structure.
1653  * @param parser
1654  *   Internal parser structure.
1655  * @param flow
1656  *   Pointer to the rte_flow.
1657  * @param[out] error
1658  *   Perform verbose error reporting if not NULL.
1659  *
1660  * @return
1661  *   0 on success, errno value on failure.
1662  */
1663 static int
1664 priv_flow_create_action_queue_drop(struct priv *priv,
1665                                    struct mlx5_flow_parse *parser,
1666                                    struct rte_flow *flow,
1667                                    struct rte_flow_error *error)
1668 {
1669         struct ibv_flow_spec_action_drop *drop;
1670         unsigned int size = sizeof(struct ibv_flow_spec_action_drop);
1671         int err = 0;
1672
1673         assert(priv->pd);
1674         assert(priv->ctx);
1675         flow->drop = 1;
1676         drop = (void *)((uintptr_t)parser->drop_q.ibv_attr +
1677                         parser->drop_q.offset);
1678         *drop = (struct ibv_flow_spec_action_drop){
1679                         .type = IBV_FLOW_SPEC_ACTION_DROP,
1680                         .size = size,
1681         };
1682         ++parser->drop_q.ibv_attr->num_of_specs;
1683         parser->drop_q.offset += size;
1684         flow->drxq.ibv_attr = parser->drop_q.ibv_attr;
1685         if (!priv->dev->data->dev_started)
1686                 return 0;
1687         parser->drop_q.ibv_attr = NULL;
1688         flow->drxq.ibv_flow = ibv_create_flow(priv->flow_drop_queue->qp,
1689                                               flow->drxq.ibv_attr);
1690         if (parser->count)
1691                 flow->cs = parser->cs;
1692         if (!flow->drxq.ibv_flow) {
1693                 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1694                                    NULL, "flow rule creation failure");
1695                 err = ENOMEM;
1696                 goto error;
1697         }
1698         return 0;
1699 error:
1700         assert(flow);
1701         if (flow->drxq.ibv_flow) {
1702                 claim_zero(ibv_destroy_flow(flow->drxq.ibv_flow));
1703                 flow->drxq.ibv_flow = NULL;
1704         }
1705         if (flow->drxq.ibv_attr) {
1706                 rte_free(flow->drxq.ibv_attr);
1707                 flow->drxq.ibv_attr = NULL;
1708         }
1709         if (flow->cs) {
1710                 claim_zero(ibv_destroy_counter_set(flow->cs));
1711                 flow->cs = NULL;
1712                 parser->cs = NULL;
1713         }
1714         return err;
1715 }
1716
1717 /**
1718  * Create hash Rx queues when RSS is enabled.
1719  *
1720  * @param priv
1721  *   Pointer to private structure.
1722  * @param parser
1723  *   Internal parser structure.
1724  * @param flow
1725  *   Pointer to the rte_flow.
1726  * @param[out] error
1727  *   Perform verbose error reporting if not NULL.
1728  *
1729  * @return
1730  *   0 on success, a errno value otherwise and rte_errno is set.
1731  */
1732 static int
1733 priv_flow_create_action_queue_rss(struct priv *priv,
1734                                   struct mlx5_flow_parse *parser,
1735                                   struct rte_flow *flow,
1736                                   struct rte_flow_error *error)
1737 {
1738         unsigned int i;
1739
1740         for (i = 0; i != hash_rxq_init_n; ++i) {
1741                 uint64_t hash_fields;
1742
1743                 if (!parser->queue[i].ibv_attr)
1744                         continue;
1745                 flow->frxq[i].ibv_attr = parser->queue[i].ibv_attr;
1746                 parser->queue[i].ibv_attr = NULL;
1747                 hash_fields = hash_rxq_init[i].hash_fields;
1748                 if (!priv->dev->data->dev_started)
1749                         continue;
1750                 flow->frxq[i].hrxq =
1751                         mlx5_priv_hrxq_get(priv,
1752                                            parser->rss_conf.rss_key,
1753                                            parser->rss_conf.rss_key_len,
1754                                            hash_fields,
1755                                            parser->queues,
1756                                            hash_fields ? parser->queues_n : 1);
1757                 if (flow->frxq[i].hrxq)
1758                         continue;
1759                 flow->frxq[i].hrxq =
1760                         mlx5_priv_hrxq_new(priv,
1761                                            parser->rss_conf.rss_key,
1762                                            parser->rss_conf.rss_key_len,
1763                                            hash_fields,
1764                                            parser->queues,
1765                                            hash_fields ? parser->queues_n : 1);
1766                 if (!flow->frxq[i].hrxq) {
1767                         rte_flow_error_set(error, ENOMEM,
1768                                            RTE_FLOW_ERROR_TYPE_HANDLE,
1769                                            NULL, "cannot create hash rxq");
1770                         return ENOMEM;
1771                 }
1772         }
1773         return 0;
1774 }
1775
1776 /**
1777  * Complete flow rule creation.
1778  *
1779  * @param priv
1780  *   Pointer to private structure.
1781  * @param parser
1782  *   Internal parser structure.
1783  * @param flow
1784  *   Pointer to the rte_flow.
1785  * @param[out] error
1786  *   Perform verbose error reporting if not NULL.
1787  *
1788  * @return
1789  *   0 on success, a errno value otherwise and rte_errno is set.
1790  */
1791 static int
1792 priv_flow_create_action_queue(struct priv *priv,
1793                               struct mlx5_flow_parse *parser,
1794                               struct rte_flow *flow,
1795                               struct rte_flow_error *error)
1796 {
1797         int err = 0;
1798         unsigned int i;
1799
1800         assert(priv->pd);
1801         assert(priv->ctx);
1802         assert(!parser->drop);
1803         err = priv_flow_create_action_queue_rss(priv, parser, flow, error);
1804         if (err)
1805                 goto error;
1806         if (parser->count)
1807                 flow->cs = parser->cs;
1808         if (!priv->dev->data->dev_started)
1809                 return 0;
1810         for (i = 0; i != hash_rxq_init_n; ++i) {
1811                 if (!flow->frxq[i].hrxq)
1812                         continue;
1813                 flow->frxq[i].ibv_flow =
1814                         ibv_create_flow(flow->frxq[i].hrxq->qp,
1815                                         flow->frxq[i].ibv_attr);
1816                 if (!flow->frxq[i].ibv_flow) {
1817                         rte_flow_error_set(error, ENOMEM,
1818                                            RTE_FLOW_ERROR_TYPE_HANDLE,
1819                                            NULL, "flow rule creation failure");
1820                         err = ENOMEM;
1821                         goto error;
1822                 }
1823                 DEBUG("%p type %d QP %p ibv_flow %p",
1824                       (void *)flow, i,
1825                       (void *)flow->frxq[i].hrxq,
1826                       (void *)flow->frxq[i].ibv_flow);
1827         }
1828         for (i = 0; i != parser->queues_n; ++i) {
1829                 struct mlx5_rxq_data *q =
1830                         (*priv->rxqs)[parser->queues[i]];
1831
1832                 q->mark |= parser->mark;
1833         }
1834         return 0;
1835 error:
1836         assert(flow);
1837         for (i = 0; i != hash_rxq_init_n; ++i) {
1838                 if (flow->frxq[i].ibv_flow) {
1839                         struct ibv_flow *ibv_flow = flow->frxq[i].ibv_flow;
1840
1841                         claim_zero(ibv_destroy_flow(ibv_flow));
1842                 }
1843                 if (flow->frxq[i].hrxq)
1844                         mlx5_priv_hrxq_release(priv, flow->frxq[i].hrxq);
1845                 if (flow->frxq[i].ibv_attr)
1846                         rte_free(flow->frxq[i].ibv_attr);
1847         }
1848         if (flow->cs) {
1849                 claim_zero(ibv_destroy_counter_set(flow->cs));
1850                 flow->cs = NULL;
1851                 parser->cs = NULL;
1852         }
1853         return err;
1854 }
1855
1856 /**
1857  * Convert a flow.
1858  *
1859  * @param priv
1860  *   Pointer to private structure.
1861  * @param list
1862  *   Pointer to a TAILQ flow list.
1863  * @param[in] attr
1864  *   Flow rule attributes.
1865  * @param[in] pattern
1866  *   Pattern specification (list terminated by the END pattern item).
1867  * @param[in] actions
1868  *   Associated actions (list terminated by the END action).
1869  * @param[out] error
1870  *   Perform verbose error reporting if not NULL.
1871  *
1872  * @return
1873  *   A flow on success, NULL otherwise.
1874  */
1875 static struct rte_flow *
1876 priv_flow_create(struct priv *priv,
1877                  struct mlx5_flows *list,
1878                  const struct rte_flow_attr *attr,
1879                  const struct rte_flow_item items[],
1880                  const struct rte_flow_action actions[],
1881                  struct rte_flow_error *error)
1882 {
1883         struct mlx5_flow_parse parser = { .create = 1, };
1884         struct rte_flow *flow = NULL;
1885         unsigned int i;
1886         int err;
1887
1888         err = priv_flow_convert(priv, attr, items, actions, error, &parser);
1889         if (err)
1890                 goto exit;
1891         flow = rte_calloc(__func__, 1,
1892                           sizeof(*flow) + parser.queues_n * sizeof(uint16_t),
1893                           0);
1894         if (!flow) {
1895                 rte_flow_error_set(error, ENOMEM,
1896                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1897                                    NULL,
1898                                    "cannot allocate flow memory");
1899                 return NULL;
1900         }
1901         /* Copy queues configuration. */
1902         flow->queues = (uint16_t (*)[])(flow + 1);
1903         memcpy(flow->queues, parser.queues, parser.queues_n * sizeof(uint16_t));
1904         flow->queues_n = parser.queues_n;
1905         /* Copy RSS configuration. */
1906         flow->rss_conf = parser.rss_conf;
1907         flow->rss_conf.rss_key = flow->rss_key;
1908         memcpy(flow->rss_key, parser.rss_key, parser.rss_conf.rss_key_len);
1909         /* finalise the flow. */
1910         if (parser.drop)
1911                 err = priv_flow_create_action_queue_drop(priv, &parser, flow,
1912                                                          error);
1913         else
1914                 err = priv_flow_create_action_queue(priv, &parser, flow, error);
1915         if (err)
1916                 goto exit;
1917         TAILQ_INSERT_TAIL(list, flow, next);
1918         DEBUG("Flow created %p", (void *)flow);
1919         return flow;
1920 exit:
1921         if (parser.drop) {
1922                 rte_free(parser.drop_q.ibv_attr);
1923         } else {
1924                 for (i = 0; i != hash_rxq_init_n; ++i) {
1925                         if (parser.queue[i].ibv_attr)
1926                                 rte_free(parser.queue[i].ibv_attr);
1927                 }
1928         }
1929         rte_free(flow);
1930         return NULL;
1931 }
1932
1933 /**
1934  * Validate a flow supported by the NIC.
1935  *
1936  * @see rte_flow_validate()
1937  * @see rte_flow_ops
1938  */
1939 int
1940 mlx5_flow_validate(struct rte_eth_dev *dev,
1941                    const struct rte_flow_attr *attr,
1942                    const struct rte_flow_item items[],
1943                    const struct rte_flow_action actions[],
1944                    struct rte_flow_error *error)
1945 {
1946         struct priv *priv = dev->data->dev_private;
1947         int ret;
1948         struct mlx5_flow_parse parser = { .create = 0, };
1949
1950         priv_lock(priv);
1951         ret = priv_flow_convert(priv, attr, items, actions, error, &parser);
1952         priv_unlock(priv);
1953         return ret;
1954 }
1955
1956 /**
1957  * Create a flow.
1958  *
1959  * @see rte_flow_create()
1960  * @see rte_flow_ops
1961  */
1962 struct rte_flow *
1963 mlx5_flow_create(struct rte_eth_dev *dev,
1964                  const struct rte_flow_attr *attr,
1965                  const struct rte_flow_item items[],
1966                  const struct rte_flow_action actions[],
1967                  struct rte_flow_error *error)
1968 {
1969         struct priv *priv = dev->data->dev_private;
1970         struct rte_flow *flow;
1971
1972         priv_lock(priv);
1973         flow = priv_flow_create(priv, &priv->flows, attr, items, actions,
1974                                 error);
1975         priv_unlock(priv);
1976         return flow;
1977 }
1978
1979 /**
1980  * Destroy a flow.
1981  *
1982  * @param priv
1983  *   Pointer to private structure.
1984  * @param list
1985  *   Pointer to a TAILQ flow list.
1986  * @param[in] flow
1987  *   Flow to destroy.
1988  */
1989 static void
1990 priv_flow_destroy(struct priv *priv,
1991                   struct mlx5_flows *list,
1992                   struct rte_flow *flow)
1993 {
1994         unsigned int i;
1995
1996         if (flow->cs) {
1997                 claim_zero(ibv_destroy_counter_set(flow->cs));
1998                 flow->cs = NULL;
1999         }
2000         if (flow->drop || !flow->mark)
2001                 goto free;
2002         for (i = 0; i != flow->queues_n; ++i) {
2003                 struct rte_flow *tmp;
2004                 int mark = 0;
2005
2006                 /*
2007                  * To remove the mark from the queue, the queue must not be
2008                  * present in any other marked flow (RSS or not).
2009                  */
2010                 TAILQ_FOREACH(tmp, list, next) {
2011                         unsigned int j;
2012                         uint16_t *tqs = NULL;
2013                         uint16_t tq_n = 0;
2014
2015                         if (!tmp->mark)
2016                                 continue;
2017                         for (j = 0; j != hash_rxq_init_n; ++j) {
2018                                 if (!tmp->frxq[j].hrxq)
2019                                         continue;
2020                                 tqs = tmp->frxq[j].hrxq->ind_table->queues;
2021                                 tq_n = tmp->frxq[j].hrxq->ind_table->queues_n;
2022                         }
2023                         if (!tq_n)
2024                                 continue;
2025                         for (j = 0; (j != tq_n) && !mark; j++)
2026                                 if (tqs[j] == (*flow->queues)[i])
2027                                         mark = 1;
2028                 }
2029                 (*priv->rxqs)[(*flow->queues)[i]]->mark = mark;
2030         }
2031 free:
2032         if (flow->drop) {
2033                 if (flow->drxq.ibv_flow)
2034                         claim_zero(ibv_destroy_flow(flow->drxq.ibv_flow));
2035                 rte_free(flow->drxq.ibv_attr);
2036         } else {
2037                 for (i = 0; i != hash_rxq_init_n; ++i) {
2038                         struct mlx5_flow *frxq = &flow->frxq[i];
2039
2040                         if (frxq->ibv_flow)
2041                                 claim_zero(ibv_destroy_flow(frxq->ibv_flow));
2042                         if (frxq->hrxq)
2043                                 mlx5_priv_hrxq_release(priv, frxq->hrxq);
2044                         if (frxq->ibv_attr)
2045                                 rte_free(frxq->ibv_attr);
2046                 }
2047         }
2048         TAILQ_REMOVE(list, flow, next);
2049         DEBUG("Flow destroyed %p", (void *)flow);
2050         rte_free(flow);
2051 }
2052
2053 /**
2054  * Destroy all flows.
2055  *
2056  * @param priv
2057  *   Pointer to private structure.
2058  * @param list
2059  *   Pointer to a TAILQ flow list.
2060  */
2061 void
2062 priv_flow_flush(struct priv *priv, struct mlx5_flows *list)
2063 {
2064         while (!TAILQ_EMPTY(list)) {
2065                 struct rte_flow *flow;
2066
2067                 flow = TAILQ_FIRST(list);
2068                 priv_flow_destroy(priv, list, flow);
2069         }
2070 }
2071
2072 /**
2073  * Create drop queue.
2074  *
2075  * @param priv
2076  *   Pointer to private structure.
2077  *
2078  * @return
2079  *   0 on success.
2080  */
2081 int
2082 priv_flow_create_drop_queue(struct priv *priv)
2083 {
2084         struct mlx5_hrxq_drop *fdq = NULL;
2085
2086         assert(priv->pd);
2087         assert(priv->ctx);
2088         fdq = rte_calloc(__func__, 1, sizeof(*fdq), 0);
2089         if (!fdq) {
2090                 WARN("cannot allocate memory for drop queue");
2091                 goto error;
2092         }
2093         fdq->cq = ibv_create_cq(priv->ctx, 1, NULL, NULL, 0);
2094         if (!fdq->cq) {
2095                 WARN("cannot allocate CQ for drop queue");
2096                 goto error;
2097         }
2098         fdq->wq = ibv_create_wq(priv->ctx,
2099                         &(struct ibv_wq_init_attr){
2100                         .wq_type = IBV_WQT_RQ,
2101                         .max_wr = 1,
2102                         .max_sge = 1,
2103                         .pd = priv->pd,
2104                         .cq = fdq->cq,
2105                         });
2106         if (!fdq->wq) {
2107                 WARN("cannot allocate WQ for drop queue");
2108                 goto error;
2109         }
2110         fdq->ind_table = ibv_create_rwq_ind_table(priv->ctx,
2111                         &(struct ibv_rwq_ind_table_init_attr){
2112                         .log_ind_tbl_size = 0,
2113                         .ind_tbl = &fdq->wq,
2114                         .comp_mask = 0,
2115                         });
2116         if (!fdq->ind_table) {
2117                 WARN("cannot allocate indirection table for drop queue");
2118                 goto error;
2119         }
2120         fdq->qp = ibv_create_qp_ex(priv->ctx,
2121                 &(struct ibv_qp_init_attr_ex){
2122                         .qp_type = IBV_QPT_RAW_PACKET,
2123                         .comp_mask =
2124                                 IBV_QP_INIT_ATTR_PD |
2125                                 IBV_QP_INIT_ATTR_IND_TABLE |
2126                                 IBV_QP_INIT_ATTR_RX_HASH,
2127                         .rx_hash_conf = (struct ibv_rx_hash_conf){
2128                                 .rx_hash_function =
2129                                         IBV_RX_HASH_FUNC_TOEPLITZ,
2130                                 .rx_hash_key_len = rss_hash_default_key_len,
2131                                 .rx_hash_key = rss_hash_default_key,
2132                                 .rx_hash_fields_mask = 0,
2133                                 },
2134                         .rwq_ind_tbl = fdq->ind_table,
2135                         .pd = priv->pd
2136                 });
2137         if (!fdq->qp) {
2138                 WARN("cannot allocate QP for drop queue");
2139                 goto error;
2140         }
2141         priv->flow_drop_queue = fdq;
2142         return 0;
2143 error:
2144         if (fdq->qp)
2145                 claim_zero(ibv_destroy_qp(fdq->qp));
2146         if (fdq->ind_table)
2147                 claim_zero(ibv_destroy_rwq_ind_table(fdq->ind_table));
2148         if (fdq->wq)
2149                 claim_zero(ibv_destroy_wq(fdq->wq));
2150         if (fdq->cq)
2151                 claim_zero(ibv_destroy_cq(fdq->cq));
2152         if (fdq)
2153                 rte_free(fdq);
2154         priv->flow_drop_queue = NULL;
2155         return -1;
2156 }
2157
2158 /**
2159  * Delete drop queue.
2160  *
2161  * @param priv
2162  *   Pointer to private structure.
2163  */
2164 void
2165 priv_flow_delete_drop_queue(struct priv *priv)
2166 {
2167         struct mlx5_hrxq_drop *fdq = priv->flow_drop_queue;
2168
2169         if (!fdq)
2170                 return;
2171         if (fdq->qp)
2172                 claim_zero(ibv_destroy_qp(fdq->qp));
2173         if (fdq->ind_table)
2174                 claim_zero(ibv_destroy_rwq_ind_table(fdq->ind_table));
2175         if (fdq->wq)
2176                 claim_zero(ibv_destroy_wq(fdq->wq));
2177         if (fdq->cq)
2178                 claim_zero(ibv_destroy_cq(fdq->cq));
2179         rte_free(fdq);
2180         priv->flow_drop_queue = NULL;
2181 }
2182
2183 /**
2184  * Remove all flows.
2185  *
2186  * @param priv
2187  *   Pointer to private structure.
2188  * @param list
2189  *   Pointer to a TAILQ flow list.
2190  */
2191 void
2192 priv_flow_stop(struct priv *priv, struct mlx5_flows *list)
2193 {
2194         struct rte_flow *flow;
2195
2196         TAILQ_FOREACH_REVERSE(flow, list, mlx5_flows, next) {
2197                 unsigned int i;
2198
2199                 if (flow->drop) {
2200                         if (!flow->drxq.ibv_flow)
2201                                 continue;
2202                         claim_zero(ibv_destroy_flow(flow->drxq.ibv_flow));
2203                         flow->drxq.ibv_flow = NULL;
2204                         /* Next flow. */
2205                         continue;
2206                 }
2207                 if (flow->mark) {
2208                         struct mlx5_ind_table_ibv *ind_tbl = NULL;
2209
2210                         for (i = 0; i != hash_rxq_init_n; ++i) {
2211                                 if (!flow->frxq[i].hrxq)
2212                                         continue;
2213                                 ind_tbl = flow->frxq[i].hrxq->ind_table;
2214                         }
2215                         assert(ind_tbl);
2216                         for (i = 0; i != ind_tbl->queues_n; ++i)
2217                                 (*priv->rxqs)[ind_tbl->queues[i]]->mark = 0;
2218                 }
2219                 for (i = 0; i != hash_rxq_init_n; ++i) {
2220                         if (!flow->frxq[i].ibv_flow)
2221                                 continue;
2222                         claim_zero(ibv_destroy_flow(flow->frxq[i].ibv_flow));
2223                         flow->frxq[i].ibv_flow = NULL;
2224                         mlx5_priv_hrxq_release(priv, flow->frxq[i].hrxq);
2225                         flow->frxq[i].hrxq = NULL;
2226                 }
2227                 DEBUG("Flow %p removed", (void *)flow);
2228         }
2229 }
2230
2231 /**
2232  * Add all flows.
2233  *
2234  * @param priv
2235  *   Pointer to private structure.
2236  * @param list
2237  *   Pointer to a TAILQ flow list.
2238  *
2239  * @return
2240  *   0 on success, a errno value otherwise and rte_errno is set.
2241  */
2242 int
2243 priv_flow_start(struct priv *priv, struct mlx5_flows *list)
2244 {
2245         struct rte_flow *flow;
2246
2247         TAILQ_FOREACH(flow, list, next) {
2248                 unsigned int i;
2249
2250                 if (flow->drop) {
2251                         flow->drxq.ibv_flow =
2252                                 ibv_create_flow(priv->flow_drop_queue->qp,
2253                                                 flow->drxq.ibv_attr);
2254                         if (!flow->drxq.ibv_flow) {
2255                                 DEBUG("Flow %p cannot be applied",
2256                                       (void *)flow);
2257                                 rte_errno = EINVAL;
2258                                 return rte_errno;
2259                         }
2260                         DEBUG("Flow %p applied", (void *)flow);
2261                         /* Next flow. */
2262                         continue;
2263                 }
2264                 for (i = 0; i != hash_rxq_init_n; ++i) {
2265                         if (!flow->frxq[i].ibv_attr)
2266                                 continue;
2267                         flow->frxq[i].hrxq =
2268                                 mlx5_priv_hrxq_get(priv, flow->rss_conf.rss_key,
2269                                                    flow->rss_conf.rss_key_len,
2270                                                    hash_rxq_init[i].hash_fields,
2271                                                    (*flow->queues),
2272                                                    flow->queues_n);
2273                         if (flow->frxq[i].hrxq)
2274                                 goto flow_create;
2275                         flow->frxq[i].hrxq =
2276                                 mlx5_priv_hrxq_new(priv, flow->rss_conf.rss_key,
2277                                                    flow->rss_conf.rss_key_len,
2278                                                    hash_rxq_init[i].hash_fields,
2279                                                    (*flow->queues),
2280                                                    flow->queues_n);
2281                         if (!flow->frxq[i].hrxq) {
2282                                 DEBUG("Flow %p cannot be applied",
2283                                       (void *)flow);
2284                                 rte_errno = EINVAL;
2285                                 return rte_errno;
2286                         }
2287 flow_create:
2288                         flow->frxq[i].ibv_flow =
2289                                 ibv_create_flow(flow->frxq[i].hrxq->qp,
2290                                                 flow->frxq[i].ibv_attr);
2291                         if (!flow->frxq[i].ibv_flow) {
2292                                 DEBUG("Flow %p cannot be applied",
2293                                       (void *)flow);
2294                                 rte_errno = EINVAL;
2295                                 return rte_errno;
2296                         }
2297                         DEBUG("Flow %p applied", (void *)flow);
2298                 }
2299                 if (!flow->mark)
2300                         continue;
2301                 for (i = 0; i != flow->queues_n; ++i)
2302                         (*priv->rxqs)[(*flow->queues)[i]]->mark = 1;
2303         }
2304         return 0;
2305 }
2306
2307 /**
2308  * Verify the flow list is empty
2309  *
2310  * @param priv
2311  *  Pointer to private structure.
2312  *
2313  * @return the number of flows not released.
2314  */
2315 int
2316 priv_flow_verify(struct priv *priv)
2317 {
2318         struct rte_flow *flow;
2319         int ret = 0;
2320
2321         TAILQ_FOREACH(flow, &priv->flows, next) {
2322                 DEBUG("%p: flow %p still referenced", (void *)priv,
2323                       (void *)flow);
2324                 ++ret;
2325         }
2326         return ret;
2327 }
2328
2329 /**
2330  * Enable a control flow configured from the control plane.
2331  *
2332  * @param dev
2333  *   Pointer to Ethernet device.
2334  * @param eth_spec
2335  *   An Ethernet flow spec to apply.
2336  * @param eth_mask
2337  *   An Ethernet flow mask to apply.
2338  * @param vlan_spec
2339  *   A VLAN flow spec to apply.
2340  * @param vlan_mask
2341  *   A VLAN flow mask to apply.
2342  *
2343  * @return
2344  *   0 on success.
2345  */
2346 int
2347 mlx5_ctrl_flow_vlan(struct rte_eth_dev *dev,
2348                     struct rte_flow_item_eth *eth_spec,
2349                     struct rte_flow_item_eth *eth_mask,
2350                     struct rte_flow_item_vlan *vlan_spec,
2351                     struct rte_flow_item_vlan *vlan_mask)
2352 {
2353         struct priv *priv = dev->data->dev_private;
2354         const struct rte_flow_attr attr = {
2355                 .ingress = 1,
2356                 .priority = MLX5_CTRL_FLOW_PRIORITY,
2357         };
2358         struct rte_flow_item items[] = {
2359                 {
2360                         .type = RTE_FLOW_ITEM_TYPE_ETH,
2361                         .spec = eth_spec,
2362                         .last = NULL,
2363                         .mask = eth_mask,
2364                 },
2365                 {
2366                         .type = (vlan_spec) ? RTE_FLOW_ITEM_TYPE_VLAN :
2367                                 RTE_FLOW_ITEM_TYPE_END,
2368                         .spec = vlan_spec,
2369                         .last = NULL,
2370                         .mask = vlan_mask,
2371                 },
2372                 {
2373                         .type = RTE_FLOW_ITEM_TYPE_END,
2374                 },
2375         };
2376         struct rte_flow_action actions[] = {
2377                 {
2378                         .type = RTE_FLOW_ACTION_TYPE_RSS,
2379                 },
2380                 {
2381                         .type = RTE_FLOW_ACTION_TYPE_END,
2382                 },
2383         };
2384         struct rte_flow *flow;
2385         struct rte_flow_error error;
2386         unsigned int i;
2387         union {
2388                 struct rte_flow_action_rss rss;
2389                 struct {
2390                         const struct rte_eth_rss_conf *rss_conf;
2391                         uint16_t num;
2392                         uint16_t queue[RTE_MAX_QUEUES_PER_PORT];
2393                 } local;
2394         } action_rss;
2395
2396         if (!priv->reta_idx_n)
2397                 return EINVAL;
2398         for (i = 0; i != priv->reta_idx_n; ++i)
2399                 action_rss.local.queue[i] = (*priv->reta_idx)[i];
2400         action_rss.local.rss_conf = &priv->rss_conf;
2401         action_rss.local.num = priv->reta_idx_n;
2402         actions[0].conf = (const void *)&action_rss.rss;
2403         flow = priv_flow_create(priv, &priv->ctrl_flows, &attr, items, actions,
2404                                 &error);
2405         if (!flow)
2406                 return rte_errno;
2407         return 0;
2408 }
2409
2410 /**
2411  * Enable a flow control configured from the control plane.
2412  *
2413  * @param dev
2414  *   Pointer to Ethernet device.
2415  * @param eth_spec
2416  *   An Ethernet flow spec to apply.
2417  * @param eth_mask
2418  *   An Ethernet flow mask to apply.
2419  *
2420  * @return
2421  *   0 on success.
2422  */
2423 int
2424 mlx5_ctrl_flow(struct rte_eth_dev *dev,
2425                struct rte_flow_item_eth *eth_spec,
2426                struct rte_flow_item_eth *eth_mask)
2427 {
2428         return mlx5_ctrl_flow_vlan(dev, eth_spec, eth_mask, NULL, NULL);
2429 }
2430
2431 /**
2432  * Destroy a flow.
2433  *
2434  * @see rte_flow_destroy()
2435  * @see rte_flow_ops
2436  */
2437 int
2438 mlx5_flow_destroy(struct rte_eth_dev *dev,
2439                   struct rte_flow *flow,
2440                   struct rte_flow_error *error)
2441 {
2442         struct priv *priv = dev->data->dev_private;
2443
2444         (void)error;
2445         priv_lock(priv);
2446         priv_flow_destroy(priv, &priv->flows, flow);
2447         priv_unlock(priv);
2448         return 0;
2449 }
2450
2451 /**
2452  * Destroy all flows.
2453  *
2454  * @see rte_flow_flush()
2455  * @see rte_flow_ops
2456  */
2457 int
2458 mlx5_flow_flush(struct rte_eth_dev *dev,
2459                 struct rte_flow_error *error)
2460 {
2461         struct priv *priv = dev->data->dev_private;
2462
2463         (void)error;
2464         priv_lock(priv);
2465         priv_flow_flush(priv, &priv->flows);
2466         priv_unlock(priv);
2467         return 0;
2468 }
2469
2470 #ifdef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
2471 /**
2472  * Query flow counter.
2473  *
2474  * @param cs
2475  *   the counter set.
2476  * @param counter_value
2477  *   returned data from the counter.
2478  *
2479  * @return
2480  *   0 on success, a errno value otherwise and rte_errno is set.
2481  */
2482 static int
2483 priv_flow_query_count(struct ibv_counter_set *cs,
2484                       struct mlx5_flow_counter_stats *counter_stats,
2485                       struct rte_flow_query_count *query_count,
2486                       struct rte_flow_error *error)
2487 {
2488         uint64_t counters[2];
2489         struct ibv_query_counter_set_attr query_cs_attr = {
2490                 .cs = cs,
2491                 .query_flags = IBV_COUNTER_SET_FORCE_UPDATE,
2492         };
2493         struct ibv_counter_set_data query_out = {
2494                 .out = counters,
2495                 .outlen = 2 * sizeof(uint64_t),
2496         };
2497         int res = ibv_query_counter_set(&query_cs_attr, &query_out);
2498
2499         if (res) {
2500                 rte_flow_error_set(error, -res,
2501                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2502                                    NULL,
2503                                    "cannot read counter");
2504                 return -res;
2505         }
2506         query_count->hits_set = 1;
2507         query_count->bytes_set = 1;
2508         query_count->hits = counters[0] - counter_stats->hits;
2509         query_count->bytes = counters[1] - counter_stats->bytes;
2510         if (query_count->reset) {
2511                 counter_stats->hits = counters[0];
2512                 counter_stats->bytes = counters[1];
2513         }
2514         return 0;
2515 }
2516
2517 /**
2518  * Query a flows.
2519  *
2520  * @see rte_flow_query()
2521  * @see rte_flow_ops
2522  */
2523 int
2524 mlx5_flow_query(struct rte_eth_dev *dev,
2525                 struct rte_flow *flow,
2526                 enum rte_flow_action_type action __rte_unused,
2527                 void *data,
2528                 struct rte_flow_error *error)
2529 {
2530         struct priv *priv = dev->data->dev_private;
2531         int res = EINVAL;
2532
2533         priv_lock(priv);
2534         if (flow->cs) {
2535                 res = priv_flow_query_count(flow->cs,
2536                                         &flow->counter_stats,
2537                                         (struct rte_flow_query_count *)data,
2538                                         error);
2539         } else {
2540                 rte_flow_error_set(error, res,
2541                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2542                                    NULL,
2543                                    "no counter found for flow");
2544         }
2545         priv_unlock(priv);
2546         return -res;
2547 }
2548 #endif
2549
2550 /**
2551  * Isolated mode.
2552  *
2553  * @see rte_flow_isolate()
2554  * @see rte_flow_ops
2555  */
2556 int
2557 mlx5_flow_isolate(struct rte_eth_dev *dev,
2558                   int enable,
2559                   struct rte_flow_error *error)
2560 {
2561         struct priv *priv = dev->data->dev_private;
2562
2563         priv_lock(priv);
2564         if (dev->data->dev_started) {
2565                 rte_flow_error_set(error, EBUSY,
2566                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2567                                    NULL,
2568                                    "port must be stopped first");
2569                 priv_unlock(priv);
2570                 return -rte_errno;
2571         }
2572         priv->isolated = !!enable;
2573         if (enable)
2574                 priv->dev->dev_ops = &mlx5_dev_ops_isolate;
2575         else
2576                 priv->dev->dev_ops = &mlx5_dev_ops;
2577         priv_unlock(priv);
2578         return 0;
2579 }
2580
2581 /**
2582  * Convert a flow director filter to a generic flow.
2583  *
2584  * @param priv
2585  *   Private structure.
2586  * @param fdir_filter
2587  *   Flow director filter to add.
2588  * @param attributes
2589  *   Generic flow parameters structure.
2590  *
2591  * @return
2592  *  0 on success, errno value on error.
2593  */
2594 static int
2595 priv_fdir_filter_convert(struct priv *priv,
2596                          const struct rte_eth_fdir_filter *fdir_filter,
2597                          struct mlx5_fdir *attributes)
2598 {
2599         const struct rte_eth_fdir_input *input = &fdir_filter->input;
2600
2601         /* Validate queue number. */
2602         if (fdir_filter->action.rx_queue >= priv->rxqs_n) {
2603                 ERROR("invalid queue number %d", fdir_filter->action.rx_queue);
2604                 return EINVAL;
2605         }
2606         attributes->attr.ingress = 1;
2607         attributes->items[0] = (struct rte_flow_item) {
2608                 .type = RTE_FLOW_ITEM_TYPE_ETH,
2609                 .spec = &attributes->l2,
2610         };
2611         switch (fdir_filter->action.behavior) {
2612         case RTE_ETH_FDIR_ACCEPT:
2613                 attributes->actions[0] = (struct rte_flow_action){
2614                         .type = RTE_FLOW_ACTION_TYPE_QUEUE,
2615                         .conf = &attributes->queue,
2616                 };
2617                 break;
2618         case RTE_ETH_FDIR_REJECT:
2619                 attributes->actions[0] = (struct rte_flow_action){
2620                         .type = RTE_FLOW_ACTION_TYPE_DROP,
2621                 };
2622                 break;
2623         default:
2624                 ERROR("invalid behavior %d", fdir_filter->action.behavior);
2625                 return ENOTSUP;
2626         }
2627         attributes->queue.index = fdir_filter->action.rx_queue;
2628         switch (fdir_filter->input.flow_type) {
2629         case RTE_ETH_FLOW_NONFRAG_IPV4_UDP:
2630                 attributes->l3.ipv4.hdr = (struct ipv4_hdr){
2631                         .src_addr = input->flow.udp4_flow.ip.src_ip,
2632                         .dst_addr = input->flow.udp4_flow.ip.dst_ip,
2633                         .time_to_live = input->flow.udp4_flow.ip.ttl,
2634                         .type_of_service = input->flow.udp4_flow.ip.tos,
2635                         .next_proto_id = input->flow.udp4_flow.ip.proto,
2636                 };
2637                 attributes->l4.udp.hdr = (struct udp_hdr){
2638                         .src_port = input->flow.udp4_flow.src_port,
2639                         .dst_port = input->flow.udp4_flow.dst_port,
2640                 };
2641                 attributes->items[1] = (struct rte_flow_item){
2642                         .type = RTE_FLOW_ITEM_TYPE_IPV4,
2643                         .spec = &attributes->l3,
2644                 };
2645                 attributes->items[2] = (struct rte_flow_item){
2646                         .type = RTE_FLOW_ITEM_TYPE_UDP,
2647                         .spec = &attributes->l4,
2648                 };
2649                 break;
2650         case RTE_ETH_FLOW_NONFRAG_IPV4_TCP:
2651                 attributes->l3.ipv4.hdr = (struct ipv4_hdr){
2652                         .src_addr = input->flow.tcp4_flow.ip.src_ip,
2653                         .dst_addr = input->flow.tcp4_flow.ip.dst_ip,
2654                         .time_to_live = input->flow.tcp4_flow.ip.ttl,
2655                         .type_of_service = input->flow.tcp4_flow.ip.tos,
2656                         .next_proto_id = input->flow.tcp4_flow.ip.proto,
2657                 };
2658                 attributes->l4.tcp.hdr = (struct tcp_hdr){
2659                         .src_port = input->flow.tcp4_flow.src_port,
2660                         .dst_port = input->flow.tcp4_flow.dst_port,
2661                 };
2662                 attributes->items[1] = (struct rte_flow_item){
2663                         .type = RTE_FLOW_ITEM_TYPE_IPV4,
2664                         .spec = &attributes->l3,
2665                 };
2666                 attributes->items[2] = (struct rte_flow_item){
2667                         .type = RTE_FLOW_ITEM_TYPE_TCP,
2668                         .spec = &attributes->l4,
2669                 };
2670                 break;
2671         case RTE_ETH_FLOW_NONFRAG_IPV4_OTHER:
2672                 attributes->l3.ipv4.hdr = (struct ipv4_hdr){
2673                         .src_addr = input->flow.ip4_flow.src_ip,
2674                         .dst_addr = input->flow.ip4_flow.dst_ip,
2675                         .time_to_live = input->flow.ip4_flow.ttl,
2676                         .type_of_service = input->flow.ip4_flow.tos,
2677                         .next_proto_id = input->flow.ip4_flow.proto,
2678                 };
2679                 attributes->items[1] = (struct rte_flow_item){
2680                         .type = RTE_FLOW_ITEM_TYPE_IPV4,
2681                         .spec = &attributes->l3,
2682                 };
2683                 break;
2684         case RTE_ETH_FLOW_NONFRAG_IPV6_UDP:
2685                 attributes->l3.ipv6.hdr = (struct ipv6_hdr){
2686                         .hop_limits = input->flow.udp6_flow.ip.hop_limits,
2687                         .proto = input->flow.udp6_flow.ip.proto,
2688                 };
2689                 memcpy(attributes->l3.ipv6.hdr.src_addr,
2690                        input->flow.udp6_flow.ip.src_ip,
2691                        RTE_DIM(attributes->l3.ipv6.hdr.src_addr));
2692                 memcpy(attributes->l3.ipv6.hdr.dst_addr,
2693                        input->flow.udp6_flow.ip.dst_ip,
2694                        RTE_DIM(attributes->l3.ipv6.hdr.src_addr));
2695                 attributes->l4.udp.hdr = (struct udp_hdr){
2696                         .src_port = input->flow.udp6_flow.src_port,
2697                         .dst_port = input->flow.udp6_flow.dst_port,
2698                 };
2699                 attributes->items[1] = (struct rte_flow_item){
2700                         .type = RTE_FLOW_ITEM_TYPE_IPV6,
2701                         .spec = &attributes->l3,
2702                 };
2703                 attributes->items[2] = (struct rte_flow_item){
2704                         .type = RTE_FLOW_ITEM_TYPE_UDP,
2705                         .spec = &attributes->l4,
2706                 };
2707                 break;
2708         case RTE_ETH_FLOW_NONFRAG_IPV6_TCP:
2709                 attributes->l3.ipv6.hdr = (struct ipv6_hdr){
2710                         .hop_limits = input->flow.tcp6_flow.ip.hop_limits,
2711                         .proto = input->flow.tcp6_flow.ip.proto,
2712                 };
2713                 memcpy(attributes->l3.ipv6.hdr.src_addr,
2714                        input->flow.tcp6_flow.ip.src_ip,
2715                        RTE_DIM(attributes->l3.ipv6.hdr.src_addr));
2716                 memcpy(attributes->l3.ipv6.hdr.dst_addr,
2717                        input->flow.tcp6_flow.ip.dst_ip,
2718                        RTE_DIM(attributes->l3.ipv6.hdr.src_addr));
2719                 attributes->l4.tcp.hdr = (struct tcp_hdr){
2720                         .src_port = input->flow.tcp6_flow.src_port,
2721                         .dst_port = input->flow.tcp6_flow.dst_port,
2722                 };
2723                 attributes->items[1] = (struct rte_flow_item){
2724                         .type = RTE_FLOW_ITEM_TYPE_IPV6,
2725                         .spec = &attributes->l3,
2726                 };
2727                 attributes->items[2] = (struct rte_flow_item){
2728                         .type = RTE_FLOW_ITEM_TYPE_UDP,
2729                         .spec = &attributes->l4,
2730                 };
2731                 break;
2732         case RTE_ETH_FLOW_NONFRAG_IPV6_OTHER:
2733                 attributes->l3.ipv6.hdr = (struct ipv6_hdr){
2734                         .hop_limits = input->flow.ipv6_flow.hop_limits,
2735                         .proto = input->flow.ipv6_flow.proto,
2736                 };
2737                 memcpy(attributes->l3.ipv6.hdr.src_addr,
2738                        input->flow.ipv6_flow.src_ip,
2739                        RTE_DIM(attributes->l3.ipv6.hdr.src_addr));
2740                 memcpy(attributes->l3.ipv6.hdr.dst_addr,
2741                        input->flow.ipv6_flow.dst_ip,
2742                        RTE_DIM(attributes->l3.ipv6.hdr.src_addr));
2743                 attributes->items[1] = (struct rte_flow_item){
2744                         .type = RTE_FLOW_ITEM_TYPE_IPV6,
2745                         .spec = &attributes->l3,
2746                 };
2747                 break;
2748         default:
2749                 ERROR("invalid flow type%d",
2750                       fdir_filter->input.flow_type);
2751                 return ENOTSUP;
2752         }
2753         return 0;
2754 }
2755
2756 /**
2757  * Add new flow director filter and store it in list.
2758  *
2759  * @param priv
2760  *   Private structure.
2761  * @param fdir_filter
2762  *   Flow director filter to add.
2763  *
2764  * @return
2765  *   0 on success, errno value on failure.
2766  */
2767 static int
2768 priv_fdir_filter_add(struct priv *priv,
2769                      const struct rte_eth_fdir_filter *fdir_filter)
2770 {
2771         struct mlx5_fdir attributes = {
2772                 .attr.group = 0,
2773         };
2774         struct mlx5_flow_parse parser = {
2775                 .layer = HASH_RXQ_ETH,
2776         };
2777         struct rte_flow_error error;
2778         struct rte_flow *flow;
2779         int ret;
2780
2781         ret = priv_fdir_filter_convert(priv, fdir_filter, &attributes);
2782         if (ret)
2783                 return -ret;
2784         ret = priv_flow_convert(priv, &attributes.attr, attributes.items,
2785                                 attributes.actions, &error, &parser);
2786         if (ret)
2787                 return -ret;
2788         flow = priv_flow_create(priv,
2789                                 &priv->flows,
2790                                 &attributes.attr,
2791                                 attributes.items,
2792                                 attributes.actions,
2793                                 &error);
2794         if (flow) {
2795                 TAILQ_INSERT_TAIL(&priv->flows, flow, next);
2796                 DEBUG("FDIR created %p", (void *)flow);
2797                 return 0;
2798         }
2799         return ENOTSUP;
2800 }
2801
2802 /**
2803  * Delete specific filter.
2804  *
2805  * @param priv
2806  *   Private structure.
2807  * @param fdir_filter
2808  *   Filter to be deleted.
2809  *
2810  * @return
2811  *   0 on success, errno value on failure.
2812  */
2813 static int
2814 priv_fdir_filter_delete(struct priv *priv,
2815                         const struct rte_eth_fdir_filter *fdir_filter)
2816 {
2817         struct mlx5_fdir attributes;
2818         struct mlx5_flow_parse parser = {
2819                 .create = 1,
2820                 .layer = HASH_RXQ_ETH,
2821         };
2822         struct rte_flow_error error;
2823         struct rte_flow *flow;
2824         unsigned int i;
2825         int ret;
2826
2827         ret = priv_fdir_filter_convert(priv, fdir_filter, &attributes);
2828         if (ret)
2829                 return -ret;
2830         ret = priv_flow_convert(priv, &attributes.attr, attributes.items,
2831                                 attributes.actions, &error, &parser);
2832         if (ret)
2833                 goto exit;
2834         TAILQ_FOREACH(flow, &priv->flows, next) {
2835                 struct ibv_flow_attr *attr;
2836                 struct ibv_spec_header *attr_h;
2837                 void *spec;
2838                 struct ibv_flow_attr *flow_attr;
2839                 struct ibv_spec_header *flow_h;
2840                 void *flow_spec;
2841                 unsigned int specs_n;
2842
2843                 if (parser.drop)
2844                         attr = parser.drop_q.ibv_attr;
2845                 else
2846                         attr = parser.queue[HASH_RXQ_ETH].ibv_attr;
2847                 if (flow->drop)
2848                         flow_attr = flow->drxq.ibv_attr;
2849                 else
2850                         flow_attr = flow->frxq[HASH_RXQ_ETH].ibv_attr;
2851                 /* Compare first the attributes. */
2852                 if (memcmp(attr, flow_attr, sizeof(struct ibv_flow_attr)))
2853                         continue;
2854                 if (attr->num_of_specs == 0)
2855                         continue;
2856                 spec = (void *)((uintptr_t)attr +
2857                                 sizeof(struct ibv_flow_attr));
2858                 flow_spec = (void *)((uintptr_t)flow_attr +
2859                                      sizeof(struct ibv_flow_attr));
2860                 specs_n = RTE_MIN(attr->num_of_specs, flow_attr->num_of_specs);
2861                 for (i = 0; i != specs_n; ++i) {
2862                         attr_h = spec;
2863                         flow_h = flow_spec;
2864                         if (memcmp(spec, flow_spec,
2865                                    RTE_MIN(attr_h->size, flow_h->size)))
2866                                 continue;
2867                         spec = (void *)((uintptr_t)attr + attr_h->size);
2868                         flow_spec = (void *)((uintptr_t)flow_attr +
2869                                              flow_h->size);
2870                 }
2871                 /* At this point, the flow match. */
2872                 break;
2873         }
2874         if (flow)
2875                 priv_flow_destroy(priv, &priv->flows, flow);
2876 exit:
2877         if (parser.drop) {
2878                 rte_free(parser.drop_q.ibv_attr);
2879         } else {
2880                 for (i = 0; i != hash_rxq_init_n; ++i) {
2881                         if (parser.queue[i].ibv_attr)
2882                                 rte_free(parser.queue[i].ibv_attr);
2883                 }
2884         }
2885         return -ret;
2886 }
2887
2888 /**
2889  * Update queue for specific filter.
2890  *
2891  * @param priv
2892  *   Private structure.
2893  * @param fdir_filter
2894  *   Filter to be updated.
2895  *
2896  * @return
2897  *   0 on success, errno value on failure.
2898  */
2899 static int
2900 priv_fdir_filter_update(struct priv *priv,
2901                         const struct rte_eth_fdir_filter *fdir_filter)
2902 {
2903         int ret;
2904
2905         ret = priv_fdir_filter_delete(priv, fdir_filter);
2906         if (ret)
2907                 return ret;
2908         ret = priv_fdir_filter_add(priv, fdir_filter);
2909         return ret;
2910 }
2911
2912 /**
2913  * Flush all filters.
2914  *
2915  * @param priv
2916  *   Private structure.
2917  */
2918 static void
2919 priv_fdir_filter_flush(struct priv *priv)
2920 {
2921         priv_flow_flush(priv, &priv->flows);
2922 }
2923
2924 /**
2925  * Get flow director information.
2926  *
2927  * @param priv
2928  *   Private structure.
2929  * @param[out] fdir_info
2930  *   Resulting flow director information.
2931  */
2932 static void
2933 priv_fdir_info_get(struct priv *priv, struct rte_eth_fdir_info *fdir_info)
2934 {
2935         struct rte_eth_fdir_masks *mask =
2936                 &priv->dev->data->dev_conf.fdir_conf.mask;
2937
2938         fdir_info->mode = priv->dev->data->dev_conf.fdir_conf.mode;
2939         fdir_info->guarant_spc = 0;
2940         rte_memcpy(&fdir_info->mask, mask, sizeof(fdir_info->mask));
2941         fdir_info->max_flexpayload = 0;
2942         fdir_info->flow_types_mask[0] = 0;
2943         fdir_info->flex_payload_unit = 0;
2944         fdir_info->max_flex_payload_segment_num = 0;
2945         fdir_info->flex_payload_limit = 0;
2946         memset(&fdir_info->flex_conf, 0, sizeof(fdir_info->flex_conf));
2947 }
2948
2949 /**
2950  * Deal with flow director operations.
2951  *
2952  * @param priv
2953  *   Pointer to private structure.
2954  * @param filter_op
2955  *   Operation to perform.
2956  * @param arg
2957  *   Pointer to operation-specific structure.
2958  *
2959  * @return
2960  *   0 on success, errno value on failure.
2961  */
2962 static int
2963 priv_fdir_ctrl_func(struct priv *priv, enum rte_filter_op filter_op, void *arg)
2964 {
2965         enum rte_fdir_mode fdir_mode =
2966                 priv->dev->data->dev_conf.fdir_conf.mode;
2967         int ret = 0;
2968
2969         if (filter_op == RTE_ETH_FILTER_NOP)
2970                 return 0;
2971         if (fdir_mode != RTE_FDIR_MODE_PERFECT &&
2972             fdir_mode != RTE_FDIR_MODE_PERFECT_MAC_VLAN) {
2973                 ERROR("%p: flow director mode %d not supported",
2974                       (void *)priv, fdir_mode);
2975                 return EINVAL;
2976         }
2977         switch (filter_op) {
2978         case RTE_ETH_FILTER_ADD:
2979                 ret = priv_fdir_filter_add(priv, arg);
2980                 break;
2981         case RTE_ETH_FILTER_UPDATE:
2982                 ret = priv_fdir_filter_update(priv, arg);
2983                 break;
2984         case RTE_ETH_FILTER_DELETE:
2985                 ret = priv_fdir_filter_delete(priv, arg);
2986                 break;
2987         case RTE_ETH_FILTER_FLUSH:
2988                 priv_fdir_filter_flush(priv);
2989                 break;
2990         case RTE_ETH_FILTER_INFO:
2991                 priv_fdir_info_get(priv, arg);
2992                 break;
2993         default:
2994                 DEBUG("%p: unknown operation %u", (void *)priv,
2995                       filter_op);
2996                 ret = EINVAL;
2997                 break;
2998         }
2999         return ret;
3000 }
3001
3002 /**
3003  * Manage filter operations.
3004  *
3005  * @param dev
3006  *   Pointer to Ethernet device structure.
3007  * @param filter_type
3008  *   Filter type.
3009  * @param filter_op
3010  *   Operation to perform.
3011  * @param arg
3012  *   Pointer to operation-specific structure.
3013  *
3014  * @return
3015  *   0 on success, negative errno value on failure.
3016  */
3017 int
3018 mlx5_dev_filter_ctrl(struct rte_eth_dev *dev,
3019                      enum rte_filter_type filter_type,
3020                      enum rte_filter_op filter_op,
3021                      void *arg)
3022 {
3023         int ret = EINVAL;
3024         struct priv *priv = dev->data->dev_private;
3025
3026         switch (filter_type) {
3027         case RTE_ETH_FILTER_GENERIC:
3028                 if (filter_op != RTE_ETH_FILTER_GET)
3029                         return -EINVAL;
3030                 *(const void **)arg = &mlx5_flow_ops;
3031                 return 0;
3032         case RTE_ETH_FILTER_FDIR:
3033                 priv_lock(priv);
3034                 ret = priv_fdir_ctrl_func(priv, filter_op, arg);
3035                 priv_unlock(priv);
3036                 break;
3037         default:
3038                 ERROR("%p: filter type (%d) not supported",
3039                       (void *)dev, filter_type);
3040                 break;
3041         }
3042         return -ret;
3043 }