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