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