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