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