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