net/mlx5: move verbs flows and attributes
[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 static int
59 mlx5_flow_create_eth(const struct rte_flow_item *item,
60                      const void *default_mask,
61                      void *data);
62
63 static int
64 mlx5_flow_create_vlan(const struct rte_flow_item *item,
65                       const void *default_mask,
66                       void *data);
67
68 static int
69 mlx5_flow_create_ipv4(const struct rte_flow_item *item,
70                       const void *default_mask,
71                       void *data);
72
73 static int
74 mlx5_flow_create_ipv6(const struct rte_flow_item *item,
75                       const void *default_mask,
76                       void *data);
77
78 static int
79 mlx5_flow_create_udp(const struct rte_flow_item *item,
80                      const void *default_mask,
81                      void *data);
82
83 static int
84 mlx5_flow_create_tcp(const struct rte_flow_item *item,
85                      const void *default_mask,
86                      void *data);
87
88 static int
89 mlx5_flow_create_vxlan(const struct rte_flow_item *item,
90                        const void *default_mask,
91                        void *data);
92
93 /** Structure for Drop queue. */
94 struct mlx5_hrxq_drop {
95         struct ibv_rwq_ind_table *ind_table; /**< Indirection table. */
96         struct ibv_qp *qp; /**< Verbs queue pair. */
97         struct ibv_wq *wq; /**< Verbs work queue. */
98         struct ibv_cq *cq; /**< Verbs completion queue. */
99 };
100
101 /* Flows structures. */
102 struct mlx5_flow {
103         uint64_t hash_fields; /**< Fields that participate in the hash. */
104         struct ibv_flow_attr *ibv_attr; /**< Pointer to Verbs attributes. */
105         struct ibv_flow *ibv_flow; /**< Verbs flow. */
106         struct mlx5_hrxq *hrxq; /**< Hash Rx queues. */
107 };
108
109 /* Drop flows structures. */
110 struct mlx5_flow_drop {
111         struct ibv_flow_attr *ibv_attr; /**< Pointer to Verbs attributes. */
112         struct ibv_flow *ibv_flow; /**< Verbs flow. */
113         struct mlx5_hrxq_drop hrxq; /**< Drop hash Rx queue. */
114 };
115
116 struct rte_flow {
117         TAILQ_ENTRY(rte_flow) next; /**< Pointer to the next flow structure. */
118         uint32_t mark:1; /**< Set if the flow is marked. */
119         uint32_t drop:1; /**< Drop queue. */
120         uint16_t queues_n; /**< Number of entries in queue[]. */
121         uint16_t (*queues)[]; /**< Queues indexes to use. */
122         union {
123                 struct mlx5_flow frxq; /**< Flow with Rx queue. */
124                 struct mlx5_flow_drop drxq; /**< Flow with drop Rx queue. */
125         };
126 };
127
128 /** Static initializer for items. */
129 #define ITEMS(...) \
130         (const enum rte_flow_item_type []){ \
131                 __VA_ARGS__, RTE_FLOW_ITEM_TYPE_END, \
132         }
133
134 /** Structure to generate a simple graph of layers supported by the NIC. */
135 struct mlx5_flow_items {
136         /** List of possible actions for these items. */
137         const enum rte_flow_action_type *const actions;
138         /** Bit-masks corresponding to the possibilities for the item. */
139         const void *mask;
140         /**
141          * Default bit-masks to use when item->mask is not provided. When
142          * \default_mask is also NULL, the full supported bit-mask (\mask) is
143          * used instead.
144          */
145         const void *default_mask;
146         /** Bit-masks size in bytes. */
147         const unsigned int mask_sz;
148         /**
149          * Conversion function from rte_flow to NIC specific flow.
150          *
151          * @param item
152          *   rte_flow item to convert.
153          * @param default_mask
154          *   Default bit-masks to use when item->mask is not provided.
155          * @param data
156          *   Internal structure to store the conversion.
157          *
158          * @return
159          *   0 on success, negative value otherwise.
160          */
161         int (*convert)(const struct rte_flow_item *item,
162                        const void *default_mask,
163                        void *data);
164         /** Size in bytes of the destination structure. */
165         const unsigned int dst_sz;
166         /** List of possible following items.  */
167         const enum rte_flow_item_type *const items;
168 };
169
170 /** Valid action for this PMD. */
171 static const enum rte_flow_action_type valid_actions[] = {
172         RTE_FLOW_ACTION_TYPE_DROP,
173         RTE_FLOW_ACTION_TYPE_QUEUE,
174         RTE_FLOW_ACTION_TYPE_MARK,
175         RTE_FLOW_ACTION_TYPE_FLAG,
176         RTE_FLOW_ACTION_TYPE_END,
177 };
178
179 /** Graph of supported items and associated actions. */
180 static const struct mlx5_flow_items mlx5_flow_items[] = {
181         [RTE_FLOW_ITEM_TYPE_END] = {
182                 .items = ITEMS(RTE_FLOW_ITEM_TYPE_ETH,
183                                RTE_FLOW_ITEM_TYPE_VXLAN),
184         },
185         [RTE_FLOW_ITEM_TYPE_ETH] = {
186                 .items = ITEMS(RTE_FLOW_ITEM_TYPE_VLAN,
187                                RTE_FLOW_ITEM_TYPE_IPV4,
188                                RTE_FLOW_ITEM_TYPE_IPV6),
189                 .actions = valid_actions,
190                 .mask = &(const struct rte_flow_item_eth){
191                         .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
192                         .src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
193                         .type = -1,
194                 },
195                 .default_mask = &rte_flow_item_eth_mask,
196                 .mask_sz = sizeof(struct rte_flow_item_eth),
197                 .convert = mlx5_flow_create_eth,
198                 .dst_sz = sizeof(struct ibv_flow_spec_eth),
199         },
200         [RTE_FLOW_ITEM_TYPE_VLAN] = {
201                 .items = ITEMS(RTE_FLOW_ITEM_TYPE_IPV4,
202                                RTE_FLOW_ITEM_TYPE_IPV6),
203                 .actions = valid_actions,
204                 .mask = &(const struct rte_flow_item_vlan){
205                         .tci = -1,
206                 },
207                 .default_mask = &rte_flow_item_vlan_mask,
208                 .mask_sz = sizeof(struct rte_flow_item_vlan),
209                 .convert = mlx5_flow_create_vlan,
210                 .dst_sz = 0,
211         },
212         [RTE_FLOW_ITEM_TYPE_IPV4] = {
213                 .items = ITEMS(RTE_FLOW_ITEM_TYPE_UDP,
214                                RTE_FLOW_ITEM_TYPE_TCP),
215                 .actions = valid_actions,
216                 .mask = &(const struct rte_flow_item_ipv4){
217                         .hdr = {
218                                 .src_addr = -1,
219                                 .dst_addr = -1,
220                                 .type_of_service = -1,
221                                 .next_proto_id = -1,
222                         },
223                 },
224                 .default_mask = &rte_flow_item_ipv4_mask,
225                 .mask_sz = sizeof(struct rte_flow_item_ipv4),
226                 .convert = mlx5_flow_create_ipv4,
227                 .dst_sz = sizeof(struct ibv_flow_spec_ipv4),
228         },
229         [RTE_FLOW_ITEM_TYPE_IPV6] = {
230                 .items = ITEMS(RTE_FLOW_ITEM_TYPE_UDP,
231                                RTE_FLOW_ITEM_TYPE_TCP),
232                 .actions = valid_actions,
233                 .mask = &(const struct rte_flow_item_ipv6){
234                         .hdr = {
235                                 .src_addr = {
236                                         0xff, 0xff, 0xff, 0xff,
237                                         0xff, 0xff, 0xff, 0xff,
238                                         0xff, 0xff, 0xff, 0xff,
239                                         0xff, 0xff, 0xff, 0xff,
240                                 },
241                                 .dst_addr = {
242                                         0xff, 0xff, 0xff, 0xff,
243                                         0xff, 0xff, 0xff, 0xff,
244                                         0xff, 0xff, 0xff, 0xff,
245                                         0xff, 0xff, 0xff, 0xff,
246                                 },
247                                 .vtc_flow = -1,
248                                 .proto = -1,
249                                 .hop_limits = -1,
250                         },
251                 },
252                 .default_mask = &rte_flow_item_ipv6_mask,
253                 .mask_sz = sizeof(struct rte_flow_item_ipv6),
254                 .convert = mlx5_flow_create_ipv6,
255                 .dst_sz = sizeof(struct ibv_flow_spec_ipv6),
256         },
257         [RTE_FLOW_ITEM_TYPE_UDP] = {
258                 .items = ITEMS(RTE_FLOW_ITEM_TYPE_VXLAN),
259                 .actions = valid_actions,
260                 .mask = &(const struct rte_flow_item_udp){
261                         .hdr = {
262                                 .src_port = -1,
263                                 .dst_port = -1,
264                         },
265                 },
266                 .default_mask = &rte_flow_item_udp_mask,
267                 .mask_sz = sizeof(struct rte_flow_item_udp),
268                 .convert = mlx5_flow_create_udp,
269                 .dst_sz = sizeof(struct ibv_flow_spec_tcp_udp),
270         },
271         [RTE_FLOW_ITEM_TYPE_TCP] = {
272                 .actions = valid_actions,
273                 .mask = &(const struct rte_flow_item_tcp){
274                         .hdr = {
275                                 .src_port = -1,
276                                 .dst_port = -1,
277                         },
278                 },
279                 .default_mask = &rte_flow_item_tcp_mask,
280                 .mask_sz = sizeof(struct rte_flow_item_tcp),
281                 .convert = mlx5_flow_create_tcp,
282                 .dst_sz = sizeof(struct ibv_flow_spec_tcp_udp),
283         },
284         [RTE_FLOW_ITEM_TYPE_VXLAN] = {
285                 .items = ITEMS(RTE_FLOW_ITEM_TYPE_ETH),
286                 .actions = valid_actions,
287                 .mask = &(const struct rte_flow_item_vxlan){
288                         .vni = "\xff\xff\xff",
289                 },
290                 .default_mask = &rte_flow_item_vxlan_mask,
291                 .mask_sz = sizeof(struct rte_flow_item_vxlan),
292                 .convert = mlx5_flow_create_vxlan,
293                 .dst_sz = sizeof(struct ibv_flow_spec_tunnel),
294         },
295 };
296
297 /** Structure to pass to the conversion function. */
298 struct mlx5_flow_parse {
299         struct ibv_flow_attr *ibv_attr; /**< Verbs attribute. */
300         unsigned int offset; /**< Offset in bytes in the ibv_attr buffer. */
301         uint32_t inner; /**< Set once VXLAN is encountered. */
302         uint32_t create:1; /**< Leave allocated resources on exit. */
303         uint32_t queue:1; /**< Target is a receive queue. */
304         uint32_t drop:1; /**< Target is a drop queue. */
305         uint32_t mark:1; /**< Mark is present in the flow. */
306         uint32_t mark_id; /**< Mark identifier. */
307         uint64_t hash_fields; /**< Fields that participate in the hash. */
308         uint16_t queues[RTE_MAX_QUEUES_PER_PORT]; /**< Queues indexes to use. */
309         uint16_t queues_n; /**< Number of entries in queue[]. */
310 };
311
312 static const struct rte_flow_ops mlx5_flow_ops = {
313         .validate = mlx5_flow_validate,
314         .create = mlx5_flow_create,
315         .destroy = mlx5_flow_destroy,
316         .flush = mlx5_flow_flush,
317         .query = NULL,
318         .isolate = mlx5_flow_isolate,
319 };
320
321 /**
322  * Manage filter operations.
323  *
324  * @param dev
325  *   Pointer to Ethernet device structure.
326  * @param filter_type
327  *   Filter type.
328  * @param filter_op
329  *   Operation to perform.
330  * @param arg
331  *   Pointer to operation-specific structure.
332  *
333  * @return
334  *   0 on success, negative errno value on failure.
335  */
336 int
337 mlx5_dev_filter_ctrl(struct rte_eth_dev *dev,
338                      enum rte_filter_type filter_type,
339                      enum rte_filter_op filter_op,
340                      void *arg)
341 {
342         int ret = EINVAL;
343
344         if (filter_type == RTE_ETH_FILTER_GENERIC) {
345                 if (filter_op != RTE_ETH_FILTER_GET)
346                         return -EINVAL;
347                 *(const void **)arg = &mlx5_flow_ops;
348                 return 0;
349         }
350         ERROR("%p: filter type (%d) not supported",
351               (void *)dev, filter_type);
352         return -ret;
353 }
354
355 /**
356  * Check support for a given item.
357  *
358  * @param item[in]
359  *   Item specification.
360  * @param mask[in]
361  *   Bit-masks covering supported fields to compare with spec, last and mask in
362  *   \item.
363  * @param size
364  *   Bit-Mask size in bytes.
365  *
366  * @return
367  *   0 on success.
368  */
369 static int
370 mlx5_flow_item_validate(const struct rte_flow_item *item,
371                         const uint8_t *mask, unsigned int size)
372 {
373         int ret = 0;
374
375         if (!item->spec && (item->mask || item->last))
376                 return -1;
377         if (item->spec && !item->mask) {
378                 unsigned int i;
379                 const uint8_t *spec = item->spec;
380
381                 for (i = 0; i < size; ++i)
382                         if ((spec[i] | mask[i]) != mask[i])
383                                 return -1;
384         }
385         if (item->last && !item->mask) {
386                 unsigned int i;
387                 const uint8_t *spec = item->last;
388
389                 for (i = 0; i < size; ++i)
390                         if ((spec[i] | mask[i]) != mask[i])
391                                 return -1;
392         }
393         if (item->mask) {
394                 unsigned int i;
395                 const uint8_t *spec = item->mask;
396
397                 for (i = 0; i < size; ++i)
398                         if ((spec[i] | mask[i]) != mask[i])
399                                 return -1;
400         }
401         if (item->spec && item->last) {
402                 uint8_t spec[size];
403                 uint8_t last[size];
404                 const uint8_t *apply = mask;
405                 unsigned int i;
406
407                 if (item->mask)
408                         apply = item->mask;
409                 for (i = 0; i < size; ++i) {
410                         spec[i] = ((const uint8_t *)item->spec)[i] & apply[i];
411                         last[i] = ((const uint8_t *)item->last)[i] & apply[i];
412                 }
413                 ret = memcmp(spec, last, size);
414         }
415         return ret;
416 }
417
418 /**
419  * Validate and convert a flow supported by the NIC.
420  *
421  * @param priv
422  *   Pointer to private structure.
423  * @param[in] attr
424  *   Flow rule attributes.
425  * @param[in] pattern
426  *   Pattern specification (list terminated by the END pattern item).
427  * @param[in] actions
428  *   Associated actions (list terminated by the END action).
429  * @param[out] error
430  *   Perform verbose error reporting if not NULL.
431  * @param[in, out] parser
432  *   Internal parser structure.
433  *
434  * @return
435  *   0 on success, a negative errno value otherwise and rte_errno is set.
436  */
437 static int
438 priv_flow_convert(struct priv *priv,
439                   const struct rte_flow_attr *attr,
440                   const struct rte_flow_item items[],
441                   const struct rte_flow_action actions[],
442                   struct rte_flow_error *error,
443                   struct mlx5_flow_parse *parser)
444 {
445         const struct mlx5_flow_items *cur_item = mlx5_flow_items;
446
447         (void)priv;
448         *parser = (struct mlx5_flow_parse){
449                 .ibv_attr = parser->ibv_attr,
450                 .create = parser->create,
451                 .offset = sizeof(struct ibv_flow_attr),
452                 .mark_id = MLX5_FLOW_MARK_DEFAULT,
453         };
454         if (attr->group) {
455                 rte_flow_error_set(error, ENOTSUP,
456                                    RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
457                                    NULL,
458                                    "groups are not supported");
459                 return -rte_errno;
460         }
461         if (attr->priority && attr->priority != MLX5_CTRL_FLOW_PRIORITY) {
462                 rte_flow_error_set(error, ENOTSUP,
463                                    RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
464                                    NULL,
465                                    "priorities are not supported");
466                 return -rte_errno;
467         }
468         if (attr->egress) {
469                 rte_flow_error_set(error, ENOTSUP,
470                                    RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
471                                    NULL,
472                                    "egress is not supported");
473                 return -rte_errno;
474         }
475         if (!attr->ingress) {
476                 rte_flow_error_set(error, ENOTSUP,
477                                    RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
478                                    NULL,
479                                    "only ingress is supported");
480                 return -rte_errno;
481         }
482         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; ++actions) {
483                 if (actions->type == RTE_FLOW_ACTION_TYPE_VOID) {
484                         continue;
485                 } else if (actions->type == RTE_FLOW_ACTION_TYPE_DROP) {
486                         parser->drop = 1;
487                 } else if (actions->type == RTE_FLOW_ACTION_TYPE_QUEUE) {
488                         const struct rte_flow_action_queue *queue =
489                                 (const struct rte_flow_action_queue *)
490                                 actions->conf;
491                         uint16_t n;
492                         uint16_t found = 0;
493
494                         if (!queue || (queue->index > (priv->rxqs_n - 1)))
495                                 goto exit_action_not_supported;
496                         for (n = 0; n < parser->queues_n; ++n) {
497                                 if (parser->queues[n] == queue->index) {
498                                         found = 1;
499                                         break;
500                                 }
501                         }
502                         if (parser->queues_n > 1 && !found) {
503                                 rte_flow_error_set(error, ENOTSUP,
504                                            RTE_FLOW_ERROR_TYPE_ACTION,
505                                            actions,
506                                            "queue action not in RSS queues");
507                                 return -rte_errno;
508                         }
509                         if (!found) {
510                                 parser->queue = 1;
511                                 parser->queues_n = 1;
512                                 parser->queues[0] = queue->index;
513                         }
514                 } else if (actions->type == RTE_FLOW_ACTION_TYPE_RSS) {
515                         const struct rte_flow_action_rss *rss =
516                                 (const struct rte_flow_action_rss *)
517                                 actions->conf;
518                         uint16_t n;
519
520                         if (!rss || !rss->num) {
521                                 rte_flow_error_set(error, EINVAL,
522                                                    RTE_FLOW_ERROR_TYPE_ACTION,
523                                                    actions,
524                                                    "no valid queues");
525                                 return -rte_errno;
526                         }
527                         if (parser->queues_n == 1) {
528                                 uint16_t found = 0;
529
530                                 assert(parser->queues_n);
531                                 for (n = 0; n < rss->num; ++n) {
532                                         if (parser->queues[0] ==
533                                             rss->queue[n]) {
534                                                 found = 1;
535                                                 break;
536                                         }
537                                 }
538                                 if (!found) {
539                                         rte_flow_error_set(error, ENOTSUP,
540                                                    RTE_FLOW_ERROR_TYPE_ACTION,
541                                                    actions,
542                                                    "queue action not in RSS"
543                                                    " queues");
544                                         return -rte_errno;
545                                 }
546                         }
547                         for (n = 0; n < rss->num; ++n) {
548                                 if (rss->queue[n] >= priv->rxqs_n) {
549                                         rte_flow_error_set(error, EINVAL,
550                                                    RTE_FLOW_ERROR_TYPE_ACTION,
551                                                    actions,
552                                                    "queue id > number of"
553                                                    " queues");
554                                         return -rte_errno;
555                                 }
556                         }
557                         parser->queue = 1;
558                         for (n = 0; n < rss->num; ++n)
559                                 parser->queues[n] = rss->queue[n];
560                         parser->queues_n = rss->num;
561                 } else if (actions->type == RTE_FLOW_ACTION_TYPE_MARK) {
562                         const struct rte_flow_action_mark *mark =
563                                 (const struct rte_flow_action_mark *)
564                                 actions->conf;
565
566                         if (!mark) {
567                                 rte_flow_error_set(error, EINVAL,
568                                                    RTE_FLOW_ERROR_TYPE_ACTION,
569                                                    actions,
570                                                    "mark must be defined");
571                                 return -rte_errno;
572                         } else if (mark->id >= MLX5_FLOW_MARK_MAX) {
573                                 rte_flow_error_set(error, ENOTSUP,
574                                                    RTE_FLOW_ERROR_TYPE_ACTION,
575                                                    actions,
576                                                    "mark must be between 0"
577                                                    " and 16777199");
578                                 return -rte_errno;
579                         }
580                         parser->mark = 1;
581                         parser->mark_id = mark->id;
582                 } else if (actions->type == RTE_FLOW_ACTION_TYPE_FLAG) {
583                         parser->mark = 1;
584                 } else {
585                         goto exit_action_not_supported;
586                 }
587         }
588         if (parser->mark && !parser->ibv_attr && !parser->drop)
589                 parser->offset += sizeof(struct ibv_flow_spec_action_tag);
590         if (!parser->ibv_attr && parser->drop)
591                 parser->offset += sizeof(struct ibv_flow_spec_action_drop);
592         if (!parser->queue && !parser->drop) {
593                 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
594                                    NULL, "no valid action");
595                 return -rte_errno;
596         }
597         for (; items->type != RTE_FLOW_ITEM_TYPE_END; ++items) {
598                 const struct mlx5_flow_items *token = NULL;
599                 unsigned int i;
600                 int err;
601
602                 if (items->type == RTE_FLOW_ITEM_TYPE_VOID)
603                         continue;
604                 for (i = 0;
605                      cur_item->items &&
606                      cur_item->items[i] != RTE_FLOW_ITEM_TYPE_END;
607                      ++i) {
608                         if (cur_item->items[i] == items->type) {
609                                 token = &mlx5_flow_items[items->type];
610                                 break;
611                         }
612                 }
613                 if (!token)
614                         goto exit_item_not_supported;
615                 cur_item = token;
616                 err = mlx5_flow_item_validate(items,
617                                               (const uint8_t *)cur_item->mask,
618                                               cur_item->mask_sz);
619                 if (err)
620                         goto exit_item_not_supported;
621                 if (parser->ibv_attr && cur_item->convert) {
622                         err = cur_item->convert(items,
623                                                 (cur_item->default_mask ?
624                                                  cur_item->default_mask :
625                                                  cur_item->mask),
626                                                 parser);
627                         if (err)
628                                 goto exit_item_not_supported;
629                 } else if (items->type == RTE_FLOW_ITEM_TYPE_VXLAN) {
630                         if (parser->inner) {
631                                 rte_flow_error_set(error, ENOTSUP,
632                                                    RTE_FLOW_ERROR_TYPE_ITEM,
633                                                    items,
634                                                    "cannot recognize multiple"
635                                                    " VXLAN encapsulations");
636                                 return -rte_errno;
637                         }
638                         parser->inner = 1;
639                 }
640                 parser->offset += cur_item->dst_sz;
641         }
642         return 0;
643 exit_item_not_supported:
644         rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
645                            items, "item not supported");
646         return -rte_errno;
647 exit_action_not_supported:
648         rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
649                            actions, "action not supported");
650         return -rte_errno;
651 }
652
653 /**
654  * Convert Ethernet item to Verbs specification.
655  *
656  * @param item[in]
657  *   Item specification.
658  * @param default_mask[in]
659  *   Default bit-masks to use when item->mask is not provided.
660  * @param data[in, out]
661  *   User structure.
662  */
663 static int
664 mlx5_flow_create_eth(const struct rte_flow_item *item,
665                      const void *default_mask,
666                      void *data)
667 {
668         const struct rte_flow_item_eth *spec = item->spec;
669         const struct rte_flow_item_eth *mask = item->mask;
670         struct mlx5_flow_parse *parser = (struct mlx5_flow_parse *)data;
671         struct ibv_flow_spec_eth *eth;
672         const unsigned int eth_size = sizeof(struct ibv_flow_spec_eth);
673         unsigned int i;
674
675         ++parser->ibv_attr->num_of_specs;
676         parser->hash_fields = 0;
677         eth = (void *)((uintptr_t)parser->ibv_attr + parser->offset);
678         *eth = (struct ibv_flow_spec_eth) {
679                 .type = parser->inner | IBV_FLOW_SPEC_ETH,
680                 .size = eth_size,
681         };
682         if (!spec)
683                 return 0;
684         if (!mask)
685                 mask = default_mask;
686         memcpy(eth->val.dst_mac, spec->dst.addr_bytes, ETHER_ADDR_LEN);
687         memcpy(eth->val.src_mac, spec->src.addr_bytes, ETHER_ADDR_LEN);
688         eth->val.ether_type = spec->type;
689         memcpy(eth->mask.dst_mac, mask->dst.addr_bytes, ETHER_ADDR_LEN);
690         memcpy(eth->mask.src_mac, mask->src.addr_bytes, ETHER_ADDR_LEN);
691         eth->mask.ether_type = mask->type;
692         /* Remove unwanted bits from values. */
693         for (i = 0; i < ETHER_ADDR_LEN; ++i) {
694                 eth->val.dst_mac[i] &= eth->mask.dst_mac[i];
695                 eth->val.src_mac[i] &= eth->mask.src_mac[i];
696         }
697         eth->val.ether_type &= eth->mask.ether_type;
698         return 0;
699 }
700
701 /**
702  * Convert VLAN item to Verbs specification.
703  *
704  * @param item[in]
705  *   Item specification.
706  * @param default_mask[in]
707  *   Default bit-masks to use when item->mask is not provided.
708  * @param data[in, out]
709  *   User structure.
710  */
711 static int
712 mlx5_flow_create_vlan(const struct rte_flow_item *item,
713                       const void *default_mask,
714                       void *data)
715 {
716         const struct rte_flow_item_vlan *spec = item->spec;
717         const struct rte_flow_item_vlan *mask = item->mask;
718         struct mlx5_flow_parse *parser = (struct mlx5_flow_parse *)data;
719         struct ibv_flow_spec_eth *eth;
720         const unsigned int eth_size = sizeof(struct ibv_flow_spec_eth);
721
722         eth = (void *)((uintptr_t)parser->ibv_attr + parser->offset - eth_size);
723         if (!spec)
724                 return 0;
725         if (!mask)
726                 mask = default_mask;
727         eth->val.vlan_tag = spec->tci;
728         eth->mask.vlan_tag = mask->tci;
729         eth->val.vlan_tag &= eth->mask.vlan_tag;
730         return 0;
731 }
732
733 /**
734  * Convert IPv4 item to Verbs specification.
735  *
736  * @param item[in]
737  *   Item specification.
738  * @param default_mask[in]
739  *   Default bit-masks to use when item->mask is not provided.
740  * @param data[in, out]
741  *   User structure.
742  */
743 static int
744 mlx5_flow_create_ipv4(const struct rte_flow_item *item,
745                       const void *default_mask,
746                       void *data)
747 {
748         const struct rte_flow_item_ipv4 *spec = item->spec;
749         const struct rte_flow_item_ipv4 *mask = item->mask;
750         struct mlx5_flow_parse *parser = (struct mlx5_flow_parse *)data;
751         struct ibv_flow_spec_ipv4_ext *ipv4;
752         unsigned int ipv4_size = sizeof(struct ibv_flow_spec_ipv4_ext);
753
754         ++parser->ibv_attr->num_of_specs;
755         parser->hash_fields = (IBV_RX_HASH_SRC_IPV4 | IBV_RX_HASH_DST_IPV4);
756         ipv4 = (void *)((uintptr_t)parser->ibv_attr + parser->offset);
757         *ipv4 = (struct ibv_flow_spec_ipv4_ext) {
758                 .type = parser->inner | IBV_FLOW_SPEC_IPV4_EXT,
759                 .size = ipv4_size,
760         };
761         if (!spec)
762                 return 0;
763         if (!mask)
764                 mask = default_mask;
765         ipv4->val = (struct ibv_flow_ipv4_ext_filter){
766                 .src_ip = spec->hdr.src_addr,
767                 .dst_ip = spec->hdr.dst_addr,
768                 .proto = spec->hdr.next_proto_id,
769                 .tos = spec->hdr.type_of_service,
770         };
771         ipv4->mask = (struct ibv_flow_ipv4_ext_filter){
772                 .src_ip = mask->hdr.src_addr,
773                 .dst_ip = mask->hdr.dst_addr,
774                 .proto = mask->hdr.next_proto_id,
775                 .tos = mask->hdr.type_of_service,
776         };
777         /* Remove unwanted bits from values. */
778         ipv4->val.src_ip &= ipv4->mask.src_ip;
779         ipv4->val.dst_ip &= ipv4->mask.dst_ip;
780         ipv4->val.proto &= ipv4->mask.proto;
781         ipv4->val.tos &= ipv4->mask.tos;
782         return 0;
783 }
784
785 /**
786  * Convert IPv6 item to Verbs specification.
787  *
788  * @param item[in]
789  *   Item specification.
790  * @param default_mask[in]
791  *   Default bit-masks to use when item->mask is not provided.
792  * @param data[in, out]
793  *   User structure.
794  */
795 static int
796 mlx5_flow_create_ipv6(const struct rte_flow_item *item,
797                       const void *default_mask,
798                       void *data)
799 {
800         const struct rte_flow_item_ipv6 *spec = item->spec;
801         const struct rte_flow_item_ipv6 *mask = item->mask;
802         struct mlx5_flow_parse *parser = (struct mlx5_flow_parse *)data;
803         struct ibv_flow_spec_ipv6 *ipv6;
804         unsigned int ipv6_size = sizeof(struct ibv_flow_spec_ipv6);
805         unsigned int i;
806
807         ++parser->ibv_attr->num_of_specs;
808         parser->hash_fields = (IBV_RX_HASH_SRC_IPV6 | IBV_RX_HASH_DST_IPV6);
809         ipv6 = (void *)((uintptr_t)parser->ibv_attr + parser->offset);
810         *ipv6 = (struct ibv_flow_spec_ipv6) {
811                 .type = parser->inner | IBV_FLOW_SPEC_IPV6,
812                 .size = ipv6_size,
813         };
814         if (!spec)
815                 return 0;
816         if (!mask)
817                 mask = default_mask;
818         memcpy(ipv6->val.src_ip, spec->hdr.src_addr,
819                RTE_DIM(ipv6->val.src_ip));
820         memcpy(ipv6->val.dst_ip, spec->hdr.dst_addr,
821                RTE_DIM(ipv6->val.dst_ip));
822         memcpy(ipv6->mask.src_ip, mask->hdr.src_addr,
823                RTE_DIM(ipv6->mask.src_ip));
824         memcpy(ipv6->mask.dst_ip, mask->hdr.dst_addr,
825                RTE_DIM(ipv6->mask.dst_ip));
826         ipv6->mask.flow_label = mask->hdr.vtc_flow;
827         ipv6->mask.next_hdr = mask->hdr.proto;
828         ipv6->mask.hop_limit = mask->hdr.hop_limits;
829         /* Remove unwanted bits from values. */
830         for (i = 0; i < RTE_DIM(ipv6->val.src_ip); ++i) {
831                 ipv6->val.src_ip[i] &= ipv6->mask.src_ip[i];
832                 ipv6->val.dst_ip[i] &= ipv6->mask.dst_ip[i];
833         }
834         ipv6->val.flow_label &= ipv6->mask.flow_label;
835         ipv6->val.next_hdr &= ipv6->mask.next_hdr;
836         ipv6->val.hop_limit &= ipv6->mask.hop_limit;
837         return 0;
838 }
839
840 /**
841  * Convert UDP item to Verbs specification.
842  *
843  * @param item[in]
844  *   Item specification.
845  * @param default_mask[in]
846  *   Default bit-masks to use when item->mask is not provided.
847  * @param data[in, out]
848  *   User structure.
849  */
850 static int
851 mlx5_flow_create_udp(const struct rte_flow_item *item,
852                      const void *default_mask,
853                      void *data)
854 {
855         const struct rte_flow_item_udp *spec = item->spec;
856         const struct rte_flow_item_udp *mask = item->mask;
857         struct mlx5_flow_parse *parser = (struct mlx5_flow_parse *)data;
858         struct ibv_flow_spec_tcp_udp *udp;
859         unsigned int udp_size = sizeof(struct ibv_flow_spec_tcp_udp);
860
861         ++parser->ibv_attr->num_of_specs;
862         parser->hash_fields |= (IBV_RX_HASH_SRC_PORT_UDP |
863                                 IBV_RX_HASH_DST_PORT_UDP);
864         udp = (void *)((uintptr_t)parser->ibv_attr + parser->offset);
865         *udp = (struct ibv_flow_spec_tcp_udp) {
866                 .type = parser->inner | IBV_FLOW_SPEC_UDP,
867                 .size = udp_size,
868         };
869         if (!spec)
870                 return 0;
871         if (!mask)
872                 mask = default_mask;
873         udp->val.dst_port = spec->hdr.dst_port;
874         udp->val.src_port = spec->hdr.src_port;
875         udp->mask.dst_port = mask->hdr.dst_port;
876         udp->mask.src_port = mask->hdr.src_port;
877         /* Remove unwanted bits from values. */
878         udp->val.src_port &= udp->mask.src_port;
879         udp->val.dst_port &= udp->mask.dst_port;
880         return 0;
881 }
882
883 /**
884  * Convert TCP item to Verbs specification.
885  *
886  * @param item[in]
887  *   Item specification.
888  * @param default_mask[in]
889  *   Default bit-masks to use when item->mask is not provided.
890  * @param data[in, out]
891  *   User structure.
892  */
893 static int
894 mlx5_flow_create_tcp(const struct rte_flow_item *item,
895                      const void *default_mask,
896                      void *data)
897 {
898         const struct rte_flow_item_tcp *spec = item->spec;
899         const struct rte_flow_item_tcp *mask = item->mask;
900         struct mlx5_flow_parse *parser = (struct mlx5_flow_parse *)data;
901         struct ibv_flow_spec_tcp_udp *tcp;
902         unsigned int tcp_size = sizeof(struct ibv_flow_spec_tcp_udp);
903
904         ++parser->ibv_attr->num_of_specs;
905         parser->hash_fields |= (IBV_RX_HASH_SRC_PORT_TCP |
906                                 IBV_RX_HASH_DST_PORT_TCP);
907         tcp = (void *)((uintptr_t)parser->ibv_attr + parser->offset);
908         *tcp = (struct ibv_flow_spec_tcp_udp) {
909                 .type = parser->inner | IBV_FLOW_SPEC_TCP,
910                 .size = tcp_size,
911         };
912         if (!spec)
913                 return 0;
914         if (!mask)
915                 mask = default_mask;
916         tcp->val.dst_port = spec->hdr.dst_port;
917         tcp->val.src_port = spec->hdr.src_port;
918         tcp->mask.dst_port = mask->hdr.dst_port;
919         tcp->mask.src_port = mask->hdr.src_port;
920         /* Remove unwanted bits from values. */
921         tcp->val.src_port &= tcp->mask.src_port;
922         tcp->val.dst_port &= tcp->mask.dst_port;
923         return 0;
924 }
925
926 /**
927  * Convert VXLAN item to Verbs specification.
928  *
929  * @param item[in]
930  *   Item specification.
931  * @param default_mask[in]
932  *   Default bit-masks to use when item->mask is not provided.
933  * @param data[in, out]
934  *   User structure.
935  */
936 static int
937 mlx5_flow_create_vxlan(const struct rte_flow_item *item,
938                        const void *default_mask,
939                        void *data)
940 {
941         const struct rte_flow_item_vxlan *spec = item->spec;
942         const struct rte_flow_item_vxlan *mask = item->mask;
943         struct mlx5_flow_parse *parser = (struct mlx5_flow_parse *)data;
944         struct ibv_flow_spec_tunnel *vxlan;
945         unsigned int size = sizeof(struct ibv_flow_spec_tunnel);
946         union vni {
947                 uint32_t vlan_id;
948                 uint8_t vni[4];
949         } id;
950
951         ++parser->ibv_attr->num_of_specs;
952         id.vni[0] = 0;
953         vxlan = (void *)((uintptr_t)parser->ibv_attr + parser->offset);
954         *vxlan = (struct ibv_flow_spec_tunnel) {
955                 .type = parser->inner | IBV_FLOW_SPEC_VXLAN_TUNNEL,
956                 .size = size,
957         };
958         parser->inner = IBV_FLOW_SPEC_INNER;
959         if (!spec)
960                 return 0;
961         if (!mask)
962                 mask = default_mask;
963         memcpy(&id.vni[1], spec->vni, 3);
964         vxlan->val.tunnel_id = id.vlan_id;
965         memcpy(&id.vni[1], mask->vni, 3);
966         vxlan->mask.tunnel_id = id.vlan_id;
967         /* Remove unwanted bits from values. */
968         vxlan->val.tunnel_id &= vxlan->mask.tunnel_id;
969         return 0;
970 }
971
972 /**
973  * Convert mark/flag action to Verbs specification.
974  *
975  * @param parser
976  *   Internal parser structure.
977  * @param mark_id
978  *   Mark identifier.
979  */
980 static int
981 mlx5_flow_create_flag_mark(struct mlx5_flow_parse *parser, uint32_t mark_id)
982 {
983         struct ibv_flow_spec_action_tag *tag;
984         unsigned int size = sizeof(struct ibv_flow_spec_action_tag);
985
986         assert(parser->mark);
987         tag = (void *)((uintptr_t)parser->ibv_attr + parser->offset);
988         *tag = (struct ibv_flow_spec_action_tag){
989                 .type = IBV_FLOW_SPEC_ACTION_TAG,
990                 .size = size,
991                 .tag_id = mlx5_flow_mark_set(mark_id),
992         };
993         ++parser->ibv_attr->num_of_specs;
994         parser->offset += size;
995         return 0;
996 }
997
998 /**
999  * Complete flow rule creation with a drop queue.
1000  *
1001  * @param priv
1002  *   Pointer to private structure.
1003  * @param parser
1004  *   Internal parser structure.
1005  * @param[out] error
1006  *   Perform verbose error reporting if not NULL.
1007  *
1008  * @return
1009  *   A flow if the rule could be created.
1010  */
1011 static struct rte_flow *
1012 priv_flow_create_action_queue_drop(struct priv *priv,
1013                                    struct mlx5_flow_parse *parser,
1014                                    struct rte_flow_error *error)
1015 {
1016         struct rte_flow *rte_flow;
1017         struct ibv_flow_spec_action_drop *drop;
1018         unsigned int size = sizeof(struct ibv_flow_spec_action_drop);
1019
1020         assert(priv->pd);
1021         assert(priv->ctx);
1022         rte_flow = rte_calloc(__func__, 1, sizeof(*rte_flow), 0);
1023         if (!rte_flow) {
1024                 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1025                                    NULL, "cannot allocate flow memory");
1026                 return NULL;
1027         }
1028         rte_flow->drop = 1;
1029         drop = (void *)((uintptr_t)parser->ibv_attr + parser->offset);
1030         *drop = (struct ibv_flow_spec_action_drop){
1031                         .type = IBV_FLOW_SPEC_ACTION_DROP,
1032                         .size = size,
1033         };
1034         ++parser->ibv_attr->num_of_specs;
1035         parser->offset += sizeof(struct ibv_flow_spec_action_drop);
1036         rte_flow->drxq.ibv_attr = parser->ibv_attr;
1037         if (!priv->dev->data->dev_started)
1038                 return rte_flow;
1039         rte_flow->drxq.hrxq.qp = priv->flow_drop_queue->qp;
1040         rte_flow->drxq.ibv_flow = ibv_create_flow(rte_flow->drxq.hrxq.qp,
1041                                                   rte_flow->drxq.ibv_attr);
1042         if (!rte_flow->drxq.ibv_flow) {
1043                 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1044                                    NULL, "flow rule creation failure");
1045                 goto error;
1046         }
1047         return rte_flow;
1048 error:
1049         assert(rte_flow);
1050         rte_free(rte_flow);
1051         return NULL;
1052 }
1053
1054 /**
1055  * Complete flow rule creation.
1056  *
1057  * @param priv
1058  *   Pointer to private structure.
1059  * @param parser
1060  *   MLX5 flow parser attributes (filled by mlx5_flow_validate()).
1061  * @param[out] error
1062  *   Perform verbose error reporting if not NULL.
1063  *
1064  * @return
1065  *   A flow if the rule could be created.
1066  */
1067 static struct rte_flow *
1068 priv_flow_create_action_queue(struct priv *priv,
1069                               struct mlx5_flow_parse *parser,
1070                               struct rte_flow_error *error)
1071 {
1072         struct rte_flow *rte_flow;
1073         unsigned int i;
1074
1075         assert(priv->pd);
1076         assert(priv->ctx);
1077         assert(!parser->drop);
1078         rte_flow = rte_calloc(__func__, 1,
1079                               sizeof(*rte_flow) +
1080                               parser->queues_n * sizeof(uint16_t),
1081                               0);
1082         if (!rte_flow) {
1083                 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1084                                    NULL, "cannot allocate flow memory");
1085                 return NULL;
1086         }
1087         rte_flow->mark = parser->mark;
1088         rte_flow->frxq.ibv_attr = parser->ibv_attr;
1089         rte_flow->queues = (uint16_t (*)[])(rte_flow + 1);
1090         memcpy(rte_flow->queues, parser->queues,
1091                parser->queues_n * sizeof(uint16_t));
1092         rte_flow->queues_n = parser->queues_n;
1093         rte_flow->frxq.hash_fields = parser->hash_fields;
1094         rte_flow->frxq.hrxq = mlx5_priv_hrxq_get(priv, rss_hash_default_key,
1095                                                  rss_hash_default_key_len,
1096                                                  parser->hash_fields,
1097                                                  (*rte_flow->queues),
1098                                                  rte_flow->queues_n);
1099         if (!rte_flow->frxq.hrxq) {
1100                 rte_flow->frxq.hrxq =
1101                         mlx5_priv_hrxq_new(priv, rss_hash_default_key,
1102                                            rss_hash_default_key_len,
1103                                            parser->hash_fields,
1104                                            (*rte_flow->queues),
1105                                            rte_flow->queues_n);
1106                 if (!rte_flow->frxq.hrxq) {
1107                         rte_flow_error_set(error, ENOMEM,
1108                                            RTE_FLOW_ERROR_TYPE_HANDLE,
1109                                            NULL, "cannot create hash rxq");
1110                         goto error;
1111                 }
1112         }
1113         for (i = 0; i != parser->queues_n; ++i) {
1114                 struct mlx5_rxq_data *q =
1115                         (*priv->rxqs)[parser->queues[i]];
1116
1117                 q->mark |= parser->mark;
1118         }
1119         if (!priv->dev->data->dev_started)
1120                 return rte_flow;
1121         rte_flow->frxq.ibv_flow = ibv_create_flow(rte_flow->frxq.hrxq->qp,
1122                                                   rte_flow->frxq.ibv_attr);
1123         if (!rte_flow->frxq.ibv_flow) {
1124                 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1125                                    NULL, "flow rule creation failure");
1126                 goto error;
1127         }
1128         return rte_flow;
1129 error:
1130         assert(rte_flow);
1131         if (rte_flow->frxq.hrxq)
1132                 mlx5_priv_hrxq_release(priv, rte_flow->frxq.hrxq);
1133         rte_free(rte_flow);
1134         return NULL;
1135 }
1136
1137 /**
1138  * Validate a flow.
1139  *
1140  * @param priv
1141  *   Pointer to private structure.
1142  * @param[in] attr
1143  *   Flow rule attributes.
1144  * @param[in] pattern
1145  *   Pattern specification (list terminated by the END pattern item).
1146  * @param[in] actions
1147  *   Associated actions (list terminated by the END action).
1148  * @param[out] error
1149  *   Perform verbose error reporting if not NULL.
1150  * @param[in,out] parser
1151  *   MLX5 parser structure.
1152  *
1153  * @return
1154  *   0 on success, negative errno value on failure.
1155  */
1156 static int
1157 priv_flow_validate(struct priv *priv,
1158                    const struct rte_flow_attr *attr,
1159                    const struct rte_flow_item items[],
1160                    const struct rte_flow_action actions[],
1161                    struct rte_flow_error *error,
1162                    struct mlx5_flow_parse *parser)
1163 {
1164         int err;
1165
1166         err = priv_flow_convert(priv, attr, items, actions, error, parser);
1167         if (err)
1168                 goto exit;
1169         if (parser->mark)
1170                 parser->offset += sizeof(struct ibv_flow_spec_action_tag);
1171         parser->ibv_attr = rte_malloc(__func__, parser->offset, 0);
1172         if (!parser->ibv_attr) {
1173                 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1174                                    NULL, "cannot allocate ibv_attr memory");
1175                 err = rte_errno;
1176                 goto exit;
1177         }
1178         *parser->ibv_attr = (struct ibv_flow_attr){
1179                 .type = IBV_FLOW_ATTR_NORMAL,
1180                 .size = sizeof(struct ibv_flow_attr),
1181                 .priority = attr->priority,
1182                 .num_of_specs = 0,
1183                 .port = 0,
1184                 .flags = 0,
1185         };
1186         err = priv_flow_convert(priv, attr, items, actions, error, parser);
1187         if (err || parser->create)
1188                 goto exit;
1189         if (parser->mark)
1190                 mlx5_flow_create_flag_mark(parser, parser->mark_id);
1191         return 0;
1192 exit:
1193         if (parser->ibv_attr)
1194                 rte_free(parser->ibv_attr);
1195         return err;
1196 }
1197
1198 /**
1199  * Convert a flow.
1200  *
1201  * @param priv
1202  *   Pointer to private structure.
1203  * @param list
1204  *   Pointer to a TAILQ flow list.
1205  * @param[in] attr
1206  *   Flow rule attributes.
1207  * @param[in] pattern
1208  *   Pattern specification (list terminated by the END pattern item).
1209  * @param[in] actions
1210  *   Associated actions (list terminated by the END action).
1211  * @param[out] error
1212  *   Perform verbose error reporting if not NULL.
1213  *
1214  * @return
1215  *   A flow on success, NULL otherwise.
1216  */
1217 static struct rte_flow *
1218 priv_flow_create(struct priv *priv,
1219                  struct mlx5_flows *list,
1220                  const struct rte_flow_attr *attr,
1221                  const struct rte_flow_item items[],
1222                  const struct rte_flow_action actions[],
1223                  struct rte_flow_error *error)
1224 {
1225         struct mlx5_flow_parse parser = { .create = 1, };
1226         struct rte_flow *flow;
1227         int err;
1228
1229         err = priv_flow_validate(priv, attr, items, actions, error, &parser);
1230         if (err)
1231                 goto exit;
1232         if (parser.drop)
1233                 flow = priv_flow_create_action_queue_drop(priv, &parser, error);
1234         else
1235                 flow = priv_flow_create_action_queue(priv, &parser, error);
1236         if (!flow)
1237                 goto exit;
1238         TAILQ_INSERT_TAIL(list, flow, next);
1239         DEBUG("Flow created %p", (void *)flow);
1240         return flow;
1241 exit:
1242         if (parser.ibv_attr)
1243                 rte_free(parser.ibv_attr);
1244         return NULL;
1245 }
1246
1247 /**
1248  * Validate a flow supported by the NIC.
1249  *
1250  * @see rte_flow_validate()
1251  * @see rte_flow_ops
1252  */
1253 int
1254 mlx5_flow_validate(struct rte_eth_dev *dev,
1255                    const struct rte_flow_attr *attr,
1256                    const struct rte_flow_item items[],
1257                    const struct rte_flow_action actions[],
1258                    struct rte_flow_error *error)
1259 {
1260         struct priv *priv = dev->data->dev_private;
1261         int ret;
1262         struct mlx5_flow_parse parser = { .create = 0, };
1263
1264         priv_lock(priv);
1265         ret = priv_flow_validate(priv, attr, items, actions, error, &parser);
1266         priv_unlock(priv);
1267         return ret;
1268 }
1269
1270 /**
1271  * Create a flow.
1272  *
1273  * @see rte_flow_create()
1274  * @see rte_flow_ops
1275  */
1276 struct rte_flow *
1277 mlx5_flow_create(struct rte_eth_dev *dev,
1278                  const struct rte_flow_attr *attr,
1279                  const struct rte_flow_item items[],
1280                  const struct rte_flow_action actions[],
1281                  struct rte_flow_error *error)
1282 {
1283         struct priv *priv = dev->data->dev_private;
1284         struct rte_flow *flow;
1285
1286         priv_lock(priv);
1287         flow = priv_flow_create(priv, &priv->flows, attr, items, actions,
1288                                 error);
1289         priv_unlock(priv);
1290         return flow;
1291 }
1292
1293 /**
1294  * Destroy a flow.
1295  *
1296  * @param priv
1297  *   Pointer to private structure.
1298  * @param list
1299  *   Pointer to a TAILQ flow list.
1300  * @param[in] flow
1301  *   Flow to destroy.
1302  */
1303 static void
1304 priv_flow_destroy(struct priv *priv,
1305                   struct mlx5_flows *list,
1306                   struct rte_flow *flow)
1307 {
1308         unsigned int i;
1309         uint16_t *queues;
1310         uint16_t queues_n;
1311
1312         if (flow->drop || !flow->mark)
1313                 goto free;
1314         queues = flow->frxq.hrxq->ind_table->queues;
1315         queues_n = flow->frxq.hrxq->ind_table->queues_n;
1316         for (i = 0; i != queues_n; ++i) {
1317                 struct rte_flow *tmp;
1318                 struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[queues[i]];
1319                 int mark = 0;
1320
1321                 /*
1322                  * To remove the mark from the queue, the queue must not be
1323                  * present in any other marked flow (RSS or not).
1324                  */
1325                 TAILQ_FOREACH(tmp, list, next) {
1326                         unsigned int j;
1327
1328                         if (!tmp->mark)
1329                                 continue;
1330                         for (j = 0;
1331                              (j != tmp->frxq.hrxq->ind_table->queues_n) &&
1332                              !mark;
1333                              j++)
1334                                 if (tmp->frxq.hrxq->ind_table->queues[j] ==
1335                                     queues[i])
1336                                         mark = 1;
1337                 }
1338                 rxq_data->mark = mark;
1339         }
1340 free:
1341         if (flow->drop) {
1342                 if (flow->drxq.ibv_flow)
1343                         claim_zero(ibv_destroy_flow(flow->drxq.ibv_flow));
1344                 rte_free(flow->drxq.ibv_attr);
1345         } else {
1346                 mlx5_priv_hrxq_release(priv, flow->frxq.hrxq);
1347                 if (flow->frxq.ibv_flow)
1348                         claim_zero(ibv_destroy_flow(flow->frxq.ibv_flow));
1349                 rte_free(flow->frxq.ibv_attr);
1350         }
1351         TAILQ_REMOVE(list, flow, next);
1352         DEBUG("Flow destroyed %p", (void *)flow);
1353         rte_free(flow);
1354 }
1355
1356 /**
1357  * Destroy all flows.
1358  *
1359  * @param priv
1360  *   Pointer to private structure.
1361  * @param list
1362  *   Pointer to a TAILQ flow list.
1363  */
1364 void
1365 priv_flow_flush(struct priv *priv, struct mlx5_flows *list)
1366 {
1367         while (!TAILQ_EMPTY(list)) {
1368                 struct rte_flow *flow;
1369
1370                 flow = TAILQ_FIRST(list);
1371                 priv_flow_destroy(priv, list, flow);
1372         }
1373 }
1374
1375 /**
1376  * Create drop queue.
1377  *
1378  * @param priv
1379  *   Pointer to private structure.
1380  *
1381  * @return
1382  *   0 on success.
1383  */
1384 int
1385 priv_flow_create_drop_queue(struct priv *priv)
1386 {
1387         struct mlx5_hrxq_drop *fdq = NULL;
1388
1389         assert(priv->pd);
1390         assert(priv->ctx);
1391         fdq = rte_calloc(__func__, 1, sizeof(*fdq), 0);
1392         if (!fdq) {
1393                 WARN("cannot allocate memory for drop queue");
1394                 goto error;
1395         }
1396         fdq->cq = ibv_create_cq(priv->ctx, 1, NULL, NULL, 0);
1397         if (!fdq->cq) {
1398                 WARN("cannot allocate CQ for drop queue");
1399                 goto error;
1400         }
1401         fdq->wq = ibv_create_wq(priv->ctx,
1402                         &(struct ibv_wq_init_attr){
1403                         .wq_type = IBV_WQT_RQ,
1404                         .max_wr = 1,
1405                         .max_sge = 1,
1406                         .pd = priv->pd,
1407                         .cq = fdq->cq,
1408                         });
1409         if (!fdq->wq) {
1410                 WARN("cannot allocate WQ for drop queue");
1411                 goto error;
1412         }
1413         fdq->ind_table = ibv_create_rwq_ind_table(priv->ctx,
1414                         &(struct ibv_rwq_ind_table_init_attr){
1415                         .log_ind_tbl_size = 0,
1416                         .ind_tbl = &fdq->wq,
1417                         .comp_mask = 0,
1418                         });
1419         if (!fdq->ind_table) {
1420                 WARN("cannot allocate indirection table for drop queue");
1421                 goto error;
1422         }
1423         fdq->qp = ibv_create_qp_ex(priv->ctx,
1424                 &(struct ibv_qp_init_attr_ex){
1425                         .qp_type = IBV_QPT_RAW_PACKET,
1426                         .comp_mask =
1427                                 IBV_QP_INIT_ATTR_PD |
1428                                 IBV_QP_INIT_ATTR_IND_TABLE |
1429                                 IBV_QP_INIT_ATTR_RX_HASH,
1430                         .rx_hash_conf = (struct ibv_rx_hash_conf){
1431                                 .rx_hash_function =
1432                                         IBV_RX_HASH_FUNC_TOEPLITZ,
1433                                 .rx_hash_key_len = rss_hash_default_key_len,
1434                                 .rx_hash_key = rss_hash_default_key,
1435                                 .rx_hash_fields_mask = 0,
1436                                 },
1437                         .rwq_ind_tbl = fdq->ind_table,
1438                         .pd = priv->pd
1439                 });
1440         if (!fdq->qp) {
1441                 WARN("cannot allocate QP for drop queue");
1442                 goto error;
1443         }
1444         priv->flow_drop_queue = fdq;
1445         return 0;
1446 error:
1447         if (fdq->qp)
1448                 claim_zero(ibv_destroy_qp(fdq->qp));
1449         if (fdq->ind_table)
1450                 claim_zero(ibv_destroy_rwq_ind_table(fdq->ind_table));
1451         if (fdq->wq)
1452                 claim_zero(ibv_destroy_wq(fdq->wq));
1453         if (fdq->cq)
1454                 claim_zero(ibv_destroy_cq(fdq->cq));
1455         if (fdq)
1456                 rte_free(fdq);
1457         priv->flow_drop_queue = NULL;
1458         return -1;
1459 }
1460
1461 /**
1462  * Delete drop queue.
1463  *
1464  * @param priv
1465  *   Pointer to private structure.
1466  */
1467 void
1468 priv_flow_delete_drop_queue(struct priv *priv)
1469 {
1470         struct mlx5_hrxq_drop *fdq = priv->flow_drop_queue;
1471
1472         if (!fdq)
1473                 return;
1474         if (fdq->qp)
1475                 claim_zero(ibv_destroy_qp(fdq->qp));
1476         if (fdq->ind_table)
1477                 claim_zero(ibv_destroy_rwq_ind_table(fdq->ind_table));
1478         if (fdq->wq)
1479                 claim_zero(ibv_destroy_wq(fdq->wq));
1480         if (fdq->cq)
1481                 claim_zero(ibv_destroy_cq(fdq->cq));
1482         rte_free(fdq);
1483         priv->flow_drop_queue = NULL;
1484 }
1485
1486 /**
1487  * Remove all flows.
1488  *
1489  * @param priv
1490  *   Pointer to private structure.
1491  * @param list
1492  *   Pointer to a TAILQ flow list.
1493  */
1494 void
1495 priv_flow_stop(struct priv *priv, struct mlx5_flows *list)
1496 {
1497         struct rte_flow *flow;
1498
1499         TAILQ_FOREACH_REVERSE(flow, list, mlx5_flows, next) {
1500                 assert(!flow->drop);
1501                 claim_zero(ibv_destroy_flow(flow->frxq.ibv_flow));
1502                 flow->frxq.ibv_flow = NULL;
1503                 mlx5_priv_hrxq_release(priv, flow->frxq.hrxq);
1504                 flow->frxq.hrxq = NULL;
1505                 if (flow->mark) {
1506                         unsigned int n;
1507                         struct mlx5_ind_table_ibv *ind_tbl =
1508                                 flow->frxq.hrxq->ind_table;
1509
1510                         for (n = 0; n < ind_tbl->queues_n; ++n)
1511                                 (*priv->rxqs)[ind_tbl->queues[n]]->mark = 0;
1512                 }
1513                 DEBUG("Flow %p removed", (void *)flow);
1514         }
1515 }
1516
1517 /**
1518  * Add all flows.
1519  *
1520  * @param priv
1521  *   Pointer to private structure.
1522  * @param list
1523  *   Pointer to a TAILQ flow list.
1524  *
1525  * @return
1526  *   0 on success, a errno value otherwise and rte_errno is set.
1527  */
1528 int
1529 priv_flow_start(struct priv *priv, struct mlx5_flows *list)
1530 {
1531         struct rte_flow *flow;
1532
1533         TAILQ_FOREACH(flow, list, next) {
1534                 if (flow->frxq.hrxq)
1535                         goto flow_create;
1536                 flow->frxq.hrxq =
1537                         mlx5_priv_hrxq_get(priv, rss_hash_default_key,
1538                                            rss_hash_default_key_len,
1539                                            flow->frxq.hash_fields,
1540                                            (*flow->queues),
1541                                            flow->queues_n);
1542                 if (flow->frxq.hrxq)
1543                         goto flow_create;
1544                 flow->frxq.hrxq =
1545                         mlx5_priv_hrxq_new(priv, rss_hash_default_key,
1546                                            rss_hash_default_key_len,
1547                                            flow->frxq.hash_fields,
1548                                            (*flow->queues),
1549                                            flow->queues_n);
1550                 if (!flow->frxq.hrxq) {
1551                         DEBUG("Flow %p cannot be applied",
1552                               (void *)flow);
1553                         rte_errno = EINVAL;
1554                         return rte_errno;
1555                 }
1556 flow_create:
1557                 flow->frxq.ibv_flow = ibv_create_flow(flow->frxq.hrxq->qp,
1558                                                       flow->frxq.ibv_attr);
1559                 if (!flow->frxq.ibv_flow) {
1560                         DEBUG("Flow %p cannot be applied", (void *)flow);
1561                         rte_errno = EINVAL;
1562                         return rte_errno;
1563                 }
1564                 DEBUG("Flow %p applied", (void *)flow);
1565                 if (flow->mark) {
1566                         unsigned int n;
1567
1568                         for (n = 0;
1569                              n < flow->frxq.hrxq->ind_table->queues_n;
1570                              ++n) {
1571                                 uint16_t idx =
1572                                         flow->frxq.hrxq->ind_table->queues[n];
1573                                 (*priv->rxqs)[idx]->mark = 1;
1574                         }
1575                 }
1576         }
1577         return 0;
1578 }
1579
1580 /**
1581  * Verify the flow list is empty
1582  *
1583  * @param priv
1584  *  Pointer to private structure.
1585  *
1586  * @return the number of flows not released.
1587  */
1588 int
1589 priv_flow_verify(struct priv *priv)
1590 {
1591         struct rte_flow *flow;
1592         int ret = 0;
1593
1594         TAILQ_FOREACH(flow, &priv->flows, next) {
1595                 DEBUG("%p: flow %p still referenced", (void *)priv,
1596                       (void *)flow);
1597                 ++ret;
1598         }
1599         return ret;
1600 }
1601
1602 /**
1603  * Enable a control flow configured from the control plane.
1604  *
1605  * @param dev
1606  *   Pointer to Ethernet device.
1607  * @param eth_spec
1608  *   An Ethernet flow spec to apply.
1609  * @param eth_mask
1610  *   An Ethernet flow mask to apply.
1611  * @param vlan_spec
1612  *   A VLAN flow spec to apply.
1613  * @param vlan_mask
1614  *   A VLAN flow mask to apply.
1615  *
1616  * @return
1617  *   0 on success.
1618  */
1619 int
1620 mlx5_ctrl_flow_vlan(struct rte_eth_dev *dev,
1621                     struct rte_flow_item_eth *eth_spec,
1622                     struct rte_flow_item_eth *eth_mask,
1623                     struct rte_flow_item_vlan *vlan_spec,
1624                     struct rte_flow_item_vlan *vlan_mask)
1625 {
1626         struct priv *priv = dev->data->dev_private;
1627         const struct rte_flow_attr attr = {
1628                 .ingress = 1,
1629                 .priority = MLX5_CTRL_FLOW_PRIORITY,
1630         };
1631         struct rte_flow_item items[] = {
1632                 {
1633                         .type = RTE_FLOW_ITEM_TYPE_ETH,
1634                         .spec = eth_spec,
1635                         .last = NULL,
1636                         .mask = eth_mask,
1637                 },
1638                 {
1639                         .type = (vlan_spec) ? RTE_FLOW_ITEM_TYPE_VLAN :
1640                                 RTE_FLOW_ITEM_TYPE_END,
1641                         .spec = vlan_spec,
1642                         .last = NULL,
1643                         .mask = vlan_mask,
1644                 },
1645                 {
1646                         .type = RTE_FLOW_ITEM_TYPE_END,
1647                 },
1648         };
1649         struct rte_flow_action actions[] = {
1650                 {
1651                         .type = RTE_FLOW_ACTION_TYPE_QUEUE,
1652                         .conf = &(struct rte_flow_action_queue){
1653                                 .index = 0,
1654                         },
1655                 },
1656                 {
1657                         .type = RTE_FLOW_ACTION_TYPE_END,
1658                 },
1659         };
1660         struct rte_flow *flow;
1661         struct rte_flow_error error;
1662
1663         flow = priv_flow_create(priv, &priv->ctrl_flows, &attr, items, actions,
1664                                 &error);
1665         if (!flow)
1666                 return rte_errno;
1667         return 0;
1668 }
1669
1670 /**
1671  * Enable a flow control configured from the control plane.
1672  *
1673  * @param dev
1674  *   Pointer to Ethernet device.
1675  * @param eth_spec
1676  *   An Ethernet flow spec to apply.
1677  * @param eth_mask
1678  *   An Ethernet flow mask to apply.
1679  *
1680  * @return
1681  *   0 on success.
1682  */
1683 int
1684 mlx5_ctrl_flow(struct rte_eth_dev *dev,
1685                struct rte_flow_item_eth *eth_spec,
1686                struct rte_flow_item_eth *eth_mask)
1687 {
1688         return mlx5_ctrl_flow_vlan(dev, eth_spec, eth_mask, NULL, NULL);
1689 }
1690
1691 /**
1692  * Destroy a flow.
1693  *
1694  * @see rte_flow_destroy()
1695  * @see rte_flow_ops
1696  */
1697 int
1698 mlx5_flow_destroy(struct rte_eth_dev *dev,
1699                   struct rte_flow *flow,
1700                   struct rte_flow_error *error)
1701 {
1702         struct priv *priv = dev->data->dev_private;
1703
1704         (void)error;
1705         priv_lock(priv);
1706         priv_flow_destroy(priv, &priv->flows, flow);
1707         priv_unlock(priv);
1708         return 0;
1709 }
1710
1711 /**
1712  * Destroy all flows.
1713  *
1714  * @see rte_flow_flush()
1715  * @see rte_flow_ops
1716  */
1717 int
1718 mlx5_flow_flush(struct rte_eth_dev *dev,
1719                 struct rte_flow_error *error)
1720 {
1721         struct priv *priv = dev->data->dev_private;
1722
1723         (void)error;
1724         priv_lock(priv);
1725         priv_flow_flush(priv, &priv->flows);
1726         priv_unlock(priv);
1727         return 0;
1728 }
1729
1730 /**
1731  * Isolated mode.
1732  *
1733  * @see rte_flow_isolate()
1734  * @see rte_flow_ops
1735  */
1736 int
1737 mlx5_flow_isolate(struct rte_eth_dev *dev,
1738                   int enable,
1739                   struct rte_flow_error *error)
1740 {
1741         struct priv *priv = dev->data->dev_private;
1742
1743         priv_lock(priv);
1744         if (dev->data->dev_started) {
1745                 rte_flow_error_set(error, EBUSY,
1746                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1747                                    NULL,
1748                                    "port must be stopped first");
1749                 priv_unlock(priv);
1750                 return -rte_errno;
1751         }
1752         priv->isolated = !!enable;
1753         priv_unlock(priv);
1754         return 0;
1755 }