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