net/mlx5: support basic flow items and actions
[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
54 static int
55 mlx5_flow_create_eth(const struct rte_flow_item *item,
56                      const void *default_mask,
57                      void *data);
58
59 static int
60 mlx5_flow_create_ipv4(const struct rte_flow_item *item,
61                       const void *default_mask,
62                       void *data);
63
64 static int
65 mlx5_flow_create_ipv6(const struct rte_flow_item *item,
66                       const void *default_mask,
67                       void *data);
68
69 static int
70 mlx5_flow_create_udp(const struct rte_flow_item *item,
71                      const void *default_mask,
72                      void *data);
73
74 static int
75 mlx5_flow_create_tcp(const struct rte_flow_item *item,
76                      const void *default_mask,
77                      void *data);
78
79 struct rte_flow {
80         LIST_ENTRY(rte_flow) next; /**< Pointer to the next flow structure. */
81         struct ibv_exp_flow_attr *ibv_attr; /**< Pointer to Verbs attributes. */
82         struct ibv_exp_rwq_ind_table *ind_table; /**< Indirection table. */
83         struct ibv_qp *qp; /**< Verbs queue pair. */
84         struct ibv_exp_flow *ibv_flow; /**< Verbs flow. */
85         struct ibv_exp_wq *wq; /**< Verbs work queue. */
86         struct ibv_cq *cq; /**< Verbs completion queue. */
87         struct rxq *rxq; /**< Pointer to the queue, NULL if drop queue. */
88 };
89
90 /** Static initializer for items. */
91 #define ITEMS(...) \
92         (const enum rte_flow_item_type []){ \
93                 __VA_ARGS__, RTE_FLOW_ITEM_TYPE_END, \
94         }
95
96 /** Structure to generate a simple graph of layers supported by the NIC. */
97 struct mlx5_flow_items {
98         /** List of possible actions for these items. */
99         const enum rte_flow_action_type *const actions;
100         /** Bit-masks corresponding to the possibilities for the item. */
101         const void *mask;
102         /** Bit-masks size in bytes. */
103         const unsigned int mask_sz;
104         /**
105          * Conversion function from rte_flow to NIC specific flow.
106          *
107          * @param item
108          *   rte_flow item to convert.
109          * @param default_mask
110          *   Default bit-masks to use when item->mask is not provided.
111          * @param data
112          *   Internal structure to store the conversion.
113          *
114          * @return
115          *   0 on success, negative value otherwise.
116          */
117         int (*convert)(const struct rte_flow_item *item,
118                        const void *default_mask,
119                        void *data);
120         /** Size in bytes of the destination structure. */
121         const unsigned int dst_sz;
122         /** List of possible following items.  */
123         const enum rte_flow_item_type *const items;
124 };
125
126 /** Valid action for this PMD. */
127 static const enum rte_flow_action_type valid_actions[] = {
128         RTE_FLOW_ACTION_TYPE_DROP,
129         RTE_FLOW_ACTION_TYPE_QUEUE,
130         RTE_FLOW_ACTION_TYPE_END,
131 };
132
133 /** Graph of supported items and associated actions. */
134 static const struct mlx5_flow_items mlx5_flow_items[] = {
135         [RTE_FLOW_ITEM_TYPE_END] = {
136                 .items = ITEMS(RTE_FLOW_ITEM_TYPE_ETH),
137         },
138         [RTE_FLOW_ITEM_TYPE_ETH] = {
139                 .items = ITEMS(RTE_FLOW_ITEM_TYPE_IPV4,
140                                RTE_FLOW_ITEM_TYPE_IPV6),
141                 .actions = valid_actions,
142                 .mask = &(const struct rte_flow_item_eth){
143                         .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
144                         .src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
145                 },
146                 .mask_sz = sizeof(struct rte_flow_item_eth),
147                 .convert = mlx5_flow_create_eth,
148                 .dst_sz = sizeof(struct ibv_exp_flow_spec_eth),
149         },
150         [RTE_FLOW_ITEM_TYPE_IPV4] = {
151                 .items = ITEMS(RTE_FLOW_ITEM_TYPE_UDP,
152                                RTE_FLOW_ITEM_TYPE_TCP),
153                 .actions = valid_actions,
154                 .mask = &(const struct rte_flow_item_ipv4){
155                         .hdr = {
156                                 .src_addr = -1,
157                                 .dst_addr = -1,
158                         },
159                 },
160                 .mask_sz = sizeof(struct rte_flow_item_ipv4),
161                 .convert = mlx5_flow_create_ipv4,
162                 .dst_sz = sizeof(struct ibv_exp_flow_spec_ipv4),
163         },
164         [RTE_FLOW_ITEM_TYPE_IPV6] = {
165                 .items = ITEMS(RTE_FLOW_ITEM_TYPE_UDP,
166                                RTE_FLOW_ITEM_TYPE_TCP),
167                 .actions = valid_actions,
168                 .mask = &(const struct rte_flow_item_ipv6){
169                         .hdr = {
170                                 .src_addr = {
171                                         0xff, 0xff, 0xff, 0xff,
172                                         0xff, 0xff, 0xff, 0xff,
173                                         0xff, 0xff, 0xff, 0xff,
174                                         0xff, 0xff, 0xff, 0xff,
175                                 },
176                                 .dst_addr = {
177                                         0xff, 0xff, 0xff, 0xff,
178                                         0xff, 0xff, 0xff, 0xff,
179                                         0xff, 0xff, 0xff, 0xff,
180                                         0xff, 0xff, 0xff, 0xff,
181                                 },
182                         },
183                 },
184                 .mask_sz = sizeof(struct rte_flow_item_ipv6),
185                 .convert = mlx5_flow_create_ipv6,
186                 .dst_sz = sizeof(struct ibv_exp_flow_spec_ipv6),
187         },
188         [RTE_FLOW_ITEM_TYPE_UDP] = {
189                 .actions = valid_actions,
190                 .mask = &(const struct rte_flow_item_udp){
191                         .hdr = {
192                                 .src_port = -1,
193                                 .dst_port = -1,
194                         },
195                 },
196                 .mask_sz = sizeof(struct rte_flow_item_udp),
197                 .convert = mlx5_flow_create_udp,
198                 .dst_sz = sizeof(struct ibv_exp_flow_spec_tcp_udp),
199         },
200         [RTE_FLOW_ITEM_TYPE_TCP] = {
201                 .actions = valid_actions,
202                 .mask = &(const struct rte_flow_item_tcp){
203                         .hdr = {
204                                 .src_port = -1,
205                                 .dst_port = -1,
206                         },
207                 },
208                 .mask_sz = sizeof(struct rte_flow_item_tcp),
209                 .convert = mlx5_flow_create_tcp,
210                 .dst_sz = sizeof(struct ibv_exp_flow_spec_tcp_udp),
211         },
212 };
213
214 /** Structure to pass to the conversion function. */
215 struct mlx5_flow {
216         struct ibv_exp_flow_attr *ibv_attr; /**< Verbs attribute. */
217         unsigned int offset; /**< Offset in bytes in the ibv_attr buffer. */
218 };
219
220 struct mlx5_flow_action {
221         uint32_t queue:1; /**< Target is a receive queue. */
222         uint32_t drop:1; /**< Target is a drop queue. */
223         uint32_t queue_id; /**< Identifier of the queue. */
224 };
225
226 /**
227  * Check support for a given item.
228  *
229  * @param item[in]
230  *   Item specification.
231  * @param mask[in]
232  *   Bit-masks covering supported fields to compare with spec, last and mask in
233  *   \item.
234  * @param size
235  *   Bit-Mask size in bytes.
236  *
237  * @return
238  *   0 on success.
239  */
240 static int
241 mlx5_flow_item_validate(const struct rte_flow_item *item,
242                         const uint8_t *mask, unsigned int size)
243 {
244         int ret = 0;
245
246         if (!item->spec && (item->mask || item->last))
247                 return -1;
248         if (item->spec && !item->mask) {
249                 unsigned int i;
250                 const uint8_t *spec = item->spec;
251
252                 for (i = 0; i < size; ++i)
253                         if ((spec[i] | mask[i]) != mask[i])
254                                 return -1;
255         }
256         if (item->last && !item->mask) {
257                 unsigned int i;
258                 const uint8_t *spec = item->last;
259
260                 for (i = 0; i < size; ++i)
261                         if ((spec[i] | mask[i]) != mask[i])
262                                 return -1;
263         }
264         if (item->mask) {
265                 unsigned int i;
266                 const uint8_t *spec = item->mask;
267
268                 for (i = 0; i < size; ++i)
269                         if ((spec[i] | mask[i]) != mask[i])
270                                 return -1;
271         }
272         if (item->spec && item->last) {
273                 uint8_t spec[size];
274                 uint8_t last[size];
275                 const uint8_t *apply = mask;
276                 unsigned int i;
277
278                 if (item->mask)
279                         apply = item->mask;
280                 for (i = 0; i < size; ++i) {
281                         spec[i] = ((const uint8_t *)item->spec)[i] & apply[i];
282                         last[i] = ((const uint8_t *)item->last)[i] & apply[i];
283                 }
284                 ret = memcmp(spec, last, size);
285         }
286         return ret;
287 }
288
289 /**
290  * Validate a flow supported by the NIC.
291  *
292  * @param priv
293  *   Pointer to private structure.
294  * @param[in] attr
295  *   Flow rule attributes.
296  * @param[in] pattern
297  *   Pattern specification (list terminated by the END pattern item).
298  * @param[in] actions
299  *   Associated actions (list terminated by the END action).
300  * @param[out] error
301  *   Perform verbose error reporting if not NULL.
302  * @param[in, out] flow
303  *   Flow structure to update.
304  *
305  * @return
306  *   0 on success, a negative errno value otherwise and rte_errno is set.
307  */
308 static int
309 priv_flow_validate(struct priv *priv,
310                    const struct rte_flow_attr *attr,
311                    const struct rte_flow_item items[],
312                    const struct rte_flow_action actions[],
313                    struct rte_flow_error *error,
314                    struct mlx5_flow *flow)
315 {
316         const struct mlx5_flow_items *cur_item = mlx5_flow_items;
317         struct mlx5_flow_action action = {
318                 .queue = 0,
319                 .drop = 0,
320         };
321
322         (void)priv;
323         if (attr->group) {
324                 rte_flow_error_set(error, ENOTSUP,
325                                    RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
326                                    NULL,
327                                    "groups are not supported");
328                 return -rte_errno;
329         }
330         if (attr->priority) {
331                 rte_flow_error_set(error, ENOTSUP,
332                                    RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
333                                    NULL,
334                                    "priorities are not supported");
335                 return -rte_errno;
336         }
337         if (attr->egress) {
338                 rte_flow_error_set(error, ENOTSUP,
339                                    RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
340                                    NULL,
341                                    "egress is not supported");
342                 return -rte_errno;
343         }
344         if (!attr->ingress) {
345                 rte_flow_error_set(error, ENOTSUP,
346                                    RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
347                                    NULL,
348                                    "only ingress is supported");
349                 return -rte_errno;
350         }
351         for (; items->type != RTE_FLOW_ITEM_TYPE_END; ++items) {
352                 const struct mlx5_flow_items *token = NULL;
353                 unsigned int i;
354                 int err;
355
356                 if (items->type == RTE_FLOW_ITEM_TYPE_VOID)
357                         continue;
358                 for (i = 0;
359                      cur_item->items &&
360                      cur_item->items[i] != RTE_FLOW_ITEM_TYPE_END;
361                      ++i) {
362                         if (cur_item->items[i] == items->type) {
363                                 token = &mlx5_flow_items[items->type];
364                                 break;
365                         }
366                 }
367                 if (!token)
368                         goto exit_item_not_supported;
369                 cur_item = token;
370                 err = mlx5_flow_item_validate(items,
371                                               (const uint8_t *)cur_item->mask,
372                                               sizeof(cur_item->mask_sz));
373                 if (err)
374                         goto exit_item_not_supported;
375                 if (flow->ibv_attr && cur_item->convert) {
376                         err = cur_item->convert(items, cur_item->mask, flow);
377                         if (err)
378                                 goto exit_item_not_supported;
379                 }
380                 flow->offset += cur_item->dst_sz;
381         }
382         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; ++actions) {
383                 if (actions->type == RTE_FLOW_ACTION_TYPE_VOID) {
384                         continue;
385                 } else if (actions->type == RTE_FLOW_ACTION_TYPE_DROP) {
386                         action.drop = 1;
387                 } else if (actions->type == RTE_FLOW_ACTION_TYPE_QUEUE) {
388                         const struct rte_flow_action_queue *queue =
389                                 (const struct rte_flow_action_queue *)
390                                 actions->conf;
391
392                         if (!queue || (queue->index > (priv->rxqs_n - 1)))
393                                 goto exit_action_not_supported;
394                         action.queue = 1;
395                 } else {
396                         goto exit_action_not_supported;
397                 }
398         }
399         if (!action.queue && !action.drop) {
400                 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
401                                    NULL, "no valid action");
402                 return -rte_errno;
403         }
404         return 0;
405 exit_item_not_supported:
406         rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
407                            items, "item not supported");
408         return -rte_errno;
409 exit_action_not_supported:
410         rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
411                            actions, "action not supported");
412         return -rte_errno;
413 }
414
415 /**
416  * Validate a flow supported by the NIC.
417  *
418  * @see rte_flow_validate()
419  * @see rte_flow_ops
420  */
421 int
422 mlx5_flow_validate(struct rte_eth_dev *dev,
423                    const struct rte_flow_attr *attr,
424                    const struct rte_flow_item items[],
425                    const struct rte_flow_action actions[],
426                    struct rte_flow_error *error)
427 {
428         struct priv *priv = dev->data->dev_private;
429         int ret;
430         struct mlx5_flow flow = { .offset = sizeof(struct ibv_exp_flow_attr) };
431
432         priv_lock(priv);
433         ret = priv_flow_validate(priv, attr, items, actions, error, &flow);
434         priv_unlock(priv);
435         return ret;
436 }
437
438 /**
439  * Convert Ethernet item to Verbs specification.
440  *
441  * @param item[in]
442  *   Item specification.
443  * @param default_mask[in]
444  *   Default bit-masks to use when item->mask is not provided.
445  * @param data[in, out]
446  *   User structure.
447  */
448 static int
449 mlx5_flow_create_eth(const struct rte_flow_item *item,
450                      const void *default_mask,
451                      void *data)
452 {
453         const struct rte_flow_item_eth *spec = item->spec;
454         const struct rte_flow_item_eth *mask = item->mask;
455         struct mlx5_flow *flow = (struct mlx5_flow *)data;
456         struct ibv_exp_flow_spec_eth *eth;
457         const unsigned int eth_size = sizeof(struct ibv_exp_flow_spec_eth);
458         unsigned int i;
459
460         ++flow->ibv_attr->num_of_specs;
461         flow->ibv_attr->priority = 2;
462         eth = (void *)((uintptr_t)flow->ibv_attr + flow->offset);
463         *eth = (struct ibv_exp_flow_spec_eth) {
464                 .type = IBV_EXP_FLOW_SPEC_ETH,
465                 .size = eth_size,
466         };
467         if (!spec)
468                 return 0;
469         if (!mask)
470                 mask = default_mask;
471         memcpy(eth->val.dst_mac, spec->dst.addr_bytes, ETHER_ADDR_LEN);
472         memcpy(eth->val.src_mac, spec->src.addr_bytes, ETHER_ADDR_LEN);
473         memcpy(eth->mask.dst_mac, mask->dst.addr_bytes, ETHER_ADDR_LEN);
474         memcpy(eth->mask.src_mac, mask->src.addr_bytes, ETHER_ADDR_LEN);
475         /* Remove unwanted bits from values. */
476         for (i = 0; i < ETHER_ADDR_LEN; ++i) {
477                 eth->val.dst_mac[i] &= eth->mask.dst_mac[i];
478                 eth->val.src_mac[i] &= eth->mask.src_mac[i];
479         }
480         return 0;
481 }
482
483 /**
484  * Convert IPv4 item to Verbs specification.
485  *
486  * @param item[in]
487  *   Item specification.
488  * @param default_mask[in]
489  *   Default bit-masks to use when item->mask is not provided.
490  * @param data[in, out]
491  *   User structure.
492  */
493 static int
494 mlx5_flow_create_ipv4(const struct rte_flow_item *item,
495                       const void *default_mask,
496                       void *data)
497 {
498         const struct rte_flow_item_ipv4 *spec = item->spec;
499         const struct rte_flow_item_ipv4 *mask = item->mask;
500         struct mlx5_flow *flow = (struct mlx5_flow *)data;
501         struct ibv_exp_flow_spec_ipv4 *ipv4;
502         unsigned int ipv4_size = sizeof(struct ibv_exp_flow_spec_ipv4);
503
504         ++flow->ibv_attr->num_of_specs;
505         flow->ibv_attr->priority = 1;
506         ipv4 = (void *)((uintptr_t)flow->ibv_attr + flow->offset);
507         *ipv4 = (struct ibv_exp_flow_spec_ipv4) {
508                 .type = IBV_EXP_FLOW_SPEC_IPV4,
509                 .size = ipv4_size,
510         };
511         if (!spec)
512                 return 0;
513         if (!mask)
514                 mask = default_mask;
515         ipv4->val = (struct ibv_exp_flow_ipv4_filter){
516                 .src_ip = spec->hdr.src_addr,
517                 .dst_ip = spec->hdr.dst_addr,
518         };
519         ipv4->mask = (struct ibv_exp_flow_ipv4_filter){
520                 .src_ip = mask->hdr.src_addr,
521                 .dst_ip = mask->hdr.dst_addr,
522         };
523         /* Remove unwanted bits from values. */
524         ipv4->val.src_ip &= ipv4->mask.src_ip;
525         ipv4->val.dst_ip &= ipv4->mask.dst_ip;
526         return 0;
527 }
528
529 /**
530  * Convert IPv6 item to Verbs specification.
531  *
532  * @param item[in]
533  *   Item specification.
534  * @param default_mask[in]
535  *   Default bit-masks to use when item->mask is not provided.
536  * @param data[in, out]
537  *   User structure.
538  */
539 static int
540 mlx5_flow_create_ipv6(const struct rte_flow_item *item,
541                       const void *default_mask,
542                       void *data)
543 {
544         const struct rte_flow_item_ipv6 *spec = item->spec;
545         const struct rte_flow_item_ipv6 *mask = item->mask;
546         struct mlx5_flow *flow = (struct mlx5_flow *)data;
547         struct ibv_exp_flow_spec_ipv6 *ipv6;
548         unsigned int ipv6_size = sizeof(struct ibv_exp_flow_spec_ipv6);
549         unsigned int i;
550
551         ++flow->ibv_attr->num_of_specs;
552         flow->ibv_attr->priority = 1;
553         ipv6 = (void *)((uintptr_t)flow->ibv_attr + flow->offset);
554         *ipv6 = (struct ibv_exp_flow_spec_ipv6) {
555                 .type = IBV_EXP_FLOW_SPEC_IPV6,
556                 .size = ipv6_size,
557         };
558         if (!spec)
559                 return 0;
560         if (!mask)
561                 mask = default_mask;
562         memcpy(ipv6->val.src_ip, spec->hdr.src_addr,
563                RTE_DIM(ipv6->val.src_ip));
564         memcpy(ipv6->val.dst_ip, spec->hdr.dst_addr,
565                RTE_DIM(ipv6->val.dst_ip));
566         memcpy(ipv6->mask.src_ip, mask->hdr.src_addr,
567                RTE_DIM(ipv6->mask.src_ip));
568         memcpy(ipv6->mask.dst_ip, mask->hdr.dst_addr,
569                RTE_DIM(ipv6->mask.dst_ip));
570         /* Remove unwanted bits from values. */
571         for (i = 0; i < RTE_DIM(ipv6->val.src_ip); ++i) {
572                 ipv6->val.src_ip[i] &= ipv6->mask.src_ip[i];
573                 ipv6->val.dst_ip[i] &= ipv6->mask.dst_ip[i];
574         }
575         return 0;
576 }
577
578 /**
579  * Convert UDP 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_udp(const struct rte_flow_item *item,
590                      const void *default_mask,
591                      void *data)
592 {
593         const struct rte_flow_item_udp *spec = item->spec;
594         const struct rte_flow_item_udp *mask = item->mask;
595         struct mlx5_flow *flow = (struct mlx5_flow *)data;
596         struct ibv_exp_flow_spec_tcp_udp *udp;
597         unsigned int udp_size = sizeof(struct ibv_exp_flow_spec_tcp_udp);
598
599         ++flow->ibv_attr->num_of_specs;
600         flow->ibv_attr->priority = 0;
601         udp = (void *)((uintptr_t)flow->ibv_attr + flow->offset);
602         *udp = (struct ibv_exp_flow_spec_tcp_udp) {
603                 .type = IBV_EXP_FLOW_SPEC_UDP,
604                 .size = udp_size,
605         };
606         if (!spec)
607                 return 0;
608         if (!mask)
609                 mask = default_mask;
610         udp->val.dst_port = spec->hdr.dst_port;
611         udp->val.src_port = spec->hdr.src_port;
612         udp->mask.dst_port = mask->hdr.dst_port;
613         udp->mask.src_port = mask->hdr.src_port;
614         /* Remove unwanted bits from values. */
615         udp->val.src_port &= udp->mask.src_port;
616         udp->val.dst_port &= udp->mask.dst_port;
617         return 0;
618 }
619
620 /**
621  * Convert TCP item to Verbs specification.
622  *
623  * @param item[in]
624  *   Item specification.
625  * @param default_mask[in]
626  *   Default bit-masks to use when item->mask is not provided.
627  * @param data[in, out]
628  *   User structure.
629  */
630 static int
631 mlx5_flow_create_tcp(const struct rte_flow_item *item,
632                      const void *default_mask,
633                      void *data)
634 {
635         const struct rte_flow_item_tcp *spec = item->spec;
636         const struct rte_flow_item_tcp *mask = item->mask;
637         struct mlx5_flow *flow = (struct mlx5_flow *)data;
638         struct ibv_exp_flow_spec_tcp_udp *tcp;
639         unsigned int tcp_size = sizeof(struct ibv_exp_flow_spec_tcp_udp);
640
641         ++flow->ibv_attr->num_of_specs;
642         flow->ibv_attr->priority = 0;
643         tcp = (void *)((uintptr_t)flow->ibv_attr + flow->offset);
644         *tcp = (struct ibv_exp_flow_spec_tcp_udp) {
645                 .type = IBV_EXP_FLOW_SPEC_TCP,
646                 .size = tcp_size,
647         };
648         if (!spec)
649                 return 0;
650         if (!mask)
651                 mask = default_mask;
652         tcp->val.dst_port = spec->hdr.dst_port;
653         tcp->val.src_port = spec->hdr.src_port;
654         tcp->mask.dst_port = mask->hdr.dst_port;
655         tcp->mask.src_port = mask->hdr.src_port;
656         /* Remove unwanted bits from values. */
657         tcp->val.src_port &= tcp->mask.src_port;
658         tcp->val.dst_port &= tcp->mask.dst_port;
659         return 0;
660 }
661
662 /**
663  * Complete flow rule creation.
664  *
665  * @param priv
666  *   Pointer to private structure.
667  * @param ibv_attr
668  *   Verbs flow attributes.
669  * @param action
670  *   Target action structure.
671  * @param[out] error
672  *   Perform verbose error reporting if not NULL.
673  *
674  * @return
675  *   A flow if the rule could be created.
676  */
677 static struct rte_flow *
678 priv_flow_create_action_queue(struct priv *priv,
679                               struct ibv_exp_flow_attr *ibv_attr,
680                               struct mlx5_flow_action *action,
681                               struct rte_flow_error *error)
682 {
683         struct rxq_ctrl *rxq;
684         struct rte_flow *rte_flow;
685
686         assert(priv->pd);
687         assert(priv->ctx);
688         rte_flow = rte_calloc(__func__, 1, sizeof(*rte_flow), 0);
689         if (!rte_flow) {
690                 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
691                                    NULL, "cannot allocate flow memory");
692                 return NULL;
693         }
694         if (action->drop) {
695                 rte_flow->cq =
696                         ibv_exp_create_cq(priv->ctx, 1, NULL, NULL, 0,
697                                           &(struct ibv_exp_cq_init_attr){
698                                                   .comp_mask = 0,
699                                           });
700                 if (!rte_flow->cq) {
701                         rte_flow_error_set(error, ENOMEM,
702                                            RTE_FLOW_ERROR_TYPE_HANDLE,
703                                            NULL, "cannot allocate CQ");
704                         goto error;
705                 }
706                 rte_flow->wq = ibv_exp_create_wq(priv->ctx,
707                                                  &(struct ibv_exp_wq_init_attr){
708                                                  .wq_type = IBV_EXP_WQT_RQ,
709                                                  .max_recv_wr = 1,
710                                                  .max_recv_sge = 1,
711                                                  .pd = priv->pd,
712                                                  .cq = rte_flow->cq,
713                                                  });
714         } else {
715                 rxq = container_of((*priv->rxqs)[action->queue_id],
716                                    struct rxq_ctrl, rxq);
717                 rte_flow->rxq = &rxq->rxq;
718                 rte_flow->wq = rxq->wq;
719         }
720         rte_flow->ibv_attr = ibv_attr;
721         rte_flow->ind_table = ibv_exp_create_rwq_ind_table(
722                 priv->ctx,
723                 &(struct ibv_exp_rwq_ind_table_init_attr){
724                         .pd = priv->pd,
725                         .log_ind_tbl_size = 0,
726                         .ind_tbl = &rte_flow->wq,
727                         .comp_mask = 0,
728                 });
729         if (!rte_flow->ind_table) {
730                 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
731                                    NULL, "cannot allocate indirection table");
732                 goto error;
733         }
734         rte_flow->qp = ibv_exp_create_qp(
735                 priv->ctx,
736                 &(struct ibv_exp_qp_init_attr){
737                         .qp_type = IBV_QPT_RAW_PACKET,
738                         .comp_mask =
739                                 IBV_EXP_QP_INIT_ATTR_PD |
740                                 IBV_EXP_QP_INIT_ATTR_PORT |
741                                 IBV_EXP_QP_INIT_ATTR_RX_HASH,
742                         .pd = priv->pd,
743                         .rx_hash_conf = &(struct ibv_exp_rx_hash_conf){
744                                 .rx_hash_function =
745                                         IBV_EXP_RX_HASH_FUNC_TOEPLITZ,
746                                 .rx_hash_key_len = rss_hash_default_key_len,
747                                 .rx_hash_key = rss_hash_default_key,
748                                 .rx_hash_fields_mask = 0,
749                                 .rwq_ind_tbl = rte_flow->ind_table,
750                         },
751                         .port_num = priv->port,
752                 });
753         if (!rte_flow->qp) {
754                 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
755                                    NULL, "cannot allocate QP");
756                 goto error;
757         }
758         rte_flow->ibv_flow = ibv_exp_create_flow(rte_flow->qp,
759                                                  rte_flow->ibv_attr);
760         if (!rte_flow->ibv_flow) {
761                 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
762                                    NULL, "flow rule creation failure");
763                 goto error;
764         }
765         return rte_flow;
766 error:
767         assert(rte_flow);
768         if (rte_flow->qp)
769                 ibv_destroy_qp(rte_flow->qp);
770         if (rte_flow->ind_table)
771                 ibv_exp_destroy_rwq_ind_table(rte_flow->ind_table);
772         if (!rte_flow->rxq && rte_flow->wq)
773                 ibv_exp_destroy_wq(rte_flow->wq);
774         if (!rte_flow->rxq && rte_flow->cq)
775                 ibv_destroy_cq(rte_flow->cq);
776         rte_free(rte_flow->ibv_attr);
777         rte_free(rte_flow);
778         return NULL;
779 }
780
781 /**
782  * Convert a flow.
783  *
784  * @param priv
785  *   Pointer to private structure.
786  * @param[in] attr
787  *   Flow rule attributes.
788  * @param[in] pattern
789  *   Pattern specification (list terminated by the END pattern item).
790  * @param[in] actions
791  *   Associated actions (list terminated by the END action).
792  * @param[out] error
793  *   Perform verbose error reporting if not NULL.
794  *
795  * @return
796  *   A flow on success, NULL otherwise.
797  */
798 static struct rte_flow *
799 priv_flow_create(struct priv *priv,
800                  const struct rte_flow_attr *attr,
801                  const struct rte_flow_item items[],
802                  const struct rte_flow_action actions[],
803                  struct rte_flow_error *error)
804 {
805         struct rte_flow *rte_flow;
806         struct mlx5_flow_action action;
807         struct mlx5_flow flow = { .offset = sizeof(struct ibv_exp_flow_attr), };
808         int err;
809
810         err = priv_flow_validate(priv, attr, items, actions, error, &flow);
811         if (err)
812                 goto exit;
813         flow.ibv_attr = rte_malloc(__func__, flow.offset, 0);
814         flow.offset = sizeof(struct ibv_exp_flow_attr);
815         if (!flow.ibv_attr) {
816                 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
817                                    NULL, "cannot allocate ibv_attr memory");
818                 goto exit;
819         }
820         *flow.ibv_attr = (struct ibv_exp_flow_attr){
821                 .type = IBV_EXP_FLOW_ATTR_NORMAL,
822                 .size = sizeof(struct ibv_exp_flow_attr),
823                 .priority = attr->priority,
824                 .num_of_specs = 0,
825                 .port = 0,
826                 .flags = 0,
827                 .reserved = 0,
828         };
829         claim_zero(priv_flow_validate(priv, attr, items, actions,
830                                       error, &flow));
831         action = (struct mlx5_flow_action){
832                 .queue = 0,
833                 .drop = 0,
834         };
835         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; ++actions) {
836                 if (actions->type == RTE_FLOW_ACTION_TYPE_VOID) {
837                         continue;
838                 } else if (actions->type == RTE_FLOW_ACTION_TYPE_QUEUE) {
839                         action.queue = 1;
840                         action.queue_id =
841                                 ((const struct rte_flow_action_queue *)
842                                  actions->conf)->index;
843                 } else if (actions->type == RTE_FLOW_ACTION_TYPE_DROP) {
844                         action.drop = 1;
845                 } else {
846                         rte_flow_error_set(error, ENOTSUP,
847                                            RTE_FLOW_ERROR_TYPE_ACTION,
848                                            actions, "unsupported action");
849                         goto exit;
850                 }
851         }
852         rte_flow = priv_flow_create_action_queue(priv, flow.ibv_attr,
853                                                  &action, error);
854         return rte_flow;
855 exit:
856         rte_free(flow.ibv_attr);
857         return NULL;
858 }
859
860 /**
861  * Create a flow.
862  *
863  * @see rte_flow_create()
864  * @see rte_flow_ops
865  */
866 struct rte_flow *
867 mlx5_flow_create(struct rte_eth_dev *dev,
868                  const struct rte_flow_attr *attr,
869                  const struct rte_flow_item items[],
870                  const struct rte_flow_action actions[],
871                  struct rte_flow_error *error)
872 {
873         struct priv *priv = dev->data->dev_private;
874         struct rte_flow *flow;
875
876         priv_lock(priv);
877         flow = priv_flow_create(priv, attr, items, actions, error);
878         if (flow) {
879                 LIST_INSERT_HEAD(&priv->flows, flow, next);
880                 DEBUG("Flow created %p", (void *)flow);
881         }
882         priv_unlock(priv);
883         return flow;
884 }
885
886 /**
887  * Destroy a flow.
888  *
889  * @param priv
890  *   Pointer to private structure.
891  * @param[in] flow
892  *   Flow to destroy.
893  */
894 static void
895 priv_flow_destroy(struct priv *priv,
896                   struct rte_flow *flow)
897 {
898         (void)priv;
899         LIST_REMOVE(flow, next);
900         if (flow->ibv_flow)
901                 claim_zero(ibv_exp_destroy_flow(flow->ibv_flow));
902         if (flow->qp)
903                 claim_zero(ibv_destroy_qp(flow->qp));
904         if (flow->ind_table)
905                 claim_zero(ibv_exp_destroy_rwq_ind_table(flow->ind_table));
906         if (!flow->rxq && flow->wq)
907                 claim_zero(ibv_exp_destroy_wq(flow->wq));
908         if (!flow->rxq && flow->cq)
909                 claim_zero(ibv_destroy_cq(flow->cq));
910         rte_free(flow->ibv_attr);
911         DEBUG("Flow destroyed %p", (void *)flow);
912         rte_free(flow);
913 }
914
915 /**
916  * Destroy a flow.
917  *
918  * @see rte_flow_destroy()
919  * @see rte_flow_ops
920  */
921 int
922 mlx5_flow_destroy(struct rte_eth_dev *dev,
923                   struct rte_flow *flow,
924                   struct rte_flow_error *error)
925 {
926         struct priv *priv = dev->data->dev_private;
927
928         (void)error;
929         priv_lock(priv);
930         priv_flow_destroy(priv, flow);
931         priv_unlock(priv);
932         return 0;
933 }
934
935 /**
936  * Destroy all flows.
937  *
938  * @param priv
939  *   Pointer to private structure.
940  */
941 static void
942 priv_flow_flush(struct priv *priv)
943 {
944         while (!LIST_EMPTY(&priv->flows)) {
945                 struct rte_flow *flow;
946
947                 flow = LIST_FIRST(&priv->flows);
948                 priv_flow_destroy(priv, flow);
949         }
950 }
951
952 /**
953  * Destroy all flows.
954  *
955  * @see rte_flow_flush()
956  * @see rte_flow_ops
957  */
958 int
959 mlx5_flow_flush(struct rte_eth_dev *dev,
960                 struct rte_flow_error *error)
961 {
962         struct priv *priv = dev->data->dev_private;
963
964         (void)error;
965         priv_lock(priv);
966         priv_flow_flush(priv);
967         priv_unlock(priv);
968         return 0;
969 }
970
971 /**
972  * Remove all flows.
973  *
974  * Called by dev_stop() to remove all flows.
975  *
976  * @param priv
977  *   Pointer to private structure.
978  */
979 void
980 priv_flow_stop(struct priv *priv)
981 {
982         struct rte_flow *flow;
983
984         for (flow = LIST_FIRST(&priv->flows);
985              flow;
986              flow = LIST_NEXT(flow, next)) {
987                 claim_zero(ibv_exp_destroy_flow(flow->ibv_flow));
988                 flow->ibv_flow = NULL;
989                 DEBUG("Flow %p removed", (void *)flow);
990         }
991 }
992
993 /**
994  * Add all flows.
995  *
996  * @param priv
997  *   Pointer to private structure.
998  *
999  * @return
1000  *   0 on success, a errno value otherwise and rte_errno is set.
1001  */
1002 int
1003 priv_flow_start(struct priv *priv)
1004 {
1005         struct rte_flow *flow;
1006
1007         for (flow = LIST_FIRST(&priv->flows);
1008              flow;
1009              flow = LIST_NEXT(flow, next)) {
1010                 flow->ibv_flow = ibv_exp_create_flow(flow->qp,
1011                                                      flow->ibv_attr);
1012                 if (!flow->ibv_flow) {
1013                         DEBUG("Flow %p cannot be applied", (void *)flow);
1014                         rte_errno = EINVAL;
1015                         return rte_errno;
1016                 }
1017                 DEBUG("Flow %p applied", (void *)flow);
1018         }
1019         return 0;
1020 }