New upstream version 18.11-rc2
[deb_dpdk.git] / drivers / net / mlx5 / mlx5_flow_dv.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2018 Mellanox Technologies, Ltd
3  */
4
5 #include <sys/queue.h>
6 #include <stdalign.h>
7 #include <stdint.h>
8 #include <string.h>
9
10 /* Verbs header. */
11 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
12 #ifdef PEDANTIC
13 #pragma GCC diagnostic ignored "-Wpedantic"
14 #endif
15 #include <infiniband/verbs.h>
16 #ifdef PEDANTIC
17 #pragma GCC diagnostic error "-Wpedantic"
18 #endif
19
20 #include <rte_common.h>
21 #include <rte_ether.h>
22 #include <rte_eth_ctrl.h>
23 #include <rte_ethdev_driver.h>
24 #include <rte_flow.h>
25 #include <rte_flow_driver.h>
26 #include <rte_malloc.h>
27 #include <rte_ip.h>
28 #include <rte_gre.h>
29
30 #include "mlx5.h"
31 #include "mlx5_defs.h"
32 #include "mlx5_prm.h"
33 #include "mlx5_glue.h"
34 #include "mlx5_flow.h"
35
36 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
37
38 /**
39  * Validate META item.
40  *
41  * @param[in] dev
42  *   Pointer to the rte_eth_dev structure.
43  * @param[in] item
44  *   Item specification.
45  * @param[in] attr
46  *   Attributes of flow that includes this item.
47  * @param[out] error
48  *   Pointer to error structure.
49  *
50  * @return
51  *   0 on success, a negative errno value otherwise and rte_errno is set.
52  */
53 static int
54 flow_dv_validate_item_meta(struct rte_eth_dev *dev,
55                            const struct rte_flow_item *item,
56                            const struct rte_flow_attr *attr,
57                            struct rte_flow_error *error)
58 {
59         const struct rte_flow_item_meta *spec = item->spec;
60         const struct rte_flow_item_meta *mask = item->mask;
61         const struct rte_flow_item_meta nic_mask = {
62                 .data = RTE_BE32(UINT32_MAX)
63         };
64         int ret;
65         uint64_t offloads = dev->data->dev_conf.txmode.offloads;
66
67         if (!(offloads & DEV_TX_OFFLOAD_MATCH_METADATA))
68                 return rte_flow_error_set(error, EPERM,
69                                           RTE_FLOW_ERROR_TYPE_ITEM,
70                                           NULL,
71                                           "match on metadata offload "
72                                           "configuration is off for this port");
73         if (!spec)
74                 return rte_flow_error_set(error, EINVAL,
75                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
76                                           item->spec,
77                                           "data cannot be empty");
78         if (!spec->data)
79                 return rte_flow_error_set(error, EINVAL,
80                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
81                                           NULL,
82                                           "data cannot be zero");
83         if (!mask)
84                 mask = &rte_flow_item_meta_mask;
85         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
86                                         (const uint8_t *)&nic_mask,
87                                         sizeof(struct rte_flow_item_meta),
88                                         error);
89         if (ret < 0)
90                 return ret;
91         if (attr->ingress)
92                 return rte_flow_error_set(error, ENOTSUP,
93                                           RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
94                                           NULL,
95                                           "pattern not supported for ingress");
96         return 0;
97 }
98
99 /**
100  * Validate the L2 encap action.
101  *
102  * @param[in] action_flags
103  *   Holds the actions detected until now.
104  * @param[in] action
105  *   Pointer to the encap action.
106  * @param[in] attr
107  *   Pointer to flow attributes
108  * @param[out] error
109  *   Pointer to error structure.
110  *
111  * @return
112  *   0 on success, a negative errno value otherwise and rte_errno is set.
113  */
114 static int
115 flow_dv_validate_action_l2_encap(uint64_t action_flags,
116                                  const struct rte_flow_action *action,
117                                  const struct rte_flow_attr *attr,
118                                  struct rte_flow_error *error)
119 {
120         if (!(action->conf))
121                 return rte_flow_error_set(error, EINVAL,
122                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
123                                           "configuration cannot be null");
124         if (action_flags & MLX5_FLOW_ACTION_DROP)
125                 return rte_flow_error_set(error, EINVAL,
126                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
127                                           "can't drop and encap in same flow");
128         if (action_flags & (MLX5_FLOW_ENCAP_ACTIONS | MLX5_FLOW_DECAP_ACTIONS))
129                 return rte_flow_error_set(error, EINVAL,
130                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
131                                           "can only have a single encap or"
132                                           " decap action in a flow");
133         if (attr->ingress)
134                 return rte_flow_error_set(error, ENOTSUP,
135                                           RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
136                                           NULL,
137                                           "encap action not supported for "
138                                           "ingress");
139         return 0;
140 }
141
142 /**
143  * Validate the L2 decap action.
144  *
145  * @param[in] action_flags
146  *   Holds the actions detected until now.
147  * @param[in] attr
148  *   Pointer to flow attributes
149  * @param[out] error
150  *   Pointer to error structure.
151  *
152  * @return
153  *   0 on success, a negative errno value otherwise and rte_errno is set.
154  */
155 static int
156 flow_dv_validate_action_l2_decap(uint64_t action_flags,
157                                  const struct rte_flow_attr *attr,
158                                  struct rte_flow_error *error)
159 {
160         if (action_flags & MLX5_FLOW_ACTION_DROP)
161                 return rte_flow_error_set(error, EINVAL,
162                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
163                                           "can't drop and decap in same flow");
164         if (action_flags & (MLX5_FLOW_ENCAP_ACTIONS | MLX5_FLOW_DECAP_ACTIONS))
165                 return rte_flow_error_set(error, EINVAL,
166                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
167                                           "can only have a single encap or"
168                                           " decap action in a flow");
169         if (attr->egress)
170                 return rte_flow_error_set(error, ENOTSUP,
171                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
172                                           NULL,
173                                           "decap action not supported for "
174                                           "egress");
175         return 0;
176 }
177
178 /**
179  * Validate the raw encap action.
180  *
181  * @param[in] action_flags
182  *   Holds the actions detected until now.
183  * @param[in] action
184  *   Pointer to the encap action.
185  * @param[in] attr
186  *   Pointer to flow attributes
187  * @param[out] error
188  *   Pointer to error structure.
189  *
190  * @return
191  *   0 on success, a negative errno value otherwise and rte_errno is set.
192  */
193 static int
194 flow_dv_validate_action_raw_encap(uint64_t action_flags,
195                                   const struct rte_flow_action *action,
196                                   const struct rte_flow_attr *attr,
197                                   struct rte_flow_error *error)
198 {
199         if (!(action->conf))
200                 return rte_flow_error_set(error, EINVAL,
201                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
202                                           "configuration cannot be null");
203         if (action_flags & MLX5_FLOW_ACTION_DROP)
204                 return rte_flow_error_set(error, EINVAL,
205                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
206                                           "can't drop and encap in same flow");
207         if (action_flags & MLX5_FLOW_ENCAP_ACTIONS)
208                 return rte_flow_error_set(error, EINVAL,
209                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
210                                           "can only have a single encap"
211                                           " action in a flow");
212         /* encap without preceding decap is not supported for ingress */
213         if (attr->ingress && !(action_flags & MLX5_FLOW_ACTION_RAW_DECAP))
214                 return rte_flow_error_set(error, ENOTSUP,
215                                           RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
216                                           NULL,
217                                           "encap action not supported for "
218                                           "ingress");
219         return 0;
220 }
221
222 /**
223  * Validate the raw decap action.
224  *
225  * @param[in] action_flags
226  *   Holds the actions detected until now.
227  * @param[in] action
228  *   Pointer to the encap action.
229  * @param[in] attr
230  *   Pointer to flow attributes
231  * @param[out] error
232  *   Pointer to error structure.
233  *
234  * @return
235  *   0 on success, a negative errno value otherwise and rte_errno is set.
236  */
237 static int
238 flow_dv_validate_action_raw_decap(uint64_t action_flags,
239                                   const struct rte_flow_action *action,
240                                   const struct rte_flow_attr *attr,
241                                   struct rte_flow_error *error)
242 {
243         if (action_flags & MLX5_FLOW_ACTION_DROP)
244                 return rte_flow_error_set(error, EINVAL,
245                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
246                                           "can't drop and decap in same flow");
247         if (action_flags & MLX5_FLOW_ENCAP_ACTIONS)
248                 return rte_flow_error_set(error, EINVAL,
249                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
250                                           "can't have encap action before"
251                                           " decap action");
252         if (action_flags & MLX5_FLOW_DECAP_ACTIONS)
253                 return rte_flow_error_set(error, EINVAL,
254                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
255                                           "can only have a single decap"
256                                           " action in a flow");
257         /* decap action is valid on egress only if it is followed by encap */
258         if (attr->egress) {
259                 for (; action->type != RTE_FLOW_ACTION_TYPE_END &&
260                        action->type != RTE_FLOW_ACTION_TYPE_RAW_ENCAP;
261                        action++) {
262                 }
263                 if (action->type != RTE_FLOW_ACTION_TYPE_RAW_ENCAP)
264                         return rte_flow_error_set
265                                         (error, ENOTSUP,
266                                          RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
267                                          NULL, "decap action not supported"
268                                          " for egress");
269         }
270         return 0;
271 }
272
273
274 /**
275  * Find existing encap/decap resource or create and register a new one.
276  *
277  * @param dev[in, out]
278  *   Pointer to rte_eth_dev structure.
279  * @param[in, out] resource
280  *   Pointer to encap/decap resource.
281  * @parm[in, out] dev_flow
282  *   Pointer to the dev_flow.
283  * @param[out] error
284  *   pointer to error structure.
285  *
286  * @return
287  *   0 on success otherwise -errno and errno is set.
288  */
289 static int
290 flow_dv_encap_decap_resource_register
291                         (struct rte_eth_dev *dev,
292                          struct mlx5_flow_dv_encap_decap_resource *resource,
293                          struct mlx5_flow *dev_flow,
294                          struct rte_flow_error *error)
295 {
296         struct priv *priv = dev->data->dev_private;
297         struct mlx5_flow_dv_encap_decap_resource *cache_resource;
298
299         /* Lookup a matching resource from cache. */
300         LIST_FOREACH(cache_resource, &priv->encaps_decaps, next) {
301                 if (resource->reformat_type == cache_resource->reformat_type &&
302                     resource->ft_type == cache_resource->ft_type &&
303                     resource->size == cache_resource->size &&
304                     !memcmp((const void *)resource->buf,
305                             (const void *)cache_resource->buf,
306                             resource->size)) {
307                         DRV_LOG(DEBUG, "encap/decap resource %p: refcnt %d++",
308                                 (void *)cache_resource,
309                                 rte_atomic32_read(&cache_resource->refcnt));
310                         rte_atomic32_inc(&cache_resource->refcnt);
311                         dev_flow->dv.encap_decap = cache_resource;
312                         return 0;
313                 }
314         }
315         /* Register new encap/decap resource. */
316         cache_resource = rte_calloc(__func__, 1, sizeof(*cache_resource), 0);
317         if (!cache_resource)
318                 return rte_flow_error_set(error, ENOMEM,
319                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
320                                           "cannot allocate resource memory");
321         *cache_resource = *resource;
322         cache_resource->verbs_action =
323                 mlx5_glue->dv_create_flow_action_packet_reformat
324                         (priv->ctx, cache_resource->size,
325                          (cache_resource->size ? cache_resource->buf : NULL),
326                          cache_resource->reformat_type,
327                          cache_resource->ft_type);
328         if (!cache_resource->verbs_action) {
329                 rte_free(cache_resource);
330                 return rte_flow_error_set(error, ENOMEM,
331                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
332                                           NULL, "cannot create action");
333         }
334         rte_atomic32_init(&cache_resource->refcnt);
335         rte_atomic32_inc(&cache_resource->refcnt);
336         LIST_INSERT_HEAD(&priv->encaps_decaps, cache_resource, next);
337         dev_flow->dv.encap_decap = cache_resource;
338         DRV_LOG(DEBUG, "new encap/decap resource %p: refcnt %d++",
339                 (void *)cache_resource,
340                 rte_atomic32_read(&cache_resource->refcnt));
341         return 0;
342 }
343
344 /**
345  * Get the size of specific rte_flow_item_type
346  *
347  * @param[in] item_type
348  *   Tested rte_flow_item_type.
349  *
350  * @return
351  *   sizeof struct item_type, 0 if void or irrelevant.
352  */
353 static size_t
354 flow_dv_get_item_len(const enum rte_flow_item_type item_type)
355 {
356         size_t retval;
357
358         switch (item_type) {
359         case RTE_FLOW_ITEM_TYPE_ETH:
360                 retval = sizeof(struct rte_flow_item_eth);
361                 break;
362         case RTE_FLOW_ITEM_TYPE_VLAN:
363                 retval = sizeof(struct rte_flow_item_vlan);
364                 break;
365         case RTE_FLOW_ITEM_TYPE_IPV4:
366                 retval = sizeof(struct rte_flow_item_ipv4);
367                 break;
368         case RTE_FLOW_ITEM_TYPE_IPV6:
369                 retval = sizeof(struct rte_flow_item_ipv6);
370                 break;
371         case RTE_FLOW_ITEM_TYPE_UDP:
372                 retval = sizeof(struct rte_flow_item_udp);
373                 break;
374         case RTE_FLOW_ITEM_TYPE_TCP:
375                 retval = sizeof(struct rte_flow_item_tcp);
376                 break;
377         case RTE_FLOW_ITEM_TYPE_VXLAN:
378                 retval = sizeof(struct rte_flow_item_vxlan);
379                 break;
380         case RTE_FLOW_ITEM_TYPE_GRE:
381                 retval = sizeof(struct rte_flow_item_gre);
382                 break;
383         case RTE_FLOW_ITEM_TYPE_NVGRE:
384                 retval = sizeof(struct rte_flow_item_nvgre);
385                 break;
386         case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
387                 retval = sizeof(struct rte_flow_item_vxlan_gpe);
388                 break;
389         case RTE_FLOW_ITEM_TYPE_MPLS:
390                 retval = sizeof(struct rte_flow_item_mpls);
391                 break;
392         case RTE_FLOW_ITEM_TYPE_VOID: /* Fall through. */
393         default:
394                 retval = 0;
395                 break;
396         }
397         return retval;
398 }
399
400 #define MLX5_ENCAP_IPV4_VERSION         0x40
401 #define MLX5_ENCAP_IPV4_IHL_MIN         0x05
402 #define MLX5_ENCAP_IPV4_TTL_DEF         0x40
403 #define MLX5_ENCAP_IPV6_VTC_FLOW        0x60000000
404 #define MLX5_ENCAP_IPV6_HOP_LIMIT       0xff
405 #define MLX5_ENCAP_VXLAN_FLAGS          0x08000000
406 #define MLX5_ENCAP_VXLAN_GPE_FLAGS      0x04
407
408 /**
409  * Convert the encap action data from list of rte_flow_item to raw buffer
410  *
411  * @param[in] items
412  *   Pointer to rte_flow_item objects list.
413  * @param[out] buf
414  *   Pointer to the output buffer.
415  * @param[out] size
416  *   Pointer to the output buffer size.
417  * @param[out] error
418  *   Pointer to the error structure.
419  *
420  * @return
421  *   0 on success, a negative errno value otherwise and rte_errno is set.
422  */
423 static int
424 flow_dv_convert_encap_data(const struct rte_flow_item *items, uint8_t *buf,
425                            size_t *size, struct rte_flow_error *error)
426 {
427         struct ether_hdr *eth = NULL;
428         struct vlan_hdr *vlan = NULL;
429         struct ipv4_hdr *ipv4 = NULL;
430         struct ipv6_hdr *ipv6 = NULL;
431         struct udp_hdr *udp = NULL;
432         struct vxlan_hdr *vxlan = NULL;
433         struct vxlan_gpe_hdr *vxlan_gpe = NULL;
434         struct gre_hdr *gre = NULL;
435         size_t len;
436         size_t temp_size = 0;
437
438         if (!items)
439                 return rte_flow_error_set(error, EINVAL,
440                                           RTE_FLOW_ERROR_TYPE_ACTION,
441                                           NULL, "invalid empty data");
442         for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
443                 len = flow_dv_get_item_len(items->type);
444                 if (len + temp_size > MLX5_ENCAP_MAX_LEN)
445                         return rte_flow_error_set(error, EINVAL,
446                                                   RTE_FLOW_ERROR_TYPE_ACTION,
447                                                   (void *)items->type,
448                                                   "items total size is too big"
449                                                   " for encap action");
450                 rte_memcpy((void *)&buf[temp_size], items->spec, len);
451                 switch (items->type) {
452                 case RTE_FLOW_ITEM_TYPE_ETH:
453                         eth = (struct ether_hdr *)&buf[temp_size];
454                         break;
455                 case RTE_FLOW_ITEM_TYPE_VLAN:
456                         vlan = (struct vlan_hdr *)&buf[temp_size];
457                         if (!eth)
458                                 return rte_flow_error_set(error, EINVAL,
459                                                 RTE_FLOW_ERROR_TYPE_ACTION,
460                                                 (void *)items->type,
461                                                 "eth header not found");
462                         if (!eth->ether_type)
463                                 eth->ether_type = RTE_BE16(ETHER_TYPE_VLAN);
464                         break;
465                 case RTE_FLOW_ITEM_TYPE_IPV4:
466                         ipv4 = (struct ipv4_hdr *)&buf[temp_size];
467                         if (!vlan && !eth)
468                                 return rte_flow_error_set(error, EINVAL,
469                                                 RTE_FLOW_ERROR_TYPE_ACTION,
470                                                 (void *)items->type,
471                                                 "neither eth nor vlan"
472                                                 " header found");
473                         if (vlan && !vlan->eth_proto)
474                                 vlan->eth_proto = RTE_BE16(ETHER_TYPE_IPv4);
475                         else if (eth && !eth->ether_type)
476                                 eth->ether_type = RTE_BE16(ETHER_TYPE_IPv4);
477                         if (!ipv4->version_ihl)
478                                 ipv4->version_ihl = MLX5_ENCAP_IPV4_VERSION |
479                                                     MLX5_ENCAP_IPV4_IHL_MIN;
480                         if (!ipv4->time_to_live)
481                                 ipv4->time_to_live = MLX5_ENCAP_IPV4_TTL_DEF;
482                         break;
483                 case RTE_FLOW_ITEM_TYPE_IPV6:
484                         ipv6 = (struct ipv6_hdr *)&buf[temp_size];
485                         if (!vlan && !eth)
486                                 return rte_flow_error_set(error, EINVAL,
487                                                 RTE_FLOW_ERROR_TYPE_ACTION,
488                                                 (void *)items->type,
489                                                 "neither eth nor vlan"
490                                                 " header found");
491                         if (vlan && !vlan->eth_proto)
492                                 vlan->eth_proto = RTE_BE16(ETHER_TYPE_IPv6);
493                         else if (eth && !eth->ether_type)
494                                 eth->ether_type = RTE_BE16(ETHER_TYPE_IPv6);
495                         if (!ipv6->vtc_flow)
496                                 ipv6->vtc_flow =
497                                         RTE_BE32(MLX5_ENCAP_IPV6_VTC_FLOW);
498                         if (!ipv6->hop_limits)
499                                 ipv6->hop_limits = MLX5_ENCAP_IPV6_HOP_LIMIT;
500                         break;
501                 case RTE_FLOW_ITEM_TYPE_UDP:
502                         udp = (struct udp_hdr *)&buf[temp_size];
503                         if (!ipv4 && !ipv6)
504                                 return rte_flow_error_set(error, EINVAL,
505                                                 RTE_FLOW_ERROR_TYPE_ACTION,
506                                                 (void *)items->type,
507                                                 "ip header not found");
508                         if (ipv4 && !ipv4->next_proto_id)
509                                 ipv4->next_proto_id = IPPROTO_UDP;
510                         else if (ipv6 && !ipv6->proto)
511                                 ipv6->proto = IPPROTO_UDP;
512                         break;
513                 case RTE_FLOW_ITEM_TYPE_VXLAN:
514                         vxlan = (struct vxlan_hdr *)&buf[temp_size];
515                         if (!udp)
516                                 return rte_flow_error_set(error, EINVAL,
517                                                 RTE_FLOW_ERROR_TYPE_ACTION,
518                                                 (void *)items->type,
519                                                 "udp header not found");
520                         if (!udp->dst_port)
521                                 udp->dst_port = RTE_BE16(MLX5_UDP_PORT_VXLAN);
522                         if (!vxlan->vx_flags)
523                                 vxlan->vx_flags =
524                                         RTE_BE32(MLX5_ENCAP_VXLAN_FLAGS);
525                         break;
526                 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
527                         vxlan_gpe = (struct vxlan_gpe_hdr *)&buf[temp_size];
528                         if (!udp)
529                                 return rte_flow_error_set(error, EINVAL,
530                                                 RTE_FLOW_ERROR_TYPE_ACTION,
531                                                 (void *)items->type,
532                                                 "udp header not found");
533                         if (!vxlan_gpe->proto)
534                                 return rte_flow_error_set(error, EINVAL,
535                                                 RTE_FLOW_ERROR_TYPE_ACTION,
536                                                 (void *)items->type,
537                                                 "next protocol not found");
538                         if (!udp->dst_port)
539                                 udp->dst_port =
540                                         RTE_BE16(MLX5_UDP_PORT_VXLAN_GPE);
541                         if (!vxlan_gpe->vx_flags)
542                                 vxlan_gpe->vx_flags =
543                                                 MLX5_ENCAP_VXLAN_GPE_FLAGS;
544                         break;
545                 case RTE_FLOW_ITEM_TYPE_GRE:
546                 case RTE_FLOW_ITEM_TYPE_NVGRE:
547                         gre = (struct gre_hdr *)&buf[temp_size];
548                         if (!gre->proto)
549                                 return rte_flow_error_set(error, EINVAL,
550                                                 RTE_FLOW_ERROR_TYPE_ACTION,
551                                                 (void *)items->type,
552                                                 "next protocol not found");
553                         if (!ipv4 && !ipv6)
554                                 return rte_flow_error_set(error, EINVAL,
555                                                 RTE_FLOW_ERROR_TYPE_ACTION,
556                                                 (void *)items->type,
557                                                 "ip header not found");
558                         if (ipv4 && !ipv4->next_proto_id)
559                                 ipv4->next_proto_id = IPPROTO_GRE;
560                         else if (ipv6 && !ipv6->proto)
561                                 ipv6->proto = IPPROTO_GRE;
562                         break;
563                 case RTE_FLOW_ITEM_TYPE_VOID:
564                         break;
565                 default:
566                         return rte_flow_error_set(error, EINVAL,
567                                                   RTE_FLOW_ERROR_TYPE_ACTION,
568                                                   (void *)items->type,
569                                                   "unsupported item type");
570                         break;
571                 }
572                 temp_size += len;
573         }
574         *size = temp_size;
575         return 0;
576 }
577
578 /**
579  * Convert L2 encap action to DV specification.
580  *
581  * @param[in] dev
582  *   Pointer to rte_eth_dev structure.
583  * @param[in] action
584  *   Pointer to action structure.
585  * @param[in, out] dev_flow
586  *   Pointer to the mlx5_flow.
587  * @param[out] error
588  *   Pointer to the error structure.
589  *
590  * @return
591  *   0 on success, a negative errno value otherwise and rte_errno is set.
592  */
593 static int
594 flow_dv_create_action_l2_encap(struct rte_eth_dev *dev,
595                                const struct rte_flow_action *action,
596                                struct mlx5_flow *dev_flow,
597                                struct rte_flow_error *error)
598 {
599         const struct rte_flow_item *encap_data;
600         const struct rte_flow_action_raw_encap *raw_encap_data;
601         struct mlx5_flow_dv_encap_decap_resource res = {
602                 .reformat_type =
603                         MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TO_L2_TUNNEL,
604                 .ft_type = MLX5DV_FLOW_TABLE_TYPE_NIC_TX,
605         };
606
607         if (action->type == RTE_FLOW_ACTION_TYPE_RAW_ENCAP) {
608                 raw_encap_data =
609                         (const struct rte_flow_action_raw_encap *)action->conf;
610                 res.size = raw_encap_data->size;
611                 memcpy(res.buf, raw_encap_data->data, res.size);
612         } else {
613                 if (action->type == RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP)
614                         encap_data =
615                                 ((const struct rte_flow_action_vxlan_encap *)
616                                                 action->conf)->definition;
617                 else
618                         encap_data =
619                                 ((const struct rte_flow_action_nvgre_encap *)
620                                                 action->conf)->definition;
621                 if (flow_dv_convert_encap_data(encap_data, res.buf,
622                                                &res.size, error))
623                         return -rte_errno;
624         }
625         if (flow_dv_encap_decap_resource_register(dev, &res, dev_flow, error))
626                 return rte_flow_error_set(error, EINVAL,
627                                           RTE_FLOW_ERROR_TYPE_ACTION,
628                                           NULL, "can't create L2 encap action");
629         return 0;
630 }
631
632 /**
633  * Convert L2 decap action to DV specification.
634  *
635  * @param[in] dev
636  *   Pointer to rte_eth_dev structure.
637  * @param[in, out] dev_flow
638  *   Pointer to the mlx5_flow.
639  * @param[out] error
640  *   Pointer to the error structure.
641  *
642  * @return
643  *   0 on success, a negative errno value otherwise and rte_errno is set.
644  */
645 static int
646 flow_dv_create_action_l2_decap(struct rte_eth_dev *dev,
647                                struct mlx5_flow *dev_flow,
648                                struct rte_flow_error *error)
649 {
650         struct mlx5_flow_dv_encap_decap_resource res = {
651                 .size = 0,
652                 .reformat_type =
653                         MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TUNNEL_TO_L2,
654                 .ft_type = MLX5DV_FLOW_TABLE_TYPE_NIC_RX,
655         };
656
657         if (flow_dv_encap_decap_resource_register(dev, &res, dev_flow, error))
658                 return rte_flow_error_set(error, EINVAL,
659                                           RTE_FLOW_ERROR_TYPE_ACTION,
660                                           NULL, "can't create L2 decap action");
661         return 0;
662 }
663
664 /**
665  * Convert raw decap/encap (L3 tunnel) action to DV specification.
666  *
667  * @param[in] dev
668  *   Pointer to rte_eth_dev structure.
669  * @param[in] action
670  *   Pointer to action structure.
671  * @param[in, out] dev_flow
672  *   Pointer to the mlx5_flow.
673  * @param[in] attr
674  *   Pointer to the flow attributes.
675  * @param[out] error
676  *   Pointer to the error structure.
677  *
678  * @return
679  *   0 on success, a negative errno value otherwise and rte_errno is set.
680  */
681 static int
682 flow_dv_create_action_raw_encap(struct rte_eth_dev *dev,
683                                 const struct rte_flow_action *action,
684                                 struct mlx5_flow *dev_flow,
685                                 const struct rte_flow_attr *attr,
686                                 struct rte_flow_error *error)
687 {
688         const struct rte_flow_action_raw_encap *encap_data;
689         struct mlx5_flow_dv_encap_decap_resource res;
690
691         encap_data = (const struct rte_flow_action_raw_encap *)action->conf;
692         res.size = encap_data->size;
693         memcpy(res.buf, encap_data->data, res.size);
694         res.reformat_type = attr->egress ?
695                 MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TO_L3_TUNNEL :
696                 MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L3_TUNNEL_TO_L2;
697         res.ft_type = attr->egress ? MLX5DV_FLOW_TABLE_TYPE_NIC_TX :
698                                      MLX5DV_FLOW_TABLE_TYPE_NIC_RX;
699         if (flow_dv_encap_decap_resource_register(dev, &res, dev_flow, error))
700                 return rte_flow_error_set(error, EINVAL,
701                                           RTE_FLOW_ERROR_TYPE_ACTION,
702                                           NULL, "can't create encap action");
703         return 0;
704 }
705
706 /**
707  * Verify the @p attributes will be correctly understood by the NIC and store
708  * them in the @p flow if everything is correct.
709  *
710  * @param[in] dev
711  *   Pointer to dev struct.
712  * @param[in] attributes
713  *   Pointer to flow attributes
714  * @param[out] error
715  *   Pointer to error structure.
716  *
717  * @return
718  *   0 on success, a negative errno value otherwise and rte_errno is set.
719  */
720 static int
721 flow_dv_validate_attributes(struct rte_eth_dev *dev,
722                             const struct rte_flow_attr *attributes,
723                             struct rte_flow_error *error)
724 {
725         struct priv *priv = dev->data->dev_private;
726         uint32_t priority_max = priv->config.flow_prio - 1;
727
728         if (attributes->group)
729                 return rte_flow_error_set(error, ENOTSUP,
730                                           RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
731                                           NULL,
732                                           "groups is not supported");
733         if (attributes->priority != MLX5_FLOW_PRIO_RSVD &&
734             attributes->priority >= priority_max)
735                 return rte_flow_error_set(error, ENOTSUP,
736                                           RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
737                                           NULL,
738                                           "priority out of range");
739         if (attributes->transfer)
740                 return rte_flow_error_set(error, ENOTSUP,
741                                           RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
742                                           NULL,
743                                           "transfer is not supported");
744         if (!(attributes->egress ^ attributes->ingress))
745                 return rte_flow_error_set(error, ENOTSUP,
746                                           RTE_FLOW_ERROR_TYPE_ATTR, NULL,
747                                           "must specify exactly one of "
748                                           "ingress or egress");
749         return 0;
750 }
751
752 /**
753  * Internal validation function. For validating both actions and items.
754  *
755  * @param[in] dev
756  *   Pointer to the rte_eth_dev structure.
757  * @param[in] attr
758  *   Pointer to the flow attributes.
759  * @param[in] items
760  *   Pointer to the list of items.
761  * @param[in] actions
762  *   Pointer to the list of actions.
763  * @param[out] error
764  *   Pointer to the error structure.
765  *
766  * @return
767  *   0 on success, a negative errno value otherwise and rte_ernno is set.
768  */
769 static int
770 flow_dv_validate(struct rte_eth_dev *dev, const struct rte_flow_attr *attr,
771                  const struct rte_flow_item items[],
772                  const struct rte_flow_action actions[],
773                  struct rte_flow_error *error)
774 {
775         int ret;
776         uint64_t action_flags = 0;
777         uint64_t item_flags = 0;
778         int tunnel = 0;
779         uint8_t next_protocol = 0xff;
780         int actions_n = 0;
781
782         if (items == NULL)
783                 return -1;
784         ret = flow_dv_validate_attributes(dev, attr, error);
785         if (ret < 0)
786                 return ret;
787         for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
788                 tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
789                 switch (items->type) {
790                 case RTE_FLOW_ITEM_TYPE_VOID:
791                         break;
792                 case RTE_FLOW_ITEM_TYPE_ETH:
793                         ret = mlx5_flow_validate_item_eth(items, item_flags,
794                                                           error);
795                         if (ret < 0)
796                                 return ret;
797                         item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
798                                                MLX5_FLOW_LAYER_OUTER_L2;
799                         break;
800                 case RTE_FLOW_ITEM_TYPE_VLAN:
801                         ret = mlx5_flow_validate_item_vlan(items, item_flags,
802                                                            error);
803                         if (ret < 0)
804                                 return ret;
805                         item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_VLAN :
806                                                MLX5_FLOW_LAYER_OUTER_VLAN;
807                         break;
808                 case RTE_FLOW_ITEM_TYPE_IPV4:
809                         ret = mlx5_flow_validate_item_ipv4(items, item_flags,
810                                                            error);
811                         if (ret < 0)
812                                 return ret;
813                         item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
814                                                MLX5_FLOW_LAYER_OUTER_L3_IPV4;
815                         if (items->mask != NULL &&
816                             ((const struct rte_flow_item_ipv4 *)
817                              items->mask)->hdr.next_proto_id)
818                                 next_protocol =
819                                         ((const struct rte_flow_item_ipv4 *)
820                                          (items->spec))->hdr.next_proto_id;
821                         break;
822                 case RTE_FLOW_ITEM_TYPE_IPV6:
823                         ret = mlx5_flow_validate_item_ipv6(items, item_flags,
824                                                            error);
825                         if (ret < 0)
826                                 return ret;
827                         item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
828                                                MLX5_FLOW_LAYER_OUTER_L3_IPV6;
829                         if (items->mask != NULL &&
830                             ((const struct rte_flow_item_ipv6 *)
831                              items->mask)->hdr.proto)
832                                 next_protocol =
833                                         ((const struct rte_flow_item_ipv6 *)
834                                          items->spec)->hdr.proto;
835                         break;
836                 case RTE_FLOW_ITEM_TYPE_TCP:
837                         ret = mlx5_flow_validate_item_tcp
838                                                 (items, item_flags,
839                                                  next_protocol,
840                                                  &rte_flow_item_tcp_mask,
841                                                  error);
842                         if (ret < 0)
843                                 return ret;
844                         item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
845                                                MLX5_FLOW_LAYER_OUTER_L4_TCP;
846                         break;
847                 case RTE_FLOW_ITEM_TYPE_UDP:
848                         ret = mlx5_flow_validate_item_udp(items, item_flags,
849                                                           next_protocol,
850                                                           error);
851                         if (ret < 0)
852                                 return ret;
853                         item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
854                                                MLX5_FLOW_LAYER_OUTER_L4_UDP;
855                         break;
856                 case RTE_FLOW_ITEM_TYPE_GRE:
857                 case RTE_FLOW_ITEM_TYPE_NVGRE:
858                         ret = mlx5_flow_validate_item_gre(items, item_flags,
859                                                           next_protocol, error);
860                         if (ret < 0)
861                                 return ret;
862                         item_flags |= MLX5_FLOW_LAYER_GRE;
863                         break;
864                 case RTE_FLOW_ITEM_TYPE_VXLAN:
865                         ret = mlx5_flow_validate_item_vxlan(items, item_flags,
866                                                             error);
867                         if (ret < 0)
868                                 return ret;
869                         item_flags |= MLX5_FLOW_LAYER_VXLAN;
870                         break;
871                 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
872                         ret = mlx5_flow_validate_item_vxlan_gpe(items,
873                                                                 item_flags, dev,
874                                                                 error);
875                         if (ret < 0)
876                                 return ret;
877                         item_flags |= MLX5_FLOW_LAYER_VXLAN_GPE;
878                         break;
879                 case RTE_FLOW_ITEM_TYPE_META:
880                         ret = flow_dv_validate_item_meta(dev, items, attr,
881                                                          error);
882                         if (ret < 0)
883                                 return ret;
884                         item_flags |= MLX5_FLOW_ITEM_METADATA;
885                         break;
886                 default:
887                         return rte_flow_error_set(error, ENOTSUP,
888                                                   RTE_FLOW_ERROR_TYPE_ITEM,
889                                                   NULL, "item not supported");
890                 }
891         }
892         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
893                 if (actions_n == MLX5_DV_MAX_NUMBER_OF_ACTIONS)
894                         return rte_flow_error_set(error, ENOTSUP,
895                                                   RTE_FLOW_ERROR_TYPE_ACTION,
896                                                   actions, "too many actions");
897                 switch (actions->type) {
898                 case RTE_FLOW_ACTION_TYPE_VOID:
899                         break;
900                 case RTE_FLOW_ACTION_TYPE_FLAG:
901                         ret = mlx5_flow_validate_action_flag(action_flags,
902                                                              attr, error);
903                         if (ret < 0)
904                                 return ret;
905                         action_flags |= MLX5_FLOW_ACTION_FLAG;
906                         ++actions_n;
907                         break;
908                 case RTE_FLOW_ACTION_TYPE_MARK:
909                         ret = mlx5_flow_validate_action_mark(actions,
910                                                              action_flags,
911                                                              attr, error);
912                         if (ret < 0)
913                                 return ret;
914                         action_flags |= MLX5_FLOW_ACTION_MARK;
915                         ++actions_n;
916                         break;
917                 case RTE_FLOW_ACTION_TYPE_DROP:
918                         ret = mlx5_flow_validate_action_drop(action_flags,
919                                                              attr, error);
920                         if (ret < 0)
921                                 return ret;
922                         action_flags |= MLX5_FLOW_ACTION_DROP;
923                         ++actions_n;
924                         break;
925                 case RTE_FLOW_ACTION_TYPE_QUEUE:
926                         ret = mlx5_flow_validate_action_queue(actions,
927                                                               action_flags, dev,
928                                                               attr, error);
929                         if (ret < 0)
930                                 return ret;
931                         action_flags |= MLX5_FLOW_ACTION_QUEUE;
932                         ++actions_n;
933                         break;
934                 case RTE_FLOW_ACTION_TYPE_RSS:
935                         ret = mlx5_flow_validate_action_rss(actions,
936                                                             action_flags, dev,
937                                                             attr, error);
938                         if (ret < 0)
939                                 return ret;
940                         action_flags |= MLX5_FLOW_ACTION_RSS;
941                         ++actions_n;
942                         break;
943                 case RTE_FLOW_ACTION_TYPE_COUNT:
944                         ret = mlx5_flow_validate_action_count(dev, attr, error);
945                         if (ret < 0)
946                                 return ret;
947                         action_flags |= MLX5_FLOW_ACTION_COUNT;
948                         ++actions_n;
949                         break;
950                 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
951                 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
952                         ret = flow_dv_validate_action_l2_encap(action_flags,
953                                                                actions, attr,
954                                                                error);
955                         if (ret < 0)
956                                 return ret;
957                         action_flags |= actions->type ==
958                                         RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP ?
959                                         MLX5_FLOW_ACTION_VXLAN_ENCAP :
960                                         MLX5_FLOW_ACTION_NVGRE_ENCAP;
961                         ++actions_n;
962                         break;
963                 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
964                 case RTE_FLOW_ACTION_TYPE_NVGRE_DECAP:
965                         ret = flow_dv_validate_action_l2_decap(action_flags,
966                                                                attr, error);
967                         if (ret < 0)
968                                 return ret;
969                         action_flags |= actions->type ==
970                                         RTE_FLOW_ACTION_TYPE_VXLAN_DECAP ?
971                                         MLX5_FLOW_ACTION_VXLAN_DECAP :
972                                         MLX5_FLOW_ACTION_NVGRE_DECAP;
973                         ++actions_n;
974                         break;
975                 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
976                         ret = flow_dv_validate_action_raw_encap(action_flags,
977                                                                 actions, attr,
978                                                                 error);
979                         if (ret < 0)
980                                 return ret;
981                         action_flags |= MLX5_FLOW_ACTION_RAW_ENCAP;
982                         ++actions_n;
983                         break;
984                 case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
985                         ret = flow_dv_validate_action_raw_decap(action_flags,
986                                                                 actions, attr,
987                                                                 error);
988                         if (ret < 0)
989                                 return ret;
990                         action_flags |= MLX5_FLOW_ACTION_RAW_DECAP;
991                         ++actions_n;
992                         break;
993                 default:
994                         return rte_flow_error_set(error, ENOTSUP,
995                                                   RTE_FLOW_ERROR_TYPE_ACTION,
996                                                   actions,
997                                                   "action not supported");
998                 }
999         }
1000         if (!(action_flags & MLX5_FLOW_FATE_ACTIONS) && attr->ingress)
1001                 return rte_flow_error_set(error, EINVAL,
1002                                           RTE_FLOW_ERROR_TYPE_ACTION, actions,
1003                                           "no fate action is found");
1004         return 0;
1005 }
1006
1007 /**
1008  * Internal preparation function. Allocates the DV flow size,
1009  * this size is constant.
1010  *
1011  * @param[in] attr
1012  *   Pointer to the flow attributes.
1013  * @param[in] items
1014  *   Pointer to the list of items.
1015  * @param[in] actions
1016  *   Pointer to the list of actions.
1017  * @param[out] error
1018  *   Pointer to the error structure.
1019  *
1020  * @return
1021  *   Pointer to mlx5_flow object on success,
1022  *   otherwise NULL and rte_ernno is set.
1023  */
1024 static struct mlx5_flow *
1025 flow_dv_prepare(const struct rte_flow_attr *attr __rte_unused,
1026                 const struct rte_flow_item items[] __rte_unused,
1027                 const struct rte_flow_action actions[] __rte_unused,
1028                 struct rte_flow_error *error)
1029 {
1030         uint32_t size = sizeof(struct mlx5_flow);
1031         struct mlx5_flow *flow;
1032
1033         flow = rte_calloc(__func__, 1, size, 0);
1034         if (!flow) {
1035                 rte_flow_error_set(error, ENOMEM,
1036                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1037                                    "not enough memory to create flow");
1038                 return NULL;
1039         }
1040         flow->dv.value.size = MLX5_ST_SZ_DB(fte_match_param);
1041         return flow;
1042 }
1043
1044 /**
1045  * Add Ethernet item to matcher and to the value.
1046  *
1047  * @param[in, out] matcher
1048  *   Flow matcher.
1049  * @param[in, out] key
1050  *   Flow matcher value.
1051  * @param[in] item
1052  *   Flow pattern to translate.
1053  * @param[in] inner
1054  *   Item is inner pattern.
1055  */
1056 static void
1057 flow_dv_translate_item_eth(void *matcher, void *key,
1058                            const struct rte_flow_item *item, int inner)
1059 {
1060         const struct rte_flow_item_eth *eth_m = item->mask;
1061         const struct rte_flow_item_eth *eth_v = item->spec;
1062         const struct rte_flow_item_eth nic_mask = {
1063                 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
1064                 .src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
1065                 .type = RTE_BE16(0xffff),
1066         };
1067         void *headers_m;
1068         void *headers_v;
1069         char *l24_v;
1070         unsigned int i;
1071
1072         if (!eth_v)
1073                 return;
1074         if (!eth_m)
1075                 eth_m = &nic_mask;
1076         if (inner) {
1077                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
1078                                          inner_headers);
1079                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
1080         } else {
1081                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
1082                                          outer_headers);
1083                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
1084         }
1085         memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m, dmac_47_16),
1086                &eth_m->dst, sizeof(eth_m->dst));
1087         /* The value must be in the range of the mask. */
1088         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, dmac_47_16);
1089         for (i = 0; i < sizeof(eth_m->dst); ++i)
1090                 l24_v[i] = eth_m->dst.addr_bytes[i] & eth_v->dst.addr_bytes[i];
1091         memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m, smac_47_16),
1092                &eth_m->src, sizeof(eth_m->src));
1093         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, smac_47_16);
1094         /* The value must be in the range of the mask. */
1095         for (i = 0; i < sizeof(eth_m->dst); ++i)
1096                 l24_v[i] = eth_m->src.addr_bytes[i] & eth_v->src.addr_bytes[i];
1097         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ethertype,
1098                  rte_be_to_cpu_16(eth_m->type));
1099         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, ethertype);
1100         *(uint16_t *)(l24_v) = eth_m->type & eth_v->type;
1101 }
1102
1103 /**
1104  * Add VLAN item to matcher and to the value.
1105  *
1106  * @param[in, out] matcher
1107  *   Flow matcher.
1108  * @param[in, out] key
1109  *   Flow matcher value.
1110  * @param[in] item
1111  *   Flow pattern to translate.
1112  * @param[in] inner
1113  *   Item is inner pattern.
1114  */
1115 static void
1116 flow_dv_translate_item_vlan(void *matcher, void *key,
1117                             const struct rte_flow_item *item,
1118                             int inner)
1119 {
1120         const struct rte_flow_item_vlan *vlan_m = item->mask;
1121         const struct rte_flow_item_vlan *vlan_v = item->spec;
1122         const struct rte_flow_item_vlan nic_mask = {
1123                 .tci = RTE_BE16(0x0fff),
1124                 .inner_type = RTE_BE16(0xffff),
1125         };
1126         void *headers_m;
1127         void *headers_v;
1128         uint16_t tci_m;
1129         uint16_t tci_v;
1130
1131         if (!vlan_v)
1132                 return;
1133         if (!vlan_m)
1134                 vlan_m = &nic_mask;
1135         if (inner) {
1136                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
1137                                          inner_headers);
1138                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
1139         } else {
1140                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
1141                                          outer_headers);
1142                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
1143         }
1144         tci_m = rte_be_to_cpu_16(vlan_m->tci);
1145         tci_v = rte_be_to_cpu_16(vlan_m->tci & vlan_v->tci);
1146         MLX5_SET(fte_match_set_lyr_2_4, headers_m, cvlan_tag, 1);
1147         MLX5_SET(fte_match_set_lyr_2_4, headers_v, cvlan_tag, 1);
1148         MLX5_SET(fte_match_set_lyr_2_4, headers_m, first_vid, tci_m);
1149         MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_vid, tci_v);
1150         MLX5_SET(fte_match_set_lyr_2_4, headers_m, first_cfi, tci_m >> 12);
1151         MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_cfi, tci_v >> 12);
1152         MLX5_SET(fte_match_set_lyr_2_4, headers_m, first_prio, tci_m >> 13);
1153         MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_prio, tci_v >> 13);
1154 }
1155
1156 /**
1157  * Add IPV4 item to matcher and to the value.
1158  *
1159  * @param[in, out] matcher
1160  *   Flow matcher.
1161  * @param[in, out] key
1162  *   Flow matcher value.
1163  * @param[in] item
1164  *   Flow pattern to translate.
1165  * @param[in] inner
1166  *   Item is inner pattern.
1167  */
1168 static void
1169 flow_dv_translate_item_ipv4(void *matcher, void *key,
1170                             const struct rte_flow_item *item,
1171                             int inner)
1172 {
1173         const struct rte_flow_item_ipv4 *ipv4_m = item->mask;
1174         const struct rte_flow_item_ipv4 *ipv4_v = item->spec;
1175         const struct rte_flow_item_ipv4 nic_mask = {
1176                 .hdr = {
1177                         .src_addr = RTE_BE32(0xffffffff),
1178                         .dst_addr = RTE_BE32(0xffffffff),
1179                         .type_of_service = 0xff,
1180                         .next_proto_id = 0xff,
1181                 },
1182         };
1183         void *headers_m;
1184         void *headers_v;
1185         char *l24_m;
1186         char *l24_v;
1187         uint8_t tos;
1188
1189         if (inner) {
1190                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
1191                                          inner_headers);
1192                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
1193         } else {
1194                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
1195                                          outer_headers);
1196                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
1197         }
1198         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version, 0xf);
1199         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_version, 4);
1200         if (!ipv4_v)
1201                 return;
1202         if (!ipv4_m)
1203                 ipv4_m = &nic_mask;
1204         l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
1205                              dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
1206         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1207                              dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
1208         *(uint32_t *)l24_m = ipv4_m->hdr.dst_addr;
1209         *(uint32_t *)l24_v = ipv4_m->hdr.dst_addr & ipv4_v->hdr.dst_addr;
1210         l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
1211                           src_ipv4_src_ipv6.ipv4_layout.ipv4);
1212         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1213                           src_ipv4_src_ipv6.ipv4_layout.ipv4);
1214         *(uint32_t *)l24_m = ipv4_m->hdr.src_addr;
1215         *(uint32_t *)l24_v = ipv4_m->hdr.src_addr & ipv4_v->hdr.src_addr;
1216         tos = ipv4_m->hdr.type_of_service & ipv4_v->hdr.type_of_service;
1217         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ecn,
1218                  ipv4_m->hdr.type_of_service);
1219         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn, tos);
1220         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_dscp,
1221                  ipv4_m->hdr.type_of_service >> 2);
1222         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp, tos >> 2);
1223         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol,
1224                  ipv4_m->hdr.next_proto_id);
1225         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
1226                  ipv4_v->hdr.next_proto_id & ipv4_m->hdr.next_proto_id);
1227 }
1228
1229 /**
1230  * Add IPV6 item to matcher and to the value.
1231  *
1232  * @param[in, out] matcher
1233  *   Flow matcher.
1234  * @param[in, out] key
1235  *   Flow matcher value.
1236  * @param[in] item
1237  *   Flow pattern to translate.
1238  * @param[in] inner
1239  *   Item is inner pattern.
1240  */
1241 static void
1242 flow_dv_translate_item_ipv6(void *matcher, void *key,
1243                             const struct rte_flow_item *item,
1244                             int inner)
1245 {
1246         const struct rte_flow_item_ipv6 *ipv6_m = item->mask;
1247         const struct rte_flow_item_ipv6 *ipv6_v = item->spec;
1248         const struct rte_flow_item_ipv6 nic_mask = {
1249                 .hdr = {
1250                         .src_addr =
1251                                 "\xff\xff\xff\xff\xff\xff\xff\xff"
1252                                 "\xff\xff\xff\xff\xff\xff\xff\xff",
1253                         .dst_addr =
1254                                 "\xff\xff\xff\xff\xff\xff\xff\xff"
1255                                 "\xff\xff\xff\xff\xff\xff\xff\xff",
1256                         .vtc_flow = RTE_BE32(0xffffffff),
1257                         .proto = 0xff,
1258                         .hop_limits = 0xff,
1259                 },
1260         };
1261         void *headers_m;
1262         void *headers_v;
1263         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
1264         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
1265         char *l24_m;
1266         char *l24_v;
1267         uint32_t vtc_m;
1268         uint32_t vtc_v;
1269         int i;
1270         int size;
1271
1272         if (inner) {
1273                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
1274                                          inner_headers);
1275                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
1276         } else {
1277                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
1278                                          outer_headers);
1279                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
1280         }
1281         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version, 0xf);
1282         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_version, 6);
1283         if (!ipv6_v)
1284                 return;
1285         if (!ipv6_m)
1286                 ipv6_m = &nic_mask;
1287         size = sizeof(ipv6_m->hdr.dst_addr);
1288         l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
1289                              dst_ipv4_dst_ipv6.ipv6_layout.ipv6);
1290         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1291                              dst_ipv4_dst_ipv6.ipv6_layout.ipv6);
1292         memcpy(l24_m, ipv6_m->hdr.dst_addr, size);
1293         for (i = 0; i < size; ++i)
1294                 l24_v[i] = l24_m[i] & ipv6_v->hdr.dst_addr[i];
1295         l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
1296                              src_ipv4_src_ipv6.ipv6_layout.ipv6);
1297         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1298                              src_ipv4_src_ipv6.ipv6_layout.ipv6);
1299         memcpy(l24_m, ipv6_m->hdr.src_addr, size);
1300         for (i = 0; i < size; ++i)
1301                 l24_v[i] = l24_m[i] & ipv6_v->hdr.src_addr[i];
1302         /* TOS. */
1303         vtc_m = rte_be_to_cpu_32(ipv6_m->hdr.vtc_flow);
1304         vtc_v = rte_be_to_cpu_32(ipv6_m->hdr.vtc_flow & ipv6_v->hdr.vtc_flow);
1305         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ecn, vtc_m >> 20);
1306         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn, vtc_v >> 20);
1307         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_dscp, vtc_m >> 22);
1308         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp, vtc_v >> 22);
1309         /* Label. */
1310         if (inner) {
1311                 MLX5_SET(fte_match_set_misc, misc_m, inner_ipv6_flow_label,
1312                          vtc_m);
1313                 MLX5_SET(fte_match_set_misc, misc_v, inner_ipv6_flow_label,
1314                          vtc_v);
1315         } else {
1316                 MLX5_SET(fte_match_set_misc, misc_m, outer_ipv6_flow_label,
1317                          vtc_m);
1318                 MLX5_SET(fte_match_set_misc, misc_v, outer_ipv6_flow_label,
1319                          vtc_v);
1320         }
1321         /* Protocol. */
1322         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol,
1323                  ipv6_m->hdr.proto);
1324         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
1325                  ipv6_v->hdr.proto & ipv6_m->hdr.proto);
1326 }
1327
1328 /**
1329  * Add TCP item to matcher and to the value.
1330  *
1331  * @param[in, out] matcher
1332  *   Flow matcher.
1333  * @param[in, out] key
1334  *   Flow matcher value.
1335  * @param[in] item
1336  *   Flow pattern to translate.
1337  * @param[in] inner
1338  *   Item is inner pattern.
1339  */
1340 static void
1341 flow_dv_translate_item_tcp(void *matcher, void *key,
1342                            const struct rte_flow_item *item,
1343                            int inner)
1344 {
1345         const struct rte_flow_item_tcp *tcp_m = item->mask;
1346         const struct rte_flow_item_tcp *tcp_v = item->spec;
1347         void *headers_m;
1348         void *headers_v;
1349
1350         if (inner) {
1351                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
1352                                          inner_headers);
1353                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
1354         } else {
1355                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
1356                                          outer_headers);
1357                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
1358         }
1359         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
1360         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_TCP);
1361         if (!tcp_v)
1362                 return;
1363         if (!tcp_m)
1364                 tcp_m = &rte_flow_item_tcp_mask;
1365         MLX5_SET(fte_match_set_lyr_2_4, headers_m, tcp_sport,
1366                  rte_be_to_cpu_16(tcp_m->hdr.src_port));
1367         MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
1368                  rte_be_to_cpu_16(tcp_v->hdr.src_port & tcp_m->hdr.src_port));
1369         MLX5_SET(fte_match_set_lyr_2_4, headers_m, tcp_dport,
1370                  rte_be_to_cpu_16(tcp_m->hdr.dst_port));
1371         MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
1372                  rte_be_to_cpu_16(tcp_v->hdr.dst_port & tcp_m->hdr.dst_port));
1373 }
1374
1375 /**
1376  * Add UDP item to matcher and to the value.
1377  *
1378  * @param[in, out] matcher
1379  *   Flow matcher.
1380  * @param[in, out] key
1381  *   Flow matcher value.
1382  * @param[in] item
1383  *   Flow pattern to translate.
1384  * @param[in] inner
1385  *   Item is inner pattern.
1386  */
1387 static void
1388 flow_dv_translate_item_udp(void *matcher, void *key,
1389                            const struct rte_flow_item *item,
1390                            int inner)
1391 {
1392         const struct rte_flow_item_udp *udp_m = item->mask;
1393         const struct rte_flow_item_udp *udp_v = item->spec;
1394         void *headers_m;
1395         void *headers_v;
1396
1397         if (inner) {
1398                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
1399                                          inner_headers);
1400                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
1401         } else {
1402                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
1403                                          outer_headers);
1404                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
1405         }
1406         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
1407         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_UDP);
1408         if (!udp_v)
1409                 return;
1410         if (!udp_m)
1411                 udp_m = &rte_flow_item_udp_mask;
1412         MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_sport,
1413                  rte_be_to_cpu_16(udp_m->hdr.src_port));
1414         MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
1415                  rte_be_to_cpu_16(udp_v->hdr.src_port & udp_m->hdr.src_port));
1416         MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport,
1417                  rte_be_to_cpu_16(udp_m->hdr.dst_port));
1418         MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
1419                  rte_be_to_cpu_16(udp_v->hdr.dst_port & udp_m->hdr.dst_port));
1420 }
1421
1422 /**
1423  * Add GRE item to matcher and to the value.
1424  *
1425  * @param[in, out] matcher
1426  *   Flow matcher.
1427  * @param[in, out] key
1428  *   Flow matcher value.
1429  * @param[in] item
1430  *   Flow pattern to translate.
1431  * @param[in] inner
1432  *   Item is inner pattern.
1433  */
1434 static void
1435 flow_dv_translate_item_gre(void *matcher, void *key,
1436                            const struct rte_flow_item *item,
1437                            int inner)
1438 {
1439         const struct rte_flow_item_gre *gre_m = item->mask;
1440         const struct rte_flow_item_gre *gre_v = item->spec;
1441         void *headers_m;
1442         void *headers_v;
1443         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
1444         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
1445
1446         if (inner) {
1447                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
1448                                          inner_headers);
1449                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
1450         } else {
1451                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
1452                                          outer_headers);
1453                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
1454         }
1455         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
1456         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_GRE);
1457         if (!gre_v)
1458                 return;
1459         if (!gre_m)
1460                 gre_m = &rte_flow_item_gre_mask;
1461         MLX5_SET(fte_match_set_misc, misc_m, gre_protocol,
1462                  rte_be_to_cpu_16(gre_m->protocol));
1463         MLX5_SET(fte_match_set_misc, misc_v, gre_protocol,
1464                  rte_be_to_cpu_16(gre_v->protocol & gre_m->protocol));
1465 }
1466
1467 /**
1468  * Add NVGRE item to matcher and to the value.
1469  *
1470  * @param[in, out] matcher
1471  *   Flow matcher.
1472  * @param[in, out] key
1473  *   Flow matcher value.
1474  * @param[in] item
1475  *   Flow pattern to translate.
1476  * @param[in] inner
1477  *   Item is inner pattern.
1478  */
1479 static void
1480 flow_dv_translate_item_nvgre(void *matcher, void *key,
1481                              const struct rte_flow_item *item,
1482                              int inner)
1483 {
1484         const struct rte_flow_item_nvgre *nvgre_m = item->mask;
1485         const struct rte_flow_item_nvgre *nvgre_v = item->spec;
1486         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
1487         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
1488         const char *tni_flow_id_m = (const char *)nvgre_m->tni;
1489         const char *tni_flow_id_v = (const char *)nvgre_v->tni;
1490         char *gre_key_m;
1491         char *gre_key_v;
1492         int size;
1493         int i;
1494
1495         flow_dv_translate_item_gre(matcher, key, item, inner);
1496         if (!nvgre_v)
1497                 return;
1498         if (!nvgre_m)
1499                 nvgre_m = &rte_flow_item_nvgre_mask;
1500         size = sizeof(nvgre_m->tni) + sizeof(nvgre_m->flow_id);
1501         gre_key_m = MLX5_ADDR_OF(fte_match_set_misc, misc_m, gre_key_h);
1502         gre_key_v = MLX5_ADDR_OF(fte_match_set_misc, misc_v, gre_key_h);
1503         memcpy(gre_key_m, tni_flow_id_m, size);
1504         for (i = 0; i < size; ++i)
1505                 gre_key_v[i] = gre_key_m[i] & tni_flow_id_v[i];
1506 }
1507
1508 /**
1509  * Add VXLAN item to matcher and to the value.
1510  *
1511  * @param[in, out] matcher
1512  *   Flow matcher.
1513  * @param[in, out] key
1514  *   Flow matcher value.
1515  * @param[in] item
1516  *   Flow pattern to translate.
1517  * @param[in] inner
1518  *   Item is inner pattern.
1519  */
1520 static void
1521 flow_dv_translate_item_vxlan(void *matcher, void *key,
1522                              const struct rte_flow_item *item,
1523                              int inner)
1524 {
1525         const struct rte_flow_item_vxlan *vxlan_m = item->mask;
1526         const struct rte_flow_item_vxlan *vxlan_v = item->spec;
1527         void *headers_m;
1528         void *headers_v;
1529         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
1530         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
1531         char *vni_m;
1532         char *vni_v;
1533         uint16_t dport;
1534         int size;
1535         int i;
1536
1537         if (inner) {
1538                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
1539                                          inner_headers);
1540                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
1541         } else {
1542                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
1543                                          outer_headers);
1544                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
1545         }
1546         dport = item->type == RTE_FLOW_ITEM_TYPE_VXLAN ?
1547                 MLX5_UDP_PORT_VXLAN : MLX5_UDP_PORT_VXLAN_GPE;
1548         if (!MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport)) {
1549                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xFFFF);
1550                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, dport);
1551         }
1552         if (!vxlan_v)
1553                 return;
1554         if (!vxlan_m)
1555                 vxlan_m = &rte_flow_item_vxlan_mask;
1556         size = sizeof(vxlan_m->vni);
1557         vni_m = MLX5_ADDR_OF(fte_match_set_misc, misc_m, vxlan_vni);
1558         vni_v = MLX5_ADDR_OF(fte_match_set_misc, misc_v, vxlan_vni);
1559         memcpy(vni_m, vxlan_m->vni, size);
1560         for (i = 0; i < size; ++i)
1561                 vni_v[i] = vni_m[i] & vxlan_v->vni[i];
1562 }
1563
1564 /**
1565  * Add META item to matcher
1566  *
1567  * @param[in, out] matcher
1568  *   Flow matcher.
1569  * @param[in, out] key
1570  *   Flow matcher value.
1571  * @param[in] item
1572  *   Flow pattern to translate.
1573  * @param[in] inner
1574  *   Item is inner pattern.
1575  */
1576 static void
1577 flow_dv_translate_item_meta(void *matcher, void *key,
1578                             const struct rte_flow_item *item)
1579 {
1580         const struct rte_flow_item_meta *meta_m;
1581         const struct rte_flow_item_meta *meta_v;
1582         void *misc2_m =
1583                 MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters_2);
1584         void *misc2_v =
1585                 MLX5_ADDR_OF(fte_match_param, key, misc_parameters_2);
1586
1587         meta_m = (const void *)item->mask;
1588         if (!meta_m)
1589                 meta_m = &rte_flow_item_meta_mask;
1590         meta_v = (const void *)item->spec;
1591         if (meta_v) {
1592                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_a,
1593                          rte_be_to_cpu_32(meta_m->data));
1594                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_a,
1595                          rte_be_to_cpu_32(meta_v->data & meta_m->data));
1596         }
1597 }
1598
1599 static uint32_t matcher_zero[MLX5_ST_SZ_DW(fte_match_param)] = { 0 };
1600
1601 #define HEADER_IS_ZERO(match_criteria, headers)                              \
1602         !(memcmp(MLX5_ADDR_OF(fte_match_param, match_criteria, headers),     \
1603                  matcher_zero, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
1604
1605 /**
1606  * Calculate flow matcher enable bitmap.
1607  *
1608  * @param match_criteria
1609  *   Pointer to flow matcher criteria.
1610  *
1611  * @return
1612  *   Bitmap of enabled fields.
1613  */
1614 static uint8_t
1615 flow_dv_matcher_enable(uint32_t *match_criteria)
1616 {
1617         uint8_t match_criteria_enable;
1618
1619         match_criteria_enable =
1620                 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
1621                 MLX5_MATCH_CRITERIA_ENABLE_OUTER_BIT;
1622         match_criteria_enable |=
1623                 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
1624                 MLX5_MATCH_CRITERIA_ENABLE_MISC_BIT;
1625         match_criteria_enable |=
1626                 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
1627                 MLX5_MATCH_CRITERIA_ENABLE_INNER_BIT;
1628         match_criteria_enable |=
1629                 (!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
1630                 MLX5_MATCH_CRITERIA_ENABLE_MISC2_BIT;
1631
1632         return match_criteria_enable;
1633 }
1634
1635 /**
1636  * Register the flow matcher.
1637  *
1638  * @param dev[in, out]
1639  *   Pointer to rte_eth_dev structure.
1640  * @param[in, out] matcher
1641  *   Pointer to flow matcher.
1642  * @parm[in, out] dev_flow
1643  *   Pointer to the dev_flow.
1644  * @param[out] error
1645  *   pointer to error structure.
1646  *
1647  * @return
1648  *   0 on success otherwise -errno and errno is set.
1649  */
1650 static int
1651 flow_dv_matcher_register(struct rte_eth_dev *dev,
1652                          struct mlx5_flow_dv_matcher *matcher,
1653                          struct mlx5_flow *dev_flow,
1654                          struct rte_flow_error *error)
1655 {
1656         struct priv *priv = dev->data->dev_private;
1657         struct mlx5_flow_dv_matcher *cache_matcher;
1658         struct mlx5dv_flow_matcher_attr dv_attr = {
1659                 .type = IBV_FLOW_ATTR_NORMAL,
1660                 .match_mask = (void *)&matcher->mask,
1661         };
1662
1663         /* Lookup from cache. */
1664         LIST_FOREACH(cache_matcher, &priv->matchers, next) {
1665                 if (matcher->crc == cache_matcher->crc &&
1666                     matcher->priority == cache_matcher->priority &&
1667                     matcher->egress == cache_matcher->egress &&
1668                     !memcmp((const void *)matcher->mask.buf,
1669                             (const void *)cache_matcher->mask.buf,
1670                             cache_matcher->mask.size)) {
1671                         DRV_LOG(DEBUG,
1672                                 "priority %hd use %s matcher %p: refcnt %d++",
1673                                 cache_matcher->priority,
1674                                 cache_matcher->egress ? "tx" : "rx",
1675                                 (void *)cache_matcher,
1676                                 rte_atomic32_read(&cache_matcher->refcnt));
1677                         rte_atomic32_inc(&cache_matcher->refcnt);
1678                         dev_flow->dv.matcher = cache_matcher;
1679                         return 0;
1680                 }
1681         }
1682         /* Register new matcher. */
1683         cache_matcher = rte_calloc(__func__, 1, sizeof(*cache_matcher), 0);
1684         if (!cache_matcher)
1685                 return rte_flow_error_set(error, ENOMEM,
1686                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1687                                           "cannot allocate matcher memory");
1688         *cache_matcher = *matcher;
1689         dv_attr.match_criteria_enable =
1690                 flow_dv_matcher_enable(cache_matcher->mask.buf);
1691         dv_attr.priority = matcher->priority;
1692         if (matcher->egress)
1693                 dv_attr.flags |= IBV_FLOW_ATTR_FLAGS_EGRESS;
1694         cache_matcher->matcher_object =
1695                 mlx5_glue->dv_create_flow_matcher(priv->ctx, &dv_attr);
1696         if (!cache_matcher->matcher_object) {
1697                 rte_free(cache_matcher);
1698                 return rte_flow_error_set(error, ENOMEM,
1699                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1700                                           NULL, "cannot create matcher");
1701         }
1702         rte_atomic32_inc(&cache_matcher->refcnt);
1703         LIST_INSERT_HEAD(&priv->matchers, cache_matcher, next);
1704         dev_flow->dv.matcher = cache_matcher;
1705         DRV_LOG(DEBUG, "priority %hd new %s matcher %p: refcnt %d",
1706                 cache_matcher->priority,
1707                 cache_matcher->egress ? "tx" : "rx", (void *)cache_matcher,
1708                 rte_atomic32_read(&cache_matcher->refcnt));
1709         return 0;
1710 }
1711
1712 /**
1713  * Fill the flow with DV spec.
1714  *
1715  * @param[in] dev
1716  *   Pointer to rte_eth_dev structure.
1717  * @param[in, out] dev_flow
1718  *   Pointer to the sub flow.
1719  * @param[in] attr
1720  *   Pointer to the flow attributes.
1721  * @param[in] items
1722  *   Pointer to the list of items.
1723  * @param[in] actions
1724  *   Pointer to the list of actions.
1725  * @param[out] error
1726  *   Pointer to the error structure.
1727  *
1728  * @return
1729  *   0 on success, a negative errno value otherwise and rte_ernno is set.
1730  */
1731 static int
1732 flow_dv_translate(struct rte_eth_dev *dev,
1733                   struct mlx5_flow *dev_flow,
1734                   const struct rte_flow_attr *attr,
1735                   const struct rte_flow_item items[],
1736                   const struct rte_flow_action actions[],
1737                   struct rte_flow_error *error)
1738 {
1739         struct priv *priv = dev->data->dev_private;
1740         struct rte_flow *flow = dev_flow->flow;
1741         uint64_t item_flags = 0;
1742         uint64_t action_flags = 0;
1743         uint64_t priority = attr->priority;
1744         struct mlx5_flow_dv_matcher matcher = {
1745                 .mask = {
1746                         .size = sizeof(matcher.mask.buf),
1747                 },
1748         };
1749         int actions_n = 0;
1750
1751         if (priority == MLX5_FLOW_PRIO_RSVD)
1752                 priority = priv->config.flow_prio - 1;
1753         for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
1754                 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1755                 void *match_mask = matcher.mask.buf;
1756                 void *match_value = dev_flow->dv.value.buf;
1757
1758                 switch (items->type) {
1759                 case RTE_FLOW_ITEM_TYPE_ETH:
1760                         flow_dv_translate_item_eth(match_mask, match_value,
1761                                                    items, tunnel);
1762                         matcher.priority = MLX5_PRIORITY_MAP_L2;
1763                         item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
1764                                                MLX5_FLOW_LAYER_OUTER_L2;
1765                         break;
1766                 case RTE_FLOW_ITEM_TYPE_VLAN:
1767                         flow_dv_translate_item_vlan(match_mask, match_value,
1768                                                     items, tunnel);
1769                         matcher.priority = MLX5_PRIORITY_MAP_L2;
1770                         item_flags |= tunnel ? (MLX5_FLOW_LAYER_INNER_L2 |
1771                                                 MLX5_FLOW_LAYER_INNER_VLAN) :
1772                                                (MLX5_FLOW_LAYER_OUTER_L2 |
1773                                                 MLX5_FLOW_LAYER_OUTER_VLAN);
1774                         break;
1775                 case RTE_FLOW_ITEM_TYPE_IPV4:
1776                         flow_dv_translate_item_ipv4(match_mask, match_value,
1777                                                     items, tunnel);
1778                         matcher.priority = MLX5_PRIORITY_MAP_L3;
1779                         dev_flow->dv.hash_fields |=
1780                                 mlx5_flow_hashfields_adjust
1781                                         (dev_flow, tunnel,
1782                                          MLX5_IPV4_LAYER_TYPES,
1783                                          MLX5_IPV4_IBV_RX_HASH);
1784                         item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
1785                                                MLX5_FLOW_LAYER_OUTER_L3_IPV4;
1786                         break;
1787                 case RTE_FLOW_ITEM_TYPE_IPV6:
1788                         flow_dv_translate_item_ipv6(match_mask, match_value,
1789                                                     items, tunnel);
1790                         matcher.priority = MLX5_PRIORITY_MAP_L3;
1791                         dev_flow->dv.hash_fields |=
1792                                 mlx5_flow_hashfields_adjust
1793                                         (dev_flow, tunnel,
1794                                          MLX5_IPV6_LAYER_TYPES,
1795                                          MLX5_IPV6_IBV_RX_HASH);
1796                         item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
1797                                                MLX5_FLOW_LAYER_OUTER_L3_IPV6;
1798                         break;
1799                 case RTE_FLOW_ITEM_TYPE_TCP:
1800                         flow_dv_translate_item_tcp(match_mask, match_value,
1801                                                    items, tunnel);
1802                         matcher.priority = MLX5_PRIORITY_MAP_L4;
1803                         dev_flow->dv.hash_fields |=
1804                                 mlx5_flow_hashfields_adjust
1805                                         (dev_flow, tunnel, ETH_RSS_TCP,
1806                                          IBV_RX_HASH_SRC_PORT_TCP |
1807                                          IBV_RX_HASH_DST_PORT_TCP);
1808                         item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
1809                                                MLX5_FLOW_LAYER_OUTER_L4_TCP;
1810                         break;
1811                 case RTE_FLOW_ITEM_TYPE_UDP:
1812                         flow_dv_translate_item_udp(match_mask, match_value,
1813                                                    items, tunnel);
1814                         matcher.priority = MLX5_PRIORITY_MAP_L4;
1815                         dev_flow->verbs.hash_fields |=
1816                                 mlx5_flow_hashfields_adjust
1817                                         (dev_flow, tunnel, ETH_RSS_UDP,
1818                                          IBV_RX_HASH_SRC_PORT_UDP |
1819                                          IBV_RX_HASH_DST_PORT_UDP);
1820                         item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
1821                                                MLX5_FLOW_LAYER_OUTER_L4_UDP;
1822                         break;
1823                 case RTE_FLOW_ITEM_TYPE_GRE:
1824                         flow_dv_translate_item_gre(match_mask, match_value,
1825                                                    items, tunnel);
1826                         item_flags |= MLX5_FLOW_LAYER_GRE;
1827                         break;
1828                 case RTE_FLOW_ITEM_TYPE_NVGRE:
1829                         flow_dv_translate_item_nvgre(match_mask, match_value,
1830                                                      items, tunnel);
1831                         item_flags |= MLX5_FLOW_LAYER_GRE;
1832                         break;
1833                 case RTE_FLOW_ITEM_TYPE_VXLAN:
1834                         flow_dv_translate_item_vxlan(match_mask, match_value,
1835                                                      items, tunnel);
1836                         item_flags |= MLX5_FLOW_LAYER_VXLAN;
1837                         break;
1838                 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
1839                         flow_dv_translate_item_vxlan(match_mask, match_value,
1840                                                      items, tunnel);
1841                         item_flags |= MLX5_FLOW_LAYER_VXLAN_GPE;
1842                         break;
1843                 case RTE_FLOW_ITEM_TYPE_META:
1844                         flow_dv_translate_item_meta(match_mask, match_value,
1845                                                     items);
1846                         item_flags |= MLX5_FLOW_ITEM_METADATA;
1847                         break;
1848                 default:
1849                         break;
1850                 }
1851         }
1852         dev_flow->layers = item_flags;
1853         /* Register matcher. */
1854         matcher.crc = rte_raw_cksum((const void *)matcher.mask.buf,
1855                                     matcher.mask.size);
1856         matcher.priority = mlx5_flow_adjust_priority(dev, priority,
1857                                                      matcher.priority);
1858         matcher.egress = attr->egress;
1859         if (flow_dv_matcher_register(dev, &matcher, dev_flow, error))
1860                 return -rte_errno;
1861         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1862                 const struct rte_flow_action_queue *queue;
1863                 const struct rte_flow_action_rss *rss;
1864                 const struct rte_flow_action *action = actions;
1865                 const uint8_t *rss_key;
1866
1867                 switch (actions->type) {
1868                 case RTE_FLOW_ACTION_TYPE_VOID:
1869                         break;
1870                 case RTE_FLOW_ACTION_TYPE_FLAG:
1871                         dev_flow->dv.actions[actions_n].type =
1872                                 MLX5DV_FLOW_ACTION_TAG;
1873                         dev_flow->dv.actions[actions_n].tag_value =
1874                                 mlx5_flow_mark_set(MLX5_FLOW_MARK_DEFAULT);
1875                         actions_n++;
1876                         action_flags |= MLX5_FLOW_ACTION_FLAG;
1877                         break;
1878                 case RTE_FLOW_ACTION_TYPE_MARK:
1879                         dev_flow->dv.actions[actions_n].type =
1880                                 MLX5DV_FLOW_ACTION_TAG;
1881                         dev_flow->dv.actions[actions_n].tag_value =
1882                                 mlx5_flow_mark_set
1883                                 (((const struct rte_flow_action_mark *)
1884                                   (actions->conf))->id);
1885                         actions_n++;
1886                         action_flags |= MLX5_FLOW_ACTION_MARK;
1887                         break;
1888                 case RTE_FLOW_ACTION_TYPE_DROP:
1889                         dev_flow->dv.actions[actions_n].type =
1890                                 MLX5DV_FLOW_ACTION_DROP;
1891                         action_flags |= MLX5_FLOW_ACTION_DROP;
1892                         break;
1893                 case RTE_FLOW_ACTION_TYPE_QUEUE:
1894                         queue = actions->conf;
1895                         flow->rss.queue_num = 1;
1896                         (*flow->queue)[0] = queue->index;
1897                         action_flags |= MLX5_FLOW_ACTION_QUEUE;
1898                         break;
1899                 case RTE_FLOW_ACTION_TYPE_RSS:
1900                         rss = actions->conf;
1901                         if (flow->queue)
1902                                 memcpy((*flow->queue), rss->queue,
1903                                        rss->queue_num * sizeof(uint16_t));
1904                         flow->rss.queue_num = rss->queue_num;
1905                         /* NULL RSS key indicates default RSS key. */
1906                         rss_key = !rss->key ? rss_hash_default_key : rss->key;
1907                         memcpy(flow->key, rss_key, MLX5_RSS_HASH_KEY_LEN);
1908                         /* RSS type 0 indicates default RSS type ETH_RSS_IP. */
1909                         flow->rss.types = !rss->types ? ETH_RSS_IP : rss->types;
1910                         flow->rss.level = rss->level;
1911                         action_flags |= MLX5_FLOW_ACTION_RSS;
1912                         break;
1913                 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
1914                 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
1915                         if (flow_dv_create_action_l2_encap(dev, actions,
1916                                                            dev_flow, error))
1917                                 return -rte_errno;
1918                         dev_flow->dv.actions[actions_n].type =
1919                                 MLX5DV_FLOW_ACTION_IBV_FLOW_ACTION;
1920                         dev_flow->dv.actions[actions_n].action =
1921                                 dev_flow->dv.encap_decap->verbs_action;
1922                         actions_n++;
1923                         action_flags |= actions->type ==
1924                                         RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP ?
1925                                         MLX5_FLOW_ACTION_VXLAN_ENCAP :
1926                                         MLX5_FLOW_ACTION_NVGRE_ENCAP;
1927                         break;
1928                 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
1929                 case RTE_FLOW_ACTION_TYPE_NVGRE_DECAP:
1930                         if (flow_dv_create_action_l2_decap(dev, dev_flow,
1931                                                            error))
1932                                 return -rte_errno;
1933                         dev_flow->dv.actions[actions_n].type =
1934                                 MLX5DV_FLOW_ACTION_IBV_FLOW_ACTION;
1935                         dev_flow->dv.actions[actions_n].action =
1936                                 dev_flow->dv.encap_decap->verbs_action;
1937                         actions_n++;
1938                         action_flags |= actions->type ==
1939                                         RTE_FLOW_ACTION_TYPE_VXLAN_DECAP ?
1940                                         MLX5_FLOW_ACTION_VXLAN_DECAP :
1941                                         MLX5_FLOW_ACTION_NVGRE_DECAP;
1942                         break;
1943                 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
1944                         /* Handle encap with preceding decap. */
1945                         if (action_flags & MLX5_FLOW_ACTION_RAW_DECAP) {
1946                                 if (flow_dv_create_action_raw_encap
1947                                         (dev, actions, dev_flow, attr, error))
1948                                         return -rte_errno;
1949                                 dev_flow->dv.actions[actions_n].type =
1950                                         MLX5DV_FLOW_ACTION_IBV_FLOW_ACTION;
1951                                 dev_flow->dv.actions[actions_n].action =
1952                                         dev_flow->dv.encap_decap->verbs_action;
1953                         } else {
1954                                 /* Handle encap without preceding decap. */
1955                                 if (flow_dv_create_action_l2_encap(dev, actions,
1956                                                                    dev_flow,
1957                                                                    error))
1958                                         return -rte_errno;
1959                                 dev_flow->dv.actions[actions_n].type =
1960                                         MLX5DV_FLOW_ACTION_IBV_FLOW_ACTION;
1961                                 dev_flow->dv.actions[actions_n].action =
1962                                         dev_flow->dv.encap_decap->verbs_action;
1963                         }
1964                         actions_n++;
1965                         action_flags |= MLX5_FLOW_ACTION_RAW_ENCAP;
1966                         break;
1967                 case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
1968                         /* Check if this decap is followed by encap. */
1969                         for (; action->type != RTE_FLOW_ACTION_TYPE_END &&
1970                                action->type != RTE_FLOW_ACTION_TYPE_RAW_ENCAP;
1971                                action++) {
1972                         }
1973                         /* Handle decap only if it isn't followed by encap. */
1974                         if (action->type != RTE_FLOW_ACTION_TYPE_RAW_ENCAP) {
1975                                 if (flow_dv_create_action_l2_decap(dev,
1976                                                                    dev_flow,
1977                                                                    error))
1978                                         return -rte_errno;
1979                                 dev_flow->dv.actions[actions_n].type =
1980                                         MLX5DV_FLOW_ACTION_IBV_FLOW_ACTION;
1981                                 dev_flow->dv.actions[actions_n].action =
1982                                         dev_flow->dv.encap_decap->verbs_action;
1983                                 actions_n++;
1984                         }
1985                         /* If decap is followed by encap, handle it at encap. */
1986                         action_flags |= MLX5_FLOW_ACTION_RAW_DECAP;
1987                         break;
1988                 default:
1989                         break;
1990                 }
1991         }
1992         dev_flow->dv.actions_n = actions_n;
1993         flow->actions = action_flags;
1994         return 0;
1995 }
1996
1997 /**
1998  * Apply the flow to the NIC.
1999  *
2000  * @param[in] dev
2001  *   Pointer to the Ethernet device structure.
2002  * @param[in, out] flow
2003  *   Pointer to flow structure.
2004  * @param[out] error
2005  *   Pointer to error structure.
2006  *
2007  * @return
2008  *   0 on success, a negative errno value otherwise and rte_errno is set.
2009  */
2010 static int
2011 flow_dv_apply(struct rte_eth_dev *dev, struct rte_flow *flow,
2012               struct rte_flow_error *error)
2013 {
2014         struct mlx5_flow_dv *dv;
2015         struct mlx5_flow *dev_flow;
2016         int n;
2017         int err;
2018
2019         LIST_FOREACH(dev_flow, &flow->dev_flows, next) {
2020                 dv = &dev_flow->dv;
2021                 n = dv->actions_n;
2022                 if (flow->actions & MLX5_FLOW_ACTION_DROP) {
2023                         dv->hrxq = mlx5_hrxq_drop_new(dev);
2024                         if (!dv->hrxq) {
2025                                 rte_flow_error_set
2026                                         (error, errno,
2027                                          RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2028                                          "cannot get drop hash queue");
2029                                 goto error;
2030                         }
2031                         dv->actions[n].type = MLX5DV_FLOW_ACTION_DEST_IBV_QP;
2032                         dv->actions[n].qp = dv->hrxq->qp;
2033                         n++;
2034                 } else if (flow->actions &
2035                            (MLX5_FLOW_ACTION_QUEUE | MLX5_FLOW_ACTION_RSS)) {
2036                         struct mlx5_hrxq *hrxq;
2037                         hrxq = mlx5_hrxq_get(dev, flow->key,
2038                                              MLX5_RSS_HASH_KEY_LEN,
2039                                              dv->hash_fields,
2040                                              (*flow->queue),
2041                                              flow->rss.queue_num);
2042                         if (!hrxq)
2043                                 hrxq = mlx5_hrxq_new
2044                                         (dev, flow->key, MLX5_RSS_HASH_KEY_LEN,
2045                                          dv->hash_fields, (*flow->queue),
2046                                          flow->rss.queue_num,
2047                                          !!(dev_flow->layers &
2048                                             MLX5_FLOW_LAYER_TUNNEL));
2049                         if (!hrxq) {
2050                                 rte_flow_error_set
2051                                         (error, rte_errno,
2052                                          RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2053                                          "cannot get hash queue");
2054                                 goto error;
2055                         }
2056                         dv->hrxq = hrxq;
2057                         dv->actions[n].type = MLX5DV_FLOW_ACTION_DEST_IBV_QP;
2058                         dv->actions[n].qp = hrxq->qp;
2059                         n++;
2060                 }
2061                 dv->flow =
2062                         mlx5_glue->dv_create_flow(dv->matcher->matcher_object,
2063                                                   (void *)&dv->value, n,
2064                                                   dv->actions);
2065                 if (!dv->flow) {
2066                         rte_flow_error_set(error, errno,
2067                                            RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2068                                            NULL,
2069                                            "hardware refuses to create flow");
2070                         goto error;
2071                 }
2072         }
2073         return 0;
2074 error:
2075         err = rte_errno; /* Save rte_errno before cleanup. */
2076         LIST_FOREACH(dev_flow, &flow->dev_flows, next) {
2077                 struct mlx5_flow_dv *dv = &dev_flow->dv;
2078                 if (dv->hrxq) {
2079                         if (flow->actions & MLX5_FLOW_ACTION_DROP)
2080                                 mlx5_hrxq_drop_release(dev);
2081                         else
2082                                 mlx5_hrxq_release(dev, dv->hrxq);
2083                         dv->hrxq = NULL;
2084                 }
2085         }
2086         rte_errno = err; /* Restore rte_errno. */
2087         return -rte_errno;
2088 }
2089
2090 /**
2091  * Release the flow matcher.
2092  *
2093  * @param dev
2094  *   Pointer to Ethernet device.
2095  * @param flow
2096  *   Pointer to mlx5_flow.
2097  *
2098  * @return
2099  *   1 while a reference on it exists, 0 when freed.
2100  */
2101 static int
2102 flow_dv_matcher_release(struct rte_eth_dev *dev,
2103                         struct mlx5_flow *flow)
2104 {
2105         struct mlx5_flow_dv_matcher *matcher = flow->dv.matcher;
2106
2107         assert(matcher->matcher_object);
2108         DRV_LOG(DEBUG, "port %u matcher %p: refcnt %d--",
2109                 dev->data->port_id, (void *)matcher,
2110                 rte_atomic32_read(&matcher->refcnt));
2111         if (rte_atomic32_dec_and_test(&matcher->refcnt)) {
2112                 claim_zero(mlx5_glue->dv_destroy_flow_matcher
2113                            (matcher->matcher_object));
2114                 LIST_REMOVE(matcher, next);
2115                 rte_free(matcher);
2116                 DRV_LOG(DEBUG, "port %u matcher %p: removed",
2117                         dev->data->port_id, (void *)matcher);
2118                 return 0;
2119         }
2120         return 1;
2121 }
2122
2123 /**
2124  * Release an encap/decap resource.
2125  *
2126  * @param flow
2127  *   Pointer to mlx5_flow.
2128  *
2129  * @return
2130  *   1 while a reference on it exists, 0 when freed.
2131  */
2132 static int
2133 flow_dv_encap_decap_resource_release(struct mlx5_flow *flow)
2134 {
2135         struct mlx5_flow_dv_encap_decap_resource *cache_resource =
2136                                                 flow->dv.encap_decap;
2137
2138         assert(cache_resource->verbs_action);
2139         DRV_LOG(DEBUG, "encap/decap resource %p: refcnt %d--",
2140                 (void *)cache_resource,
2141                 rte_atomic32_read(&cache_resource->refcnt));
2142         if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
2143                 claim_zero(mlx5_glue->destroy_flow_action
2144                                 (cache_resource->verbs_action));
2145                 LIST_REMOVE(cache_resource, next);
2146                 rte_free(cache_resource);
2147                 DRV_LOG(DEBUG, "encap/decap resource %p: removed",
2148                         (void *)cache_resource);
2149                 return 0;
2150         }
2151         return 1;
2152 }
2153
2154 /**
2155  * Remove the flow from the NIC but keeps it in memory.
2156  *
2157  * @param[in] dev
2158  *   Pointer to Ethernet device.
2159  * @param[in, out] flow
2160  *   Pointer to flow structure.
2161  */
2162 static void
2163 flow_dv_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
2164 {
2165         struct mlx5_flow_dv *dv;
2166         struct mlx5_flow *dev_flow;
2167
2168         if (!flow)
2169                 return;
2170         LIST_FOREACH(dev_flow, &flow->dev_flows, next) {
2171                 dv = &dev_flow->dv;
2172                 if (dv->flow) {
2173                         claim_zero(mlx5_glue->destroy_flow(dv->flow));
2174                         dv->flow = NULL;
2175                 }
2176                 if (dv->hrxq) {
2177                         if (flow->actions & MLX5_FLOW_ACTION_DROP)
2178                                 mlx5_hrxq_drop_release(dev);
2179                         else
2180                                 mlx5_hrxq_release(dev, dv->hrxq);
2181                         dv->hrxq = NULL;
2182                 }
2183         }
2184         if (flow->counter)
2185                 flow->counter = NULL;
2186 }
2187
2188 /**
2189  * Remove the flow from the NIC and the memory.
2190  *
2191  * @param[in] dev
2192  *   Pointer to the Ethernet device structure.
2193  * @param[in, out] flow
2194  *   Pointer to flow structure.
2195  */
2196 static void
2197 flow_dv_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
2198 {
2199         struct mlx5_flow *dev_flow;
2200
2201         if (!flow)
2202                 return;
2203         flow_dv_remove(dev, flow);
2204         while (!LIST_EMPTY(&flow->dev_flows)) {
2205                 dev_flow = LIST_FIRST(&flow->dev_flows);
2206                 LIST_REMOVE(dev_flow, next);
2207                 if (dev_flow->dv.matcher)
2208                         flow_dv_matcher_release(dev, dev_flow);
2209                 if (dev_flow->dv.encap_decap)
2210                         flow_dv_encap_decap_resource_release(dev_flow);
2211                 rte_free(dev_flow);
2212         }
2213 }
2214
2215 /**
2216  * Query a flow.
2217  *
2218  * @see rte_flow_query()
2219  * @see rte_flow_ops
2220  */
2221 static int
2222 flow_dv_query(struct rte_eth_dev *dev __rte_unused,
2223               struct rte_flow *flow __rte_unused,
2224               const struct rte_flow_action *actions __rte_unused,
2225               void *data __rte_unused,
2226               struct rte_flow_error *error __rte_unused)
2227 {
2228         rte_errno = ENOTSUP;
2229         return -rte_errno;
2230 }
2231
2232
2233 const struct mlx5_flow_driver_ops mlx5_flow_dv_drv_ops = {
2234         .validate = flow_dv_validate,
2235         .prepare = flow_dv_prepare,
2236         .translate = flow_dv_translate,
2237         .apply = flow_dv_apply,
2238         .remove = flow_dv_remove,
2239         .destroy = flow_dv_destroy,
2240         .query = flow_dv_query,
2241 };
2242
2243 #endif /* HAVE_IBV_FLOW_DV_SUPPORT */