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