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