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