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