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