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