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