net/mlx5: support GRE tunnel flow
[dpdk.git] / drivers / net / mlx5 / mlx5_flow.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016 6WIND S.A.
3  * Copyright 2016 Mellanox Technologies, Ltd
4  */
5
6 #include <sys/queue.h>
7 #include <stdint.h>
8 #include <string.h>
9
10 /* Verbs header. */
11 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
12 #ifdef PEDANTIC
13 #pragma GCC diagnostic ignored "-Wpedantic"
14 #endif
15 #include <infiniband/verbs.h>
16 #ifdef PEDANTIC
17 #pragma GCC diagnostic error "-Wpedantic"
18 #endif
19
20 #include <rte_common.h>
21 #include <rte_ether.h>
22 #include <rte_eth_ctrl.h>
23 #include <rte_ethdev_driver.h>
24 #include <rte_flow.h>
25 #include <rte_flow_driver.h>
26 #include <rte_malloc.h>
27 #include <rte_ip.h>
28
29 #include "mlx5.h"
30 #include "mlx5_defs.h"
31 #include "mlx5_prm.h"
32 #include "mlx5_glue.h"
33
34 /* Flow priority for control plane flows. */
35 #define MLX5_CTRL_FLOW_PRIORITY 1
36
37 /* Internet Protocol versions. */
38 #define MLX5_IPV4 4
39 #define MLX5_IPV6 6
40 #define MLX5_GRE 47
41
42 #ifndef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
43 struct ibv_flow_spec_counter_action {
44         int dummy;
45 };
46 #endif
47
48 /* Dev ops structure defined in mlx5.c */
49 extern const struct eth_dev_ops mlx5_dev_ops;
50 extern const struct eth_dev_ops mlx5_dev_ops_isolate;
51
52 /** Structure give to the conversion functions. */
53 struct mlx5_flow_data {
54         struct mlx5_flow_parse *parser; /** Parser context. */
55         struct rte_flow_error *error; /** Error context. */
56 };
57
58 static int
59 mlx5_flow_create_eth(const struct rte_flow_item *item,
60                      const void *default_mask,
61                      struct mlx5_flow_data *data);
62
63 static int
64 mlx5_flow_create_vlan(const struct rte_flow_item *item,
65                       const void *default_mask,
66                       struct mlx5_flow_data *data);
67
68 static int
69 mlx5_flow_create_ipv4(const struct rte_flow_item *item,
70                       const void *default_mask,
71                       struct mlx5_flow_data *data);
72
73 static int
74 mlx5_flow_create_ipv6(const struct rte_flow_item *item,
75                       const void *default_mask,
76                       struct mlx5_flow_data *data);
77
78 static int
79 mlx5_flow_create_udp(const struct rte_flow_item *item,
80                      const void *default_mask,
81                      struct mlx5_flow_data *data);
82
83 static int
84 mlx5_flow_create_tcp(const struct rte_flow_item *item,
85                      const void *default_mask,
86                      struct mlx5_flow_data *data);
87
88 static int
89 mlx5_flow_create_vxlan(const struct rte_flow_item *item,
90                        const void *default_mask,
91                        struct mlx5_flow_data *data);
92
93 static int
94 mlx5_flow_create_gre(const struct rte_flow_item *item,
95                      const void *default_mask,
96                      struct mlx5_flow_data *data);
97
98 struct mlx5_flow_parse;
99
100 static void
101 mlx5_flow_create_copy(struct mlx5_flow_parse *parser, void *src,
102                       unsigned int size);
103
104 static int
105 mlx5_flow_create_flag_mark(struct mlx5_flow_parse *parser, uint32_t mark_id);
106
107 static int
108 mlx5_flow_create_count(struct rte_eth_dev *dev, struct mlx5_flow_parse *parser);
109
110 /* Hash RX queue types. */
111 enum hash_rxq_type {
112         HASH_RXQ_TCPV4,
113         HASH_RXQ_UDPV4,
114         HASH_RXQ_IPV4,
115         HASH_RXQ_TCPV6,
116         HASH_RXQ_UDPV6,
117         HASH_RXQ_IPV6,
118         HASH_RXQ_ETH,
119 };
120
121 /* Initialization data for hash RX queue. */
122 struct hash_rxq_init {
123         uint64_t hash_fields; /* Fields that participate in the hash. */
124         uint64_t dpdk_rss_hf; /* Matching DPDK RSS hash fields. */
125         unsigned int flow_priority; /* Flow priority to use. */
126         unsigned int ip_version; /* Internet protocol. */
127 };
128
129 /* Initialization data for hash RX queues. */
130 const struct hash_rxq_init hash_rxq_init[] = {
131         [HASH_RXQ_TCPV4] = {
132                 .hash_fields = (IBV_RX_HASH_SRC_IPV4 |
133                                 IBV_RX_HASH_DST_IPV4 |
134                                 IBV_RX_HASH_SRC_PORT_TCP |
135                                 IBV_RX_HASH_DST_PORT_TCP),
136                 .dpdk_rss_hf = ETH_RSS_NONFRAG_IPV4_TCP,
137                 .flow_priority = 0,
138                 .ip_version = MLX5_IPV4,
139         },
140         [HASH_RXQ_UDPV4] = {
141                 .hash_fields = (IBV_RX_HASH_SRC_IPV4 |
142                                 IBV_RX_HASH_DST_IPV4 |
143                                 IBV_RX_HASH_SRC_PORT_UDP |
144                                 IBV_RX_HASH_DST_PORT_UDP),
145                 .dpdk_rss_hf = ETH_RSS_NONFRAG_IPV4_UDP,
146                 .flow_priority = 0,
147                 .ip_version = MLX5_IPV4,
148         },
149         [HASH_RXQ_IPV4] = {
150                 .hash_fields = (IBV_RX_HASH_SRC_IPV4 |
151                                 IBV_RX_HASH_DST_IPV4),
152                 .dpdk_rss_hf = (ETH_RSS_IPV4 |
153                                 ETH_RSS_FRAG_IPV4),
154                 .flow_priority = 1,
155                 .ip_version = MLX5_IPV4,
156         },
157         [HASH_RXQ_TCPV6] = {
158                 .hash_fields = (IBV_RX_HASH_SRC_IPV6 |
159                                 IBV_RX_HASH_DST_IPV6 |
160                                 IBV_RX_HASH_SRC_PORT_TCP |
161                                 IBV_RX_HASH_DST_PORT_TCP),
162                 .dpdk_rss_hf = ETH_RSS_NONFRAG_IPV6_TCP,
163                 .flow_priority = 0,
164                 .ip_version = MLX5_IPV6,
165         },
166         [HASH_RXQ_UDPV6] = {
167                 .hash_fields = (IBV_RX_HASH_SRC_IPV6 |
168                                 IBV_RX_HASH_DST_IPV6 |
169                                 IBV_RX_HASH_SRC_PORT_UDP |
170                                 IBV_RX_HASH_DST_PORT_UDP),
171                 .dpdk_rss_hf = ETH_RSS_NONFRAG_IPV6_UDP,
172                 .flow_priority = 0,
173                 .ip_version = MLX5_IPV6,
174         },
175         [HASH_RXQ_IPV6] = {
176                 .hash_fields = (IBV_RX_HASH_SRC_IPV6 |
177                                 IBV_RX_HASH_DST_IPV6),
178                 .dpdk_rss_hf = (ETH_RSS_IPV6 |
179                                 ETH_RSS_FRAG_IPV6),
180                 .flow_priority = 1,
181                 .ip_version = MLX5_IPV6,
182         },
183         [HASH_RXQ_ETH] = {
184                 .hash_fields = 0,
185                 .dpdk_rss_hf = 0,
186                 .flow_priority = 2,
187         },
188 };
189
190 /* Number of entries in hash_rxq_init[]. */
191 const unsigned int hash_rxq_init_n = RTE_DIM(hash_rxq_init);
192
193 /** Structure for holding counter stats. */
194 struct mlx5_flow_counter_stats {
195         uint64_t hits; /**< Number of packets matched by the rule. */
196         uint64_t bytes; /**< Number of bytes matched by the rule. */
197 };
198
199 /** Structure for Drop queue. */
200 struct mlx5_hrxq_drop {
201         struct ibv_rwq_ind_table *ind_table; /**< Indirection table. */
202         struct ibv_qp *qp; /**< Verbs queue pair. */
203         struct ibv_wq *wq; /**< Verbs work queue. */
204         struct ibv_cq *cq; /**< Verbs completion queue. */
205 };
206
207 /* Flows structures. */
208 struct mlx5_flow {
209         uint64_t hash_fields; /**< Fields that participate in the hash. */
210         struct ibv_flow_attr *ibv_attr; /**< Pointer to Verbs attributes. */
211         struct ibv_flow *ibv_flow; /**< Verbs flow. */
212         struct mlx5_hrxq *hrxq; /**< Hash Rx queues. */
213 };
214
215 /* Drop flows structures. */
216 struct mlx5_flow_drop {
217         struct ibv_flow_attr *ibv_attr; /**< Pointer to Verbs attributes. */
218         struct ibv_flow *ibv_flow; /**< Verbs flow. */
219 };
220
221 struct rte_flow {
222         TAILQ_ENTRY(rte_flow) next; /**< Pointer to the next flow structure. */
223         uint32_t mark:1; /**< Set if the flow is marked. */
224         uint32_t drop:1; /**< Drop queue. */
225         struct rte_flow_action_rss rss_conf; /**< RSS configuration */
226         uint16_t (*queues)[]; /**< Queues indexes to use. */
227         uint8_t rss_key[40]; /**< copy of the RSS key. */
228         struct ibv_counter_set *cs; /**< Holds the counters for the rule. */
229         struct mlx5_flow_counter_stats counter_stats;/**<The counter stats. */
230         struct mlx5_flow frxq[RTE_DIM(hash_rxq_init)];
231         /**< Flow with Rx queue. */
232 };
233
234 /** Static initializer for items. */
235 #define ITEMS(...) \
236         (const enum rte_flow_item_type []){ \
237                 __VA_ARGS__, RTE_FLOW_ITEM_TYPE_END, \
238         }
239
240 #define IS_TUNNEL(type) ( \
241         (type) == RTE_FLOW_ITEM_TYPE_VXLAN || \
242         (type) == RTE_FLOW_ITEM_TYPE_GRE)
243
244 /** Structure to generate a simple graph of layers supported by the NIC. */
245 struct mlx5_flow_items {
246         /** List of possible actions for these items. */
247         const enum rte_flow_action_type *const actions;
248         /** Bit-masks corresponding to the possibilities for the item. */
249         const void *mask;
250         /**
251          * Default bit-masks to use when item->mask is not provided. When
252          * \default_mask is also NULL, the full supported bit-mask (\mask) is
253          * used instead.
254          */
255         const void *default_mask;
256         /** Bit-masks size in bytes. */
257         const unsigned int mask_sz;
258         /**
259          * Conversion function from rte_flow to NIC specific flow.
260          *
261          * @param item
262          *   rte_flow item to convert.
263          * @param default_mask
264          *   Default bit-masks to use when item->mask is not provided.
265          * @param data
266          *   Internal structure to store the conversion.
267          *
268          * @return
269          *   0 on success, a negative errno value otherwise and rte_errno is
270          *   set.
271          */
272         int (*convert)(const struct rte_flow_item *item,
273                        const void *default_mask,
274                        struct mlx5_flow_data *data);
275         /** Size in bytes of the destination structure. */
276         const unsigned int dst_sz;
277         /** List of possible following items.  */
278         const enum rte_flow_item_type *const items;
279 };
280
281 /** Valid action for this PMD. */
282 static const enum rte_flow_action_type valid_actions[] = {
283         RTE_FLOW_ACTION_TYPE_DROP,
284         RTE_FLOW_ACTION_TYPE_QUEUE,
285         RTE_FLOW_ACTION_TYPE_MARK,
286         RTE_FLOW_ACTION_TYPE_FLAG,
287 #ifdef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
288         RTE_FLOW_ACTION_TYPE_COUNT,
289 #endif
290         RTE_FLOW_ACTION_TYPE_END,
291 };
292
293 /** Graph of supported items and associated actions. */
294 static const struct mlx5_flow_items mlx5_flow_items[] = {
295         [RTE_FLOW_ITEM_TYPE_END] = {
296                 .items = ITEMS(RTE_FLOW_ITEM_TYPE_ETH,
297                                RTE_FLOW_ITEM_TYPE_VXLAN,
298                                RTE_FLOW_ITEM_TYPE_GRE),
299         },
300         [RTE_FLOW_ITEM_TYPE_ETH] = {
301                 .items = ITEMS(RTE_FLOW_ITEM_TYPE_VLAN,
302                                RTE_FLOW_ITEM_TYPE_IPV4,
303                                RTE_FLOW_ITEM_TYPE_IPV6),
304                 .actions = valid_actions,
305                 .mask = &(const struct rte_flow_item_eth){
306                         .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
307                         .src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
308                         .type = -1,
309                 },
310                 .default_mask = &rte_flow_item_eth_mask,
311                 .mask_sz = sizeof(struct rte_flow_item_eth),
312                 .convert = mlx5_flow_create_eth,
313                 .dst_sz = sizeof(struct ibv_flow_spec_eth),
314         },
315         [RTE_FLOW_ITEM_TYPE_VLAN] = {
316                 .items = ITEMS(RTE_FLOW_ITEM_TYPE_IPV4,
317                                RTE_FLOW_ITEM_TYPE_IPV6),
318                 .actions = valid_actions,
319                 .mask = &(const struct rte_flow_item_vlan){
320                         .tci = -1,
321                         .inner_type = -1,
322                 },
323                 .default_mask = &rte_flow_item_vlan_mask,
324                 .mask_sz = sizeof(struct rte_flow_item_vlan),
325                 .convert = mlx5_flow_create_vlan,
326                 .dst_sz = 0,
327         },
328         [RTE_FLOW_ITEM_TYPE_IPV4] = {
329                 .items = ITEMS(RTE_FLOW_ITEM_TYPE_UDP,
330                                RTE_FLOW_ITEM_TYPE_TCP,
331                                RTE_FLOW_ITEM_TYPE_GRE),
332                 .actions = valid_actions,
333                 .mask = &(const struct rte_flow_item_ipv4){
334                         .hdr = {
335                                 .src_addr = -1,
336                                 .dst_addr = -1,
337                                 .type_of_service = -1,
338                                 .next_proto_id = -1,
339                         },
340                 },
341                 .default_mask = &rte_flow_item_ipv4_mask,
342                 .mask_sz = sizeof(struct rte_flow_item_ipv4),
343                 .convert = mlx5_flow_create_ipv4,
344                 .dst_sz = sizeof(struct ibv_flow_spec_ipv4_ext),
345         },
346         [RTE_FLOW_ITEM_TYPE_IPV6] = {
347                 .items = ITEMS(RTE_FLOW_ITEM_TYPE_UDP,
348                                RTE_FLOW_ITEM_TYPE_TCP,
349                                RTE_FLOW_ITEM_TYPE_GRE),
350                 .actions = valid_actions,
351                 .mask = &(const struct rte_flow_item_ipv6){
352                         .hdr = {
353                                 .src_addr = {
354                                         0xff, 0xff, 0xff, 0xff,
355                                         0xff, 0xff, 0xff, 0xff,
356                                         0xff, 0xff, 0xff, 0xff,
357                                         0xff, 0xff, 0xff, 0xff,
358                                 },
359                                 .dst_addr = {
360                                         0xff, 0xff, 0xff, 0xff,
361                                         0xff, 0xff, 0xff, 0xff,
362                                         0xff, 0xff, 0xff, 0xff,
363                                         0xff, 0xff, 0xff, 0xff,
364                                 },
365                                 .vtc_flow = -1,
366                                 .proto = -1,
367                                 .hop_limits = -1,
368                         },
369                 },
370                 .default_mask = &rte_flow_item_ipv6_mask,
371                 .mask_sz = sizeof(struct rte_flow_item_ipv6),
372                 .convert = mlx5_flow_create_ipv6,
373                 .dst_sz = sizeof(struct ibv_flow_spec_ipv6),
374         },
375         [RTE_FLOW_ITEM_TYPE_UDP] = {
376                 .items = ITEMS(RTE_FLOW_ITEM_TYPE_VXLAN),
377                 .actions = valid_actions,
378                 .mask = &(const struct rte_flow_item_udp){
379                         .hdr = {
380                                 .src_port = -1,
381                                 .dst_port = -1,
382                         },
383                 },
384                 .default_mask = &rte_flow_item_udp_mask,
385                 .mask_sz = sizeof(struct rte_flow_item_udp),
386                 .convert = mlx5_flow_create_udp,
387                 .dst_sz = sizeof(struct ibv_flow_spec_tcp_udp),
388         },
389         [RTE_FLOW_ITEM_TYPE_TCP] = {
390                 .actions = valid_actions,
391                 .mask = &(const struct rte_flow_item_tcp){
392                         .hdr = {
393                                 .src_port = -1,
394                                 .dst_port = -1,
395                         },
396                 },
397                 .default_mask = &rte_flow_item_tcp_mask,
398                 .mask_sz = sizeof(struct rte_flow_item_tcp),
399                 .convert = mlx5_flow_create_tcp,
400                 .dst_sz = sizeof(struct ibv_flow_spec_tcp_udp),
401         },
402         [RTE_FLOW_ITEM_TYPE_GRE] = {
403                 .items = ITEMS(RTE_FLOW_ITEM_TYPE_ETH,
404                                RTE_FLOW_ITEM_TYPE_IPV4,
405                                RTE_FLOW_ITEM_TYPE_IPV6),
406                 .actions = valid_actions,
407                 .mask = &(const struct rte_flow_item_gre){
408                         .protocol = -1,
409                 },
410                 .default_mask = &rte_flow_item_gre_mask,
411                 .mask_sz = sizeof(struct rte_flow_item_gre),
412                 .convert = mlx5_flow_create_gre,
413                 .dst_sz = sizeof(struct ibv_flow_spec_tunnel),
414         },
415         [RTE_FLOW_ITEM_TYPE_VXLAN] = {
416                 .items = ITEMS(RTE_FLOW_ITEM_TYPE_ETH),
417                 .actions = valid_actions,
418                 .mask = &(const struct rte_flow_item_vxlan){
419                         .vni = "\xff\xff\xff",
420                 },
421                 .default_mask = &rte_flow_item_vxlan_mask,
422                 .mask_sz = sizeof(struct rte_flow_item_vxlan),
423                 .convert = mlx5_flow_create_vxlan,
424                 .dst_sz = sizeof(struct ibv_flow_spec_tunnel),
425         },
426 };
427
428 /** Structure to pass to the conversion function. */
429 struct mlx5_flow_parse {
430         uint32_t inner; /**< Verbs value, set once tunnel is encountered. */
431         uint32_t create:1;
432         /**< Whether resources should remain after a validate. */
433         uint32_t drop:1; /**< Target is a drop queue. */
434         uint32_t mark:1; /**< Mark is present in the flow. */
435         uint32_t count:1; /**< Count is present in the flow. */
436         uint32_t mark_id; /**< Mark identifier. */
437         struct rte_flow_action_rss rss_conf; /**< RSS configuration */
438         uint16_t queues[RTE_MAX_QUEUES_PER_PORT]; /**< Queues indexes to use. */
439         uint8_t rss_key[40]; /**< copy of the RSS key. */
440         enum hash_rxq_type layer; /**< Last pattern layer detected. */
441         enum hash_rxq_type out_layer; /**< Last outer pattern layer detected. */
442         struct ibv_counter_set *cs; /**< Holds the counter set for the rule */
443         struct {
444                 struct ibv_flow_attr *ibv_attr;
445                 /**< Pointer to Verbs attributes. */
446                 unsigned int offset;
447                 /**< Current position or total size of the attribute. */
448         } queue[RTE_DIM(hash_rxq_init)];
449 };
450
451 static const struct rte_flow_ops mlx5_flow_ops = {
452         .validate = mlx5_flow_validate,
453         .create = mlx5_flow_create,
454         .destroy = mlx5_flow_destroy,
455         .flush = mlx5_flow_flush,
456 #ifdef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
457         .query = mlx5_flow_query,
458 #else
459         .query = NULL,
460 #endif
461         .isolate = mlx5_flow_isolate,
462 };
463
464 /* Convert FDIR request to Generic flow. */
465 struct mlx5_fdir {
466         struct rte_flow_attr attr;
467         struct rte_flow_action actions[2];
468         struct rte_flow_item items[4];
469         struct rte_flow_item_eth l2;
470         struct rte_flow_item_eth l2_mask;
471         union {
472                 struct rte_flow_item_ipv4 ipv4;
473                 struct rte_flow_item_ipv6 ipv6;
474         } l3;
475         union {
476                 struct rte_flow_item_ipv4 ipv4;
477                 struct rte_flow_item_ipv6 ipv6;
478         } l3_mask;
479         union {
480                 struct rte_flow_item_udp udp;
481                 struct rte_flow_item_tcp tcp;
482         } l4;
483         union {
484                 struct rte_flow_item_udp udp;
485                 struct rte_flow_item_tcp tcp;
486         } l4_mask;
487         struct rte_flow_action_queue queue;
488 };
489
490 /* Verbs specification header. */
491 struct ibv_spec_header {
492         enum ibv_flow_spec_type type;
493         uint16_t size;
494 };
495
496 /**
497  * Check support for a given item.
498  *
499  * @param item[in]
500  *   Item specification.
501  * @param mask[in]
502  *   Bit-masks covering supported fields to compare with spec, last and mask in
503  *   \item.
504  * @param size
505  *   Bit-Mask size in bytes.
506  *
507  * @return
508  *   0 on success, a negative errno value otherwise and rte_errno is set.
509  */
510 static int
511 mlx5_flow_item_validate(const struct rte_flow_item *item,
512                         const uint8_t *mask, unsigned int size)
513 {
514         if (!item->spec && (item->mask || item->last)) {
515                 rte_errno = EINVAL;
516                 return -rte_errno;
517         }
518         if (item->spec && !item->mask) {
519                 unsigned int i;
520                 const uint8_t *spec = item->spec;
521
522                 for (i = 0; i < size; ++i)
523                         if ((spec[i] | mask[i]) != mask[i]) {
524                                 rte_errno = EINVAL;
525                                 return -rte_errno;
526                         }
527         }
528         if (item->last && !item->mask) {
529                 unsigned int i;
530                 const uint8_t *spec = item->last;
531
532                 for (i = 0; i < size; ++i)
533                         if ((spec[i] | mask[i]) != mask[i]) {
534                                 rte_errno = EINVAL;
535                                 return -rte_errno;
536                         }
537         }
538         if (item->mask) {
539                 unsigned int i;
540                 const uint8_t *spec = item->spec;
541
542                 for (i = 0; i < size; ++i)
543                         if ((spec[i] | mask[i]) != mask[i]) {
544                                 rte_errno = EINVAL;
545                                 return -rte_errno;
546                         }
547         }
548         if (item->spec && item->last) {
549                 uint8_t spec[size];
550                 uint8_t last[size];
551                 const uint8_t *apply = mask;
552                 unsigned int i;
553                 int ret;
554
555                 if (item->mask)
556                         apply = item->mask;
557                 for (i = 0; i < size; ++i) {
558                         spec[i] = ((const uint8_t *)item->spec)[i] & apply[i];
559                         last[i] = ((const uint8_t *)item->last)[i] & apply[i];
560                 }
561                 ret = memcmp(spec, last, size);
562                 if (ret != 0) {
563                         rte_errno = EINVAL;
564                         return -rte_errno;
565                 }
566         }
567         return 0;
568 }
569
570 /**
571  * Extract attribute to the parser.
572  *
573  * @param[in] attr
574  *   Flow rule attributes.
575  * @param[out] error
576  *   Perform verbose error reporting if not NULL.
577  *
578  * @return
579  *   0 on success, a negative errno value otherwise and rte_errno is set.
580  */
581 static int
582 mlx5_flow_convert_attributes(const struct rte_flow_attr *attr,
583                              struct rte_flow_error *error)
584 {
585         if (attr->group) {
586                 rte_flow_error_set(error, ENOTSUP,
587                                    RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
588                                    NULL,
589                                    "groups are not supported");
590                 return -rte_errno;
591         }
592         if (attr->priority && attr->priority != MLX5_CTRL_FLOW_PRIORITY) {
593                 rte_flow_error_set(error, ENOTSUP,
594                                    RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
595                                    NULL,
596                                    "priorities are not supported");
597                 return -rte_errno;
598         }
599         if (attr->egress) {
600                 rte_flow_error_set(error, ENOTSUP,
601                                    RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
602                                    NULL,
603                                    "egress is not supported");
604                 return -rte_errno;
605         }
606         if (attr->transfer) {
607                 rte_flow_error_set(error, ENOTSUP,
608                                    RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
609                                    NULL,
610                                    "transfer is not supported");
611                 return -rte_errno;
612         }
613         if (!attr->ingress) {
614                 rte_flow_error_set(error, ENOTSUP,
615                                    RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
616                                    NULL,
617                                    "only ingress is supported");
618                 return -rte_errno;
619         }
620         return 0;
621 }
622
623 /**
624  * Extract actions request to the parser.
625  *
626  * @param dev
627  *   Pointer to Ethernet device.
628  * @param[in] actions
629  *   Associated actions (list terminated by the END action).
630  * @param[out] error
631  *   Perform verbose error reporting if not NULL.
632  * @param[in, out] parser
633  *   Internal parser structure.
634  *
635  * @return
636  *   0 on success, a negative errno value otherwise and rte_errno is set.
637  */
638 static int
639 mlx5_flow_convert_actions(struct rte_eth_dev *dev,
640                           const struct rte_flow_action actions[],
641                           struct rte_flow_error *error,
642                           struct mlx5_flow_parse *parser)
643 {
644         enum { FATE = 1, MARK = 2, COUNT = 4, };
645         uint32_t overlap = 0;
646         struct priv *priv = dev->data->dev_private;
647
648         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; ++actions) {
649                 if (actions->type == RTE_FLOW_ACTION_TYPE_VOID) {
650                         continue;
651                 } else if (actions->type == RTE_FLOW_ACTION_TYPE_DROP) {
652                         if (overlap & FATE)
653                                 goto exit_action_overlap;
654                         overlap |= FATE;
655                         parser->drop = 1;
656                 } else if (actions->type == RTE_FLOW_ACTION_TYPE_QUEUE) {
657                         const struct rte_flow_action_queue *queue =
658                                 (const struct rte_flow_action_queue *)
659                                 actions->conf;
660
661                         if (overlap & FATE)
662                                 goto exit_action_overlap;
663                         overlap |= FATE;
664                         if (!queue || (queue->index > (priv->rxqs_n - 1)))
665                                 goto exit_action_not_supported;
666                         parser->queues[0] = queue->index;
667                         parser->rss_conf = (struct rte_flow_action_rss){
668                                 .queue_num = 1,
669                                 .queue = parser->queues,
670                         };
671                 } else if (actions->type == RTE_FLOW_ACTION_TYPE_RSS) {
672                         const struct rte_flow_action_rss *rss =
673                                 (const struct rte_flow_action_rss *)
674                                 actions->conf;
675                         const uint8_t *rss_key;
676                         uint32_t rss_key_len;
677                         uint16_t n;
678
679                         if (overlap & FATE)
680                                 goto exit_action_overlap;
681                         overlap |= FATE;
682                         if (rss->func &&
683                             rss->func != RTE_ETH_HASH_FUNCTION_TOEPLITZ) {
684                                 rte_flow_error_set(error, EINVAL,
685                                                    RTE_FLOW_ERROR_TYPE_ACTION,
686                                                    actions,
687                                                    "the only supported RSS hash"
688                                                    " function is Toeplitz");
689                                 return -rte_errno;
690                         }
691                         if (rss->level) {
692                                 rte_flow_error_set(error, EINVAL,
693                                                    RTE_FLOW_ERROR_TYPE_ACTION,
694                                                    actions,
695                                                    "a nonzero RSS encapsulation"
696                                                    " level is not supported");
697                                 return -rte_errno;
698                         }
699                         if (rss->types & MLX5_RSS_HF_MASK) {
700                                 rte_flow_error_set(error, EINVAL,
701                                                    RTE_FLOW_ERROR_TYPE_ACTION,
702                                                    actions,
703                                                    "unsupported RSS type"
704                                                    " requested");
705                                 return -rte_errno;
706                         }
707                         if (rss->key_len) {
708                                 rss_key_len = rss->key_len;
709                                 rss_key = rss->key;
710                         } else {
711                                 rss_key_len = rss_hash_default_key_len;
712                                 rss_key = rss_hash_default_key;
713                         }
714                         if (rss_key_len != RTE_DIM(parser->rss_key)) {
715                                 rte_flow_error_set(error, EINVAL,
716                                                    RTE_FLOW_ERROR_TYPE_ACTION,
717                                                    actions,
718                                                    "RSS hash key must be"
719                                                    " exactly 40 bytes long");
720                                 return -rte_errno;
721                         }
722                         if (!rss->queue_num) {
723                                 rte_flow_error_set(error, EINVAL,
724                                                    RTE_FLOW_ERROR_TYPE_ACTION,
725                                                    actions,
726                                                    "no valid queues");
727                                 return -rte_errno;
728                         }
729                         if (rss->queue_num > RTE_DIM(parser->queues)) {
730                                 rte_flow_error_set(error, EINVAL,
731                                                    RTE_FLOW_ERROR_TYPE_ACTION,
732                                                    actions,
733                                                    "too many queues for RSS"
734                                                    " context");
735                                 return -rte_errno;
736                         }
737                         for (n = 0; n < rss->queue_num; ++n) {
738                                 if (rss->queue[n] >= priv->rxqs_n) {
739                                         rte_flow_error_set(error, EINVAL,
740                                                    RTE_FLOW_ERROR_TYPE_ACTION,
741                                                    actions,
742                                                    "queue id > number of"
743                                                    " queues");
744                                         return -rte_errno;
745                                 }
746                         }
747                         parser->rss_conf = (struct rte_flow_action_rss){
748                                 .func = RTE_ETH_HASH_FUNCTION_DEFAULT,
749                                 .level = 0,
750                                 .types = rss->types,
751                                 .key_len = rss_key_len,
752                                 .queue_num = rss->queue_num,
753                                 .key = memcpy(parser->rss_key, rss_key,
754                                               sizeof(*rss_key) * rss_key_len),
755                                 .queue = memcpy(parser->queues, rss->queue,
756                                                 sizeof(*rss->queue) *
757                                                 rss->queue_num),
758                         };
759                 } else if (actions->type == RTE_FLOW_ACTION_TYPE_MARK) {
760                         const struct rte_flow_action_mark *mark =
761                                 (const struct rte_flow_action_mark *)
762                                 actions->conf;
763
764                         if (overlap & MARK)
765                                 goto exit_action_overlap;
766                         overlap |= MARK;
767                         if (!mark) {
768                                 rte_flow_error_set(error, EINVAL,
769                                                    RTE_FLOW_ERROR_TYPE_ACTION,
770                                                    actions,
771                                                    "mark must be defined");
772                                 return -rte_errno;
773                         } else if (mark->id >= MLX5_FLOW_MARK_MAX) {
774                                 rte_flow_error_set(error, ENOTSUP,
775                                                    RTE_FLOW_ERROR_TYPE_ACTION,
776                                                    actions,
777                                                    "mark must be between 0"
778                                                    " and 16777199");
779                                 return -rte_errno;
780                         }
781                         parser->mark = 1;
782                         parser->mark_id = mark->id;
783                 } else if (actions->type == RTE_FLOW_ACTION_TYPE_FLAG) {
784                         if (overlap & MARK)
785                                 goto exit_action_overlap;
786                         overlap |= MARK;
787                         parser->mark = 1;
788                 } else if (actions->type == RTE_FLOW_ACTION_TYPE_COUNT &&
789                            priv->config.flow_counter_en) {
790                         if (overlap & COUNT)
791                                 goto exit_action_overlap;
792                         overlap |= COUNT;
793                         parser->count = 1;
794                 } else {
795                         goto exit_action_not_supported;
796                 }
797         }
798         /* When fate is unknown, drop traffic. */
799         if (!(overlap & FATE))
800                 parser->drop = 1;
801         if (parser->drop && parser->mark)
802                 parser->mark = 0;
803         if (!parser->rss_conf.queue_num && !parser->drop) {
804                 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
805                                    NULL, "no valid action");
806                 return -rte_errno;
807         }
808         return 0;
809 exit_action_not_supported:
810         rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
811                            actions, "action not supported");
812         return -rte_errno;
813 exit_action_overlap:
814         rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
815                            actions, "overlapping actions are not supported");
816         return -rte_errno;
817 }
818
819 /**
820  * Validate items.
821  *
822  * @param[in] items
823  *   Pattern specification (list terminated by the END pattern item).
824  * @param[out] error
825  *   Perform verbose error reporting if not NULL.
826  * @param[in, out] parser
827  *   Internal parser structure.
828  *
829  * @return
830  *   0 on success, a negative errno value otherwise and rte_errno is set.
831  */
832 static int
833 mlx5_flow_convert_items_validate(const struct rte_flow_item items[],
834                                  struct rte_flow_error *error,
835                                  struct mlx5_flow_parse *parser)
836 {
837         const struct mlx5_flow_items *cur_item = mlx5_flow_items;
838         unsigned int i;
839         int ret = 0;
840
841         /* Initialise the offsets to start after verbs attribute. */
842         for (i = 0; i != hash_rxq_init_n; ++i)
843                 parser->queue[i].offset = sizeof(struct ibv_flow_attr);
844         for (; items->type != RTE_FLOW_ITEM_TYPE_END; ++items) {
845                 const struct mlx5_flow_items *token = NULL;
846                 unsigned int n;
847
848                 if (items->type == RTE_FLOW_ITEM_TYPE_VOID)
849                         continue;
850                 for (i = 0;
851                      cur_item->items &&
852                      cur_item->items[i] != RTE_FLOW_ITEM_TYPE_END;
853                      ++i) {
854                         if (cur_item->items[i] == items->type) {
855                                 token = &mlx5_flow_items[items->type];
856                                 break;
857                         }
858                 }
859                 if (!token) {
860                         ret = -ENOTSUP;
861                         goto exit_item_not_supported;
862                 }
863                 cur_item = token;
864                 ret = mlx5_flow_item_validate(items,
865                                               (const uint8_t *)cur_item->mask,
866                                               cur_item->mask_sz);
867                 if (ret)
868                         goto exit_item_not_supported;
869                 if (IS_TUNNEL(items->type)) {
870                         if (parser->inner) {
871                                 rte_flow_error_set(error, ENOTSUP,
872                                                    RTE_FLOW_ERROR_TYPE_ITEM,
873                                                    items,
874                                                    "Cannot recognize multiple"
875                                                    " tunnel encapsulations.");
876                                 return -rte_errno;
877                         }
878                         parser->inner = IBV_FLOW_SPEC_INNER;
879                 }
880                 if (parser->drop) {
881                         parser->queue[HASH_RXQ_ETH].offset += cur_item->dst_sz;
882                 } else {
883                         for (n = 0; n != hash_rxq_init_n; ++n)
884                                 parser->queue[n].offset += cur_item->dst_sz;
885                 }
886         }
887         if (parser->drop) {
888                 parser->queue[HASH_RXQ_ETH].offset +=
889                         sizeof(struct ibv_flow_spec_action_drop);
890         }
891         if (parser->mark) {
892                 for (i = 0; i != hash_rxq_init_n; ++i)
893                         parser->queue[i].offset +=
894                                 sizeof(struct ibv_flow_spec_action_tag);
895         }
896         if (parser->count) {
897                 unsigned int size = sizeof(struct ibv_flow_spec_counter_action);
898
899                 for (i = 0; i != hash_rxq_init_n; ++i)
900                         parser->queue[i].offset += size;
901         }
902         return 0;
903 exit_item_not_supported:
904         return rte_flow_error_set(error, -ret, RTE_FLOW_ERROR_TYPE_ITEM,
905                                   items, "item not supported");
906 }
907
908 /**
909  * Allocate memory space to store verbs flow attributes.
910  *
911  * @param[in] size
912  *   Amount of byte to allocate.
913  * @param[out] error
914  *   Perform verbose error reporting if not NULL.
915  *
916  * @return
917  *   A verbs flow attribute on success, NULL otherwise and rte_errno is set.
918  */
919 static struct ibv_flow_attr *
920 mlx5_flow_convert_allocate(unsigned int size, struct rte_flow_error *error)
921 {
922         struct ibv_flow_attr *ibv_attr;
923
924         ibv_attr = rte_calloc(__func__, 1, size, 0);
925         if (!ibv_attr) {
926                 rte_flow_error_set(error, ENOMEM,
927                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
928                                    NULL,
929                                    "cannot allocate verbs spec attributes");
930                 return NULL;
931         }
932         return ibv_attr;
933 }
934
935 /**
936  * Make inner packet matching with an higher priority from the non Inner
937  * matching.
938  *
939  * @param dev
940  *   Pointer to Ethernet device.
941  * @param[in, out] parser
942  *   Internal parser structure.
943  * @param attr
944  *   User flow attribute.
945  */
946 static void
947 mlx5_flow_update_priority(struct rte_eth_dev *dev,
948                           struct mlx5_flow_parse *parser,
949                           const struct rte_flow_attr *attr)
950 {
951         struct priv *priv = dev->data->dev_private;
952         unsigned int i;
953         uint16_t priority;
954
955         /*                      8 priorities    >= 16 priorities
956          * Control flow:        4-7             8-15
957          * User normal flow:    1-3             4-7
958          * User tunnel flow:    0-2             0-3
959          */
960         priority = attr->priority * MLX5_VERBS_FLOW_PRIO_8;
961         if (priv->config.max_verbs_prio == MLX5_VERBS_FLOW_PRIO_8)
962                 priority /= 2;
963         /*
964          * Lower non-tunnel flow Verbs priority 1 if only support 8 Verbs
965          * priorities, lower 4 otherwise.
966          */
967         if (!parser->inner) {
968                 if (priv->config.max_verbs_prio == MLX5_VERBS_FLOW_PRIO_8)
969                         priority += 1;
970                 else
971                         priority += MLX5_VERBS_FLOW_PRIO_8 / 2;
972         }
973         if (parser->drop) {
974                 parser->queue[HASH_RXQ_ETH].ibv_attr->priority = priority +
975                                 hash_rxq_init[HASH_RXQ_ETH].flow_priority;
976                 return;
977         }
978         for (i = 0; i != hash_rxq_init_n; ++i) {
979                 if (!parser->queue[i].ibv_attr)
980                         continue;
981                 parser->queue[i].ibv_attr->priority = priority +
982                                 hash_rxq_init[i].flow_priority;
983         }
984 }
985
986 /**
987  * Finalise verbs flow attributes.
988  *
989  * @param[in, out] parser
990  *   Internal parser structure.
991  */
992 static void
993 mlx5_flow_convert_finalise(struct mlx5_flow_parse *parser)
994 {
995         const unsigned int ipv4 =
996                 hash_rxq_init[parser->layer].ip_version == MLX5_IPV4;
997         const enum hash_rxq_type hmin = ipv4 ? HASH_RXQ_TCPV4 : HASH_RXQ_TCPV6;
998         const enum hash_rxq_type hmax = ipv4 ? HASH_RXQ_IPV4 : HASH_RXQ_IPV6;
999         const enum hash_rxq_type ohmin = ipv4 ? HASH_RXQ_TCPV6 : HASH_RXQ_TCPV4;
1000         const enum hash_rxq_type ohmax = ipv4 ? HASH_RXQ_IPV6 : HASH_RXQ_IPV4;
1001         const enum hash_rxq_type ip = ipv4 ? HASH_RXQ_IPV4 : HASH_RXQ_IPV6;
1002         unsigned int i;
1003
1004         /* Remove any other flow not matching the pattern. */
1005         if (parser->rss_conf.queue_num == 1 && !parser->rss_conf.types) {
1006                 for (i = 0; i != hash_rxq_init_n; ++i) {
1007                         if (i == HASH_RXQ_ETH)
1008                                 continue;
1009                         rte_free(parser->queue[i].ibv_attr);
1010                         parser->queue[i].ibv_attr = NULL;
1011                 }
1012                 return;
1013         }
1014         if (parser->layer == HASH_RXQ_ETH) {
1015                 goto fill;
1016         } else {
1017                 /*
1018                  * This layer becomes useless as the pattern define under
1019                  * layers.
1020                  */
1021                 rte_free(parser->queue[HASH_RXQ_ETH].ibv_attr);
1022                 parser->queue[HASH_RXQ_ETH].ibv_attr = NULL;
1023         }
1024         /* Remove opposite kind of layer e.g. IPv6 if the pattern is IPv4. */
1025         for (i = ohmin; i != (ohmax + 1); ++i) {
1026                 if (!parser->queue[i].ibv_attr)
1027                         continue;
1028                 rte_free(parser->queue[i].ibv_attr);
1029                 parser->queue[i].ibv_attr = NULL;
1030         }
1031         /* Remove impossible flow according to the RSS configuration. */
1032         if (hash_rxq_init[parser->layer].dpdk_rss_hf &
1033             parser->rss_conf.types) {
1034                 /* Remove any other flow. */
1035                 for (i = hmin; i != (hmax + 1); ++i) {
1036                         if ((i == parser->layer) ||
1037                              (!parser->queue[i].ibv_attr))
1038                                 continue;
1039                         rte_free(parser->queue[i].ibv_attr);
1040                         parser->queue[i].ibv_attr = NULL;
1041                 }
1042         } else  if (!parser->queue[ip].ibv_attr) {
1043                 /* no RSS possible with the current configuration. */
1044                 parser->rss_conf.queue_num = 1;
1045                 return;
1046         }
1047 fill:
1048         /*
1049          * Fill missing layers in verbs specifications, or compute the correct
1050          * offset to allocate the memory space for the attributes and
1051          * specifications.
1052          */
1053         for (i = 0; i != hash_rxq_init_n - 1; ++i) {
1054                 union {
1055                         struct ibv_flow_spec_ipv4_ext ipv4;
1056                         struct ibv_flow_spec_ipv6 ipv6;
1057                         struct ibv_flow_spec_tcp_udp udp_tcp;
1058                 } specs;
1059                 void *dst;
1060                 uint16_t size;
1061
1062                 if (i == parser->layer)
1063                         continue;
1064                 if (parser->layer == HASH_RXQ_ETH) {
1065                         if (hash_rxq_init[i].ip_version == MLX5_IPV4) {
1066                                 size = sizeof(struct ibv_flow_spec_ipv4_ext);
1067                                 specs.ipv4 = (struct ibv_flow_spec_ipv4_ext){
1068                                         .type = IBV_FLOW_SPEC_IPV4_EXT,
1069                                         .size = size,
1070                                 };
1071                         } else {
1072                                 size = sizeof(struct ibv_flow_spec_ipv6);
1073                                 specs.ipv6 = (struct ibv_flow_spec_ipv6){
1074                                         .type = IBV_FLOW_SPEC_IPV6,
1075                                         .size = size,
1076                                 };
1077                         }
1078                         if (parser->queue[i].ibv_attr) {
1079                                 dst = (void *)((uintptr_t)
1080                                                parser->queue[i].ibv_attr +
1081                                                parser->queue[i].offset);
1082                                 memcpy(dst, &specs, size);
1083                                 ++parser->queue[i].ibv_attr->num_of_specs;
1084                         }
1085                         parser->queue[i].offset += size;
1086                 }
1087                 if ((i == HASH_RXQ_UDPV4) || (i == HASH_RXQ_TCPV4) ||
1088                     (i == HASH_RXQ_UDPV6) || (i == HASH_RXQ_TCPV6)) {
1089                         size = sizeof(struct ibv_flow_spec_tcp_udp);
1090                         specs.udp_tcp = (struct ibv_flow_spec_tcp_udp) {
1091                                 .type = ((i == HASH_RXQ_UDPV4 ||
1092                                           i == HASH_RXQ_UDPV6) ?
1093                                          IBV_FLOW_SPEC_UDP :
1094                                          IBV_FLOW_SPEC_TCP),
1095                                 .size = size,
1096                         };
1097                         if (parser->queue[i].ibv_attr) {
1098                                 dst = (void *)((uintptr_t)
1099                                                parser->queue[i].ibv_attr +
1100                                                parser->queue[i].offset);
1101                                 memcpy(dst, &specs, size);
1102                                 ++parser->queue[i].ibv_attr->num_of_specs;
1103                         }
1104                         parser->queue[i].offset += size;
1105                 }
1106         }
1107 }
1108
1109 /**
1110  * Validate and convert a flow supported by the NIC.
1111  *
1112  * @param dev
1113  *   Pointer to Ethernet device.
1114  * @param[in] attr
1115  *   Flow rule attributes.
1116  * @param[in] pattern
1117  *   Pattern specification (list terminated by the END pattern item).
1118  * @param[in] actions
1119  *   Associated actions (list terminated by the END action).
1120  * @param[out] error
1121  *   Perform verbose error reporting if not NULL.
1122  * @param[in, out] parser
1123  *   Internal parser structure.
1124  *
1125  * @return
1126  *   0 on success, a negative errno value otherwise and rte_errno is set.
1127  */
1128 static int
1129 mlx5_flow_convert(struct rte_eth_dev *dev,
1130                   const struct rte_flow_attr *attr,
1131                   const struct rte_flow_item items[],
1132                   const struct rte_flow_action actions[],
1133                   struct rte_flow_error *error,
1134                   struct mlx5_flow_parse *parser)
1135 {
1136         const struct mlx5_flow_items *cur_item = mlx5_flow_items;
1137         unsigned int i;
1138         int ret;
1139
1140         /* First step. Validate the attributes, items and actions. */
1141         *parser = (struct mlx5_flow_parse){
1142                 .create = parser->create,
1143                 .layer = HASH_RXQ_ETH,
1144                 .mark_id = MLX5_FLOW_MARK_DEFAULT,
1145         };
1146         ret = mlx5_flow_convert_attributes(attr, error);
1147         if (ret)
1148                 return ret;
1149         ret = mlx5_flow_convert_actions(dev, actions, error, parser);
1150         if (ret)
1151                 return ret;
1152         ret = mlx5_flow_convert_items_validate(items, error, parser);
1153         if (ret)
1154                 return ret;
1155         mlx5_flow_convert_finalise(parser);
1156         /*
1157          * Second step.
1158          * Allocate the memory space to store verbs specifications.
1159          */
1160         if (parser->drop) {
1161                 unsigned int offset = parser->queue[HASH_RXQ_ETH].offset;
1162
1163                 parser->queue[HASH_RXQ_ETH].ibv_attr =
1164                         mlx5_flow_convert_allocate(offset, error);
1165                 if (!parser->queue[HASH_RXQ_ETH].ibv_attr)
1166                         goto exit_enomem;
1167                 parser->queue[HASH_RXQ_ETH].offset =
1168                         sizeof(struct ibv_flow_attr);
1169         } else {
1170                 for (i = 0; i != hash_rxq_init_n; ++i) {
1171                         unsigned int offset;
1172
1173                         if (!(parser->rss_conf.types &
1174                               hash_rxq_init[i].dpdk_rss_hf) &&
1175                             (i != HASH_RXQ_ETH))
1176                                 continue;
1177                         offset = parser->queue[i].offset;
1178                         parser->queue[i].ibv_attr =
1179                                 mlx5_flow_convert_allocate(offset, error);
1180                         if (!parser->queue[i].ibv_attr)
1181                                 goto exit_enomem;
1182                         parser->queue[i].offset = sizeof(struct ibv_flow_attr);
1183                 }
1184         }
1185         /* Third step. Conversion parse, fill the specifications. */
1186         parser->inner = 0;
1187         for (; items->type != RTE_FLOW_ITEM_TYPE_END; ++items) {
1188                 struct mlx5_flow_data data = {
1189                         .parser = parser,
1190                         .error = error,
1191                 };
1192
1193                 if (items->type == RTE_FLOW_ITEM_TYPE_VOID)
1194                         continue;
1195                 cur_item = &mlx5_flow_items[items->type];
1196                 ret = cur_item->convert(items,
1197                                         (cur_item->default_mask ?
1198                                          cur_item->default_mask :
1199                                          cur_item->mask),
1200                                          &data);
1201                 if (ret)
1202                         goto exit_free;
1203         }
1204         if (parser->mark)
1205                 mlx5_flow_create_flag_mark(parser, parser->mark_id);
1206         if (parser->count && parser->create) {
1207                 mlx5_flow_create_count(dev, parser);
1208                 if (!parser->cs)
1209                         goto exit_count_error;
1210         }
1211         /*
1212          * Last step. Complete missing specification to reach the RSS
1213          * configuration.
1214          */
1215         if (!parser->drop)
1216                 mlx5_flow_convert_finalise(parser);
1217         mlx5_flow_update_priority(dev, parser, attr);
1218 exit_free:
1219         /* Only verification is expected, all resources should be released. */
1220         if (!parser->create) {
1221                 for (i = 0; i != hash_rxq_init_n; ++i) {
1222                         if (parser->queue[i].ibv_attr) {
1223                                 rte_free(parser->queue[i].ibv_attr);
1224                                 parser->queue[i].ibv_attr = NULL;
1225                         }
1226                 }
1227         }
1228         return ret;
1229 exit_enomem:
1230         for (i = 0; i != hash_rxq_init_n; ++i) {
1231                 if (parser->queue[i].ibv_attr) {
1232                         rte_free(parser->queue[i].ibv_attr);
1233                         parser->queue[i].ibv_attr = NULL;
1234                 }
1235         }
1236         rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1237                            NULL, "cannot allocate verbs spec attributes");
1238         return -rte_errno;
1239 exit_count_error:
1240         rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1241                            NULL, "cannot create counter");
1242         return -rte_errno;
1243 }
1244
1245 /**
1246  * Copy the specification created into the flow.
1247  *
1248  * @param parser
1249  *   Internal parser structure.
1250  * @param src
1251  *   Create specification.
1252  * @param size
1253  *   Size in bytes of the specification to copy.
1254  */
1255 static void
1256 mlx5_flow_create_copy(struct mlx5_flow_parse *parser, void *src,
1257                       unsigned int size)
1258 {
1259         unsigned int i;
1260         void *dst;
1261
1262         for (i = 0; i != hash_rxq_init_n; ++i) {
1263                 if (!parser->queue[i].ibv_attr)
1264                         continue;
1265                 /* Specification must be the same l3 type or none. */
1266                 if (parser->layer == HASH_RXQ_ETH ||
1267                     (hash_rxq_init[parser->layer].ip_version ==
1268                      hash_rxq_init[i].ip_version) ||
1269                     (hash_rxq_init[i].ip_version == 0)) {
1270                         dst = (void *)((uintptr_t)parser->queue[i].ibv_attr +
1271                                         parser->queue[i].offset);
1272                         memcpy(dst, src, size);
1273                         ++parser->queue[i].ibv_attr->num_of_specs;
1274                         parser->queue[i].offset += size;
1275                 }
1276         }
1277 }
1278
1279 /**
1280  * Convert Ethernet item to Verbs specification.
1281  *
1282  * @param item[in]
1283  *   Item specification.
1284  * @param default_mask[in]
1285  *   Default bit-masks to use when item->mask is not provided.
1286  * @param data[in, out]
1287  *   User structure.
1288  *
1289  * @return
1290  *   0 on success, a negative errno value otherwise and rte_errno is set.
1291  */
1292 static int
1293 mlx5_flow_create_eth(const struct rte_flow_item *item,
1294                      const void *default_mask,
1295                      struct mlx5_flow_data *data)
1296 {
1297         const struct rte_flow_item_eth *spec = item->spec;
1298         const struct rte_flow_item_eth *mask = item->mask;
1299         struct mlx5_flow_parse *parser = data->parser;
1300         const unsigned int eth_size = sizeof(struct ibv_flow_spec_eth);
1301         struct ibv_flow_spec_eth eth = {
1302                 .type = parser->inner | IBV_FLOW_SPEC_ETH,
1303                 .size = eth_size,
1304         };
1305
1306         /* Don't update layer for the inner pattern. */
1307         if (!parser->inner)
1308                 parser->layer = HASH_RXQ_ETH;
1309         if (spec) {
1310                 unsigned int i;
1311
1312                 if (!mask)
1313                         mask = default_mask;
1314                 memcpy(&eth.val.dst_mac, spec->dst.addr_bytes, ETHER_ADDR_LEN);
1315                 memcpy(&eth.val.src_mac, spec->src.addr_bytes, ETHER_ADDR_LEN);
1316                 eth.val.ether_type = spec->type;
1317                 memcpy(&eth.mask.dst_mac, mask->dst.addr_bytes, ETHER_ADDR_LEN);
1318                 memcpy(&eth.mask.src_mac, mask->src.addr_bytes, ETHER_ADDR_LEN);
1319                 eth.mask.ether_type = mask->type;
1320                 /* Remove unwanted bits from values. */
1321                 for (i = 0; i < ETHER_ADDR_LEN; ++i) {
1322                         eth.val.dst_mac[i] &= eth.mask.dst_mac[i];
1323                         eth.val.src_mac[i] &= eth.mask.src_mac[i];
1324                 }
1325                 eth.val.ether_type &= eth.mask.ether_type;
1326         }
1327         mlx5_flow_create_copy(parser, &eth, eth_size);
1328         return 0;
1329 }
1330
1331 /**
1332  * Convert VLAN item to Verbs specification.
1333  *
1334  * @param item[in]
1335  *   Item specification.
1336  * @param default_mask[in]
1337  *   Default bit-masks to use when item->mask is not provided.
1338  * @param data[in, out]
1339  *   User structure.
1340  *
1341  * @return
1342  *   0 on success, a negative errno value otherwise and rte_errno is set.
1343  */
1344 static int
1345 mlx5_flow_create_vlan(const struct rte_flow_item *item,
1346                       const void *default_mask,
1347                       struct mlx5_flow_data *data)
1348 {
1349         const struct rte_flow_item_vlan *spec = item->spec;
1350         const struct rte_flow_item_vlan *mask = item->mask;
1351         struct mlx5_flow_parse *parser = data->parser;
1352         struct ibv_flow_spec_eth *eth;
1353         const unsigned int eth_size = sizeof(struct ibv_flow_spec_eth);
1354         const char *msg = "VLAN cannot be empty";
1355
1356         if (spec) {
1357                 unsigned int i;
1358                 if (!mask)
1359                         mask = default_mask;
1360
1361                 for (i = 0; i != hash_rxq_init_n; ++i) {
1362                         if (!parser->queue[i].ibv_attr)
1363                                 continue;
1364
1365                         eth = (void *)((uintptr_t)parser->queue[i].ibv_attr +
1366                                        parser->queue[i].offset - eth_size);
1367                         eth->val.vlan_tag = spec->tci;
1368                         eth->mask.vlan_tag = mask->tci;
1369                         eth->val.vlan_tag &= eth->mask.vlan_tag;
1370                         /*
1371                          * From verbs perspective an empty VLAN is equivalent
1372                          * to a packet without VLAN layer.
1373                          */
1374                         if (!eth->mask.vlan_tag)
1375                                 goto error;
1376                         /* Outer TPID cannot be matched. */
1377                         if (eth->mask.ether_type) {
1378                                 msg = "VLAN TPID matching is not supported";
1379                                 goto error;
1380                         }
1381                         eth->val.ether_type = spec->inner_type;
1382                         eth->mask.ether_type = mask->inner_type;
1383                         eth->val.ether_type &= eth->mask.ether_type;
1384                 }
1385                 return 0;
1386         }
1387 error:
1388         return rte_flow_error_set(data->error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
1389                                   item, msg);
1390 }
1391
1392 /**
1393  * Convert IPv4 item to Verbs specification.
1394  *
1395  * @param item[in]
1396  *   Item specification.
1397  * @param default_mask[in]
1398  *   Default bit-masks to use when item->mask is not provided.
1399  * @param data[in, out]
1400  *   User structure.
1401  *
1402  * @return
1403  *   0 on success, a negative errno value otherwise and rte_errno is set.
1404  */
1405 static int
1406 mlx5_flow_create_ipv4(const struct rte_flow_item *item,
1407                       const void *default_mask,
1408                       struct mlx5_flow_data *data)
1409 {
1410         const struct rte_flow_item_ipv4 *spec = item->spec;
1411         const struct rte_flow_item_ipv4 *mask = item->mask;
1412         struct mlx5_flow_parse *parser = data->parser;
1413         unsigned int ipv4_size = sizeof(struct ibv_flow_spec_ipv4_ext);
1414         struct ibv_flow_spec_ipv4_ext ipv4 = {
1415                 .type = parser->inner | IBV_FLOW_SPEC_IPV4_EXT,
1416                 .size = ipv4_size,
1417         };
1418
1419         /* Don't update layer for the inner pattern. */
1420         if (!parser->inner)
1421                 parser->layer = HASH_RXQ_IPV4;
1422         if (spec) {
1423                 if (!mask)
1424                         mask = default_mask;
1425                 ipv4.val = (struct ibv_flow_ipv4_ext_filter){
1426                         .src_ip = spec->hdr.src_addr,
1427                         .dst_ip = spec->hdr.dst_addr,
1428                         .proto = spec->hdr.next_proto_id,
1429                         .tos = spec->hdr.type_of_service,
1430                 };
1431                 ipv4.mask = (struct ibv_flow_ipv4_ext_filter){
1432                         .src_ip = mask->hdr.src_addr,
1433                         .dst_ip = mask->hdr.dst_addr,
1434                         .proto = mask->hdr.next_proto_id,
1435                         .tos = mask->hdr.type_of_service,
1436                 };
1437                 /* Remove unwanted bits from values. */
1438                 ipv4.val.src_ip &= ipv4.mask.src_ip;
1439                 ipv4.val.dst_ip &= ipv4.mask.dst_ip;
1440                 ipv4.val.proto &= ipv4.mask.proto;
1441                 ipv4.val.tos &= ipv4.mask.tos;
1442         }
1443         mlx5_flow_create_copy(parser, &ipv4, ipv4_size);
1444         return 0;
1445 }
1446
1447 /**
1448  * Convert IPv6 item to Verbs specification.
1449  *
1450  * @param item[in]
1451  *   Item specification.
1452  * @param default_mask[in]
1453  *   Default bit-masks to use when item->mask is not provided.
1454  * @param data[in, out]
1455  *   User structure.
1456  *
1457  * @return
1458  *   0 on success, a negative errno value otherwise and rte_errno is set.
1459  */
1460 static int
1461 mlx5_flow_create_ipv6(const struct rte_flow_item *item,
1462                       const void *default_mask,
1463                       struct mlx5_flow_data *data)
1464 {
1465         const struct rte_flow_item_ipv6 *spec = item->spec;
1466         const struct rte_flow_item_ipv6 *mask = item->mask;
1467         struct mlx5_flow_parse *parser = data->parser;
1468         unsigned int ipv6_size = sizeof(struct ibv_flow_spec_ipv6);
1469         struct ibv_flow_spec_ipv6 ipv6 = {
1470                 .type = parser->inner | IBV_FLOW_SPEC_IPV6,
1471                 .size = ipv6_size,
1472         };
1473
1474         /* Don't update layer for the inner pattern. */
1475         if (!parser->inner)
1476                 parser->layer = HASH_RXQ_IPV6;
1477         if (spec) {
1478                 unsigned int i;
1479                 uint32_t vtc_flow_val;
1480                 uint32_t vtc_flow_mask;
1481
1482                 if (!mask)
1483                         mask = default_mask;
1484                 memcpy(&ipv6.val.src_ip, spec->hdr.src_addr,
1485                        RTE_DIM(ipv6.val.src_ip));
1486                 memcpy(&ipv6.val.dst_ip, spec->hdr.dst_addr,
1487                        RTE_DIM(ipv6.val.dst_ip));
1488                 memcpy(&ipv6.mask.src_ip, mask->hdr.src_addr,
1489                        RTE_DIM(ipv6.mask.src_ip));
1490                 memcpy(&ipv6.mask.dst_ip, mask->hdr.dst_addr,
1491                        RTE_DIM(ipv6.mask.dst_ip));
1492                 vtc_flow_val = rte_be_to_cpu_32(spec->hdr.vtc_flow);
1493                 vtc_flow_mask = rte_be_to_cpu_32(mask->hdr.vtc_flow);
1494                 ipv6.val.flow_label =
1495                         rte_cpu_to_be_32((vtc_flow_val & IPV6_HDR_FL_MASK) >>
1496                                          IPV6_HDR_FL_SHIFT);
1497                 ipv6.val.traffic_class = (vtc_flow_val & IPV6_HDR_TC_MASK) >>
1498                                          IPV6_HDR_TC_SHIFT;
1499                 ipv6.val.next_hdr = spec->hdr.proto;
1500                 ipv6.val.hop_limit = spec->hdr.hop_limits;
1501                 ipv6.mask.flow_label =
1502                         rte_cpu_to_be_32((vtc_flow_mask & IPV6_HDR_FL_MASK) >>
1503                                          IPV6_HDR_FL_SHIFT);
1504                 ipv6.mask.traffic_class = (vtc_flow_mask & IPV6_HDR_TC_MASK) >>
1505                                           IPV6_HDR_TC_SHIFT;
1506                 ipv6.mask.next_hdr = mask->hdr.proto;
1507                 ipv6.mask.hop_limit = mask->hdr.hop_limits;
1508                 /* Remove unwanted bits from values. */
1509                 for (i = 0; i < RTE_DIM(ipv6.val.src_ip); ++i) {
1510                         ipv6.val.src_ip[i] &= ipv6.mask.src_ip[i];
1511                         ipv6.val.dst_ip[i] &= ipv6.mask.dst_ip[i];
1512                 }
1513                 ipv6.val.flow_label &= ipv6.mask.flow_label;
1514                 ipv6.val.traffic_class &= ipv6.mask.traffic_class;
1515                 ipv6.val.next_hdr &= ipv6.mask.next_hdr;
1516                 ipv6.val.hop_limit &= ipv6.mask.hop_limit;
1517         }
1518         mlx5_flow_create_copy(parser, &ipv6, ipv6_size);
1519         return 0;
1520 }
1521
1522 /**
1523  * Convert UDP item to Verbs specification.
1524  *
1525  * @param item[in]
1526  *   Item specification.
1527  * @param default_mask[in]
1528  *   Default bit-masks to use when item->mask is not provided.
1529  * @param data[in, out]
1530  *   User structure.
1531  *
1532  * @return
1533  *   0 on success, a negative errno value otherwise and rte_errno is set.
1534  */
1535 static int
1536 mlx5_flow_create_udp(const struct rte_flow_item *item,
1537                      const void *default_mask,
1538                      struct mlx5_flow_data *data)
1539 {
1540         const struct rte_flow_item_udp *spec = item->spec;
1541         const struct rte_flow_item_udp *mask = item->mask;
1542         struct mlx5_flow_parse *parser = data->parser;
1543         unsigned int udp_size = sizeof(struct ibv_flow_spec_tcp_udp);
1544         struct ibv_flow_spec_tcp_udp udp = {
1545                 .type = parser->inner | IBV_FLOW_SPEC_UDP,
1546                 .size = udp_size,
1547         };
1548
1549         /* Don't update layer for the inner pattern. */
1550         if (!parser->inner) {
1551                 if (parser->layer == HASH_RXQ_IPV4)
1552                         parser->layer = HASH_RXQ_UDPV4;
1553                 else
1554                         parser->layer = HASH_RXQ_UDPV6;
1555         }
1556         if (spec) {
1557                 if (!mask)
1558                         mask = default_mask;
1559                 udp.val.dst_port = spec->hdr.dst_port;
1560                 udp.val.src_port = spec->hdr.src_port;
1561                 udp.mask.dst_port = mask->hdr.dst_port;
1562                 udp.mask.src_port = mask->hdr.src_port;
1563                 /* Remove unwanted bits from values. */
1564                 udp.val.src_port &= udp.mask.src_port;
1565                 udp.val.dst_port &= udp.mask.dst_port;
1566         }
1567         mlx5_flow_create_copy(parser, &udp, udp_size);
1568         return 0;
1569 }
1570
1571 /**
1572  * Convert TCP item to Verbs specification.
1573  *
1574  * @param item[in]
1575  *   Item specification.
1576  * @param default_mask[in]
1577  *   Default bit-masks to use when item->mask is not provided.
1578  * @param data[in, out]
1579  *   User structure.
1580  *
1581  * @return
1582  *   0 on success, a negative errno value otherwise and rte_errno is set.
1583  */
1584 static int
1585 mlx5_flow_create_tcp(const struct rte_flow_item *item,
1586                      const void *default_mask,
1587                      struct mlx5_flow_data *data)
1588 {
1589         const struct rte_flow_item_tcp *spec = item->spec;
1590         const struct rte_flow_item_tcp *mask = item->mask;
1591         struct mlx5_flow_parse *parser = data->parser;
1592         unsigned int tcp_size = sizeof(struct ibv_flow_spec_tcp_udp);
1593         struct ibv_flow_spec_tcp_udp tcp = {
1594                 .type = parser->inner | IBV_FLOW_SPEC_TCP,
1595                 .size = tcp_size,
1596         };
1597
1598         /* Don't update layer for the inner pattern. */
1599         if (!parser->inner) {
1600                 if (parser->layer == HASH_RXQ_IPV4)
1601                         parser->layer = HASH_RXQ_TCPV4;
1602                 else
1603                         parser->layer = HASH_RXQ_TCPV6;
1604         }
1605         if (spec) {
1606                 if (!mask)
1607                         mask = default_mask;
1608                 tcp.val.dst_port = spec->hdr.dst_port;
1609                 tcp.val.src_port = spec->hdr.src_port;
1610                 tcp.mask.dst_port = mask->hdr.dst_port;
1611                 tcp.mask.src_port = mask->hdr.src_port;
1612                 /* Remove unwanted bits from values. */
1613                 tcp.val.src_port &= tcp.mask.src_port;
1614                 tcp.val.dst_port &= tcp.mask.dst_port;
1615         }
1616         mlx5_flow_create_copy(parser, &tcp, tcp_size);
1617         return 0;
1618 }
1619
1620 /**
1621  * Convert VXLAN item to Verbs specification.
1622  *
1623  * @param item[in]
1624  *   Item specification.
1625  * @param default_mask[in]
1626  *   Default bit-masks to use when item->mask is not provided.
1627  * @param data[in, out]
1628  *   User structure.
1629  *
1630  * @return
1631  *   0 on success, a negative errno value otherwise and rte_errno is set.
1632  */
1633 static int
1634 mlx5_flow_create_vxlan(const struct rte_flow_item *item,
1635                        const void *default_mask,
1636                        struct mlx5_flow_data *data)
1637 {
1638         const struct rte_flow_item_vxlan *spec = item->spec;
1639         const struct rte_flow_item_vxlan *mask = item->mask;
1640         struct mlx5_flow_parse *parser = data->parser;
1641         unsigned int size = sizeof(struct ibv_flow_spec_tunnel);
1642         struct ibv_flow_spec_tunnel vxlan = {
1643                 .type = parser->inner | IBV_FLOW_SPEC_VXLAN_TUNNEL,
1644                 .size = size,
1645         };
1646         union vni {
1647                 uint32_t vlan_id;
1648                 uint8_t vni[4];
1649         } id;
1650
1651         id.vni[0] = 0;
1652         parser->inner = IBV_FLOW_SPEC_INNER;
1653         if (spec) {
1654                 if (!mask)
1655                         mask = default_mask;
1656                 memcpy(&id.vni[1], spec->vni, 3);
1657                 vxlan.val.tunnel_id = id.vlan_id;
1658                 memcpy(&id.vni[1], mask->vni, 3);
1659                 vxlan.mask.tunnel_id = id.vlan_id;
1660                 /* Remove unwanted bits from values. */
1661                 vxlan.val.tunnel_id &= vxlan.mask.tunnel_id;
1662         }
1663         /*
1664          * Tunnel id 0 is equivalent as not adding a VXLAN layer, if only this
1665          * layer is defined in the Verbs specification it is interpreted as
1666          * wildcard and all packets will match this rule, if it follows a full
1667          * stack layer (ex: eth / ipv4 / udp), all packets matching the layers
1668          * before will also match this rule.
1669          * To avoid such situation, VNI 0 is currently refused.
1670          */
1671         if (!vxlan.val.tunnel_id)
1672                 return rte_flow_error_set(data->error, EINVAL,
1673                                           RTE_FLOW_ERROR_TYPE_ITEM,
1674                                           item,
1675                                           "VxLAN vni cannot be 0");
1676         mlx5_flow_create_copy(parser, &vxlan, size);
1677         return 0;
1678 }
1679
1680 /**
1681  * Convert GRE item to Verbs specification.
1682  *
1683  * @param item[in]
1684  *   Item specification.
1685  * @param default_mask[in]
1686  *   Default bit-masks to use when item->mask is not provided.
1687  * @param data[in, out]
1688  *   User structure.
1689  *
1690  * @return
1691  *   0 on success, a negative errno value otherwise and rte_errno is set.
1692  */
1693 static int
1694 mlx5_flow_create_gre(const struct rte_flow_item *item __rte_unused,
1695                      const void *default_mask __rte_unused,
1696                      struct mlx5_flow_data *data)
1697 {
1698         struct mlx5_flow_parse *parser = data->parser;
1699         unsigned int size = sizeof(struct ibv_flow_spec_tunnel);
1700         struct ibv_flow_spec_tunnel tunnel = {
1701                 .type = parser->inner | IBV_FLOW_SPEC_VXLAN_TUNNEL,
1702                 .size = size,
1703         };
1704         struct ibv_flow_spec_ipv4_ext *ipv4;
1705         struct ibv_flow_spec_ipv6 *ipv6;
1706         unsigned int i;
1707
1708         parser->inner = IBV_FLOW_SPEC_INNER;
1709         /* Update encapsulation IP layer protocol. */
1710         for (i = 0; i != hash_rxq_init_n; ++i) {
1711                 if (!parser->queue[i].ibv_attr)
1712                         continue;
1713                 if (parser->out_layer == HASH_RXQ_IPV4) {
1714                         ipv4 = (void *)((uintptr_t)parser->queue[i].ibv_attr +
1715                                 parser->queue[i].offset -
1716                                 sizeof(struct ibv_flow_spec_ipv4_ext));
1717                         if (ipv4->mask.proto && ipv4->val.proto != MLX5_GRE)
1718                                 break;
1719                         ipv4->val.proto = MLX5_GRE;
1720                         ipv4->mask.proto = 0xff;
1721                 } else if (parser->out_layer == HASH_RXQ_IPV6) {
1722                         ipv6 = (void *)((uintptr_t)parser->queue[i].ibv_attr +
1723                                 parser->queue[i].offset -
1724                                 sizeof(struct ibv_flow_spec_ipv6));
1725                         if (ipv6->mask.next_hdr &&
1726                             ipv6->val.next_hdr != MLX5_GRE)
1727                                 break;
1728                         ipv6->val.next_hdr = MLX5_GRE;
1729                         ipv6->mask.next_hdr = 0xff;
1730                 }
1731         }
1732         if (i != hash_rxq_init_n)
1733                 return rte_flow_error_set(data->error, EINVAL,
1734                                           RTE_FLOW_ERROR_TYPE_ITEM,
1735                                           item,
1736                                           "IP protocol of GRE must be 47");
1737         mlx5_flow_create_copy(parser, &tunnel, size);
1738         return 0;
1739 }
1740
1741 /**
1742  * Convert mark/flag action to Verbs specification.
1743  *
1744  * @param parser
1745  *   Internal parser structure.
1746  * @param mark_id
1747  *   Mark identifier.
1748  *
1749  * @return
1750  *   0 on success, a negative errno value otherwise and rte_errno is set.
1751  */
1752 static int
1753 mlx5_flow_create_flag_mark(struct mlx5_flow_parse *parser, uint32_t mark_id)
1754 {
1755         unsigned int size = sizeof(struct ibv_flow_spec_action_tag);
1756         struct ibv_flow_spec_action_tag tag = {
1757                 .type = IBV_FLOW_SPEC_ACTION_TAG,
1758                 .size = size,
1759                 .tag_id = mlx5_flow_mark_set(mark_id),
1760         };
1761
1762         assert(parser->mark);
1763         mlx5_flow_create_copy(parser, &tag, size);
1764         return 0;
1765 }
1766
1767 /**
1768  * Convert count action to Verbs specification.
1769  *
1770  * @param dev
1771  *   Pointer to Ethernet device.
1772  * @param parser
1773  *   Pointer to MLX5 flow parser structure.
1774  *
1775  * @return
1776  *   0 on success, a negative errno value otherwise and rte_errno is set.
1777  */
1778 static int
1779 mlx5_flow_create_count(struct rte_eth_dev *dev __rte_unused,
1780                        struct mlx5_flow_parse *parser __rte_unused)
1781 {
1782 #ifdef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
1783         struct priv *priv = dev->data->dev_private;
1784         unsigned int size = sizeof(struct ibv_flow_spec_counter_action);
1785         struct ibv_counter_set_init_attr init_attr = {0};
1786         struct ibv_flow_spec_counter_action counter = {
1787                 .type = IBV_FLOW_SPEC_ACTION_COUNT,
1788                 .size = size,
1789                 .counter_set_handle = 0,
1790         };
1791
1792         init_attr.counter_set_id = 0;
1793         parser->cs = mlx5_glue->create_counter_set(priv->ctx, &init_attr);
1794         if (!parser->cs) {
1795                 rte_errno = EINVAL;
1796                 return -rte_errno;
1797         }
1798         counter.counter_set_handle = parser->cs->handle;
1799         mlx5_flow_create_copy(parser, &counter, size);
1800 #endif
1801         return 0;
1802 }
1803
1804 /**
1805  * Complete flow rule creation with a drop queue.
1806  *
1807  * @param dev
1808  *   Pointer to Ethernet device.
1809  * @param parser
1810  *   Internal parser structure.
1811  * @param flow
1812  *   Pointer to the rte_flow.
1813  * @param[out] error
1814  *   Perform verbose error reporting if not NULL.
1815  *
1816  * @return
1817  *   0 on success, a negative errno value otherwise and rte_errno is set.
1818  */
1819 static int
1820 mlx5_flow_create_action_queue_drop(struct rte_eth_dev *dev,
1821                                    struct mlx5_flow_parse *parser,
1822                                    struct rte_flow *flow,
1823                                    struct rte_flow_error *error)
1824 {
1825         struct priv *priv = dev->data->dev_private;
1826         struct ibv_flow_spec_action_drop *drop;
1827         unsigned int size = sizeof(struct ibv_flow_spec_action_drop);
1828
1829         assert(priv->pd);
1830         assert(priv->ctx);
1831         flow->drop = 1;
1832         drop = (void *)((uintptr_t)parser->queue[HASH_RXQ_ETH].ibv_attr +
1833                         parser->queue[HASH_RXQ_ETH].offset);
1834         *drop = (struct ibv_flow_spec_action_drop){
1835                         .type = IBV_FLOW_SPEC_ACTION_DROP,
1836                         .size = size,
1837         };
1838         ++parser->queue[HASH_RXQ_ETH].ibv_attr->num_of_specs;
1839         parser->queue[HASH_RXQ_ETH].offset += size;
1840         flow->frxq[HASH_RXQ_ETH].ibv_attr =
1841                 parser->queue[HASH_RXQ_ETH].ibv_attr;
1842         if (parser->count)
1843                 flow->cs = parser->cs;
1844         if (!priv->dev->data->dev_started)
1845                 return 0;
1846         parser->queue[HASH_RXQ_ETH].ibv_attr = NULL;
1847         flow->frxq[HASH_RXQ_ETH].ibv_flow =
1848                 mlx5_glue->create_flow(priv->flow_drop_queue->qp,
1849                                        flow->frxq[HASH_RXQ_ETH].ibv_attr);
1850         if (!flow->frxq[HASH_RXQ_ETH].ibv_flow) {
1851                 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1852                                    NULL, "flow rule creation failure");
1853                 goto error;
1854         }
1855         return 0;
1856 error:
1857         assert(flow);
1858         if (flow->frxq[HASH_RXQ_ETH].ibv_flow) {
1859                 claim_zero(mlx5_glue->destroy_flow
1860                            (flow->frxq[HASH_RXQ_ETH].ibv_flow));
1861                 flow->frxq[HASH_RXQ_ETH].ibv_flow = NULL;
1862         }
1863         if (flow->frxq[HASH_RXQ_ETH].ibv_attr) {
1864                 rte_free(flow->frxq[HASH_RXQ_ETH].ibv_attr);
1865                 flow->frxq[HASH_RXQ_ETH].ibv_attr = NULL;
1866         }
1867         if (flow->cs) {
1868                 claim_zero(mlx5_glue->destroy_counter_set(flow->cs));
1869                 flow->cs = NULL;
1870                 parser->cs = NULL;
1871         }
1872         return -rte_errno;
1873 }
1874
1875 /**
1876  * Create hash Rx queues when RSS is enabled.
1877  *
1878  * @param dev
1879  *   Pointer to Ethernet device.
1880  * @param parser
1881  *   Internal parser structure.
1882  * @param flow
1883  *   Pointer to the rte_flow.
1884  * @param[out] error
1885  *   Perform verbose error reporting if not NULL.
1886  *
1887  * @return
1888  *   0 on success, a negative errno value otherwise and rte_errno is set.
1889  */
1890 static int
1891 mlx5_flow_create_action_queue_rss(struct rte_eth_dev *dev,
1892                                   struct mlx5_flow_parse *parser,
1893                                   struct rte_flow *flow,
1894                                   struct rte_flow_error *error)
1895 {
1896         struct priv *priv = dev->data->dev_private;
1897         unsigned int i;
1898
1899         for (i = 0; i != hash_rxq_init_n; ++i) {
1900                 uint64_t hash_fields;
1901
1902                 if (!parser->queue[i].ibv_attr)
1903                         continue;
1904                 flow->frxq[i].ibv_attr = parser->queue[i].ibv_attr;
1905                 parser->queue[i].ibv_attr = NULL;
1906                 hash_fields = hash_rxq_init[i].hash_fields;
1907                 if (!priv->dev->data->dev_started)
1908                         continue;
1909                 flow->frxq[i].hrxq =
1910                         mlx5_hrxq_get(dev,
1911                                       parser->rss_conf.key,
1912                                       parser->rss_conf.key_len,
1913                                       hash_fields,
1914                                       parser->rss_conf.queue,
1915                                       parser->rss_conf.queue_num);
1916                 if (flow->frxq[i].hrxq)
1917                         continue;
1918                 flow->frxq[i].hrxq =
1919                         mlx5_hrxq_new(dev,
1920                                       parser->rss_conf.key,
1921                                       parser->rss_conf.key_len,
1922                                       hash_fields,
1923                                       parser->rss_conf.queue,
1924                                       parser->rss_conf.queue_num);
1925                 if (!flow->frxq[i].hrxq) {
1926                         return rte_flow_error_set(error, ENOMEM,
1927                                                   RTE_FLOW_ERROR_TYPE_HANDLE,
1928                                                   NULL,
1929                                                   "cannot create hash rxq");
1930                 }
1931         }
1932         return 0;
1933 }
1934
1935 /**
1936  * Complete flow rule creation.
1937  *
1938  * @param dev
1939  *   Pointer to Ethernet device.
1940  * @param parser
1941  *   Internal parser structure.
1942  * @param flow
1943  *   Pointer to the rte_flow.
1944  * @param[out] error
1945  *   Perform verbose error reporting if not NULL.
1946  *
1947  * @return
1948  *   0 on success, a negative errno value otherwise and rte_errno is set.
1949  */
1950 static int
1951 mlx5_flow_create_action_queue(struct rte_eth_dev *dev,
1952                               struct mlx5_flow_parse *parser,
1953                               struct rte_flow *flow,
1954                               struct rte_flow_error *error)
1955 {
1956         struct priv *priv = dev->data->dev_private;
1957         int ret;
1958         unsigned int i;
1959         unsigned int flows_n = 0;
1960
1961         assert(priv->pd);
1962         assert(priv->ctx);
1963         assert(!parser->drop);
1964         ret = mlx5_flow_create_action_queue_rss(dev, parser, flow, error);
1965         if (ret)
1966                 goto error;
1967         if (parser->count)
1968                 flow->cs = parser->cs;
1969         if (!priv->dev->data->dev_started)
1970                 return 0;
1971         for (i = 0; i != hash_rxq_init_n; ++i) {
1972                 if (!flow->frxq[i].hrxq)
1973                         continue;
1974                 flow->frxq[i].ibv_flow =
1975                         mlx5_glue->create_flow(flow->frxq[i].hrxq->qp,
1976                                                flow->frxq[i].ibv_attr);
1977                 if (!flow->frxq[i].ibv_flow) {
1978                         rte_flow_error_set(error, ENOMEM,
1979                                            RTE_FLOW_ERROR_TYPE_HANDLE,
1980                                            NULL, "flow rule creation failure");
1981                         goto error;
1982                 }
1983                 ++flows_n;
1984                 DRV_LOG(DEBUG, "port %u %p type %d QP %p ibv_flow %p",
1985                         dev->data->port_id,
1986                         (void *)flow, i,
1987                         (void *)flow->frxq[i].hrxq,
1988                         (void *)flow->frxq[i].ibv_flow);
1989         }
1990         if (!flows_n) {
1991                 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_HANDLE,
1992                                    NULL, "internal error in flow creation");
1993                 goto error;
1994         }
1995         for (i = 0; i != parser->rss_conf.queue_num; ++i) {
1996                 struct mlx5_rxq_data *q =
1997                         (*priv->rxqs)[parser->rss_conf.queue[i]];
1998
1999                 q->mark |= parser->mark;
2000         }
2001         return 0;
2002 error:
2003         ret = rte_errno; /* Save rte_errno before cleanup. */
2004         assert(flow);
2005         for (i = 0; i != hash_rxq_init_n; ++i) {
2006                 if (flow->frxq[i].ibv_flow) {
2007                         struct ibv_flow *ibv_flow = flow->frxq[i].ibv_flow;
2008
2009                         claim_zero(mlx5_glue->destroy_flow(ibv_flow));
2010                 }
2011                 if (flow->frxq[i].hrxq)
2012                         mlx5_hrxq_release(dev, flow->frxq[i].hrxq);
2013                 if (flow->frxq[i].ibv_attr)
2014                         rte_free(flow->frxq[i].ibv_attr);
2015         }
2016         if (flow->cs) {
2017                 claim_zero(mlx5_glue->destroy_counter_set(flow->cs));
2018                 flow->cs = NULL;
2019                 parser->cs = NULL;
2020         }
2021         rte_errno = ret; /* Restore rte_errno. */
2022         return -rte_errno;
2023 }
2024
2025 /**
2026  * Convert a flow.
2027  *
2028  * @param dev
2029  *   Pointer to Ethernet device.
2030  * @param list
2031  *   Pointer to a TAILQ flow list.
2032  * @param[in] attr
2033  *   Flow rule attributes.
2034  * @param[in] pattern
2035  *   Pattern specification (list terminated by the END pattern item).
2036  * @param[in] actions
2037  *   Associated actions (list terminated by the END action).
2038  * @param[out] error
2039  *   Perform verbose error reporting if not NULL.
2040  *
2041  * @return
2042  *   A flow on success, NULL otherwise and rte_errno is set.
2043  */
2044 static struct rte_flow *
2045 mlx5_flow_list_create(struct rte_eth_dev *dev,
2046                       struct mlx5_flows *list,
2047                       const struct rte_flow_attr *attr,
2048                       const struct rte_flow_item items[],
2049                       const struct rte_flow_action actions[],
2050                       struct rte_flow_error *error)
2051 {
2052         struct mlx5_flow_parse parser = { .create = 1, };
2053         struct rte_flow *flow = NULL;
2054         unsigned int i;
2055         int ret;
2056
2057         ret = mlx5_flow_convert(dev, attr, items, actions, error, &parser);
2058         if (ret)
2059                 goto exit;
2060         flow = rte_calloc(__func__, 1,
2061                           sizeof(*flow) +
2062                           parser.rss_conf.queue_num * sizeof(uint16_t),
2063                           0);
2064         if (!flow) {
2065                 rte_flow_error_set(error, ENOMEM,
2066                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2067                                    NULL,
2068                                    "cannot allocate flow memory");
2069                 return NULL;
2070         }
2071         /* Copy configuration. */
2072         flow->queues = (uint16_t (*)[])(flow + 1);
2073         flow->rss_conf = (struct rte_flow_action_rss){
2074                 .func = RTE_ETH_HASH_FUNCTION_DEFAULT,
2075                 .level = 0,
2076                 .types = parser.rss_conf.types,
2077                 .key_len = parser.rss_conf.key_len,
2078                 .queue_num = parser.rss_conf.queue_num,
2079                 .key = memcpy(flow->rss_key, parser.rss_conf.key,
2080                               sizeof(*parser.rss_conf.key) *
2081                               parser.rss_conf.key_len),
2082                 .queue = memcpy(flow->queues, parser.rss_conf.queue,
2083                                 sizeof(*parser.rss_conf.queue) *
2084                                 parser.rss_conf.queue_num),
2085         };
2086         flow->mark = parser.mark;
2087         /* finalise the flow. */
2088         if (parser.drop)
2089                 ret = mlx5_flow_create_action_queue_drop(dev, &parser, flow,
2090                                                          error);
2091         else
2092                 ret = mlx5_flow_create_action_queue(dev, &parser, flow, error);
2093         if (ret)
2094                 goto exit;
2095         TAILQ_INSERT_TAIL(list, flow, next);
2096         DRV_LOG(DEBUG, "port %u flow created %p", dev->data->port_id,
2097                 (void *)flow);
2098         return flow;
2099 exit:
2100         DRV_LOG(ERR, "port %u flow creation error: %s", dev->data->port_id,
2101                 error->message);
2102         for (i = 0; i != hash_rxq_init_n; ++i) {
2103                 if (parser.queue[i].ibv_attr)
2104                         rte_free(parser.queue[i].ibv_attr);
2105         }
2106         rte_free(flow);
2107         return NULL;
2108 }
2109
2110 /**
2111  * Validate a flow supported by the NIC.
2112  *
2113  * @see rte_flow_validate()
2114  * @see rte_flow_ops
2115  */
2116 int
2117 mlx5_flow_validate(struct rte_eth_dev *dev,
2118                    const struct rte_flow_attr *attr,
2119                    const struct rte_flow_item items[],
2120                    const struct rte_flow_action actions[],
2121                    struct rte_flow_error *error)
2122 {
2123         struct mlx5_flow_parse parser = { .create = 0, };
2124
2125         return mlx5_flow_convert(dev, attr, items, actions, error, &parser);
2126 }
2127
2128 /**
2129  * Create a flow.
2130  *
2131  * @see rte_flow_create()
2132  * @see rte_flow_ops
2133  */
2134 struct rte_flow *
2135 mlx5_flow_create(struct rte_eth_dev *dev,
2136                  const struct rte_flow_attr *attr,
2137                  const struct rte_flow_item items[],
2138                  const struct rte_flow_action actions[],
2139                  struct rte_flow_error *error)
2140 {
2141         struct priv *priv = dev->data->dev_private;
2142
2143         return mlx5_flow_list_create(dev, &priv->flows, attr, items, actions,
2144                                      error);
2145 }
2146
2147 /**
2148  * Destroy a flow in a list.
2149  *
2150  * @param dev
2151  *   Pointer to Ethernet device.
2152  * @param list
2153  *   Pointer to a TAILQ flow list.
2154  * @param[in] flow
2155  *   Flow to destroy.
2156  */
2157 static void
2158 mlx5_flow_list_destroy(struct rte_eth_dev *dev, struct mlx5_flows *list,
2159                        struct rte_flow *flow)
2160 {
2161         struct priv *priv = dev->data->dev_private;
2162         unsigned int i;
2163
2164         if (flow->drop || !flow->mark)
2165                 goto free;
2166         for (i = 0; i != flow->rss_conf.queue_num; ++i) {
2167                 struct rte_flow *tmp;
2168                 int mark = 0;
2169
2170                 /*
2171                  * To remove the mark from the queue, the queue must not be
2172                  * present in any other marked flow (RSS or not).
2173                  */
2174                 TAILQ_FOREACH(tmp, list, next) {
2175                         unsigned int j;
2176                         uint16_t *tqs = NULL;
2177                         uint16_t tq_n = 0;
2178
2179                         if (!tmp->mark)
2180                                 continue;
2181                         for (j = 0; j != hash_rxq_init_n; ++j) {
2182                                 if (!tmp->frxq[j].hrxq)
2183                                         continue;
2184                                 tqs = tmp->frxq[j].hrxq->ind_table->queues;
2185                                 tq_n = tmp->frxq[j].hrxq->ind_table->queues_n;
2186                         }
2187                         if (!tq_n)
2188                                 continue;
2189                         for (j = 0; (j != tq_n) && !mark; j++)
2190                                 if (tqs[j] == (*flow->queues)[i])
2191                                         mark = 1;
2192                 }
2193                 (*priv->rxqs)[(*flow->queues)[i]]->mark = mark;
2194         }
2195 free:
2196         if (flow->drop) {
2197                 if (flow->frxq[HASH_RXQ_ETH].ibv_flow)
2198                         claim_zero(mlx5_glue->destroy_flow
2199                                    (flow->frxq[HASH_RXQ_ETH].ibv_flow));
2200                 rte_free(flow->frxq[HASH_RXQ_ETH].ibv_attr);
2201         } else {
2202                 for (i = 0; i != hash_rxq_init_n; ++i) {
2203                         struct mlx5_flow *frxq = &flow->frxq[i];
2204
2205                         if (frxq->ibv_flow)
2206                                 claim_zero(mlx5_glue->destroy_flow
2207                                            (frxq->ibv_flow));
2208                         if (frxq->hrxq)
2209                                 mlx5_hrxq_release(dev, frxq->hrxq);
2210                         if (frxq->ibv_attr)
2211                                 rte_free(frxq->ibv_attr);
2212                 }
2213         }
2214         if (flow->cs) {
2215                 claim_zero(mlx5_glue->destroy_counter_set(flow->cs));
2216                 flow->cs = NULL;
2217         }
2218         TAILQ_REMOVE(list, flow, next);
2219         DRV_LOG(DEBUG, "port %u flow destroyed %p", dev->data->port_id,
2220                 (void *)flow);
2221         rte_free(flow);
2222 }
2223
2224 /**
2225  * Destroy all flows.
2226  *
2227  * @param dev
2228  *   Pointer to Ethernet device.
2229  * @param list
2230  *   Pointer to a TAILQ flow list.
2231  */
2232 void
2233 mlx5_flow_list_flush(struct rte_eth_dev *dev, struct mlx5_flows *list)
2234 {
2235         while (!TAILQ_EMPTY(list)) {
2236                 struct rte_flow *flow;
2237
2238                 flow = TAILQ_FIRST(list);
2239                 mlx5_flow_list_destroy(dev, list, flow);
2240         }
2241 }
2242
2243 /**
2244  * Create drop queue.
2245  *
2246  * @param dev
2247  *   Pointer to Ethernet device.
2248  *
2249  * @return
2250  *   0 on success, a negative errno value otherwise and rte_errno is set.
2251  */
2252 int
2253 mlx5_flow_create_drop_queue(struct rte_eth_dev *dev)
2254 {
2255         struct priv *priv = dev->data->dev_private;
2256         struct mlx5_hrxq_drop *fdq = NULL;
2257
2258         assert(priv->pd);
2259         assert(priv->ctx);
2260         fdq = rte_calloc(__func__, 1, sizeof(*fdq), 0);
2261         if (!fdq) {
2262                 DRV_LOG(WARNING,
2263                         "port %u cannot allocate memory for drop queue",
2264                         dev->data->port_id);
2265                 rte_errno = ENOMEM;
2266                 return -rte_errno;
2267         }
2268         fdq->cq = mlx5_glue->create_cq(priv->ctx, 1, NULL, NULL, 0);
2269         if (!fdq->cq) {
2270                 DRV_LOG(WARNING, "port %u cannot allocate CQ for drop queue",
2271                         dev->data->port_id);
2272                 rte_errno = errno;
2273                 goto error;
2274         }
2275         fdq->wq = mlx5_glue->create_wq
2276                 (priv->ctx,
2277                  &(struct ibv_wq_init_attr){
2278                         .wq_type = IBV_WQT_RQ,
2279                         .max_wr = 1,
2280                         .max_sge = 1,
2281                         .pd = priv->pd,
2282                         .cq = fdq->cq,
2283                  });
2284         if (!fdq->wq) {
2285                 DRV_LOG(WARNING, "port %u cannot allocate WQ for drop queue",
2286                         dev->data->port_id);
2287                 rte_errno = errno;
2288                 goto error;
2289         }
2290         fdq->ind_table = mlx5_glue->create_rwq_ind_table
2291                 (priv->ctx,
2292                  &(struct ibv_rwq_ind_table_init_attr){
2293                         .log_ind_tbl_size = 0,
2294                         .ind_tbl = &fdq->wq,
2295                         .comp_mask = 0,
2296                  });
2297         if (!fdq->ind_table) {
2298                 DRV_LOG(WARNING,
2299                         "port %u cannot allocate indirection table for drop"
2300                         " queue",
2301                         dev->data->port_id);
2302                 rte_errno = errno;
2303                 goto error;
2304         }
2305         fdq->qp = mlx5_glue->create_qp_ex
2306                 (priv->ctx,
2307                  &(struct ibv_qp_init_attr_ex){
2308                         .qp_type = IBV_QPT_RAW_PACKET,
2309                         .comp_mask =
2310                                 IBV_QP_INIT_ATTR_PD |
2311                                 IBV_QP_INIT_ATTR_IND_TABLE |
2312                                 IBV_QP_INIT_ATTR_RX_HASH,
2313                         .rx_hash_conf = (struct ibv_rx_hash_conf){
2314                                 .rx_hash_function =
2315                                         IBV_RX_HASH_FUNC_TOEPLITZ,
2316                                 .rx_hash_key_len = rss_hash_default_key_len,
2317                                 .rx_hash_key = rss_hash_default_key,
2318                                 .rx_hash_fields_mask = 0,
2319                                 },
2320                         .rwq_ind_tbl = fdq->ind_table,
2321                         .pd = priv->pd
2322                  });
2323         if (!fdq->qp) {
2324                 DRV_LOG(WARNING, "port %u cannot allocate QP for drop queue",
2325                         dev->data->port_id);
2326                 rte_errno = errno;
2327                 goto error;
2328         }
2329         priv->flow_drop_queue = fdq;
2330         return 0;
2331 error:
2332         if (fdq->qp)
2333                 claim_zero(mlx5_glue->destroy_qp(fdq->qp));
2334         if (fdq->ind_table)
2335                 claim_zero(mlx5_glue->destroy_rwq_ind_table(fdq->ind_table));
2336         if (fdq->wq)
2337                 claim_zero(mlx5_glue->destroy_wq(fdq->wq));
2338         if (fdq->cq)
2339                 claim_zero(mlx5_glue->destroy_cq(fdq->cq));
2340         if (fdq)
2341                 rte_free(fdq);
2342         priv->flow_drop_queue = NULL;
2343         return -rte_errno;
2344 }
2345
2346 /**
2347  * Delete drop queue.
2348  *
2349  * @param dev
2350  *   Pointer to Ethernet device.
2351  */
2352 void
2353 mlx5_flow_delete_drop_queue(struct rte_eth_dev *dev)
2354 {
2355         struct priv *priv = dev->data->dev_private;
2356         struct mlx5_hrxq_drop *fdq = priv->flow_drop_queue;
2357
2358         if (!fdq)
2359                 return;
2360         if (fdq->qp)
2361                 claim_zero(mlx5_glue->destroy_qp(fdq->qp));
2362         if (fdq->ind_table)
2363                 claim_zero(mlx5_glue->destroy_rwq_ind_table(fdq->ind_table));
2364         if (fdq->wq)
2365                 claim_zero(mlx5_glue->destroy_wq(fdq->wq));
2366         if (fdq->cq)
2367                 claim_zero(mlx5_glue->destroy_cq(fdq->cq));
2368         rte_free(fdq);
2369         priv->flow_drop_queue = NULL;
2370 }
2371
2372 /**
2373  * Remove all flows.
2374  *
2375  * @param dev
2376  *   Pointer to Ethernet device.
2377  * @param list
2378  *   Pointer to a TAILQ flow list.
2379  */
2380 void
2381 mlx5_flow_stop(struct rte_eth_dev *dev, struct mlx5_flows *list)
2382 {
2383         struct priv *priv = dev->data->dev_private;
2384         struct rte_flow *flow;
2385
2386         TAILQ_FOREACH_REVERSE(flow, list, mlx5_flows, next) {
2387                 unsigned int i;
2388                 struct mlx5_ind_table_ibv *ind_tbl = NULL;
2389
2390                 if (flow->drop) {
2391                         if (!flow->frxq[HASH_RXQ_ETH].ibv_flow)
2392                                 continue;
2393                         claim_zero(mlx5_glue->destroy_flow
2394                                    (flow->frxq[HASH_RXQ_ETH].ibv_flow));
2395                         flow->frxq[HASH_RXQ_ETH].ibv_flow = NULL;
2396                         DRV_LOG(DEBUG, "port %u flow %p removed",
2397                                 dev->data->port_id, (void *)flow);
2398                         /* Next flow. */
2399                         continue;
2400                 }
2401                 /* Verify the flow has not already been cleaned. */
2402                 for (i = 0; i != hash_rxq_init_n; ++i) {
2403                         if (!flow->frxq[i].ibv_flow)
2404                                 continue;
2405                         /*
2406                          * Indirection table may be necessary to remove the
2407                          * flags in the Rx queues.
2408                          * This helps to speed-up the process by avoiding
2409                          * another loop.
2410                          */
2411                         ind_tbl = flow->frxq[i].hrxq->ind_table;
2412                         break;
2413                 }
2414                 if (i == hash_rxq_init_n)
2415                         return;
2416                 if (flow->mark) {
2417                         assert(ind_tbl);
2418                         for (i = 0; i != ind_tbl->queues_n; ++i)
2419                                 (*priv->rxqs)[ind_tbl->queues[i]]->mark = 0;
2420                 }
2421                 for (i = 0; i != hash_rxq_init_n; ++i) {
2422                         if (!flow->frxq[i].ibv_flow)
2423                                 continue;
2424                         claim_zero(mlx5_glue->destroy_flow
2425                                    (flow->frxq[i].ibv_flow));
2426                         flow->frxq[i].ibv_flow = NULL;
2427                         mlx5_hrxq_release(dev, flow->frxq[i].hrxq);
2428                         flow->frxq[i].hrxq = NULL;
2429                 }
2430                 DRV_LOG(DEBUG, "port %u flow %p removed", dev->data->port_id,
2431                         (void *)flow);
2432         }
2433 }
2434
2435 /**
2436  * Add all flows.
2437  *
2438  * @param dev
2439  *   Pointer to Ethernet device.
2440  * @param list
2441  *   Pointer to a TAILQ flow list.
2442  *
2443  * @return
2444  *   0 on success, a negative errno value otherwise and rte_errno is set.
2445  */
2446 int
2447 mlx5_flow_start(struct rte_eth_dev *dev, struct mlx5_flows *list)
2448 {
2449         struct priv *priv = dev->data->dev_private;
2450         struct rte_flow *flow;
2451
2452         TAILQ_FOREACH(flow, list, next) {
2453                 unsigned int i;
2454
2455                 if (flow->drop) {
2456                         flow->frxq[HASH_RXQ_ETH].ibv_flow =
2457                                 mlx5_glue->create_flow
2458                                 (priv->flow_drop_queue->qp,
2459                                  flow->frxq[HASH_RXQ_ETH].ibv_attr);
2460                         if (!flow->frxq[HASH_RXQ_ETH].ibv_flow) {
2461                                 DRV_LOG(DEBUG,
2462                                         "port %u flow %p cannot be applied",
2463                                         dev->data->port_id, (void *)flow);
2464                                 rte_errno = EINVAL;
2465                                 return -rte_errno;
2466                         }
2467                         DRV_LOG(DEBUG, "port %u flow %p applied",
2468                                 dev->data->port_id, (void *)flow);
2469                         /* Next flow. */
2470                         continue;
2471                 }
2472                 for (i = 0; i != hash_rxq_init_n; ++i) {
2473                         if (!flow->frxq[i].ibv_attr)
2474                                 continue;
2475                         flow->frxq[i].hrxq =
2476                                 mlx5_hrxq_get(dev, flow->rss_conf.key,
2477                                               flow->rss_conf.key_len,
2478                                               hash_rxq_init[i].hash_fields,
2479                                               flow->rss_conf.queue,
2480                                               flow->rss_conf.queue_num);
2481                         if (flow->frxq[i].hrxq)
2482                                 goto flow_create;
2483                         flow->frxq[i].hrxq =
2484                                 mlx5_hrxq_new(dev, flow->rss_conf.key,
2485                                               flow->rss_conf.key_len,
2486                                               hash_rxq_init[i].hash_fields,
2487                                               flow->rss_conf.queue,
2488                                               flow->rss_conf.queue_num);
2489                         if (!flow->frxq[i].hrxq) {
2490                                 DRV_LOG(DEBUG,
2491                                         "port %u flow %p cannot be applied",
2492                                         dev->data->port_id, (void *)flow);
2493                                 rte_errno = EINVAL;
2494                                 return -rte_errno;
2495                         }
2496 flow_create:
2497                         flow->frxq[i].ibv_flow =
2498                                 mlx5_glue->create_flow(flow->frxq[i].hrxq->qp,
2499                                                        flow->frxq[i].ibv_attr);
2500                         if (!flow->frxq[i].ibv_flow) {
2501                                 DRV_LOG(DEBUG,
2502                                         "port %u flow %p cannot be applied",
2503                                         dev->data->port_id, (void *)flow);
2504                                 rte_errno = EINVAL;
2505                                 return -rte_errno;
2506                         }
2507                         DRV_LOG(DEBUG, "port %u flow %p applied",
2508                                 dev->data->port_id, (void *)flow);
2509                 }
2510                 if (!flow->mark)
2511                         continue;
2512                 for (i = 0; i != flow->rss_conf.queue_num; ++i)
2513                         (*priv->rxqs)[flow->rss_conf.queue[i]]->mark = 1;
2514         }
2515         return 0;
2516 }
2517
2518 /**
2519  * Verify the flow list is empty
2520  *
2521  * @param dev
2522  *  Pointer to Ethernet device.
2523  *
2524  * @return the number of flows not released.
2525  */
2526 int
2527 mlx5_flow_verify(struct rte_eth_dev *dev)
2528 {
2529         struct priv *priv = dev->data->dev_private;
2530         struct rte_flow *flow;
2531         int ret = 0;
2532
2533         TAILQ_FOREACH(flow, &priv->flows, next) {
2534                 DRV_LOG(DEBUG, "port %u flow %p still referenced",
2535                         dev->data->port_id, (void *)flow);
2536                 ++ret;
2537         }
2538         return ret;
2539 }
2540
2541 /**
2542  * Enable a control flow configured from the control plane.
2543  *
2544  * @param dev
2545  *   Pointer to Ethernet device.
2546  * @param eth_spec
2547  *   An Ethernet flow spec to apply.
2548  * @param eth_mask
2549  *   An Ethernet flow mask to apply.
2550  * @param vlan_spec
2551  *   A VLAN flow spec to apply.
2552  * @param vlan_mask
2553  *   A VLAN flow mask to apply.
2554  *
2555  * @return
2556  *   0 on success, a negative errno value otherwise and rte_errno is set.
2557  */
2558 int
2559 mlx5_ctrl_flow_vlan(struct rte_eth_dev *dev,
2560                     struct rte_flow_item_eth *eth_spec,
2561                     struct rte_flow_item_eth *eth_mask,
2562                     struct rte_flow_item_vlan *vlan_spec,
2563                     struct rte_flow_item_vlan *vlan_mask)
2564 {
2565         struct priv *priv = dev->data->dev_private;
2566         const struct rte_flow_attr attr = {
2567                 .ingress = 1,
2568                 .priority = MLX5_CTRL_FLOW_PRIORITY,
2569         };
2570         struct rte_flow_item items[] = {
2571                 {
2572                         .type = RTE_FLOW_ITEM_TYPE_ETH,
2573                         .spec = eth_spec,
2574                         .last = NULL,
2575                         .mask = eth_mask,
2576                 },
2577                 {
2578                         .type = (vlan_spec) ? RTE_FLOW_ITEM_TYPE_VLAN :
2579                                 RTE_FLOW_ITEM_TYPE_END,
2580                         .spec = vlan_spec,
2581                         .last = NULL,
2582                         .mask = vlan_mask,
2583                 },
2584                 {
2585                         .type = RTE_FLOW_ITEM_TYPE_END,
2586                 },
2587         };
2588         uint16_t queue[priv->reta_idx_n];
2589         struct rte_flow_action_rss action_rss = {
2590                 .func = RTE_ETH_HASH_FUNCTION_DEFAULT,
2591                 .level = 0,
2592                 .types = priv->rss_conf.rss_hf,
2593                 .key_len = priv->rss_conf.rss_key_len,
2594                 .queue_num = priv->reta_idx_n,
2595                 .key = priv->rss_conf.rss_key,
2596                 .queue = queue,
2597         };
2598         struct rte_flow_action actions[] = {
2599                 {
2600                         .type = RTE_FLOW_ACTION_TYPE_RSS,
2601                         .conf = &action_rss,
2602                 },
2603                 {
2604                         .type = RTE_FLOW_ACTION_TYPE_END,
2605                 },
2606         };
2607         struct rte_flow *flow;
2608         struct rte_flow_error error;
2609         unsigned int i;
2610
2611         if (!priv->reta_idx_n) {
2612                 rte_errno = EINVAL;
2613                 return -rte_errno;
2614         }
2615         for (i = 0; i != priv->reta_idx_n; ++i)
2616                 queue[i] = (*priv->reta_idx)[i];
2617         flow = mlx5_flow_list_create(dev, &priv->ctrl_flows, &attr, items,
2618                                      actions, &error);
2619         if (!flow)
2620                 return -rte_errno;
2621         return 0;
2622 }
2623
2624 /**
2625  * Enable a flow control configured from the control plane.
2626  *
2627  * @param dev
2628  *   Pointer to Ethernet device.
2629  * @param eth_spec
2630  *   An Ethernet flow spec to apply.
2631  * @param eth_mask
2632  *   An Ethernet flow mask to apply.
2633  *
2634  * @return
2635  *   0 on success, a negative errno value otherwise and rte_errno is set.
2636  */
2637 int
2638 mlx5_ctrl_flow(struct rte_eth_dev *dev,
2639                struct rte_flow_item_eth *eth_spec,
2640                struct rte_flow_item_eth *eth_mask)
2641 {
2642         return mlx5_ctrl_flow_vlan(dev, eth_spec, eth_mask, NULL, NULL);
2643 }
2644
2645 /**
2646  * Destroy a flow.
2647  *
2648  * @see rte_flow_destroy()
2649  * @see rte_flow_ops
2650  */
2651 int
2652 mlx5_flow_destroy(struct rte_eth_dev *dev,
2653                   struct rte_flow *flow,
2654                   struct rte_flow_error *error __rte_unused)
2655 {
2656         struct priv *priv = dev->data->dev_private;
2657
2658         mlx5_flow_list_destroy(dev, &priv->flows, flow);
2659         return 0;
2660 }
2661
2662 /**
2663  * Destroy all flows.
2664  *
2665  * @see rte_flow_flush()
2666  * @see rte_flow_ops
2667  */
2668 int
2669 mlx5_flow_flush(struct rte_eth_dev *dev,
2670                 struct rte_flow_error *error __rte_unused)
2671 {
2672         struct priv *priv = dev->data->dev_private;
2673
2674         mlx5_flow_list_flush(dev, &priv->flows);
2675         return 0;
2676 }
2677
2678 #ifdef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
2679 /**
2680  * Query flow counter.
2681  *
2682  * @param cs
2683  *   the counter set.
2684  * @param counter_value
2685  *   returned data from the counter.
2686  *
2687  * @return
2688  *   0 on success, a negative errno value otherwise and rte_errno is set.
2689  */
2690 static int
2691 mlx5_flow_query_count(struct ibv_counter_set *cs,
2692                       struct mlx5_flow_counter_stats *counter_stats,
2693                       struct rte_flow_query_count *query_count,
2694                       struct rte_flow_error *error)
2695 {
2696         uint64_t counters[2];
2697         struct ibv_query_counter_set_attr query_cs_attr = {
2698                 .cs = cs,
2699                 .query_flags = IBV_COUNTER_SET_FORCE_UPDATE,
2700         };
2701         struct ibv_counter_set_data query_out = {
2702                 .out = counters,
2703                 .outlen = 2 * sizeof(uint64_t),
2704         };
2705         int err = mlx5_glue->query_counter_set(&query_cs_attr, &query_out);
2706
2707         if (err)
2708                 return rte_flow_error_set(error, err,
2709                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2710                                           NULL,
2711                                           "cannot read counter");
2712         query_count->hits_set = 1;
2713         query_count->bytes_set = 1;
2714         query_count->hits = counters[0] - counter_stats->hits;
2715         query_count->bytes = counters[1] - counter_stats->bytes;
2716         if (query_count->reset) {
2717                 counter_stats->hits = counters[0];
2718                 counter_stats->bytes = counters[1];
2719         }
2720         return 0;
2721 }
2722
2723 /**
2724  * Query a flows.
2725  *
2726  * @see rte_flow_query()
2727  * @see rte_flow_ops
2728  */
2729 int
2730 mlx5_flow_query(struct rte_eth_dev *dev __rte_unused,
2731                 struct rte_flow *flow,
2732                 enum rte_flow_action_type action __rte_unused,
2733                 void *data,
2734                 struct rte_flow_error *error)
2735 {
2736         if (flow->cs) {
2737                 int ret;
2738
2739                 ret = mlx5_flow_query_count(flow->cs,
2740                                             &flow->counter_stats,
2741                                             (struct rte_flow_query_count *)data,
2742                                             error);
2743                 if (ret)
2744                         return ret;
2745         } else {
2746                 return rte_flow_error_set(error, EINVAL,
2747                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2748                                           NULL,
2749                                           "no counter found for flow");
2750         }
2751         return 0;
2752 }
2753 #endif
2754
2755 /**
2756  * Isolated mode.
2757  *
2758  * @see rte_flow_isolate()
2759  * @see rte_flow_ops
2760  */
2761 int
2762 mlx5_flow_isolate(struct rte_eth_dev *dev,
2763                   int enable,
2764                   struct rte_flow_error *error)
2765 {
2766         struct priv *priv = dev->data->dev_private;
2767
2768         if (dev->data->dev_started) {
2769                 rte_flow_error_set(error, EBUSY,
2770                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2771                                    NULL,
2772                                    "port must be stopped first");
2773                 return -rte_errno;
2774         }
2775         priv->isolated = !!enable;
2776         if (enable)
2777                 priv->dev->dev_ops = &mlx5_dev_ops_isolate;
2778         else
2779                 priv->dev->dev_ops = &mlx5_dev_ops;
2780         return 0;
2781 }
2782
2783 /**
2784  * Convert a flow director filter to a generic flow.
2785  *
2786  * @param dev
2787  *   Pointer to Ethernet device.
2788  * @param fdir_filter
2789  *   Flow director filter to add.
2790  * @param attributes
2791  *   Generic flow parameters structure.
2792  *
2793  * @return
2794  *   0 on success, a negative errno value otherwise and rte_errno is set.
2795  */
2796 static int
2797 mlx5_fdir_filter_convert(struct rte_eth_dev *dev,
2798                          const struct rte_eth_fdir_filter *fdir_filter,
2799                          struct mlx5_fdir *attributes)
2800 {
2801         struct priv *priv = dev->data->dev_private;
2802         const struct rte_eth_fdir_input *input = &fdir_filter->input;
2803         const struct rte_eth_fdir_masks *mask =
2804                 &dev->data->dev_conf.fdir_conf.mask;
2805
2806         /* Validate queue number. */
2807         if (fdir_filter->action.rx_queue >= priv->rxqs_n) {
2808                 DRV_LOG(ERR, "port %u invalid queue number %d",
2809                         dev->data->port_id, fdir_filter->action.rx_queue);
2810                 rte_errno = EINVAL;
2811                 return -rte_errno;
2812         }
2813         attributes->attr.ingress = 1;
2814         attributes->items[0] = (struct rte_flow_item) {
2815                 .type = RTE_FLOW_ITEM_TYPE_ETH,
2816                 .spec = &attributes->l2,
2817                 .mask = &attributes->l2_mask,
2818         };
2819         switch (fdir_filter->action.behavior) {
2820         case RTE_ETH_FDIR_ACCEPT:
2821                 attributes->actions[0] = (struct rte_flow_action){
2822                         .type = RTE_FLOW_ACTION_TYPE_QUEUE,
2823                         .conf = &attributes->queue,
2824                 };
2825                 break;
2826         case RTE_ETH_FDIR_REJECT:
2827                 attributes->actions[0] = (struct rte_flow_action){
2828                         .type = RTE_FLOW_ACTION_TYPE_DROP,
2829                 };
2830                 break;
2831         default:
2832                 DRV_LOG(ERR, "port %u invalid behavior %d",
2833                         dev->data->port_id,
2834                         fdir_filter->action.behavior);
2835                 rte_errno = ENOTSUP;
2836                 return -rte_errno;
2837         }
2838         attributes->queue.index = fdir_filter->action.rx_queue;
2839         /* Handle L3. */
2840         switch (fdir_filter->input.flow_type) {
2841         case RTE_ETH_FLOW_NONFRAG_IPV4_UDP:
2842         case RTE_ETH_FLOW_NONFRAG_IPV4_TCP:
2843         case RTE_ETH_FLOW_NONFRAG_IPV4_OTHER:
2844                 attributes->l3.ipv4.hdr = (struct ipv4_hdr){
2845                         .src_addr = input->flow.ip4_flow.src_ip,
2846                         .dst_addr = input->flow.ip4_flow.dst_ip,
2847                         .time_to_live = input->flow.ip4_flow.ttl,
2848                         .type_of_service = input->flow.ip4_flow.tos,
2849                         .next_proto_id = input->flow.ip4_flow.proto,
2850                 };
2851                 attributes->l3_mask.ipv4.hdr = (struct ipv4_hdr){
2852                         .src_addr = mask->ipv4_mask.src_ip,
2853                         .dst_addr = mask->ipv4_mask.dst_ip,
2854                         .time_to_live = mask->ipv4_mask.ttl,
2855                         .type_of_service = mask->ipv4_mask.tos,
2856                         .next_proto_id = mask->ipv4_mask.proto,
2857                 };
2858                 attributes->items[1] = (struct rte_flow_item){
2859                         .type = RTE_FLOW_ITEM_TYPE_IPV4,
2860                         .spec = &attributes->l3,
2861                         .mask = &attributes->l3_mask,
2862                 };
2863                 break;
2864         case RTE_ETH_FLOW_NONFRAG_IPV6_UDP:
2865         case RTE_ETH_FLOW_NONFRAG_IPV6_TCP:
2866         case RTE_ETH_FLOW_NONFRAG_IPV6_OTHER:
2867                 attributes->l3.ipv6.hdr = (struct ipv6_hdr){
2868                         .hop_limits = input->flow.ipv6_flow.hop_limits,
2869                         .proto = input->flow.ipv6_flow.proto,
2870                 };
2871
2872                 memcpy(attributes->l3.ipv6.hdr.src_addr,
2873                        input->flow.ipv6_flow.src_ip,
2874                        RTE_DIM(attributes->l3.ipv6.hdr.src_addr));
2875                 memcpy(attributes->l3.ipv6.hdr.dst_addr,
2876                        input->flow.ipv6_flow.dst_ip,
2877                        RTE_DIM(attributes->l3.ipv6.hdr.src_addr));
2878                 memcpy(attributes->l3_mask.ipv6.hdr.src_addr,
2879                        mask->ipv6_mask.src_ip,
2880                        RTE_DIM(attributes->l3_mask.ipv6.hdr.src_addr));
2881                 memcpy(attributes->l3_mask.ipv6.hdr.dst_addr,
2882                        mask->ipv6_mask.dst_ip,
2883                        RTE_DIM(attributes->l3_mask.ipv6.hdr.src_addr));
2884                 attributes->items[1] = (struct rte_flow_item){
2885                         .type = RTE_FLOW_ITEM_TYPE_IPV6,
2886                         .spec = &attributes->l3,
2887                         .mask = &attributes->l3_mask,
2888                 };
2889                 break;
2890         default:
2891                 DRV_LOG(ERR, "port %u invalid flow type%d",
2892                         dev->data->port_id, fdir_filter->input.flow_type);
2893                 rte_errno = ENOTSUP;
2894                 return -rte_errno;
2895         }
2896         /* Handle L4. */
2897         switch (fdir_filter->input.flow_type) {
2898         case RTE_ETH_FLOW_NONFRAG_IPV4_UDP:
2899                 attributes->l4.udp.hdr = (struct udp_hdr){
2900                         .src_port = input->flow.udp4_flow.src_port,
2901                         .dst_port = input->flow.udp4_flow.dst_port,
2902                 };
2903                 attributes->l4_mask.udp.hdr = (struct udp_hdr){
2904                         .src_port = mask->src_port_mask,
2905                         .dst_port = mask->dst_port_mask,
2906                 };
2907                 attributes->items[2] = (struct rte_flow_item){
2908                         .type = RTE_FLOW_ITEM_TYPE_UDP,
2909                         .spec = &attributes->l4,
2910                         .mask = &attributes->l4_mask,
2911                 };
2912                 break;
2913         case RTE_ETH_FLOW_NONFRAG_IPV4_TCP:
2914                 attributes->l4.tcp.hdr = (struct tcp_hdr){
2915                         .src_port = input->flow.tcp4_flow.src_port,
2916                         .dst_port = input->flow.tcp4_flow.dst_port,
2917                 };
2918                 attributes->l4_mask.tcp.hdr = (struct tcp_hdr){
2919                         .src_port = mask->src_port_mask,
2920                         .dst_port = mask->dst_port_mask,
2921                 };
2922                 attributes->items[2] = (struct rte_flow_item){
2923                         .type = RTE_FLOW_ITEM_TYPE_TCP,
2924                         .spec = &attributes->l4,
2925                         .mask = &attributes->l4_mask,
2926                 };
2927                 break;
2928         case RTE_ETH_FLOW_NONFRAG_IPV6_UDP:
2929                 attributes->l4.udp.hdr = (struct udp_hdr){
2930                         .src_port = input->flow.udp6_flow.src_port,
2931                         .dst_port = input->flow.udp6_flow.dst_port,
2932                 };
2933                 attributes->l4_mask.udp.hdr = (struct udp_hdr){
2934                         .src_port = mask->src_port_mask,
2935                         .dst_port = mask->dst_port_mask,
2936                 };
2937                 attributes->items[2] = (struct rte_flow_item){
2938                         .type = RTE_FLOW_ITEM_TYPE_UDP,
2939                         .spec = &attributes->l4,
2940                         .mask = &attributes->l4_mask,
2941                 };
2942                 break;
2943         case RTE_ETH_FLOW_NONFRAG_IPV6_TCP:
2944                 attributes->l4.tcp.hdr = (struct tcp_hdr){
2945                         .src_port = input->flow.tcp6_flow.src_port,
2946                         .dst_port = input->flow.tcp6_flow.dst_port,
2947                 };
2948                 attributes->l4_mask.tcp.hdr = (struct tcp_hdr){
2949                         .src_port = mask->src_port_mask,
2950                         .dst_port = mask->dst_port_mask,
2951                 };
2952                 attributes->items[2] = (struct rte_flow_item){
2953                         .type = RTE_FLOW_ITEM_TYPE_TCP,
2954                         .spec = &attributes->l4,
2955                         .mask = &attributes->l4_mask,
2956                 };
2957                 break;
2958         case RTE_ETH_FLOW_NONFRAG_IPV4_OTHER:
2959         case RTE_ETH_FLOW_NONFRAG_IPV6_OTHER:
2960                 break;
2961         default:
2962                 DRV_LOG(ERR, "port %u invalid flow type%d",
2963                         dev->data->port_id, fdir_filter->input.flow_type);
2964                 rte_errno = ENOTSUP;
2965                 return -rte_errno;
2966         }
2967         return 0;
2968 }
2969
2970 /**
2971  * Add new flow director filter and store it in list.
2972  *
2973  * @param dev
2974  *   Pointer to Ethernet device.
2975  * @param fdir_filter
2976  *   Flow director filter to add.
2977  *
2978  * @return
2979  *   0 on success, a negative errno value otherwise and rte_errno is set.
2980  */
2981 static int
2982 mlx5_fdir_filter_add(struct rte_eth_dev *dev,
2983                      const struct rte_eth_fdir_filter *fdir_filter)
2984 {
2985         struct priv *priv = dev->data->dev_private;
2986         struct mlx5_fdir attributes = {
2987                 .attr.group = 0,
2988                 .l2_mask = {
2989                         .dst.addr_bytes = "\x00\x00\x00\x00\x00\x00",
2990                         .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
2991                         .type = 0,
2992                 },
2993         };
2994         struct mlx5_flow_parse parser = {
2995                 .layer = HASH_RXQ_ETH,
2996         };
2997         struct rte_flow_error error;
2998         struct rte_flow *flow;
2999         int ret;
3000
3001         ret = mlx5_fdir_filter_convert(dev, fdir_filter, &attributes);
3002         if (ret)
3003                 return ret;
3004         ret = mlx5_flow_convert(dev, &attributes.attr, attributes.items,
3005                                 attributes.actions, &error, &parser);
3006         if (ret)
3007                 return ret;
3008         flow = mlx5_flow_list_create(dev, &priv->flows, &attributes.attr,
3009                                      attributes.items, attributes.actions,
3010                                      &error);
3011         if (flow) {
3012                 DRV_LOG(DEBUG, "port %u FDIR created %p", dev->data->port_id,
3013                         (void *)flow);
3014                 return 0;
3015         }
3016         return -rte_errno;
3017 }
3018
3019 /**
3020  * Delete specific filter.
3021  *
3022  * @param dev
3023  *   Pointer to Ethernet device.
3024  * @param fdir_filter
3025  *   Filter to be deleted.
3026  *
3027  * @return
3028  *   0 on success, a negative errno value otherwise and rte_errno is set.
3029  */
3030 static int
3031 mlx5_fdir_filter_delete(struct rte_eth_dev *dev,
3032                         const struct rte_eth_fdir_filter *fdir_filter)
3033 {
3034         struct priv *priv = dev->data->dev_private;
3035         struct mlx5_fdir attributes = {
3036                 .attr.group = 0,
3037         };
3038         struct mlx5_flow_parse parser = {
3039                 .create = 1,
3040                 .layer = HASH_RXQ_ETH,
3041         };
3042         struct rte_flow_error error;
3043         struct rte_flow *flow;
3044         unsigned int i;
3045         int ret;
3046
3047         ret = mlx5_fdir_filter_convert(dev, fdir_filter, &attributes);
3048         if (ret)
3049                 return ret;
3050         ret = mlx5_flow_convert(dev, &attributes.attr, attributes.items,
3051                                 attributes.actions, &error, &parser);
3052         if (ret)
3053                 goto exit;
3054         /*
3055          * Special case for drop action which is only set in the
3056          * specifications when the flow is created.  In this situation the
3057          * drop specification is missing.
3058          */
3059         if (parser.drop) {
3060                 struct ibv_flow_spec_action_drop *drop;
3061
3062                 drop = (void *)((uintptr_t)parser.queue[HASH_RXQ_ETH].ibv_attr +
3063                                 parser.queue[HASH_RXQ_ETH].offset);
3064                 *drop = (struct ibv_flow_spec_action_drop){
3065                         .type = IBV_FLOW_SPEC_ACTION_DROP,
3066                         .size = sizeof(struct ibv_flow_spec_action_drop),
3067                 };
3068                 parser.queue[HASH_RXQ_ETH].ibv_attr->num_of_specs++;
3069         }
3070         TAILQ_FOREACH(flow, &priv->flows, next) {
3071                 struct ibv_flow_attr *attr;
3072                 struct ibv_spec_header *attr_h;
3073                 void *spec;
3074                 struct ibv_flow_attr *flow_attr;
3075                 struct ibv_spec_header *flow_h;
3076                 void *flow_spec;
3077                 unsigned int specs_n;
3078
3079                 attr = parser.queue[HASH_RXQ_ETH].ibv_attr;
3080                 flow_attr = flow->frxq[HASH_RXQ_ETH].ibv_attr;
3081                 /* Compare first the attributes. */
3082                 if (memcmp(attr, flow_attr, sizeof(struct ibv_flow_attr)))
3083                         continue;
3084                 if (attr->num_of_specs == 0)
3085                         continue;
3086                 spec = (void *)((uintptr_t)attr +
3087                                 sizeof(struct ibv_flow_attr));
3088                 flow_spec = (void *)((uintptr_t)flow_attr +
3089                                      sizeof(struct ibv_flow_attr));
3090                 specs_n = RTE_MIN(attr->num_of_specs, flow_attr->num_of_specs);
3091                 for (i = 0; i != specs_n; ++i) {
3092                         attr_h = spec;
3093                         flow_h = flow_spec;
3094                         if (memcmp(spec, flow_spec,
3095                                    RTE_MIN(attr_h->size, flow_h->size)))
3096                                 goto wrong_flow;
3097                         spec = (void *)((uintptr_t)spec + attr_h->size);
3098                         flow_spec = (void *)((uintptr_t)flow_spec +
3099                                              flow_h->size);
3100                 }
3101                 /* At this point, the flow match. */
3102                 break;
3103 wrong_flow:
3104                 /* The flow does not match. */
3105                 continue;
3106         }
3107         ret = rte_errno; /* Save rte_errno before cleanup. */
3108         if (flow)
3109                 mlx5_flow_list_destroy(dev, &priv->flows, flow);
3110 exit:
3111         for (i = 0; i != hash_rxq_init_n; ++i) {
3112                 if (parser.queue[i].ibv_attr)
3113                         rte_free(parser.queue[i].ibv_attr);
3114         }
3115         rte_errno = ret; /* Restore rte_errno. */
3116         return -rte_errno;
3117 }
3118
3119 /**
3120  * Update queue for specific filter.
3121  *
3122  * @param dev
3123  *   Pointer to Ethernet device.
3124  * @param fdir_filter
3125  *   Filter to be updated.
3126  *
3127  * @return
3128  *   0 on success, a negative errno value otherwise and rte_errno is set.
3129  */
3130 static int
3131 mlx5_fdir_filter_update(struct rte_eth_dev *dev,
3132                         const struct rte_eth_fdir_filter *fdir_filter)
3133 {
3134         int ret;
3135
3136         ret = mlx5_fdir_filter_delete(dev, fdir_filter);
3137         if (ret)
3138                 return ret;
3139         return mlx5_fdir_filter_add(dev, fdir_filter);
3140 }
3141
3142 /**
3143  * Flush all filters.
3144  *
3145  * @param dev
3146  *   Pointer to Ethernet device.
3147  */
3148 static void
3149 mlx5_fdir_filter_flush(struct rte_eth_dev *dev)
3150 {
3151         struct priv *priv = dev->data->dev_private;
3152
3153         mlx5_flow_list_flush(dev, &priv->flows);
3154 }
3155
3156 /**
3157  * Get flow director information.
3158  *
3159  * @param dev
3160  *   Pointer to Ethernet device.
3161  * @param[out] fdir_info
3162  *   Resulting flow director information.
3163  */
3164 static void
3165 mlx5_fdir_info_get(struct rte_eth_dev *dev, struct rte_eth_fdir_info *fdir_info)
3166 {
3167         struct priv *priv = dev->data->dev_private;
3168         struct rte_eth_fdir_masks *mask =
3169                 &priv->dev->data->dev_conf.fdir_conf.mask;
3170
3171         fdir_info->mode = priv->dev->data->dev_conf.fdir_conf.mode;
3172         fdir_info->guarant_spc = 0;
3173         rte_memcpy(&fdir_info->mask, mask, sizeof(fdir_info->mask));
3174         fdir_info->max_flexpayload = 0;
3175         fdir_info->flow_types_mask[0] = 0;
3176         fdir_info->flex_payload_unit = 0;
3177         fdir_info->max_flex_payload_segment_num = 0;
3178         fdir_info->flex_payload_limit = 0;
3179         memset(&fdir_info->flex_conf, 0, sizeof(fdir_info->flex_conf));
3180 }
3181
3182 /**
3183  * Deal with flow director operations.
3184  *
3185  * @param dev
3186  *   Pointer to Ethernet device.
3187  * @param filter_op
3188  *   Operation to perform.
3189  * @param arg
3190  *   Pointer to operation-specific structure.
3191  *
3192  * @return
3193  *   0 on success, a negative errno value otherwise and rte_errno is set.
3194  */
3195 static int
3196 mlx5_fdir_ctrl_func(struct rte_eth_dev *dev, enum rte_filter_op filter_op,
3197                     void *arg)
3198 {
3199         struct priv *priv = dev->data->dev_private;
3200         enum rte_fdir_mode fdir_mode =
3201                 priv->dev->data->dev_conf.fdir_conf.mode;
3202
3203         if (filter_op == RTE_ETH_FILTER_NOP)
3204                 return 0;
3205         if (fdir_mode != RTE_FDIR_MODE_PERFECT &&
3206             fdir_mode != RTE_FDIR_MODE_PERFECT_MAC_VLAN) {
3207                 DRV_LOG(ERR, "port %u flow director mode %d not supported",
3208                         dev->data->port_id, fdir_mode);
3209                 rte_errno = EINVAL;
3210                 return -rte_errno;
3211         }
3212         switch (filter_op) {
3213         case RTE_ETH_FILTER_ADD:
3214                 return mlx5_fdir_filter_add(dev, arg);
3215         case RTE_ETH_FILTER_UPDATE:
3216                 return mlx5_fdir_filter_update(dev, arg);
3217         case RTE_ETH_FILTER_DELETE:
3218                 return mlx5_fdir_filter_delete(dev, arg);
3219         case RTE_ETH_FILTER_FLUSH:
3220                 mlx5_fdir_filter_flush(dev);
3221                 break;
3222         case RTE_ETH_FILTER_INFO:
3223                 mlx5_fdir_info_get(dev, arg);
3224                 break;
3225         default:
3226                 DRV_LOG(DEBUG, "port %u unknown operation %u",
3227                         dev->data->port_id, filter_op);
3228                 rte_errno = EINVAL;
3229                 return -rte_errno;
3230         }
3231         return 0;
3232 }
3233
3234 /**
3235  * Manage filter operations.
3236  *
3237  * @param dev
3238  *   Pointer to Ethernet device structure.
3239  * @param filter_type
3240  *   Filter type.
3241  * @param filter_op
3242  *   Operation to perform.
3243  * @param arg
3244  *   Pointer to operation-specific structure.
3245  *
3246  * @return
3247  *   0 on success, a negative errno value otherwise and rte_errno is set.
3248  */
3249 int
3250 mlx5_dev_filter_ctrl(struct rte_eth_dev *dev,
3251                      enum rte_filter_type filter_type,
3252                      enum rte_filter_op filter_op,
3253                      void *arg)
3254 {
3255         switch (filter_type) {
3256         case RTE_ETH_FILTER_GENERIC:
3257                 if (filter_op != RTE_ETH_FILTER_GET) {
3258                         rte_errno = EINVAL;
3259                         return -rte_errno;
3260                 }
3261                 *(const void **)arg = &mlx5_flow_ops;
3262                 return 0;
3263         case RTE_ETH_FILTER_FDIR:
3264                 return mlx5_fdir_ctrl_func(dev, filter_op, arg);
3265         default:
3266                 DRV_LOG(ERR, "port %u filter type (%d) not supported",
3267                         dev->data->port_id, filter_type);
3268                 rte_errno = ENOTSUP;
3269                 return -rte_errno;
3270         }
3271         return 0;
3272 }
3273
3274 /**
3275  * Detect number of Verbs flow priorities supported.
3276  *
3277  * @param dev
3278  *   Pointer to Ethernet device.
3279  *
3280  * @return
3281  *   number of supported Verbs flow priority.
3282  */
3283 unsigned int
3284 mlx5_get_max_verbs_prio(struct rte_eth_dev *dev)
3285 {
3286         struct priv *priv = dev->data->dev_private;
3287         unsigned int verb_priorities = MLX5_VERBS_FLOW_PRIO_8;
3288         struct {
3289                 struct ibv_flow_attr attr;
3290                 struct ibv_flow_spec_eth eth;
3291                 struct ibv_flow_spec_action_drop drop;
3292         } flow_attr = {
3293                 .attr = {
3294                         .num_of_specs = 2,
3295                 },
3296                 .eth = {
3297                         .type = IBV_FLOW_SPEC_ETH,
3298                         .size = sizeof(struct ibv_flow_spec_eth),
3299                 },
3300                 .drop = {
3301                         .size = sizeof(struct ibv_flow_spec_action_drop),
3302                         .type = IBV_FLOW_SPEC_ACTION_DROP,
3303                 },
3304         };
3305         struct ibv_flow *flow;
3306
3307         do {
3308                 flow_attr.attr.priority = verb_priorities - 1;
3309                 flow = mlx5_glue->create_flow(priv->flow_drop_queue->qp,
3310                                               &flow_attr.attr);
3311                 if (flow) {
3312                         claim_zero(mlx5_glue->destroy_flow(flow));
3313                         /* Try more priorities. */
3314                         verb_priorities *= 2;
3315                 } else {
3316                         /* Failed, restore last right number. */
3317                         verb_priorities /= 2;
3318                         break;
3319                 }
3320         } while (1);
3321         DRV_LOG(DEBUG, "port %u Verbs flow priorities: %d,"
3322                 " user flow priorities: %d",
3323                 dev->data->port_id, verb_priorities, MLX5_CTRL_FLOW_PRIORITY);
3324         return verb_priorities;
3325 }