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