4c9e45fbb312308b2ae278ccfdc88d4839dc03b3
[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 #include <unistd.h>
10
11 #include <rte_common.h>
12 #include <rte_ether.h>
13 #include <rte_ethdev_driver.h>
14 #include <rte_flow.h>
15 #include <rte_flow_driver.h>
16 #include <rte_malloc.h>
17 #include <rte_cycles.h>
18 #include <rte_ip.h>
19 #include <rte_gre.h>
20 #include <rte_vxlan.h>
21 #include <rte_gtp.h>
22 #include <rte_eal_paging.h>
23 #include <rte_mpls.h>
24
25 #include <mlx5_glue.h>
26 #include <mlx5_devx_cmds.h>
27 #include <mlx5_prm.h>
28 #include <mlx5_malloc.h>
29
30 #include "mlx5_defs.h"
31 #include "mlx5.h"
32 #include "mlx5_common_os.h"
33 #include "mlx5_flow.h"
34 #include "mlx5_flow_os.h"
35 #include "mlx5_rxtx.h"
36 #include "rte_pmd_mlx5.h"
37
38 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
39
40 #ifndef HAVE_IBV_FLOW_DEVX_COUNTERS
41 #define MLX5DV_FLOW_ACTION_COUNTERS_DEVX 0
42 #endif
43
44 #ifndef HAVE_MLX5DV_DR_ESWITCH
45 #ifndef MLX5DV_FLOW_TABLE_TYPE_FDB
46 #define MLX5DV_FLOW_TABLE_TYPE_FDB 0
47 #endif
48 #endif
49
50 #ifndef HAVE_MLX5DV_DR
51 #define MLX5DV_DR_ACTION_FLAGS_ROOT_LEVEL 1
52 #endif
53
54 /* VLAN header definitions */
55 #define MLX5DV_FLOW_VLAN_PCP_SHIFT 13
56 #define MLX5DV_FLOW_VLAN_PCP_MASK (0x7 << MLX5DV_FLOW_VLAN_PCP_SHIFT)
57 #define MLX5DV_FLOW_VLAN_VID_MASK 0x0fff
58 #define MLX5DV_FLOW_VLAN_PCP_MASK_BE RTE_BE16(MLX5DV_FLOW_VLAN_PCP_MASK)
59 #define MLX5DV_FLOW_VLAN_VID_MASK_BE RTE_BE16(MLX5DV_FLOW_VLAN_VID_MASK)
60
61 union flow_dv_attr {
62         struct {
63                 uint32_t valid:1;
64                 uint32_t ipv4:1;
65                 uint32_t ipv6:1;
66                 uint32_t tcp:1;
67                 uint32_t udp:1;
68                 uint32_t reserved:27;
69         };
70         uint32_t attr;
71 };
72
73 static int
74 flow_dv_tbl_resource_release(struct rte_eth_dev *dev,
75                              struct mlx5_flow_tbl_resource *tbl);
76
77 static int
78 flow_dv_encap_decap_resource_release(struct rte_eth_dev *dev,
79                                       uint32_t encap_decap_idx);
80
81 static int
82 flow_dv_port_id_action_resource_release(struct rte_eth_dev *dev,
83                                         uint32_t port_id);
84
85 /**
86  * Initialize flow attributes structure according to flow items' types.
87  *
88  * flow_dv_validate() avoids multiple L3/L4 layers cases other than tunnel
89  * mode. For tunnel mode, the items to be modified are the outermost ones.
90  *
91  * @param[in] item
92  *   Pointer to item specification.
93  * @param[out] attr
94  *   Pointer to flow attributes structure.
95  * @param[in] dev_flow
96  *   Pointer to the sub flow.
97  * @param[in] tunnel_decap
98  *   Whether action is after tunnel decapsulation.
99  */
100 static void
101 flow_dv_attr_init(const struct rte_flow_item *item, union flow_dv_attr *attr,
102                   struct mlx5_flow *dev_flow, bool tunnel_decap)
103 {
104         uint64_t layers = dev_flow->handle->layers;
105
106         /*
107          * If layers is already initialized, it means this dev_flow is the
108          * suffix flow, the layers flags is set by the prefix flow. Need to
109          * use the layer flags from prefix flow as the suffix flow may not
110          * have the user defined items as the flow is split.
111          */
112         if (layers) {
113                 if (layers & MLX5_FLOW_LAYER_OUTER_L3_IPV4)
114                         attr->ipv4 = 1;
115                 else if (layers & MLX5_FLOW_LAYER_OUTER_L3_IPV6)
116                         attr->ipv6 = 1;
117                 if (layers & MLX5_FLOW_LAYER_OUTER_L4_TCP)
118                         attr->tcp = 1;
119                 else if (layers & MLX5_FLOW_LAYER_OUTER_L4_UDP)
120                         attr->udp = 1;
121                 attr->valid = 1;
122                 return;
123         }
124         for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
125                 uint8_t next_protocol = 0xff;
126                 switch (item->type) {
127                 case RTE_FLOW_ITEM_TYPE_GRE:
128                 case RTE_FLOW_ITEM_TYPE_NVGRE:
129                 case RTE_FLOW_ITEM_TYPE_VXLAN:
130                 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
131                 case RTE_FLOW_ITEM_TYPE_GENEVE:
132                 case RTE_FLOW_ITEM_TYPE_MPLS:
133                         if (tunnel_decap)
134                                 attr->attr = 0;
135                         break;
136                 case RTE_FLOW_ITEM_TYPE_IPV4:
137                         if (!attr->ipv6)
138                                 attr->ipv4 = 1;
139                         if (item->mask != NULL &&
140                             ((const struct rte_flow_item_ipv4 *)
141                             item->mask)->hdr.next_proto_id)
142                                 next_protocol =
143                                     ((const struct rte_flow_item_ipv4 *)
144                                       (item->spec))->hdr.next_proto_id &
145                                     ((const struct rte_flow_item_ipv4 *)
146                                       (item->mask))->hdr.next_proto_id;
147                         if ((next_protocol == IPPROTO_IPIP ||
148                             next_protocol == IPPROTO_IPV6) && tunnel_decap)
149                                 attr->attr = 0;
150                         break;
151                 case RTE_FLOW_ITEM_TYPE_IPV6:
152                         if (!attr->ipv4)
153                                 attr->ipv6 = 1;
154                         if (item->mask != NULL &&
155                             ((const struct rte_flow_item_ipv6 *)
156                             item->mask)->hdr.proto)
157                                 next_protocol =
158                                     ((const struct rte_flow_item_ipv6 *)
159                                       (item->spec))->hdr.proto &
160                                     ((const struct rte_flow_item_ipv6 *)
161                                       (item->mask))->hdr.proto;
162                         if ((next_protocol == IPPROTO_IPIP ||
163                             next_protocol == IPPROTO_IPV6) && tunnel_decap)
164                                 attr->attr = 0;
165                         break;
166                 case RTE_FLOW_ITEM_TYPE_UDP:
167                         if (!attr->tcp)
168                                 attr->udp = 1;
169                         break;
170                 case RTE_FLOW_ITEM_TYPE_TCP:
171                         if (!attr->udp)
172                                 attr->tcp = 1;
173                         break;
174                 default:
175                         break;
176                 }
177         }
178         attr->valid = 1;
179 }
180
181 /**
182  * Convert rte_mtr_color to mlx5 color.
183  *
184  * @param[in] rcol
185  *   rte_mtr_color.
186  *
187  * @return
188  *   mlx5 color.
189  */
190 static int
191 rte_col_2_mlx5_col(enum rte_color rcol)
192 {
193         switch (rcol) {
194         case RTE_COLOR_GREEN:
195                 return MLX5_FLOW_COLOR_GREEN;
196         case RTE_COLOR_YELLOW:
197                 return MLX5_FLOW_COLOR_YELLOW;
198         case RTE_COLOR_RED:
199                 return MLX5_FLOW_COLOR_RED;
200         default:
201                 break;
202         }
203         return MLX5_FLOW_COLOR_UNDEFINED;
204 }
205
206 struct field_modify_info {
207         uint32_t size; /* Size of field in protocol header, in bytes. */
208         uint32_t offset; /* Offset of field in protocol header, in bytes. */
209         enum mlx5_modification_field id;
210 };
211
212 struct field_modify_info modify_eth[] = {
213         {4,  0, MLX5_MODI_OUT_DMAC_47_16},
214         {2,  4, MLX5_MODI_OUT_DMAC_15_0},
215         {4,  6, MLX5_MODI_OUT_SMAC_47_16},
216         {2, 10, MLX5_MODI_OUT_SMAC_15_0},
217         {0, 0, 0},
218 };
219
220 struct field_modify_info modify_vlan_out_first_vid[] = {
221         /* Size in bits !!! */
222         {12, 0, MLX5_MODI_OUT_FIRST_VID},
223         {0, 0, 0},
224 };
225
226 struct field_modify_info modify_ipv4[] = {
227         {1,  1, MLX5_MODI_OUT_IP_DSCP},
228         {1,  8, MLX5_MODI_OUT_IPV4_TTL},
229         {4, 12, MLX5_MODI_OUT_SIPV4},
230         {4, 16, MLX5_MODI_OUT_DIPV4},
231         {0, 0, 0},
232 };
233
234 struct field_modify_info modify_ipv6[] = {
235         {1,  0, MLX5_MODI_OUT_IP_DSCP},
236         {1,  7, MLX5_MODI_OUT_IPV6_HOPLIMIT},
237         {4,  8, MLX5_MODI_OUT_SIPV6_127_96},
238         {4, 12, MLX5_MODI_OUT_SIPV6_95_64},
239         {4, 16, MLX5_MODI_OUT_SIPV6_63_32},
240         {4, 20, MLX5_MODI_OUT_SIPV6_31_0},
241         {4, 24, MLX5_MODI_OUT_DIPV6_127_96},
242         {4, 28, MLX5_MODI_OUT_DIPV6_95_64},
243         {4, 32, MLX5_MODI_OUT_DIPV6_63_32},
244         {4, 36, MLX5_MODI_OUT_DIPV6_31_0},
245         {0, 0, 0},
246 };
247
248 struct field_modify_info modify_udp[] = {
249         {2, 0, MLX5_MODI_OUT_UDP_SPORT},
250         {2, 2, MLX5_MODI_OUT_UDP_DPORT},
251         {0, 0, 0},
252 };
253
254 struct field_modify_info modify_tcp[] = {
255         {2, 0, MLX5_MODI_OUT_TCP_SPORT},
256         {2, 2, MLX5_MODI_OUT_TCP_DPORT},
257         {4, 4, MLX5_MODI_OUT_TCP_SEQ_NUM},
258         {4, 8, MLX5_MODI_OUT_TCP_ACK_NUM},
259         {0, 0, 0},
260 };
261
262 static void
263 mlx5_flow_tunnel_ip_check(const struct rte_flow_item *item __rte_unused,
264                           uint8_t next_protocol, uint64_t *item_flags,
265                           int *tunnel)
266 {
267         MLX5_ASSERT(item->type == RTE_FLOW_ITEM_TYPE_IPV4 ||
268                     item->type == RTE_FLOW_ITEM_TYPE_IPV6);
269         if (next_protocol == IPPROTO_IPIP) {
270                 *item_flags |= MLX5_FLOW_LAYER_IPIP;
271                 *tunnel = 1;
272         }
273         if (next_protocol == IPPROTO_IPV6) {
274                 *item_flags |= MLX5_FLOW_LAYER_IPV6_ENCAP;
275                 *tunnel = 1;
276         }
277 }
278
279 /**
280  * Acquire the synchronizing object to protect multithreaded access
281  * to shared dv context. Lock occurs only if context is actually
282  * shared, i.e. we have multiport IB device and representors are
283  * created.
284  *
285  * @param[in] dev
286  *   Pointer to the rte_eth_dev structure.
287  */
288 static void
289 flow_dv_shared_lock(struct rte_eth_dev *dev)
290 {
291         struct mlx5_priv *priv = dev->data->dev_private;
292         struct mlx5_dev_ctx_shared *sh = priv->sh;
293
294         if (sh->refcnt > 1) {
295                 int ret;
296
297                 ret = pthread_mutex_lock(&sh->dv_mutex);
298                 MLX5_ASSERT(!ret);
299                 (void)ret;
300         }
301 }
302
303 static void
304 flow_dv_shared_unlock(struct rte_eth_dev *dev)
305 {
306         struct mlx5_priv *priv = dev->data->dev_private;
307         struct mlx5_dev_ctx_shared *sh = priv->sh;
308
309         if (sh->refcnt > 1) {
310                 int ret;
311
312                 ret = pthread_mutex_unlock(&sh->dv_mutex);
313                 MLX5_ASSERT(!ret);
314                 (void)ret;
315         }
316 }
317
318 /* Update VLAN's VID/PCP based on input rte_flow_action.
319  *
320  * @param[in] action
321  *   Pointer to struct rte_flow_action.
322  * @param[out] vlan
323  *   Pointer to struct rte_vlan_hdr.
324  */
325 static void
326 mlx5_update_vlan_vid_pcp(const struct rte_flow_action *action,
327                          struct rte_vlan_hdr *vlan)
328 {
329         uint16_t vlan_tci;
330         if (action->type == RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP) {
331                 vlan_tci =
332                     ((const struct rte_flow_action_of_set_vlan_pcp *)
333                                                action->conf)->vlan_pcp;
334                 vlan_tci = vlan_tci << MLX5DV_FLOW_VLAN_PCP_SHIFT;
335                 vlan->vlan_tci &= ~MLX5DV_FLOW_VLAN_PCP_MASK;
336                 vlan->vlan_tci |= vlan_tci;
337         } else if (action->type == RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID) {
338                 vlan->vlan_tci &= ~MLX5DV_FLOW_VLAN_VID_MASK;
339                 vlan->vlan_tci |= rte_be_to_cpu_16
340                     (((const struct rte_flow_action_of_set_vlan_vid *)
341                                              action->conf)->vlan_vid);
342         }
343 }
344
345 /**
346  * Fetch 1, 2, 3 or 4 byte field from the byte array
347  * and return as unsigned integer in host-endian format.
348  *
349  * @param[in] data
350  *   Pointer to data array.
351  * @param[in] size
352  *   Size of field to extract.
353  *
354  * @return
355  *   converted field in host endian format.
356  */
357 static inline uint32_t
358 flow_dv_fetch_field(const uint8_t *data, uint32_t size)
359 {
360         uint32_t ret;
361
362         switch (size) {
363         case 1:
364                 ret = *data;
365                 break;
366         case 2:
367                 ret = rte_be_to_cpu_16(*(const unaligned_uint16_t *)data);
368                 break;
369         case 3:
370                 ret = rte_be_to_cpu_16(*(const unaligned_uint16_t *)data);
371                 ret = (ret << 8) | *(data + sizeof(uint16_t));
372                 break;
373         case 4:
374                 ret = rte_be_to_cpu_32(*(const unaligned_uint32_t *)data);
375                 break;
376         default:
377                 MLX5_ASSERT(false);
378                 ret = 0;
379                 break;
380         }
381         return ret;
382 }
383
384 /**
385  * Convert modify-header action to DV specification.
386  *
387  * Data length of each action is determined by provided field description
388  * and the item mask. Data bit offset and width of each action is determined
389  * by provided item mask.
390  *
391  * @param[in] item
392  *   Pointer to item specification.
393  * @param[in] field
394  *   Pointer to field modification information.
395  *     For MLX5_MODIFICATION_TYPE_SET specifies destination field.
396  *     For MLX5_MODIFICATION_TYPE_ADD specifies destination field.
397  *     For MLX5_MODIFICATION_TYPE_COPY specifies source field.
398  * @param[in] dcopy
399  *   Destination field info for MLX5_MODIFICATION_TYPE_COPY in @type.
400  *   Negative offset value sets the same offset as source offset.
401  *   size field is ignored, value is taken from source field.
402  * @param[in,out] resource
403  *   Pointer to the modify-header resource.
404  * @param[in] type
405  *   Type of modification.
406  * @param[out] error
407  *   Pointer to the error structure.
408  *
409  * @return
410  *   0 on success, a negative errno value otherwise and rte_errno is set.
411  */
412 static int
413 flow_dv_convert_modify_action(struct rte_flow_item *item,
414                               struct field_modify_info *field,
415                               struct field_modify_info *dcopy,
416                               struct mlx5_flow_dv_modify_hdr_resource *resource,
417                               uint32_t type, struct rte_flow_error *error)
418 {
419         uint32_t i = resource->actions_num;
420         struct mlx5_modification_cmd *actions = resource->actions;
421
422         /*
423          * The item and mask are provided in big-endian format.
424          * The fields should be presented as in big-endian format either.
425          * Mask must be always present, it defines the actual field width.
426          */
427         MLX5_ASSERT(item->mask);
428         MLX5_ASSERT(field->size);
429         do {
430                 unsigned int size_b;
431                 unsigned int off_b;
432                 uint32_t mask;
433                 uint32_t data;
434
435                 if (i >= MLX5_MAX_MODIFY_NUM)
436                         return rte_flow_error_set(error, EINVAL,
437                                  RTE_FLOW_ERROR_TYPE_ACTION, NULL,
438                                  "too many items to modify");
439                 /* Fetch variable byte size mask from the array. */
440                 mask = flow_dv_fetch_field((const uint8_t *)item->mask +
441                                            field->offset, field->size);
442                 if (!mask) {
443                         ++field;
444                         continue;
445                 }
446                 /* Deduce actual data width in bits from mask value. */
447                 off_b = rte_bsf32(mask);
448                 size_b = sizeof(uint32_t) * CHAR_BIT -
449                          off_b - __builtin_clz(mask);
450                 MLX5_ASSERT(size_b);
451                 size_b = size_b == sizeof(uint32_t) * CHAR_BIT ? 0 : size_b;
452                 actions[i] = (struct mlx5_modification_cmd) {
453                         .action_type = type,
454                         .field = field->id,
455                         .offset = off_b,
456                         .length = size_b,
457                 };
458                 /* Convert entire record to expected big-endian format. */
459                 actions[i].data0 = rte_cpu_to_be_32(actions[i].data0);
460                 if (type == MLX5_MODIFICATION_TYPE_COPY) {
461                         MLX5_ASSERT(dcopy);
462                         actions[i].dst_field = dcopy->id;
463                         actions[i].dst_offset =
464                                 (int)dcopy->offset < 0 ? off_b : dcopy->offset;
465                         /* Convert entire record to big-endian format. */
466                         actions[i].data1 = rte_cpu_to_be_32(actions[i].data1);
467                 } else {
468                         MLX5_ASSERT(item->spec);
469                         data = flow_dv_fetch_field((const uint8_t *)item->spec +
470                                                    field->offset, field->size);
471                         /* Shift out the trailing masked bits from data. */
472                         data = (data & mask) >> off_b;
473                         actions[i].data1 = rte_cpu_to_be_32(data);
474                 }
475                 ++i;
476                 ++field;
477         } while (field->size);
478         if (resource->actions_num == i)
479                 return rte_flow_error_set(error, EINVAL,
480                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
481                                           "invalid modification flow item");
482         resource->actions_num = i;
483         return 0;
484 }
485
486 /**
487  * Convert modify-header set IPv4 address action to DV specification.
488  *
489  * @param[in,out] resource
490  *   Pointer to the modify-header resource.
491  * @param[in] action
492  *   Pointer to action specification.
493  * @param[out] error
494  *   Pointer to the error structure.
495  *
496  * @return
497  *   0 on success, a negative errno value otherwise and rte_errno is set.
498  */
499 static int
500 flow_dv_convert_action_modify_ipv4
501                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
502                          const struct rte_flow_action *action,
503                          struct rte_flow_error *error)
504 {
505         const struct rte_flow_action_set_ipv4 *conf =
506                 (const struct rte_flow_action_set_ipv4 *)(action->conf);
507         struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_IPV4 };
508         struct rte_flow_item_ipv4 ipv4;
509         struct rte_flow_item_ipv4 ipv4_mask;
510
511         memset(&ipv4, 0, sizeof(ipv4));
512         memset(&ipv4_mask, 0, sizeof(ipv4_mask));
513         if (action->type == RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC) {
514                 ipv4.hdr.src_addr = conf->ipv4_addr;
515                 ipv4_mask.hdr.src_addr = rte_flow_item_ipv4_mask.hdr.src_addr;
516         } else {
517                 ipv4.hdr.dst_addr = conf->ipv4_addr;
518                 ipv4_mask.hdr.dst_addr = rte_flow_item_ipv4_mask.hdr.dst_addr;
519         }
520         item.spec = &ipv4;
521         item.mask = &ipv4_mask;
522         return flow_dv_convert_modify_action(&item, modify_ipv4, NULL, resource,
523                                              MLX5_MODIFICATION_TYPE_SET, error);
524 }
525
526 /**
527  * Convert modify-header set IPv6 address action to DV specification.
528  *
529  * @param[in,out] resource
530  *   Pointer to the modify-header resource.
531  * @param[in] action
532  *   Pointer to action specification.
533  * @param[out] error
534  *   Pointer to the error structure.
535  *
536  * @return
537  *   0 on success, a negative errno value otherwise and rte_errno is set.
538  */
539 static int
540 flow_dv_convert_action_modify_ipv6
541                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
542                          const struct rte_flow_action *action,
543                          struct rte_flow_error *error)
544 {
545         const struct rte_flow_action_set_ipv6 *conf =
546                 (const struct rte_flow_action_set_ipv6 *)(action->conf);
547         struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_IPV6 };
548         struct rte_flow_item_ipv6 ipv6;
549         struct rte_flow_item_ipv6 ipv6_mask;
550
551         memset(&ipv6, 0, sizeof(ipv6));
552         memset(&ipv6_mask, 0, sizeof(ipv6_mask));
553         if (action->type == RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC) {
554                 memcpy(&ipv6.hdr.src_addr, &conf->ipv6_addr,
555                        sizeof(ipv6.hdr.src_addr));
556                 memcpy(&ipv6_mask.hdr.src_addr,
557                        &rte_flow_item_ipv6_mask.hdr.src_addr,
558                        sizeof(ipv6.hdr.src_addr));
559         } else {
560                 memcpy(&ipv6.hdr.dst_addr, &conf->ipv6_addr,
561                        sizeof(ipv6.hdr.dst_addr));
562                 memcpy(&ipv6_mask.hdr.dst_addr,
563                        &rte_flow_item_ipv6_mask.hdr.dst_addr,
564                        sizeof(ipv6.hdr.dst_addr));
565         }
566         item.spec = &ipv6;
567         item.mask = &ipv6_mask;
568         return flow_dv_convert_modify_action(&item, modify_ipv6, NULL, resource,
569                                              MLX5_MODIFICATION_TYPE_SET, error);
570 }
571
572 /**
573  * Convert modify-header set MAC address action to DV specification.
574  *
575  * @param[in,out] resource
576  *   Pointer to the modify-header resource.
577  * @param[in] action
578  *   Pointer to action specification.
579  * @param[out] error
580  *   Pointer to the error structure.
581  *
582  * @return
583  *   0 on success, a negative errno value otherwise and rte_errno is set.
584  */
585 static int
586 flow_dv_convert_action_modify_mac
587                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
588                          const struct rte_flow_action *action,
589                          struct rte_flow_error *error)
590 {
591         const struct rte_flow_action_set_mac *conf =
592                 (const struct rte_flow_action_set_mac *)(action->conf);
593         struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_ETH };
594         struct rte_flow_item_eth eth;
595         struct rte_flow_item_eth eth_mask;
596
597         memset(&eth, 0, sizeof(eth));
598         memset(&eth_mask, 0, sizeof(eth_mask));
599         if (action->type == RTE_FLOW_ACTION_TYPE_SET_MAC_SRC) {
600                 memcpy(&eth.src.addr_bytes, &conf->mac_addr,
601                        sizeof(eth.src.addr_bytes));
602                 memcpy(&eth_mask.src.addr_bytes,
603                        &rte_flow_item_eth_mask.src.addr_bytes,
604                        sizeof(eth_mask.src.addr_bytes));
605         } else {
606                 memcpy(&eth.dst.addr_bytes, &conf->mac_addr,
607                        sizeof(eth.dst.addr_bytes));
608                 memcpy(&eth_mask.dst.addr_bytes,
609                        &rte_flow_item_eth_mask.dst.addr_bytes,
610                        sizeof(eth_mask.dst.addr_bytes));
611         }
612         item.spec = &eth;
613         item.mask = &eth_mask;
614         return flow_dv_convert_modify_action(&item, modify_eth, NULL, resource,
615                                              MLX5_MODIFICATION_TYPE_SET, error);
616 }
617
618 /**
619  * Convert modify-header set VLAN VID action to DV specification.
620  *
621  * @param[in,out] resource
622  *   Pointer to the modify-header resource.
623  * @param[in] action
624  *   Pointer to action specification.
625  * @param[out] error
626  *   Pointer to the error structure.
627  *
628  * @return
629  *   0 on success, a negative errno value otherwise and rte_errno is set.
630  */
631 static int
632 flow_dv_convert_action_modify_vlan_vid
633                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
634                          const struct rte_flow_action *action,
635                          struct rte_flow_error *error)
636 {
637         const struct rte_flow_action_of_set_vlan_vid *conf =
638                 (const struct rte_flow_action_of_set_vlan_vid *)(action->conf);
639         int i = resource->actions_num;
640         struct mlx5_modification_cmd *actions = resource->actions;
641         struct field_modify_info *field = modify_vlan_out_first_vid;
642
643         if (i >= MLX5_MAX_MODIFY_NUM)
644                 return rte_flow_error_set(error, EINVAL,
645                          RTE_FLOW_ERROR_TYPE_ACTION, NULL,
646                          "too many items to modify");
647         actions[i] = (struct mlx5_modification_cmd) {
648                 .action_type = MLX5_MODIFICATION_TYPE_SET,
649                 .field = field->id,
650                 .length = field->size,
651                 .offset = field->offset,
652         };
653         actions[i].data0 = rte_cpu_to_be_32(actions[i].data0);
654         actions[i].data1 = conf->vlan_vid;
655         actions[i].data1 = actions[i].data1 << 16;
656         resource->actions_num = ++i;
657         return 0;
658 }
659
660 /**
661  * Convert modify-header set TP action to DV specification.
662  *
663  * @param[in,out] resource
664  *   Pointer to the modify-header resource.
665  * @param[in] action
666  *   Pointer to action specification.
667  * @param[in] items
668  *   Pointer to rte_flow_item objects list.
669  * @param[in] attr
670  *   Pointer to flow attributes structure.
671  * @param[in] dev_flow
672  *   Pointer to the sub flow.
673  * @param[in] tunnel_decap
674  *   Whether action is after tunnel decapsulation.
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_convert_action_modify_tp
683                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
684                          const struct rte_flow_action *action,
685                          const struct rte_flow_item *items,
686                          union flow_dv_attr *attr, struct mlx5_flow *dev_flow,
687                          bool tunnel_decap, struct rte_flow_error *error)
688 {
689         const struct rte_flow_action_set_tp *conf =
690                 (const struct rte_flow_action_set_tp *)(action->conf);
691         struct rte_flow_item item;
692         struct rte_flow_item_udp udp;
693         struct rte_flow_item_udp udp_mask;
694         struct rte_flow_item_tcp tcp;
695         struct rte_flow_item_tcp tcp_mask;
696         struct field_modify_info *field;
697
698         if (!attr->valid)
699                 flow_dv_attr_init(items, attr, dev_flow, tunnel_decap);
700         if (attr->udp) {
701                 memset(&udp, 0, sizeof(udp));
702                 memset(&udp_mask, 0, sizeof(udp_mask));
703                 if (action->type == RTE_FLOW_ACTION_TYPE_SET_TP_SRC) {
704                         udp.hdr.src_port = conf->port;
705                         udp_mask.hdr.src_port =
706                                         rte_flow_item_udp_mask.hdr.src_port;
707                 } else {
708                         udp.hdr.dst_port = conf->port;
709                         udp_mask.hdr.dst_port =
710                                         rte_flow_item_udp_mask.hdr.dst_port;
711                 }
712                 item.type = RTE_FLOW_ITEM_TYPE_UDP;
713                 item.spec = &udp;
714                 item.mask = &udp_mask;
715                 field = modify_udp;
716         } else {
717                 MLX5_ASSERT(attr->tcp);
718                 memset(&tcp, 0, sizeof(tcp));
719                 memset(&tcp_mask, 0, sizeof(tcp_mask));
720                 if (action->type == RTE_FLOW_ACTION_TYPE_SET_TP_SRC) {
721                         tcp.hdr.src_port = conf->port;
722                         tcp_mask.hdr.src_port =
723                                         rte_flow_item_tcp_mask.hdr.src_port;
724                 } else {
725                         tcp.hdr.dst_port = conf->port;
726                         tcp_mask.hdr.dst_port =
727                                         rte_flow_item_tcp_mask.hdr.dst_port;
728                 }
729                 item.type = RTE_FLOW_ITEM_TYPE_TCP;
730                 item.spec = &tcp;
731                 item.mask = &tcp_mask;
732                 field = modify_tcp;
733         }
734         return flow_dv_convert_modify_action(&item, field, NULL, resource,
735                                              MLX5_MODIFICATION_TYPE_SET, error);
736 }
737
738 /**
739  * Convert modify-header set TTL action to DV specification.
740  *
741  * @param[in,out] resource
742  *   Pointer to the modify-header resource.
743  * @param[in] action
744  *   Pointer to action specification.
745  * @param[in] items
746  *   Pointer to rte_flow_item objects list.
747  * @param[in] attr
748  *   Pointer to flow attributes structure.
749  * @param[in] dev_flow
750  *   Pointer to the sub flow.
751  * @param[in] tunnel_decap
752  *   Whether action is after tunnel decapsulation.
753  * @param[out] error
754  *   Pointer to the error structure.
755  *
756  * @return
757  *   0 on success, a negative errno value otherwise and rte_errno is set.
758  */
759 static int
760 flow_dv_convert_action_modify_ttl
761                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
762                          const struct rte_flow_action *action,
763                          const struct rte_flow_item *items,
764                          union flow_dv_attr *attr, struct mlx5_flow *dev_flow,
765                          bool tunnel_decap, struct rte_flow_error *error)
766 {
767         const struct rte_flow_action_set_ttl *conf =
768                 (const struct rte_flow_action_set_ttl *)(action->conf);
769         struct rte_flow_item item;
770         struct rte_flow_item_ipv4 ipv4;
771         struct rte_flow_item_ipv4 ipv4_mask;
772         struct rte_flow_item_ipv6 ipv6;
773         struct rte_flow_item_ipv6 ipv6_mask;
774         struct field_modify_info *field;
775
776         if (!attr->valid)
777                 flow_dv_attr_init(items, attr, dev_flow, tunnel_decap);
778         if (attr->ipv4) {
779                 memset(&ipv4, 0, sizeof(ipv4));
780                 memset(&ipv4_mask, 0, sizeof(ipv4_mask));
781                 ipv4.hdr.time_to_live = conf->ttl_value;
782                 ipv4_mask.hdr.time_to_live = 0xFF;
783                 item.type = RTE_FLOW_ITEM_TYPE_IPV4;
784                 item.spec = &ipv4;
785                 item.mask = &ipv4_mask;
786                 field = modify_ipv4;
787         } else {
788                 MLX5_ASSERT(attr->ipv6);
789                 memset(&ipv6, 0, sizeof(ipv6));
790                 memset(&ipv6_mask, 0, sizeof(ipv6_mask));
791                 ipv6.hdr.hop_limits = conf->ttl_value;
792                 ipv6_mask.hdr.hop_limits = 0xFF;
793                 item.type = RTE_FLOW_ITEM_TYPE_IPV6;
794                 item.spec = &ipv6;
795                 item.mask = &ipv6_mask;
796                 field = modify_ipv6;
797         }
798         return flow_dv_convert_modify_action(&item, field, NULL, resource,
799                                              MLX5_MODIFICATION_TYPE_SET, error);
800 }
801
802 /**
803  * Convert modify-header decrement TTL action to DV specification.
804  *
805  * @param[in,out] resource
806  *   Pointer to the modify-header resource.
807  * @param[in] action
808  *   Pointer to action specification.
809  * @param[in] items
810  *   Pointer to rte_flow_item objects list.
811  * @param[in] attr
812  *   Pointer to flow attributes structure.
813  * @param[in] dev_flow
814  *   Pointer to the sub flow.
815  * @param[in] tunnel_decap
816  *   Whether action is after tunnel decapsulation.
817  * @param[out] error
818  *   Pointer to the error structure.
819  *
820  * @return
821  *   0 on success, a negative errno value otherwise and rte_errno is set.
822  */
823 static int
824 flow_dv_convert_action_modify_dec_ttl
825                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
826                          const struct rte_flow_item *items,
827                          union flow_dv_attr *attr, struct mlx5_flow *dev_flow,
828                          bool tunnel_decap, struct rte_flow_error *error)
829 {
830         struct rte_flow_item item;
831         struct rte_flow_item_ipv4 ipv4;
832         struct rte_flow_item_ipv4 ipv4_mask;
833         struct rte_flow_item_ipv6 ipv6;
834         struct rte_flow_item_ipv6 ipv6_mask;
835         struct field_modify_info *field;
836
837         if (!attr->valid)
838                 flow_dv_attr_init(items, attr, dev_flow, tunnel_decap);
839         if (attr->ipv4) {
840                 memset(&ipv4, 0, sizeof(ipv4));
841                 memset(&ipv4_mask, 0, sizeof(ipv4_mask));
842                 ipv4.hdr.time_to_live = 0xFF;
843                 ipv4_mask.hdr.time_to_live = 0xFF;
844                 item.type = RTE_FLOW_ITEM_TYPE_IPV4;
845                 item.spec = &ipv4;
846                 item.mask = &ipv4_mask;
847                 field = modify_ipv4;
848         } else {
849                 MLX5_ASSERT(attr->ipv6);
850                 memset(&ipv6, 0, sizeof(ipv6));
851                 memset(&ipv6_mask, 0, sizeof(ipv6_mask));
852                 ipv6.hdr.hop_limits = 0xFF;
853                 ipv6_mask.hdr.hop_limits = 0xFF;
854                 item.type = RTE_FLOW_ITEM_TYPE_IPV6;
855                 item.spec = &ipv6;
856                 item.mask = &ipv6_mask;
857                 field = modify_ipv6;
858         }
859         return flow_dv_convert_modify_action(&item, field, NULL, resource,
860                                              MLX5_MODIFICATION_TYPE_ADD, error);
861 }
862
863 /**
864  * Convert modify-header increment/decrement TCP Sequence number
865  * to DV specification.
866  *
867  * @param[in,out] resource
868  *   Pointer to the modify-header resource.
869  * @param[in] action
870  *   Pointer to action specification.
871  * @param[out] error
872  *   Pointer to the error structure.
873  *
874  * @return
875  *   0 on success, a negative errno value otherwise and rte_errno is set.
876  */
877 static int
878 flow_dv_convert_action_modify_tcp_seq
879                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
880                          const struct rte_flow_action *action,
881                          struct rte_flow_error *error)
882 {
883         const rte_be32_t *conf = (const rte_be32_t *)(action->conf);
884         uint64_t value = rte_be_to_cpu_32(*conf);
885         struct rte_flow_item item;
886         struct rte_flow_item_tcp tcp;
887         struct rte_flow_item_tcp tcp_mask;
888
889         memset(&tcp, 0, sizeof(tcp));
890         memset(&tcp_mask, 0, sizeof(tcp_mask));
891         if (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ)
892                 /*
893                  * The HW has no decrement operation, only increment operation.
894                  * To simulate decrement X from Y using increment operation
895                  * we need to add UINT32_MAX X times to Y.
896                  * Each adding of UINT32_MAX decrements Y by 1.
897                  */
898                 value *= UINT32_MAX;
899         tcp.hdr.sent_seq = rte_cpu_to_be_32((uint32_t)value);
900         tcp_mask.hdr.sent_seq = RTE_BE32(UINT32_MAX);
901         item.type = RTE_FLOW_ITEM_TYPE_TCP;
902         item.spec = &tcp;
903         item.mask = &tcp_mask;
904         return flow_dv_convert_modify_action(&item, modify_tcp, NULL, resource,
905                                              MLX5_MODIFICATION_TYPE_ADD, error);
906 }
907
908 /**
909  * Convert modify-header increment/decrement TCP Acknowledgment number
910  * to DV specification.
911  *
912  * @param[in,out] resource
913  *   Pointer to the modify-header resource.
914  * @param[in] action
915  *   Pointer to action specification.
916  * @param[out] error
917  *   Pointer to the error structure.
918  *
919  * @return
920  *   0 on success, a negative errno value otherwise and rte_errno is set.
921  */
922 static int
923 flow_dv_convert_action_modify_tcp_ack
924                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
925                          const struct rte_flow_action *action,
926                          struct rte_flow_error *error)
927 {
928         const rte_be32_t *conf = (const rte_be32_t *)(action->conf);
929         uint64_t value = rte_be_to_cpu_32(*conf);
930         struct rte_flow_item item;
931         struct rte_flow_item_tcp tcp;
932         struct rte_flow_item_tcp tcp_mask;
933
934         memset(&tcp, 0, sizeof(tcp));
935         memset(&tcp_mask, 0, sizeof(tcp_mask));
936         if (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK)
937                 /*
938                  * The HW has no decrement operation, only increment operation.
939                  * To simulate decrement X from Y using increment operation
940                  * we need to add UINT32_MAX X times to Y.
941                  * Each adding of UINT32_MAX decrements Y by 1.
942                  */
943                 value *= UINT32_MAX;
944         tcp.hdr.recv_ack = rte_cpu_to_be_32((uint32_t)value);
945         tcp_mask.hdr.recv_ack = RTE_BE32(UINT32_MAX);
946         item.type = RTE_FLOW_ITEM_TYPE_TCP;
947         item.spec = &tcp;
948         item.mask = &tcp_mask;
949         return flow_dv_convert_modify_action(&item, modify_tcp, NULL, resource,
950                                              MLX5_MODIFICATION_TYPE_ADD, error);
951 }
952
953 static enum mlx5_modification_field reg_to_field[] = {
954         [REG_NON] = MLX5_MODI_OUT_NONE,
955         [REG_A] = MLX5_MODI_META_DATA_REG_A,
956         [REG_B] = MLX5_MODI_META_DATA_REG_B,
957         [REG_C_0] = MLX5_MODI_META_REG_C_0,
958         [REG_C_1] = MLX5_MODI_META_REG_C_1,
959         [REG_C_2] = MLX5_MODI_META_REG_C_2,
960         [REG_C_3] = MLX5_MODI_META_REG_C_3,
961         [REG_C_4] = MLX5_MODI_META_REG_C_4,
962         [REG_C_5] = MLX5_MODI_META_REG_C_5,
963         [REG_C_6] = MLX5_MODI_META_REG_C_6,
964         [REG_C_7] = MLX5_MODI_META_REG_C_7,
965 };
966
967 /**
968  * Convert register set to DV specification.
969  *
970  * @param[in,out] resource
971  *   Pointer to the modify-header resource.
972  * @param[in] action
973  *   Pointer to action specification.
974  * @param[out] error
975  *   Pointer to the error structure.
976  *
977  * @return
978  *   0 on success, a negative errno value otherwise and rte_errno is set.
979  */
980 static int
981 flow_dv_convert_action_set_reg
982                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
983                          const struct rte_flow_action *action,
984                          struct rte_flow_error *error)
985 {
986         const struct mlx5_rte_flow_action_set_tag *conf = action->conf;
987         struct mlx5_modification_cmd *actions = resource->actions;
988         uint32_t i = resource->actions_num;
989
990         if (i >= MLX5_MAX_MODIFY_NUM)
991                 return rte_flow_error_set(error, EINVAL,
992                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
993                                           "too many items to modify");
994         MLX5_ASSERT(conf->id != REG_NON);
995         MLX5_ASSERT(conf->id < RTE_DIM(reg_to_field));
996         actions[i] = (struct mlx5_modification_cmd) {
997                 .action_type = MLX5_MODIFICATION_TYPE_SET,
998                 .field = reg_to_field[conf->id],
999         };
1000         actions[i].data0 = rte_cpu_to_be_32(actions[i].data0);
1001         actions[i].data1 = rte_cpu_to_be_32(conf->data);
1002         ++i;
1003         resource->actions_num = i;
1004         return 0;
1005 }
1006
1007 /**
1008  * Convert SET_TAG action to DV specification.
1009  *
1010  * @param[in] dev
1011  *   Pointer to the rte_eth_dev structure.
1012  * @param[in,out] resource
1013  *   Pointer to the modify-header resource.
1014  * @param[in] conf
1015  *   Pointer to action specification.
1016  * @param[out] error
1017  *   Pointer to the error structure.
1018  *
1019  * @return
1020  *   0 on success, a negative errno value otherwise and rte_errno is set.
1021  */
1022 static int
1023 flow_dv_convert_action_set_tag
1024                         (struct rte_eth_dev *dev,
1025                          struct mlx5_flow_dv_modify_hdr_resource *resource,
1026                          const struct rte_flow_action_set_tag *conf,
1027                          struct rte_flow_error *error)
1028 {
1029         rte_be32_t data = rte_cpu_to_be_32(conf->data);
1030         rte_be32_t mask = rte_cpu_to_be_32(conf->mask);
1031         struct rte_flow_item item = {
1032                 .spec = &data,
1033                 .mask = &mask,
1034         };
1035         struct field_modify_info reg_c_x[] = {
1036                 [1] = {0, 0, 0},
1037         };
1038         enum mlx5_modification_field reg_type;
1039         int ret;
1040
1041         ret = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, conf->index, error);
1042         if (ret < 0)
1043                 return ret;
1044         MLX5_ASSERT(ret != REG_NON);
1045         MLX5_ASSERT((unsigned int)ret < RTE_DIM(reg_to_field));
1046         reg_type = reg_to_field[ret];
1047         MLX5_ASSERT(reg_type > 0);
1048         reg_c_x[0] = (struct field_modify_info){4, 0, reg_type};
1049         return flow_dv_convert_modify_action(&item, reg_c_x, NULL, resource,
1050                                              MLX5_MODIFICATION_TYPE_SET, error);
1051 }
1052
1053 /**
1054  * Convert internal COPY_REG action to DV specification.
1055  *
1056  * @param[in] dev
1057  *   Pointer to the rte_eth_dev structure.
1058  * @param[in,out] res
1059  *   Pointer to the modify-header resource.
1060  * @param[in] action
1061  *   Pointer to action specification.
1062  * @param[out] error
1063  *   Pointer to the error structure.
1064  *
1065  * @return
1066  *   0 on success, a negative errno value otherwise and rte_errno is set.
1067  */
1068 static int
1069 flow_dv_convert_action_copy_mreg(struct rte_eth_dev *dev,
1070                                  struct mlx5_flow_dv_modify_hdr_resource *res,
1071                                  const struct rte_flow_action *action,
1072                                  struct rte_flow_error *error)
1073 {
1074         const struct mlx5_flow_action_copy_mreg *conf = action->conf;
1075         rte_be32_t mask = RTE_BE32(UINT32_MAX);
1076         struct rte_flow_item item = {
1077                 .spec = NULL,
1078                 .mask = &mask,
1079         };
1080         struct field_modify_info reg_src[] = {
1081                 {4, 0, reg_to_field[conf->src]},
1082                 {0, 0, 0},
1083         };
1084         struct field_modify_info reg_dst = {
1085                 .offset = 0,
1086                 .id = reg_to_field[conf->dst],
1087         };
1088         /* Adjust reg_c[0] usage according to reported mask. */
1089         if (conf->dst == REG_C_0 || conf->src == REG_C_0) {
1090                 struct mlx5_priv *priv = dev->data->dev_private;
1091                 uint32_t reg_c0 = priv->sh->dv_regc0_mask;
1092
1093                 MLX5_ASSERT(reg_c0);
1094                 MLX5_ASSERT(priv->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY);
1095                 if (conf->dst == REG_C_0) {
1096                         /* Copy to reg_c[0], within mask only. */
1097                         reg_dst.offset = rte_bsf32(reg_c0);
1098                         /*
1099                          * Mask is ignoring the enianness, because
1100                          * there is no conversion in datapath.
1101                          */
1102 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
1103                         /* Copy from destination lower bits to reg_c[0]. */
1104                         mask = reg_c0 >> reg_dst.offset;
1105 #else
1106                         /* Copy from destination upper bits to reg_c[0]. */
1107                         mask = reg_c0 << (sizeof(reg_c0) * CHAR_BIT -
1108                                           rte_fls_u32(reg_c0));
1109 #endif
1110                 } else {
1111                         mask = rte_cpu_to_be_32(reg_c0);
1112 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
1113                         /* Copy from reg_c[0] to destination lower bits. */
1114                         reg_dst.offset = 0;
1115 #else
1116                         /* Copy from reg_c[0] to destination upper bits. */
1117                         reg_dst.offset = sizeof(reg_c0) * CHAR_BIT -
1118                                          (rte_fls_u32(reg_c0) -
1119                                           rte_bsf32(reg_c0));
1120 #endif
1121                 }
1122         }
1123         return flow_dv_convert_modify_action(&item,
1124                                              reg_src, &reg_dst, res,
1125                                              MLX5_MODIFICATION_TYPE_COPY,
1126                                              error);
1127 }
1128
1129 /**
1130  * Convert MARK action to DV specification. This routine is used
1131  * in extensive metadata only and requires metadata register to be
1132  * handled. In legacy mode hardware tag resource is engaged.
1133  *
1134  * @param[in] dev
1135  *   Pointer to the rte_eth_dev structure.
1136  * @param[in] conf
1137  *   Pointer to MARK action specification.
1138  * @param[in,out] resource
1139  *   Pointer to the modify-header resource.
1140  * @param[out] error
1141  *   Pointer to the error structure.
1142  *
1143  * @return
1144  *   0 on success, a negative errno value otherwise and rte_errno is set.
1145  */
1146 static int
1147 flow_dv_convert_action_mark(struct rte_eth_dev *dev,
1148                             const struct rte_flow_action_mark *conf,
1149                             struct mlx5_flow_dv_modify_hdr_resource *resource,
1150                             struct rte_flow_error *error)
1151 {
1152         struct mlx5_priv *priv = dev->data->dev_private;
1153         rte_be32_t mask = rte_cpu_to_be_32(MLX5_FLOW_MARK_MASK &
1154                                            priv->sh->dv_mark_mask);
1155         rte_be32_t data = rte_cpu_to_be_32(conf->id) & mask;
1156         struct rte_flow_item item = {
1157                 .spec = &data,
1158                 .mask = &mask,
1159         };
1160         struct field_modify_info reg_c_x[] = {
1161                 [1] = {0, 0, 0},
1162         };
1163         int reg;
1164
1165         if (!mask)
1166                 return rte_flow_error_set(error, EINVAL,
1167                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1168                                           NULL, "zero mark action mask");
1169         reg = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
1170         if (reg < 0)
1171                 return reg;
1172         MLX5_ASSERT(reg > 0);
1173         if (reg == REG_C_0) {
1174                 uint32_t msk_c0 = priv->sh->dv_regc0_mask;
1175                 uint32_t shl_c0 = rte_bsf32(msk_c0);
1176
1177                 data = rte_cpu_to_be_32(rte_cpu_to_be_32(data) << shl_c0);
1178                 mask = rte_cpu_to_be_32(mask) & msk_c0;
1179                 mask = rte_cpu_to_be_32(mask << shl_c0);
1180         }
1181         reg_c_x[0] = (struct field_modify_info){4, 0, reg_to_field[reg]};
1182         return flow_dv_convert_modify_action(&item, reg_c_x, NULL, resource,
1183                                              MLX5_MODIFICATION_TYPE_SET, error);
1184 }
1185
1186 /**
1187  * Get metadata register index for specified steering domain.
1188  *
1189  * @param[in] dev
1190  *   Pointer to the rte_eth_dev structure.
1191  * @param[in] attr
1192  *   Attributes of flow to determine steering domain.
1193  * @param[out] error
1194  *   Pointer to the error structure.
1195  *
1196  * @return
1197  *   positive index on success, a negative errno value otherwise
1198  *   and rte_errno is set.
1199  */
1200 static enum modify_reg
1201 flow_dv_get_metadata_reg(struct rte_eth_dev *dev,
1202                          const struct rte_flow_attr *attr,
1203                          struct rte_flow_error *error)
1204 {
1205         int reg =
1206                 mlx5_flow_get_reg_id(dev, attr->transfer ?
1207                                           MLX5_METADATA_FDB :
1208                                             attr->egress ?
1209                                             MLX5_METADATA_TX :
1210                                             MLX5_METADATA_RX, 0, error);
1211         if (reg < 0)
1212                 return rte_flow_error_set(error,
1213                                           ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
1214                                           NULL, "unavailable "
1215                                           "metadata register");
1216         return reg;
1217 }
1218
1219 /**
1220  * Convert SET_META action to DV specification.
1221  *
1222  * @param[in] dev
1223  *   Pointer to the rte_eth_dev structure.
1224  * @param[in,out] resource
1225  *   Pointer to the modify-header resource.
1226  * @param[in] attr
1227  *   Attributes of flow that includes this item.
1228  * @param[in] conf
1229  *   Pointer to action specification.
1230  * @param[out] error
1231  *   Pointer to the error structure.
1232  *
1233  * @return
1234  *   0 on success, a negative errno value otherwise and rte_errno is set.
1235  */
1236 static int
1237 flow_dv_convert_action_set_meta
1238                         (struct rte_eth_dev *dev,
1239                          struct mlx5_flow_dv_modify_hdr_resource *resource,
1240                          const struct rte_flow_attr *attr,
1241                          const struct rte_flow_action_set_meta *conf,
1242                          struct rte_flow_error *error)
1243 {
1244         uint32_t data = conf->data;
1245         uint32_t mask = conf->mask;
1246         struct rte_flow_item item = {
1247                 .spec = &data,
1248                 .mask = &mask,
1249         };
1250         struct field_modify_info reg_c_x[] = {
1251                 [1] = {0, 0, 0},
1252         };
1253         int reg = flow_dv_get_metadata_reg(dev, attr, error);
1254
1255         if (reg < 0)
1256                 return reg;
1257         /*
1258          * In datapath code there is no endianness
1259          * coversions for perfromance reasons, all
1260          * pattern conversions are done in rte_flow.
1261          */
1262         if (reg == REG_C_0) {
1263                 struct mlx5_priv *priv = dev->data->dev_private;
1264                 uint32_t msk_c0 = priv->sh->dv_regc0_mask;
1265                 uint32_t shl_c0;
1266
1267                 MLX5_ASSERT(msk_c0);
1268 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
1269                 shl_c0 = rte_bsf32(msk_c0);
1270 #else
1271                 shl_c0 = sizeof(msk_c0) * CHAR_BIT - rte_fls_u32(msk_c0);
1272 #endif
1273                 mask <<= shl_c0;
1274                 data <<= shl_c0;
1275                 MLX5_ASSERT(!(~msk_c0 & rte_cpu_to_be_32(mask)));
1276         }
1277         reg_c_x[0] = (struct field_modify_info){4, 0, reg_to_field[reg]};
1278         /* The routine expects parameters in memory as big-endian ones. */
1279         return flow_dv_convert_modify_action(&item, reg_c_x, NULL, resource,
1280                                              MLX5_MODIFICATION_TYPE_SET, error);
1281 }
1282
1283 /**
1284  * Convert modify-header set IPv4 DSCP action to DV specification.
1285  *
1286  * @param[in,out] resource
1287  *   Pointer to the modify-header resource.
1288  * @param[in] action
1289  *   Pointer to action specification.
1290  * @param[out] error
1291  *   Pointer to the error structure.
1292  *
1293  * @return
1294  *   0 on success, a negative errno value otherwise and rte_errno is set.
1295  */
1296 static int
1297 flow_dv_convert_action_modify_ipv4_dscp
1298                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
1299                          const struct rte_flow_action *action,
1300                          struct rte_flow_error *error)
1301 {
1302         const struct rte_flow_action_set_dscp *conf =
1303                 (const struct rte_flow_action_set_dscp *)(action->conf);
1304         struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_IPV4 };
1305         struct rte_flow_item_ipv4 ipv4;
1306         struct rte_flow_item_ipv4 ipv4_mask;
1307
1308         memset(&ipv4, 0, sizeof(ipv4));
1309         memset(&ipv4_mask, 0, sizeof(ipv4_mask));
1310         ipv4.hdr.type_of_service = conf->dscp;
1311         ipv4_mask.hdr.type_of_service = RTE_IPV4_HDR_DSCP_MASK >> 2;
1312         item.spec = &ipv4;
1313         item.mask = &ipv4_mask;
1314         return flow_dv_convert_modify_action(&item, modify_ipv4, NULL, resource,
1315                                              MLX5_MODIFICATION_TYPE_SET, error);
1316 }
1317
1318 /**
1319  * Convert modify-header set IPv6 DSCP action to DV specification.
1320  *
1321  * @param[in,out] resource
1322  *   Pointer to the modify-header resource.
1323  * @param[in] action
1324  *   Pointer to action specification.
1325  * @param[out] error
1326  *   Pointer to the error structure.
1327  *
1328  * @return
1329  *   0 on success, a negative errno value otherwise and rte_errno is set.
1330  */
1331 static int
1332 flow_dv_convert_action_modify_ipv6_dscp
1333                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
1334                          const struct rte_flow_action *action,
1335                          struct rte_flow_error *error)
1336 {
1337         const struct rte_flow_action_set_dscp *conf =
1338                 (const struct rte_flow_action_set_dscp *)(action->conf);
1339         struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_IPV6 };
1340         struct rte_flow_item_ipv6 ipv6;
1341         struct rte_flow_item_ipv6 ipv6_mask;
1342
1343         memset(&ipv6, 0, sizeof(ipv6));
1344         memset(&ipv6_mask, 0, sizeof(ipv6_mask));
1345         /*
1346          * Even though the DSCP bits offset of IPv6 is not byte aligned,
1347          * rdma-core only accept the DSCP bits byte aligned start from
1348          * bit 0 to 5 as to be compatible with IPv4. No need to shift the
1349          * bits in IPv6 case as rdma-core requires byte aligned value.
1350          */
1351         ipv6.hdr.vtc_flow = conf->dscp;
1352         ipv6_mask.hdr.vtc_flow = RTE_IPV6_HDR_DSCP_MASK >> 22;
1353         item.spec = &ipv6;
1354         item.mask = &ipv6_mask;
1355         return flow_dv_convert_modify_action(&item, modify_ipv6, NULL, resource,
1356                                              MLX5_MODIFICATION_TYPE_SET, error);
1357 }
1358
1359 /**
1360  * Validate MARK item.
1361  *
1362  * @param[in] dev
1363  *   Pointer to the rte_eth_dev structure.
1364  * @param[in] item
1365  *   Item specification.
1366  * @param[in] attr
1367  *   Attributes of flow that includes this item.
1368  * @param[out] error
1369  *   Pointer to error structure.
1370  *
1371  * @return
1372  *   0 on success, a negative errno value otherwise and rte_errno is set.
1373  */
1374 static int
1375 flow_dv_validate_item_mark(struct rte_eth_dev *dev,
1376                            const struct rte_flow_item *item,
1377                            const struct rte_flow_attr *attr __rte_unused,
1378                            struct rte_flow_error *error)
1379 {
1380         struct mlx5_priv *priv = dev->data->dev_private;
1381         struct mlx5_dev_config *config = &priv->config;
1382         const struct rte_flow_item_mark *spec = item->spec;
1383         const struct rte_flow_item_mark *mask = item->mask;
1384         const struct rte_flow_item_mark nic_mask = {
1385                 .id = priv->sh->dv_mark_mask,
1386         };
1387         int ret;
1388
1389         if (config->dv_xmeta_en == MLX5_XMETA_MODE_LEGACY)
1390                 return rte_flow_error_set(error, ENOTSUP,
1391                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1392                                           "extended metadata feature"
1393                                           " isn't enabled");
1394         if (!mlx5_flow_ext_mreg_supported(dev))
1395                 return rte_flow_error_set(error, ENOTSUP,
1396                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1397                                           "extended metadata register"
1398                                           " isn't supported");
1399         if (!nic_mask.id)
1400                 return rte_flow_error_set(error, ENOTSUP,
1401                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1402                                           "extended metadata register"
1403                                           " isn't available");
1404         ret = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
1405         if (ret < 0)
1406                 return ret;
1407         if (!spec)
1408                 return rte_flow_error_set(error, EINVAL,
1409                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
1410                                           item->spec,
1411                                           "data cannot be empty");
1412         if (spec->id >= (MLX5_FLOW_MARK_MAX & nic_mask.id))
1413                 return rte_flow_error_set(error, EINVAL,
1414                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1415                                           &spec->id,
1416                                           "mark id exceeds the limit");
1417         if (!mask)
1418                 mask = &nic_mask;
1419         if (!mask->id)
1420                 return rte_flow_error_set(error, EINVAL,
1421                                         RTE_FLOW_ERROR_TYPE_ITEM_SPEC, NULL,
1422                                         "mask cannot be zero");
1423
1424         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1425                                         (const uint8_t *)&nic_mask,
1426                                         sizeof(struct rte_flow_item_mark),
1427                                         MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
1428         if (ret < 0)
1429                 return ret;
1430         return 0;
1431 }
1432
1433 /**
1434  * Validate META item.
1435  *
1436  * @param[in] dev
1437  *   Pointer to the rte_eth_dev structure.
1438  * @param[in] item
1439  *   Item specification.
1440  * @param[in] attr
1441  *   Attributes of flow that includes this item.
1442  * @param[out] error
1443  *   Pointer to error structure.
1444  *
1445  * @return
1446  *   0 on success, a negative errno value otherwise and rte_errno is set.
1447  */
1448 static int
1449 flow_dv_validate_item_meta(struct rte_eth_dev *dev __rte_unused,
1450                            const struct rte_flow_item *item,
1451                            const struct rte_flow_attr *attr,
1452                            struct rte_flow_error *error)
1453 {
1454         struct mlx5_priv *priv = dev->data->dev_private;
1455         struct mlx5_dev_config *config = &priv->config;
1456         const struct rte_flow_item_meta *spec = item->spec;
1457         const struct rte_flow_item_meta *mask = item->mask;
1458         struct rte_flow_item_meta nic_mask = {
1459                 .data = UINT32_MAX
1460         };
1461         int reg;
1462         int ret;
1463
1464         if (!spec)
1465                 return rte_flow_error_set(error, EINVAL,
1466                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
1467                                           item->spec,
1468                                           "data cannot be empty");
1469         if (config->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
1470                 if (!mlx5_flow_ext_mreg_supported(dev))
1471                         return rte_flow_error_set(error, ENOTSUP,
1472                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1473                                           "extended metadata register"
1474                                           " isn't supported");
1475                 reg = flow_dv_get_metadata_reg(dev, attr, error);
1476                 if (reg < 0)
1477                         return reg;
1478                 if (reg == REG_B)
1479                         return rte_flow_error_set(error, ENOTSUP,
1480                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1481                                           "match on reg_b "
1482                                           "isn't supported");
1483                 if (reg != REG_A)
1484                         nic_mask.data = priv->sh->dv_meta_mask;
1485         } else if (attr->transfer) {
1486                 return rte_flow_error_set(error, ENOTSUP,
1487                                         RTE_FLOW_ERROR_TYPE_ITEM, item,
1488                                         "extended metadata feature "
1489                                         "should be enabled when "
1490                                         "meta item is requested "
1491                                         "with e-switch mode ");
1492         }
1493         if (!mask)
1494                 mask = &rte_flow_item_meta_mask;
1495         if (!mask->data)
1496                 return rte_flow_error_set(error, EINVAL,
1497                                         RTE_FLOW_ERROR_TYPE_ITEM_SPEC, NULL,
1498                                         "mask cannot be zero");
1499
1500         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1501                                         (const uint8_t *)&nic_mask,
1502                                         sizeof(struct rte_flow_item_meta),
1503                                         MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
1504         return ret;
1505 }
1506
1507 /**
1508  * Validate TAG item.
1509  *
1510  * @param[in] dev
1511  *   Pointer to the rte_eth_dev structure.
1512  * @param[in] item
1513  *   Item specification.
1514  * @param[in] attr
1515  *   Attributes of flow that includes this item.
1516  * @param[out] error
1517  *   Pointer to error structure.
1518  *
1519  * @return
1520  *   0 on success, a negative errno value otherwise and rte_errno is set.
1521  */
1522 static int
1523 flow_dv_validate_item_tag(struct rte_eth_dev *dev,
1524                           const struct rte_flow_item *item,
1525                           const struct rte_flow_attr *attr __rte_unused,
1526                           struct rte_flow_error *error)
1527 {
1528         const struct rte_flow_item_tag *spec = item->spec;
1529         const struct rte_flow_item_tag *mask = item->mask;
1530         const struct rte_flow_item_tag nic_mask = {
1531                 .data = RTE_BE32(UINT32_MAX),
1532                 .index = 0xff,
1533         };
1534         int ret;
1535
1536         if (!mlx5_flow_ext_mreg_supported(dev))
1537                 return rte_flow_error_set(error, ENOTSUP,
1538                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1539                                           "extensive metadata register"
1540                                           " isn't supported");
1541         if (!spec)
1542                 return rte_flow_error_set(error, EINVAL,
1543                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
1544                                           item->spec,
1545                                           "data cannot be empty");
1546         if (!mask)
1547                 mask = &rte_flow_item_tag_mask;
1548         if (!mask->data)
1549                 return rte_flow_error_set(error, EINVAL,
1550                                         RTE_FLOW_ERROR_TYPE_ITEM_SPEC, NULL,
1551                                         "mask cannot be zero");
1552
1553         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1554                                         (const uint8_t *)&nic_mask,
1555                                         sizeof(struct rte_flow_item_tag),
1556                                         MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
1557         if (ret < 0)
1558                 return ret;
1559         if (mask->index != 0xff)
1560                 return rte_flow_error_set(error, EINVAL,
1561                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC, NULL,
1562                                           "partial mask for tag index"
1563                                           " is not supported");
1564         ret = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, spec->index, error);
1565         if (ret < 0)
1566                 return ret;
1567         MLX5_ASSERT(ret != REG_NON);
1568         return 0;
1569 }
1570
1571 /**
1572  * Validate vport item.
1573  *
1574  * @param[in] dev
1575  *   Pointer to the rte_eth_dev structure.
1576  * @param[in] item
1577  *   Item specification.
1578  * @param[in] attr
1579  *   Attributes of flow that includes this item.
1580  * @param[in] item_flags
1581  *   Bit-fields that holds the items detected until now.
1582  * @param[out] error
1583  *   Pointer to error structure.
1584  *
1585  * @return
1586  *   0 on success, a negative errno value otherwise and rte_errno is set.
1587  */
1588 static int
1589 flow_dv_validate_item_port_id(struct rte_eth_dev *dev,
1590                               const struct rte_flow_item *item,
1591                               const struct rte_flow_attr *attr,
1592                               uint64_t item_flags,
1593                               struct rte_flow_error *error)
1594 {
1595         const struct rte_flow_item_port_id *spec = item->spec;
1596         const struct rte_flow_item_port_id *mask = item->mask;
1597         const struct rte_flow_item_port_id switch_mask = {
1598                         .id = 0xffffffff,
1599         };
1600         struct mlx5_priv *esw_priv;
1601         struct mlx5_priv *dev_priv;
1602         int ret;
1603
1604         if (!attr->transfer)
1605                 return rte_flow_error_set(error, EINVAL,
1606                                           RTE_FLOW_ERROR_TYPE_ITEM,
1607                                           NULL,
1608                                           "match on port id is valid only"
1609                                           " when transfer flag is enabled");
1610         if (item_flags & MLX5_FLOW_ITEM_PORT_ID)
1611                 return rte_flow_error_set(error, ENOTSUP,
1612                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1613                                           "multiple source ports are not"
1614                                           " supported");
1615         if (!mask)
1616                 mask = &switch_mask;
1617         if (mask->id != 0xffffffff)
1618                 return rte_flow_error_set(error, ENOTSUP,
1619                                            RTE_FLOW_ERROR_TYPE_ITEM_MASK,
1620                                            mask,
1621                                            "no support for partial mask on"
1622                                            " \"id\" field");
1623         ret = mlx5_flow_item_acceptable
1624                                 (item, (const uint8_t *)mask,
1625                                  (const uint8_t *)&rte_flow_item_port_id_mask,
1626                                  sizeof(struct rte_flow_item_port_id),
1627                                  MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
1628         if (ret)
1629                 return ret;
1630         if (!spec)
1631                 return 0;
1632         esw_priv = mlx5_port_to_eswitch_info(spec->id, false);
1633         if (!esw_priv)
1634                 return rte_flow_error_set(error, rte_errno,
1635                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC, spec,
1636                                           "failed to obtain E-Switch info for"
1637                                           " port");
1638         dev_priv = mlx5_dev_to_eswitch_info(dev);
1639         if (!dev_priv)
1640                 return rte_flow_error_set(error, rte_errno,
1641                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1642                                           NULL,
1643                                           "failed to obtain E-Switch info");
1644         if (esw_priv->domain_id != dev_priv->domain_id)
1645                 return rte_flow_error_set(error, EINVAL,
1646                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC, spec,
1647                                           "cannot match on a port from a"
1648                                           " different E-Switch");
1649         return 0;
1650 }
1651
1652 /**
1653  * Validate VLAN item.
1654  *
1655  * @param[in] item
1656  *   Item specification.
1657  * @param[in] item_flags
1658  *   Bit-fields that holds the items detected until now.
1659  * @param[in] dev
1660  *   Ethernet device flow is being created on.
1661  * @param[out] error
1662  *   Pointer to error structure.
1663  *
1664  * @return
1665  *   0 on success, a negative errno value otherwise and rte_errno is set.
1666  */
1667 static int
1668 flow_dv_validate_item_vlan(const struct rte_flow_item *item,
1669                            uint64_t item_flags,
1670                            struct rte_eth_dev *dev,
1671                            struct rte_flow_error *error)
1672 {
1673         const struct rte_flow_item_vlan *mask = item->mask;
1674         const struct rte_flow_item_vlan nic_mask = {
1675                 .tci = RTE_BE16(UINT16_MAX),
1676                 .inner_type = RTE_BE16(UINT16_MAX),
1677                 .has_more_vlan = 1,
1678         };
1679         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1680         int ret;
1681         const uint64_t l34m = tunnel ? (MLX5_FLOW_LAYER_INNER_L3 |
1682                                         MLX5_FLOW_LAYER_INNER_L4) :
1683                                        (MLX5_FLOW_LAYER_OUTER_L3 |
1684                                         MLX5_FLOW_LAYER_OUTER_L4);
1685         const uint64_t vlanm = tunnel ? MLX5_FLOW_LAYER_INNER_VLAN :
1686                                         MLX5_FLOW_LAYER_OUTER_VLAN;
1687
1688         if (item_flags & vlanm)
1689                 return rte_flow_error_set(error, EINVAL,
1690                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1691                                           "multiple VLAN layers not supported");
1692         else if ((item_flags & l34m) != 0)
1693                 return rte_flow_error_set(error, EINVAL,
1694                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1695                                           "VLAN cannot follow L3/L4 layer");
1696         if (!mask)
1697                 mask = &rte_flow_item_vlan_mask;
1698         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1699                                         (const uint8_t *)&nic_mask,
1700                                         sizeof(struct rte_flow_item_vlan),
1701                                         MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
1702         if (ret)
1703                 return ret;
1704         if (!tunnel && mask->tci != RTE_BE16(0x0fff)) {
1705                 struct mlx5_priv *priv = dev->data->dev_private;
1706
1707                 if (priv->vmwa_context) {
1708                         /*
1709                          * Non-NULL context means we have a virtual machine
1710                          * and SR-IOV enabled, we have to create VLAN interface
1711                          * to make hypervisor to setup E-Switch vport
1712                          * context correctly. We avoid creating the multiple
1713                          * VLAN interfaces, so we cannot support VLAN tag mask.
1714                          */
1715                         return rte_flow_error_set(error, EINVAL,
1716                                                   RTE_FLOW_ERROR_TYPE_ITEM,
1717                                                   item,
1718                                                   "VLAN tag mask is not"
1719                                                   " supported in virtual"
1720                                                   " environment");
1721                 }
1722         }
1723         return 0;
1724 }
1725
1726 /*
1727  * GTP flags are contained in 1 byte of the format:
1728  * -------------------------------------------
1729  * | bit   | 0 - 2   | 3  | 4   | 5 | 6 | 7  |
1730  * |-----------------------------------------|
1731  * | value | Version | PT | Res | E | S | PN |
1732  * -------------------------------------------
1733  *
1734  * Matching is supported only for GTP flags E, S, PN.
1735  */
1736 #define MLX5_GTP_FLAGS_MASK     0x07
1737
1738 /**
1739  * Validate GTP item.
1740  *
1741  * @param[in] dev
1742  *   Pointer to the rte_eth_dev structure.
1743  * @param[in] item
1744  *   Item specification.
1745  * @param[in] item_flags
1746  *   Bit-fields that holds the items detected until now.
1747  * @param[out] error
1748  *   Pointer to error structure.
1749  *
1750  * @return
1751  *   0 on success, a negative errno value otherwise and rte_errno is set.
1752  */
1753 static int
1754 flow_dv_validate_item_gtp(struct rte_eth_dev *dev,
1755                           const struct rte_flow_item *item,
1756                           uint64_t item_flags,
1757                           struct rte_flow_error *error)
1758 {
1759         struct mlx5_priv *priv = dev->data->dev_private;
1760         const struct rte_flow_item_gtp *spec = item->spec;
1761         const struct rte_flow_item_gtp *mask = item->mask;
1762         const struct rte_flow_item_gtp nic_mask = {
1763                 .v_pt_rsv_flags = MLX5_GTP_FLAGS_MASK,
1764                 .msg_type = 0xff,
1765                 .teid = RTE_BE32(0xffffffff),
1766         };
1767
1768         if (!priv->config.hca_attr.tunnel_stateless_gtp)
1769                 return rte_flow_error_set(error, ENOTSUP,
1770                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1771                                           "GTP support is not enabled");
1772         if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
1773                 return rte_flow_error_set(error, ENOTSUP,
1774                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1775                                           "multiple tunnel layers not"
1776                                           " supported");
1777         if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_UDP))
1778                 return rte_flow_error_set(error, EINVAL,
1779                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1780                                           "no outer UDP layer found");
1781         if (!mask)
1782                 mask = &rte_flow_item_gtp_mask;
1783         if (spec && spec->v_pt_rsv_flags & ~MLX5_GTP_FLAGS_MASK)
1784                 return rte_flow_error_set(error, ENOTSUP,
1785                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1786                                           "Match is supported for GTP"
1787                                           " flags only");
1788         return mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1789                                          (const uint8_t *)&nic_mask,
1790                                          sizeof(struct rte_flow_item_gtp),
1791                                          MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
1792 }
1793
1794 /**
1795  * Validate IPV4 item.
1796  * Use existing validation function mlx5_flow_validate_item_ipv4(), and
1797  * add specific validation of fragment_offset field,
1798  *
1799  * @param[in] item
1800  *   Item specification.
1801  * @param[in] item_flags
1802  *   Bit-fields that holds the items detected until now.
1803  * @param[out] error
1804  *   Pointer to error structure.
1805  *
1806  * @return
1807  *   0 on success, a negative errno value otherwise and rte_errno is set.
1808  */
1809 static int
1810 flow_dv_validate_item_ipv4(const struct rte_flow_item *item,
1811                            uint64_t item_flags,
1812                            uint64_t last_item,
1813                            uint16_t ether_type,
1814                            struct rte_flow_error *error)
1815 {
1816         int ret;
1817         const struct rte_flow_item_ipv4 *spec = item->spec;
1818         const struct rte_flow_item_ipv4 *last = item->last;
1819         const struct rte_flow_item_ipv4 *mask = item->mask;
1820         rte_be16_t fragment_offset_spec = 0;
1821         rte_be16_t fragment_offset_last = 0;
1822         const struct rte_flow_item_ipv4 nic_ipv4_mask = {
1823                 .hdr = {
1824                         .src_addr = RTE_BE32(0xffffffff),
1825                         .dst_addr = RTE_BE32(0xffffffff),
1826                         .type_of_service = 0xff,
1827                         .fragment_offset = RTE_BE16(0xffff),
1828                         .next_proto_id = 0xff,
1829                         .time_to_live = 0xff,
1830                 },
1831         };
1832
1833         ret = mlx5_flow_validate_item_ipv4(item, item_flags, last_item,
1834                                            ether_type, &nic_ipv4_mask,
1835                                            MLX5_ITEM_RANGE_ACCEPTED, error);
1836         if (ret < 0)
1837                 return ret;
1838         if (spec && mask)
1839                 fragment_offset_spec = spec->hdr.fragment_offset &
1840                                        mask->hdr.fragment_offset;
1841         if (!fragment_offset_spec)
1842                 return 0;
1843         /*
1844          * spec and mask are valid, enforce using full mask to make sure the
1845          * complete value is used correctly.
1846          */
1847         if ((mask->hdr.fragment_offset & RTE_BE16(MLX5_IPV4_FRAG_OFFSET_MASK))
1848                         != RTE_BE16(MLX5_IPV4_FRAG_OFFSET_MASK))
1849                 return rte_flow_error_set(error, EINVAL,
1850                                           RTE_FLOW_ERROR_TYPE_ITEM_MASK,
1851                                           item, "must use full mask for"
1852                                           " fragment_offset");
1853         /*
1854          * Match on fragment_offset 0x2000 means MF is 1 and frag-offset is 0,
1855          * indicating this is 1st fragment of fragmented packet.
1856          * This is not yet supported in MLX5, return appropriate error message.
1857          */
1858         if (fragment_offset_spec == RTE_BE16(RTE_IPV4_HDR_MF_FLAG))
1859                 return rte_flow_error_set(error, ENOTSUP,
1860                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1861                                           "match on first fragment not "
1862                                           "supported");
1863         if (fragment_offset_spec && !last)
1864                 return rte_flow_error_set(error, ENOTSUP,
1865                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1866                                           "specified value not supported");
1867         /* spec and last are valid, validate the specified range. */
1868         fragment_offset_last = last->hdr.fragment_offset &
1869                                mask->hdr.fragment_offset;
1870         /*
1871          * Match on fragment_offset spec 0x2001 and last 0x3fff
1872          * means MF is 1 and frag-offset is > 0.
1873          * This packet is fragment 2nd and onward, excluding last.
1874          * This is not yet supported in MLX5, return appropriate
1875          * error message.
1876          */
1877         if (fragment_offset_spec == RTE_BE16(RTE_IPV4_HDR_MF_FLAG + 1) &&
1878             fragment_offset_last == RTE_BE16(MLX5_IPV4_FRAG_OFFSET_MASK))
1879                 return rte_flow_error_set(error, ENOTSUP,
1880                                           RTE_FLOW_ERROR_TYPE_ITEM_LAST,
1881                                           last, "match on following "
1882                                           "fragments not supported");
1883         /*
1884          * Match on fragment_offset spec 0x0001 and last 0x1fff
1885          * means MF is 0 and frag-offset is > 0.
1886          * This packet is last fragment of fragmented packet.
1887          * This is not yet supported in MLX5, return appropriate
1888          * error message.
1889          */
1890         if (fragment_offset_spec == RTE_BE16(1) &&
1891             fragment_offset_last == RTE_BE16(RTE_IPV4_HDR_OFFSET_MASK))
1892                 return rte_flow_error_set(error, ENOTSUP,
1893                                           RTE_FLOW_ERROR_TYPE_ITEM_LAST,
1894                                           last, "match on last "
1895                                           "fragment not supported");
1896         /*
1897          * Match on fragment_offset spec 0x0001 and last 0x3fff
1898          * means MF and/or frag-offset is not 0.
1899          * This is a fragmented packet.
1900          * Other range values are invalid and rejected.
1901          */
1902         if (!(fragment_offset_spec == RTE_BE16(1) &&
1903               fragment_offset_last == RTE_BE16(MLX5_IPV4_FRAG_OFFSET_MASK)))
1904                 return rte_flow_error_set(error, ENOTSUP,
1905                                           RTE_FLOW_ERROR_TYPE_ITEM_LAST, last,
1906                                           "specified range not supported");
1907         return 0;
1908 }
1909
1910 /**
1911  * Validate IPV6 fragment extension item.
1912  *
1913  * @param[in] item
1914  *   Item specification.
1915  * @param[in] item_flags
1916  *   Bit-fields that holds the items detected until now.
1917  * @param[out] error
1918  *   Pointer to error structure.
1919  *
1920  * @return
1921  *   0 on success, a negative errno value otherwise and rte_errno is set.
1922  */
1923 static int
1924 flow_dv_validate_item_ipv6_frag_ext(const struct rte_flow_item *item,
1925                                     uint64_t item_flags,
1926                                     struct rte_flow_error *error)
1927 {
1928         const struct rte_flow_item_ipv6_frag_ext *spec = item->spec;
1929         const struct rte_flow_item_ipv6_frag_ext *last = item->last;
1930         const struct rte_flow_item_ipv6_frag_ext *mask = item->mask;
1931         rte_be16_t frag_data_spec = 0;
1932         rte_be16_t frag_data_last = 0;
1933         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1934         const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
1935                                       MLX5_FLOW_LAYER_OUTER_L4;
1936         int ret = 0;
1937         struct rte_flow_item_ipv6_frag_ext nic_mask = {
1938                 .hdr = {
1939                         .next_header = 0xff,
1940                         .frag_data = RTE_BE16(0xffff),
1941                 },
1942         };
1943
1944         if (item_flags & l4m)
1945                 return rte_flow_error_set(error, EINVAL,
1946                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1947                                           "ipv6 fragment extension item cannot "
1948                                           "follow L4 item.");
1949         if ((tunnel && !(item_flags & MLX5_FLOW_LAYER_INNER_L3_IPV6)) ||
1950             (!tunnel && !(item_flags & MLX5_FLOW_LAYER_OUTER_L3_IPV6)))
1951                 return rte_flow_error_set(error, EINVAL,
1952                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1953                                           "ipv6 fragment extension item must "
1954                                           "follow ipv6 item");
1955         if (spec && mask)
1956                 frag_data_spec = spec->hdr.frag_data & mask->hdr.frag_data;
1957         if (!frag_data_spec)
1958                 return 0;
1959         /*
1960          * spec and mask are valid, enforce using full mask to make sure the
1961          * complete value is used correctly.
1962          */
1963         if ((mask->hdr.frag_data & RTE_BE16(RTE_IPV6_FRAG_USED_MASK)) !=
1964                                 RTE_BE16(RTE_IPV6_FRAG_USED_MASK))
1965                 return rte_flow_error_set(error, EINVAL,
1966                                           RTE_FLOW_ERROR_TYPE_ITEM_MASK,
1967                                           item, "must use full mask for"
1968                                           " frag_data");
1969         /*
1970          * Match on frag_data 0x00001 means M is 1 and frag-offset is 0.
1971          * This is 1st fragment of fragmented packet.
1972          */
1973         if (frag_data_spec == RTE_BE16(RTE_IPV6_EHDR_MF_MASK))
1974                 return rte_flow_error_set(error, ENOTSUP,
1975                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1976                                           "match on first fragment not "
1977                                           "supported");
1978         if (frag_data_spec && !last)
1979                 return rte_flow_error_set(error, EINVAL,
1980                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1981                                           "specified value not supported");
1982         ret = mlx5_flow_item_acceptable
1983                                 (item, (const uint8_t *)mask,
1984                                  (const uint8_t *)&nic_mask,
1985                                  sizeof(struct rte_flow_item_ipv6_frag_ext),
1986                                  MLX5_ITEM_RANGE_ACCEPTED, error);
1987         if (ret)
1988                 return ret;
1989         /* spec and last are valid, validate the specified range. */
1990         frag_data_last = last->hdr.frag_data & mask->hdr.frag_data;
1991         /*
1992          * Match on frag_data spec 0x0009 and last 0xfff9
1993          * means M is 1 and frag-offset is > 0.
1994          * This packet is fragment 2nd and onward, excluding last.
1995          * This is not yet supported in MLX5, return appropriate
1996          * error message.
1997          */
1998         if (frag_data_spec == RTE_BE16(RTE_IPV6_EHDR_FO_ALIGN |
1999                                        RTE_IPV6_EHDR_MF_MASK) &&
2000             frag_data_last == RTE_BE16(RTE_IPV6_FRAG_USED_MASK))
2001                 return rte_flow_error_set(error, ENOTSUP,
2002                                           RTE_FLOW_ERROR_TYPE_ITEM_LAST,
2003                                           last, "match on following "
2004                                           "fragments not supported");
2005         /*
2006          * Match on frag_data spec 0x0008 and last 0xfff8
2007          * means M is 0 and frag-offset is > 0.
2008          * This packet is last fragment of fragmented packet.
2009          * This is not yet supported in MLX5, return appropriate
2010          * error message.
2011          */
2012         if (frag_data_spec == RTE_BE16(RTE_IPV6_EHDR_FO_ALIGN) &&
2013             frag_data_last == RTE_BE16(RTE_IPV6_EHDR_FO_MASK))
2014                 return rte_flow_error_set(error, ENOTSUP,
2015                                           RTE_FLOW_ERROR_TYPE_ITEM_LAST,
2016                                           last, "match on last "
2017                                           "fragment not supported");
2018         /* Other range values are invalid and rejected. */
2019         return rte_flow_error_set(error, EINVAL,
2020                                   RTE_FLOW_ERROR_TYPE_ITEM_LAST, last,
2021                                   "specified range not supported");
2022 }
2023
2024 /**
2025  * Validate the pop VLAN action.
2026  *
2027  * @param[in] dev
2028  *   Pointer to the rte_eth_dev structure.
2029  * @param[in] action_flags
2030  *   Holds the actions detected until now.
2031  * @param[in] action
2032  *   Pointer to the pop vlan action.
2033  * @param[in] item_flags
2034  *   The items found in this flow rule.
2035  * @param[in] attr
2036  *   Pointer to flow attributes.
2037  * @param[out] error
2038  *   Pointer to error structure.
2039  *
2040  * @return
2041  *   0 on success, a negative errno value otherwise and rte_errno is set.
2042  */
2043 static int
2044 flow_dv_validate_action_pop_vlan(struct rte_eth_dev *dev,
2045                                  uint64_t action_flags,
2046                                  const struct rte_flow_action *action,
2047                                  uint64_t item_flags,
2048                                  const struct rte_flow_attr *attr,
2049                                  struct rte_flow_error *error)
2050 {
2051         const struct mlx5_priv *priv = dev->data->dev_private;
2052
2053         (void)action;
2054         (void)attr;
2055         if (!priv->sh->pop_vlan_action)
2056                 return rte_flow_error_set(error, ENOTSUP,
2057                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2058                                           NULL,
2059                                           "pop vlan action is not supported");
2060         if (attr->egress)
2061                 return rte_flow_error_set(error, ENOTSUP,
2062                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
2063                                           NULL,
2064                                           "pop vlan action not supported for "
2065                                           "egress");
2066         if (action_flags & MLX5_FLOW_VLAN_ACTIONS)
2067                 return rte_flow_error_set(error, ENOTSUP,
2068                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2069                                           "no support for multiple VLAN "
2070                                           "actions");
2071         /* Pop VLAN with preceding Decap requires inner header with VLAN. */
2072         if ((action_flags & MLX5_FLOW_ACTION_DECAP) &&
2073             !(item_flags & MLX5_FLOW_LAYER_INNER_VLAN))
2074                 return rte_flow_error_set(error, ENOTSUP,
2075                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2076                                           NULL,
2077                                           "cannot pop vlan after decap without "
2078                                           "match on inner vlan in the flow");
2079         /* Pop VLAN without preceding Decap requires outer header with VLAN. */
2080         if (!(action_flags & MLX5_FLOW_ACTION_DECAP) &&
2081             !(item_flags & MLX5_FLOW_LAYER_OUTER_VLAN))
2082                 return rte_flow_error_set(error, ENOTSUP,
2083                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2084                                           NULL,
2085                                           "cannot pop vlan without a "
2086                                           "match on (outer) vlan in the flow");
2087         if (action_flags & MLX5_FLOW_ACTION_PORT_ID)
2088                 return rte_flow_error_set(error, EINVAL,
2089                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2090                                           "wrong action order, port_id should "
2091                                           "be after pop VLAN action");
2092         if (!attr->transfer && priv->representor)
2093                 return rte_flow_error_set(error, ENOTSUP,
2094                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2095                                           "pop vlan action for VF representor "
2096                                           "not supported on NIC table");
2097         return 0;
2098 }
2099
2100 /**
2101  * Get VLAN default info from vlan match info.
2102  *
2103  * @param[in] items
2104  *   the list of item specifications.
2105  * @param[out] vlan
2106  *   pointer VLAN info to fill to.
2107  *
2108  * @return
2109  *   0 on success, a negative errno value otherwise and rte_errno is set.
2110  */
2111 static void
2112 flow_dev_get_vlan_info_from_items(const struct rte_flow_item *items,
2113                                   struct rte_vlan_hdr *vlan)
2114 {
2115         const struct rte_flow_item_vlan nic_mask = {
2116                 .tci = RTE_BE16(MLX5DV_FLOW_VLAN_PCP_MASK |
2117                                 MLX5DV_FLOW_VLAN_VID_MASK),
2118                 .inner_type = RTE_BE16(0xffff),
2119         };
2120
2121         if (items == NULL)
2122                 return;
2123         for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
2124                 int type = items->type;
2125
2126                 if (type == RTE_FLOW_ITEM_TYPE_VLAN ||
2127                     type == MLX5_RTE_FLOW_ITEM_TYPE_VLAN)
2128                         break;
2129         }
2130         if (items->type != RTE_FLOW_ITEM_TYPE_END) {
2131                 const struct rte_flow_item_vlan *vlan_m = items->mask;
2132                 const struct rte_flow_item_vlan *vlan_v = items->spec;
2133
2134                 /* If VLAN item in pattern doesn't contain data, return here. */
2135                 if (!vlan_v)
2136                         return;
2137                 if (!vlan_m)
2138                         vlan_m = &nic_mask;
2139                 /* Only full match values are accepted */
2140                 if ((vlan_m->tci & MLX5DV_FLOW_VLAN_PCP_MASK_BE) ==
2141                      MLX5DV_FLOW_VLAN_PCP_MASK_BE) {
2142                         vlan->vlan_tci &= ~MLX5DV_FLOW_VLAN_PCP_MASK;
2143                         vlan->vlan_tci |=
2144                                 rte_be_to_cpu_16(vlan_v->tci &
2145                                                  MLX5DV_FLOW_VLAN_PCP_MASK_BE);
2146                 }
2147                 if ((vlan_m->tci & MLX5DV_FLOW_VLAN_VID_MASK_BE) ==
2148                      MLX5DV_FLOW_VLAN_VID_MASK_BE) {
2149                         vlan->vlan_tci &= ~MLX5DV_FLOW_VLAN_VID_MASK;
2150                         vlan->vlan_tci |=
2151                                 rte_be_to_cpu_16(vlan_v->tci &
2152                                                  MLX5DV_FLOW_VLAN_VID_MASK_BE);
2153                 }
2154                 if (vlan_m->inner_type == nic_mask.inner_type)
2155                         vlan->eth_proto = rte_be_to_cpu_16(vlan_v->inner_type &
2156                                                            vlan_m->inner_type);
2157         }
2158 }
2159
2160 /**
2161  * Validate the push VLAN action.
2162  *
2163  * @param[in] dev
2164  *   Pointer to the rte_eth_dev structure.
2165  * @param[in] action_flags
2166  *   Holds the actions detected until now.
2167  * @param[in] item_flags
2168  *   The items found in this flow rule.
2169  * @param[in] action
2170  *   Pointer to the action structure.
2171  * @param[in] attr
2172  *   Pointer to flow attributes
2173  * @param[out] error
2174  *   Pointer to error structure.
2175  *
2176  * @return
2177  *   0 on success, a negative errno value otherwise and rte_errno is set.
2178  */
2179 static int
2180 flow_dv_validate_action_push_vlan(struct rte_eth_dev *dev,
2181                                   uint64_t action_flags,
2182                                   const struct rte_flow_item_vlan *vlan_m,
2183                                   const struct rte_flow_action *action,
2184                                   const struct rte_flow_attr *attr,
2185                                   struct rte_flow_error *error)
2186 {
2187         const struct rte_flow_action_of_push_vlan *push_vlan = action->conf;
2188         const struct mlx5_priv *priv = dev->data->dev_private;
2189
2190         if (push_vlan->ethertype != RTE_BE16(RTE_ETHER_TYPE_VLAN) &&
2191             push_vlan->ethertype != RTE_BE16(RTE_ETHER_TYPE_QINQ))
2192                 return rte_flow_error_set(error, EINVAL,
2193                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2194                                           "invalid vlan ethertype");
2195         if (action_flags & MLX5_FLOW_ACTION_PORT_ID)
2196                 return rte_flow_error_set(error, EINVAL,
2197                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2198                                           "wrong action order, port_id should "
2199                                           "be after push VLAN");
2200         if (!attr->transfer && priv->representor)
2201                 return rte_flow_error_set(error, ENOTSUP,
2202                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2203                                           "push vlan action for VF representor "
2204                                           "not supported on NIC table");
2205         if (vlan_m &&
2206             (vlan_m->tci & MLX5DV_FLOW_VLAN_PCP_MASK_BE) &&
2207             (vlan_m->tci & MLX5DV_FLOW_VLAN_PCP_MASK_BE) !=
2208                 MLX5DV_FLOW_VLAN_PCP_MASK_BE &&
2209             !(action_flags & MLX5_FLOW_ACTION_OF_SET_VLAN_PCP) &&
2210             !(mlx5_flow_find_action
2211                 (action + 1, RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP)))
2212                 return rte_flow_error_set(error, EINVAL,
2213                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2214                                           "not full match mask on VLAN PCP and "
2215                                           "there is no of_set_vlan_pcp action, "
2216                                           "push VLAN action cannot figure out "
2217                                           "PCP value");
2218         if (vlan_m &&
2219             (vlan_m->tci & MLX5DV_FLOW_VLAN_VID_MASK_BE) &&
2220             (vlan_m->tci & MLX5DV_FLOW_VLAN_VID_MASK_BE) !=
2221                 MLX5DV_FLOW_VLAN_VID_MASK_BE &&
2222             !(action_flags & MLX5_FLOW_ACTION_OF_SET_VLAN_VID) &&
2223             !(mlx5_flow_find_action
2224                 (action + 1, RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID)))
2225                 return rte_flow_error_set(error, EINVAL,
2226                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2227                                           "not full match mask on VLAN VID and "
2228                                           "there is no of_set_vlan_vid action, "
2229                                           "push VLAN action cannot figure out "
2230                                           "VID value");
2231         (void)attr;
2232         return 0;
2233 }
2234
2235 /**
2236  * Validate the set VLAN PCP.
2237  *
2238  * @param[in] action_flags
2239  *   Holds the actions detected until now.
2240  * @param[in] actions
2241  *   Pointer to the list of actions remaining in the flow rule.
2242  * @param[out] error
2243  *   Pointer to error structure.
2244  *
2245  * @return
2246  *   0 on success, a negative errno value otherwise and rte_errno is set.
2247  */
2248 static int
2249 flow_dv_validate_action_set_vlan_pcp(uint64_t action_flags,
2250                                      const struct rte_flow_action actions[],
2251                                      struct rte_flow_error *error)
2252 {
2253         const struct rte_flow_action *action = actions;
2254         const struct rte_flow_action_of_set_vlan_pcp *conf = action->conf;
2255
2256         if (conf->vlan_pcp > 7)
2257                 return rte_flow_error_set(error, EINVAL,
2258                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2259                                           "VLAN PCP value is too big");
2260         if (!(action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN))
2261                 return rte_flow_error_set(error, ENOTSUP,
2262                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2263                                           "set VLAN PCP action must follow "
2264                                           "the push VLAN action");
2265         if (action_flags & MLX5_FLOW_ACTION_OF_SET_VLAN_PCP)
2266                 return rte_flow_error_set(error, ENOTSUP,
2267                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2268                                           "Multiple VLAN PCP modification are "
2269                                           "not supported");
2270         if (action_flags & MLX5_FLOW_ACTION_PORT_ID)
2271                 return rte_flow_error_set(error, EINVAL,
2272                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2273                                           "wrong action order, port_id should "
2274                                           "be after set VLAN PCP");
2275         return 0;
2276 }
2277
2278 /**
2279  * Validate the set VLAN VID.
2280  *
2281  * @param[in] item_flags
2282  *   Holds the items detected in this rule.
2283  * @param[in] action_flags
2284  *   Holds the actions detected until now.
2285  * @param[in] actions
2286  *   Pointer to the list of actions remaining in the flow rule.
2287  * @param[out] error
2288  *   Pointer to error structure.
2289  *
2290  * @return
2291  *   0 on success, a negative errno value otherwise and rte_errno is set.
2292  */
2293 static int
2294 flow_dv_validate_action_set_vlan_vid(uint64_t item_flags,
2295                                      uint64_t action_flags,
2296                                      const struct rte_flow_action actions[],
2297                                      struct rte_flow_error *error)
2298 {
2299         const struct rte_flow_action *action = actions;
2300         const struct rte_flow_action_of_set_vlan_vid *conf = action->conf;
2301
2302         if (rte_be_to_cpu_16(conf->vlan_vid) > 0xFFE)
2303                 return rte_flow_error_set(error, EINVAL,
2304                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2305                                           "VLAN VID value is too big");
2306         if (!(action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN) &&
2307             !(item_flags & MLX5_FLOW_LAYER_OUTER_VLAN))
2308                 return rte_flow_error_set(error, ENOTSUP,
2309                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2310                                           "set VLAN VID action must follow push"
2311                                           " VLAN action or match on VLAN item");
2312         if (action_flags & MLX5_FLOW_ACTION_OF_SET_VLAN_VID)
2313                 return rte_flow_error_set(error, ENOTSUP,
2314                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2315                                           "Multiple VLAN VID modifications are "
2316                                           "not supported");
2317         if (action_flags & MLX5_FLOW_ACTION_PORT_ID)
2318                 return rte_flow_error_set(error, EINVAL,
2319                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2320                                           "wrong action order, port_id should "
2321                                           "be after set VLAN VID");
2322         return 0;
2323 }
2324
2325 /*
2326  * Validate the FLAG action.
2327  *
2328  * @param[in] dev
2329  *   Pointer to the rte_eth_dev structure.
2330  * @param[in] action_flags
2331  *   Holds the actions detected until now.
2332  * @param[in] attr
2333  *   Pointer to flow attributes
2334  * @param[out] error
2335  *   Pointer to error structure.
2336  *
2337  * @return
2338  *   0 on success, a negative errno value otherwise and rte_errno is set.
2339  */
2340 static int
2341 flow_dv_validate_action_flag(struct rte_eth_dev *dev,
2342                              uint64_t action_flags,
2343                              const struct rte_flow_attr *attr,
2344                              struct rte_flow_error *error)
2345 {
2346         struct mlx5_priv *priv = dev->data->dev_private;
2347         struct mlx5_dev_config *config = &priv->config;
2348         int ret;
2349
2350         /* Fall back if no extended metadata register support. */
2351         if (config->dv_xmeta_en == MLX5_XMETA_MODE_LEGACY)
2352                 return mlx5_flow_validate_action_flag(action_flags, attr,
2353                                                       error);
2354         /* Extensive metadata mode requires registers. */
2355         if (!mlx5_flow_ext_mreg_supported(dev))
2356                 return rte_flow_error_set(error, ENOTSUP,
2357                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2358                                           "no metadata registers "
2359                                           "to support flag action");
2360         if (!(priv->sh->dv_mark_mask & MLX5_FLOW_MARK_DEFAULT))
2361                 return rte_flow_error_set(error, ENOTSUP,
2362                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2363                                           "extended metadata register"
2364                                           " isn't available");
2365         ret = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
2366         if (ret < 0)
2367                 return ret;
2368         MLX5_ASSERT(ret > 0);
2369         if (action_flags & MLX5_FLOW_ACTION_MARK)
2370                 return rte_flow_error_set(error, EINVAL,
2371                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2372                                           "can't mark and flag in same flow");
2373         if (action_flags & MLX5_FLOW_ACTION_FLAG)
2374                 return rte_flow_error_set(error, EINVAL,
2375                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2376                                           "can't have 2 flag"
2377                                           " actions in same flow");
2378         return 0;
2379 }
2380
2381 /**
2382  * Validate MARK action.
2383  *
2384  * @param[in] dev
2385  *   Pointer to the rte_eth_dev structure.
2386  * @param[in] action
2387  *   Pointer to action.
2388  * @param[in] action_flags
2389  *   Holds the actions detected until now.
2390  * @param[in] attr
2391  *   Pointer to flow attributes
2392  * @param[out] error
2393  *   Pointer to error structure.
2394  *
2395  * @return
2396  *   0 on success, a negative errno value otherwise and rte_errno is set.
2397  */
2398 static int
2399 flow_dv_validate_action_mark(struct rte_eth_dev *dev,
2400                              const struct rte_flow_action *action,
2401                              uint64_t action_flags,
2402                              const struct rte_flow_attr *attr,
2403                              struct rte_flow_error *error)
2404 {
2405         struct mlx5_priv *priv = dev->data->dev_private;
2406         struct mlx5_dev_config *config = &priv->config;
2407         const struct rte_flow_action_mark *mark = action->conf;
2408         int ret;
2409
2410         /* Fall back if no extended metadata register support. */
2411         if (config->dv_xmeta_en == MLX5_XMETA_MODE_LEGACY)
2412                 return mlx5_flow_validate_action_mark(action, action_flags,
2413                                                       attr, error);
2414         /* Extensive metadata mode requires registers. */
2415         if (!mlx5_flow_ext_mreg_supported(dev))
2416                 return rte_flow_error_set(error, ENOTSUP,
2417                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2418                                           "no metadata registers "
2419                                           "to support mark action");
2420         if (!priv->sh->dv_mark_mask)
2421                 return rte_flow_error_set(error, ENOTSUP,
2422                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2423                                           "extended metadata register"
2424                                           " isn't available");
2425         ret = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
2426         if (ret < 0)
2427                 return ret;
2428         MLX5_ASSERT(ret > 0);
2429         if (!mark)
2430                 return rte_flow_error_set(error, EINVAL,
2431                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2432                                           "configuration cannot be null");
2433         if (mark->id >= (MLX5_FLOW_MARK_MAX & priv->sh->dv_mark_mask))
2434                 return rte_flow_error_set(error, EINVAL,
2435                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
2436                                           &mark->id,
2437                                           "mark id exceeds the limit");
2438         if (action_flags & MLX5_FLOW_ACTION_FLAG)
2439                 return rte_flow_error_set(error, EINVAL,
2440                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2441                                           "can't flag and mark in same flow");
2442         if (action_flags & MLX5_FLOW_ACTION_MARK)
2443                 return rte_flow_error_set(error, EINVAL,
2444                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2445                                           "can't have 2 mark actions in same"
2446                                           " flow");
2447         return 0;
2448 }
2449
2450 /**
2451  * Validate SET_META action.
2452  *
2453  * @param[in] dev
2454  *   Pointer to the rte_eth_dev structure.
2455  * @param[in] action
2456  *   Pointer to the action structure.
2457  * @param[in] action_flags
2458  *   Holds the actions detected until now.
2459  * @param[in] attr
2460  *   Pointer to flow attributes
2461  * @param[out] error
2462  *   Pointer to error structure.
2463  *
2464  * @return
2465  *   0 on success, a negative errno value otherwise and rte_errno is set.
2466  */
2467 static int
2468 flow_dv_validate_action_set_meta(struct rte_eth_dev *dev,
2469                                  const struct rte_flow_action *action,
2470                                  uint64_t action_flags __rte_unused,
2471                                  const struct rte_flow_attr *attr,
2472                                  struct rte_flow_error *error)
2473 {
2474         const struct rte_flow_action_set_meta *conf;
2475         uint32_t nic_mask = UINT32_MAX;
2476         int reg;
2477
2478         if (!mlx5_flow_ext_mreg_supported(dev))
2479                 return rte_flow_error_set(error, ENOTSUP,
2480                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2481                                           "extended metadata register"
2482                                           " isn't supported");
2483         reg = flow_dv_get_metadata_reg(dev, attr, error);
2484         if (reg < 0)
2485                 return reg;
2486         if (reg != REG_A && reg != REG_B) {
2487                 struct mlx5_priv *priv = dev->data->dev_private;
2488
2489                 nic_mask = priv->sh->dv_meta_mask;
2490         }
2491         if (!(action->conf))
2492                 return rte_flow_error_set(error, EINVAL,
2493                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2494                                           "configuration cannot be null");
2495         conf = (const struct rte_flow_action_set_meta *)action->conf;
2496         if (!conf->mask)
2497                 return rte_flow_error_set(error, EINVAL,
2498                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2499                                           "zero mask doesn't have any effect");
2500         if (conf->mask & ~nic_mask)
2501                 return rte_flow_error_set(error, EINVAL,
2502                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2503                                           "meta data must be within reg C0");
2504         return 0;
2505 }
2506
2507 /**
2508  * Validate SET_TAG action.
2509  *
2510  * @param[in] dev
2511  *   Pointer to the rte_eth_dev structure.
2512  * @param[in] action
2513  *   Pointer to the action structure.
2514  * @param[in] action_flags
2515  *   Holds the actions detected until now.
2516  * @param[in] attr
2517  *   Pointer to flow attributes
2518  * @param[out] error
2519  *   Pointer to error structure.
2520  *
2521  * @return
2522  *   0 on success, a negative errno value otherwise and rte_errno is set.
2523  */
2524 static int
2525 flow_dv_validate_action_set_tag(struct rte_eth_dev *dev,
2526                                 const struct rte_flow_action *action,
2527                                 uint64_t action_flags,
2528                                 const struct rte_flow_attr *attr,
2529                                 struct rte_flow_error *error)
2530 {
2531         const struct rte_flow_action_set_tag *conf;
2532         const uint64_t terminal_action_flags =
2533                 MLX5_FLOW_ACTION_DROP | MLX5_FLOW_ACTION_QUEUE |
2534                 MLX5_FLOW_ACTION_RSS;
2535         int ret;
2536
2537         if (!mlx5_flow_ext_mreg_supported(dev))
2538                 return rte_flow_error_set(error, ENOTSUP,
2539                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2540                                           "extensive metadata register"
2541                                           " isn't supported");
2542         if (!(action->conf))
2543                 return rte_flow_error_set(error, EINVAL,
2544                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2545                                           "configuration cannot be null");
2546         conf = (const struct rte_flow_action_set_tag *)action->conf;
2547         if (!conf->mask)
2548                 return rte_flow_error_set(error, EINVAL,
2549                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2550                                           "zero mask doesn't have any effect");
2551         ret = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, conf->index, error);
2552         if (ret < 0)
2553                 return ret;
2554         if (!attr->transfer && attr->ingress &&
2555             (action_flags & terminal_action_flags))
2556                 return rte_flow_error_set(error, EINVAL,
2557                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2558                                           "set_tag has no effect"
2559                                           " with terminal actions");
2560         return 0;
2561 }
2562
2563 /**
2564  * Validate count action.
2565  *
2566  * @param[in] dev
2567  *   Pointer to rte_eth_dev structure.
2568  * @param[out] error
2569  *   Pointer to error structure.
2570  *
2571  * @return
2572  *   0 on success, a negative errno value otherwise and rte_errno is set.
2573  */
2574 static int
2575 flow_dv_validate_action_count(struct rte_eth_dev *dev,
2576                               struct rte_flow_error *error)
2577 {
2578         struct mlx5_priv *priv = dev->data->dev_private;
2579
2580         if (!priv->config.devx)
2581                 goto notsup_err;
2582 #ifdef HAVE_IBV_FLOW_DEVX_COUNTERS
2583         return 0;
2584 #endif
2585 notsup_err:
2586         return rte_flow_error_set
2587                       (error, ENOTSUP,
2588                        RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2589                        NULL,
2590                        "count action not supported");
2591 }
2592
2593 /**
2594  * Validate the L2 encap action.
2595  *
2596  * @param[in] dev
2597  *   Pointer to the rte_eth_dev structure.
2598  * @param[in] action_flags
2599  *   Holds the actions detected until now.
2600  * @param[in] action
2601  *   Pointer to the action structure.
2602  * @param[in] attr
2603  *   Pointer to flow attributes.
2604  * @param[out] error
2605  *   Pointer to error structure.
2606  *
2607  * @return
2608  *   0 on success, a negative errno value otherwise and rte_errno is set.
2609  */
2610 static int
2611 flow_dv_validate_action_l2_encap(struct rte_eth_dev *dev,
2612                                  uint64_t action_flags,
2613                                  const struct rte_flow_action *action,
2614                                  const struct rte_flow_attr *attr,
2615                                  struct rte_flow_error *error)
2616 {
2617         const struct mlx5_priv *priv = dev->data->dev_private;
2618
2619         if (!(action->conf))
2620                 return rte_flow_error_set(error, EINVAL,
2621                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2622                                           "configuration cannot be null");
2623         if (action_flags & MLX5_FLOW_ACTION_ENCAP)
2624                 return rte_flow_error_set(error, EINVAL,
2625                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2626                                           "can only have a single encap action "
2627                                           "in a flow");
2628         if (!attr->transfer && priv->representor)
2629                 return rte_flow_error_set(error, ENOTSUP,
2630                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2631                                           "encap action for VF representor "
2632                                           "not supported on NIC table");
2633         return 0;
2634 }
2635
2636 /**
2637  * Validate a decap action.
2638  *
2639  * @param[in] dev
2640  *   Pointer to the rte_eth_dev structure.
2641  * @param[in] action_flags
2642  *   Holds the actions detected until now.
2643  * @param[in] attr
2644  *   Pointer to flow attributes
2645  * @param[out] error
2646  *   Pointer to error structure.
2647  *
2648  * @return
2649  *   0 on success, a negative errno value otherwise and rte_errno is set.
2650  */
2651 static int
2652 flow_dv_validate_action_decap(struct rte_eth_dev *dev,
2653                               uint64_t action_flags,
2654                               const struct rte_flow_attr *attr,
2655                               struct rte_flow_error *error)
2656 {
2657         const struct mlx5_priv *priv = dev->data->dev_private;
2658
2659         if (priv->config.hca_attr.scatter_fcs_w_decap_disable &&
2660             !priv->config.decap_en)
2661                 return rte_flow_error_set(error, ENOTSUP,
2662                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2663                                           "decap is not enabled");
2664         if (action_flags & MLX5_FLOW_XCAP_ACTIONS)
2665                 return rte_flow_error_set(error, ENOTSUP,
2666                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2667                                           action_flags &
2668                                           MLX5_FLOW_ACTION_DECAP ? "can only "
2669                                           "have a single decap action" : "decap "
2670                                           "after encap is not supported");
2671         if (action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS)
2672                 return rte_flow_error_set(error, EINVAL,
2673                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2674                                           "can't have decap action after"
2675                                           " modify action");
2676         if (attr->egress)
2677                 return rte_flow_error_set(error, ENOTSUP,
2678                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
2679                                           NULL,
2680                                           "decap action not supported for "
2681                                           "egress");
2682         if (!attr->transfer && priv->representor)
2683                 return rte_flow_error_set(error, ENOTSUP,
2684                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2685                                           "decap action for VF representor "
2686                                           "not supported on NIC table");
2687         return 0;
2688 }
2689
2690 const struct rte_flow_action_raw_decap empty_decap = {.data = NULL, .size = 0,};
2691
2692 /**
2693  * Validate the raw encap and decap actions.
2694  *
2695  * @param[in] dev
2696  *   Pointer to the rte_eth_dev structure.
2697  * @param[in] decap
2698  *   Pointer to the decap action.
2699  * @param[in] encap
2700  *   Pointer to the encap action.
2701  * @param[in] attr
2702  *   Pointer to flow attributes
2703  * @param[in/out] action_flags
2704  *   Holds the actions detected until now.
2705  * @param[out] actions_n
2706  *   pointer to the number of actions counter.
2707  * @param[out] error
2708  *   Pointer to error structure.
2709  *
2710  * @return
2711  *   0 on success, a negative errno value otherwise and rte_errno is set.
2712  */
2713 static int
2714 flow_dv_validate_action_raw_encap_decap
2715         (struct rte_eth_dev *dev,
2716          const struct rte_flow_action_raw_decap *decap,
2717          const struct rte_flow_action_raw_encap *encap,
2718          const struct rte_flow_attr *attr, uint64_t *action_flags,
2719          int *actions_n, struct rte_flow_error *error)
2720 {
2721         const struct mlx5_priv *priv = dev->data->dev_private;
2722         int ret;
2723
2724         if (encap && (!encap->size || !encap->data))
2725                 return rte_flow_error_set(error, EINVAL,
2726                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2727                                           "raw encap data cannot be empty");
2728         if (decap && encap) {
2729                 if (decap->size <= MLX5_ENCAPSULATION_DECISION_SIZE &&
2730                     encap->size > MLX5_ENCAPSULATION_DECISION_SIZE)
2731                         /* L3 encap. */
2732                         decap = NULL;
2733                 else if (encap->size <=
2734                            MLX5_ENCAPSULATION_DECISION_SIZE &&
2735                            decap->size >
2736                            MLX5_ENCAPSULATION_DECISION_SIZE)
2737                         /* L3 decap. */
2738                         encap = NULL;
2739                 else if (encap->size >
2740                            MLX5_ENCAPSULATION_DECISION_SIZE &&
2741                            decap->size >
2742                            MLX5_ENCAPSULATION_DECISION_SIZE)
2743                         /* 2 L2 actions: encap and decap. */
2744                         ;
2745                 else
2746                         return rte_flow_error_set(error,
2747                                 ENOTSUP,
2748                                 RTE_FLOW_ERROR_TYPE_ACTION,
2749                                 NULL, "unsupported too small "
2750                                 "raw decap and too small raw "
2751                                 "encap combination");
2752         }
2753         if (decap) {
2754                 ret = flow_dv_validate_action_decap(dev, *action_flags, attr,
2755                                                     error);
2756                 if (ret < 0)
2757                         return ret;
2758                 *action_flags |= MLX5_FLOW_ACTION_DECAP;
2759                 ++(*actions_n);
2760         }
2761         if (encap) {
2762                 if (encap->size <= MLX5_ENCAPSULATION_DECISION_SIZE)
2763                         return rte_flow_error_set(error, ENOTSUP,
2764                                                   RTE_FLOW_ERROR_TYPE_ACTION,
2765                                                   NULL,
2766                                                   "small raw encap size");
2767                 if (*action_flags & MLX5_FLOW_ACTION_ENCAP)
2768                         return rte_flow_error_set(error, EINVAL,
2769                                                   RTE_FLOW_ERROR_TYPE_ACTION,
2770                                                   NULL,
2771                                                   "more than one encap action");
2772                 if (!attr->transfer && priv->representor)
2773                         return rte_flow_error_set
2774                                         (error, ENOTSUP,
2775                                          RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2776                                          "encap action for VF representor "
2777                                          "not supported on NIC table");
2778                 *action_flags |= MLX5_FLOW_ACTION_ENCAP;
2779                 ++(*actions_n);
2780         }
2781         return 0;
2782 }
2783
2784 /**
2785  * Match encap_decap resource.
2786  *
2787  * @param entry
2788  *   Pointer to exist resource entry object.
2789  * @param ctx
2790  *   Pointer to new encap_decap resource.
2791  *
2792  * @return
2793  *   0 on matching, -1 otherwise.
2794  */
2795 static int
2796 flow_dv_encap_decap_resource_match(struct mlx5_hlist_entry *entry, void *ctx)
2797 {
2798         struct mlx5_flow_dv_encap_decap_resource *resource;
2799         struct mlx5_flow_dv_encap_decap_resource *cache_resource;
2800
2801         resource = (struct mlx5_flow_dv_encap_decap_resource *)ctx;
2802         cache_resource = container_of(entry,
2803                                       struct mlx5_flow_dv_encap_decap_resource,
2804                                       entry);
2805         if (resource->entry.key == cache_resource->entry.key &&
2806             resource->reformat_type == cache_resource->reformat_type &&
2807             resource->ft_type == cache_resource->ft_type &&
2808             resource->flags == cache_resource->flags &&
2809             resource->size == cache_resource->size &&
2810             !memcmp((const void *)resource->buf,
2811                     (const void *)cache_resource->buf,
2812                     resource->size))
2813                 return 0;
2814         return -1;
2815 }
2816
2817 /**
2818  * Find existing encap/decap resource or create and register a new one.
2819  *
2820  * @param[in, out] dev
2821  *   Pointer to rte_eth_dev structure.
2822  * @param[in, out] resource
2823  *   Pointer to encap/decap resource.
2824  * @parm[in, out] dev_flow
2825  *   Pointer to the dev_flow.
2826  * @param[out] error
2827  *   pointer to error structure.
2828  *
2829  * @return
2830  *   0 on success otherwise -errno and errno is set.
2831  */
2832 static int
2833 flow_dv_encap_decap_resource_register
2834                         (struct rte_eth_dev *dev,
2835                          struct mlx5_flow_dv_encap_decap_resource *resource,
2836                          struct mlx5_flow *dev_flow,
2837                          struct rte_flow_error *error)
2838 {
2839         struct mlx5_priv *priv = dev->data->dev_private;
2840         struct mlx5_dev_ctx_shared *sh = priv->sh;
2841         struct mlx5_flow_dv_encap_decap_resource *cache_resource;
2842         struct mlx5dv_dr_domain *domain;
2843         struct mlx5_hlist_entry *entry;
2844         union mlx5_flow_encap_decap_key encap_decap_key = {
2845                 {
2846                         .ft_type = resource->ft_type,
2847                         .refmt_type = resource->reformat_type,
2848                         .buf_size = resource->size,
2849                         .table_level = !!dev_flow->dv.group,
2850                         .cksum = 0,
2851                 }
2852         };
2853         int ret;
2854
2855         resource->flags = dev_flow->dv.group ? 0 : 1;
2856         if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB)
2857                 domain = sh->fdb_domain;
2858         else if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_NIC_RX)
2859                 domain = sh->rx_domain;
2860         else
2861                 domain = sh->tx_domain;
2862         encap_decap_key.cksum = __rte_raw_cksum(resource->buf,
2863                                                 resource->size, 0);
2864         resource->entry.key = encap_decap_key.v64;
2865         /* Lookup a matching resource from cache. */
2866         entry = mlx5_hlist_lookup_ex(sh->encaps_decaps, resource->entry.key,
2867                                      flow_dv_encap_decap_resource_match,
2868                                      (void *)resource);
2869         if (entry) {
2870                 cache_resource = container_of(entry,
2871                         struct mlx5_flow_dv_encap_decap_resource, entry);
2872                 DRV_LOG(DEBUG, "encap/decap resource %p: refcnt %d++",
2873                         (void *)cache_resource,
2874                         __atomic_load_n(&cache_resource->refcnt,
2875                                         __ATOMIC_RELAXED));
2876                 __atomic_fetch_add(&cache_resource->refcnt, 1,
2877                                    __ATOMIC_RELAXED);
2878                 dev_flow->handle->dvh.rix_encap_decap = cache_resource->idx;
2879                 dev_flow->dv.encap_decap = cache_resource;
2880                 return 0;
2881         }
2882         /* Register new encap/decap resource. */
2883         cache_resource = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_DECAP_ENCAP],
2884                                        &dev_flow->handle->dvh.rix_encap_decap);
2885         if (!cache_resource)
2886                 return rte_flow_error_set(error, ENOMEM,
2887                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2888                                           "cannot allocate resource memory");
2889         *cache_resource = *resource;
2890         cache_resource->idx = dev_flow->handle->dvh.rix_encap_decap;
2891         ret = mlx5_flow_os_create_flow_action_packet_reformat
2892                                         (sh->ctx, domain, cache_resource,
2893                                          &cache_resource->action);
2894         if (ret) {
2895                 mlx5_free(cache_resource);
2896                 return rte_flow_error_set(error, ENOMEM,
2897                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2898                                           NULL, "cannot create action");
2899         }
2900         __atomic_store_n(&cache_resource->refcnt, 1, __ATOMIC_RELAXED);
2901         if (mlx5_hlist_insert_ex(sh->encaps_decaps, &cache_resource->entry,
2902                                  flow_dv_encap_decap_resource_match,
2903                                  (void *)cache_resource)) {
2904                 claim_zero(mlx5_flow_os_destroy_flow_action
2905                                                 (cache_resource->action));
2906                 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_DECAP_ENCAP],
2907                                 cache_resource->idx);
2908                 return rte_flow_error_set(error, EEXIST,
2909                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2910                                           NULL, "action exist");
2911         }
2912         dev_flow->dv.encap_decap = cache_resource;
2913         DRV_LOG(DEBUG, "new encap/decap resource %p: refcnt %d++",
2914                 (void *)cache_resource,
2915                 __atomic_load_n(&cache_resource->refcnt, __ATOMIC_RELAXED));
2916         return 0;
2917 }
2918
2919 /**
2920  * Find existing table jump resource or create and register a new one.
2921  *
2922  * @param[in, out] dev
2923  *   Pointer to rte_eth_dev structure.
2924  * @param[in, out] tbl
2925  *   Pointer to flow table resource.
2926  * @parm[in, out] dev_flow
2927  *   Pointer to the dev_flow.
2928  * @param[out] error
2929  *   pointer to error structure.
2930  *
2931  * @return
2932  *   0 on success otherwise -errno and errno is set.
2933  */
2934 static int
2935 flow_dv_jump_tbl_resource_register
2936                         (struct rte_eth_dev *dev __rte_unused,
2937                          struct mlx5_flow_tbl_resource *tbl,
2938                          struct mlx5_flow *dev_flow,
2939                          struct rte_flow_error *error __rte_unused)
2940 {
2941         struct mlx5_flow_tbl_data_entry *tbl_data =
2942                 container_of(tbl, struct mlx5_flow_tbl_data_entry, tbl);
2943
2944         MLX5_ASSERT(tbl);
2945         MLX5_ASSERT(tbl_data->jump.action);
2946         dev_flow->handle->rix_jump = tbl_data->idx;
2947         dev_flow->dv.jump = &tbl_data->jump;
2948         return 0;
2949 }
2950
2951 /**
2952  * Find existing table port ID resource or create and register a new one.
2953  *
2954  * @param[in, out] dev
2955  *   Pointer to rte_eth_dev structure.
2956  * @param[in, out] resource
2957  *   Pointer to port ID action resource.
2958  * @parm[in, out] dev_flow
2959  *   Pointer to the dev_flow.
2960  * @param[out] error
2961  *   pointer to error structure.
2962  *
2963  * @return
2964  *   0 on success otherwise -errno and errno is set.
2965  */
2966 static int
2967 flow_dv_port_id_action_resource_register
2968                         (struct rte_eth_dev *dev,
2969                          struct mlx5_flow_dv_port_id_action_resource *resource,
2970                          struct mlx5_flow *dev_flow,
2971                          struct rte_flow_error *error)
2972 {
2973         struct mlx5_priv *priv = dev->data->dev_private;
2974         struct mlx5_dev_ctx_shared *sh = priv->sh;
2975         struct mlx5_flow_dv_port_id_action_resource *cache_resource;
2976         uint32_t idx = 0;
2977         int ret;
2978
2979         /* Lookup a matching resource from cache. */
2980         ILIST_FOREACH(sh->ipool[MLX5_IPOOL_PORT_ID], sh->port_id_action_list,
2981                       idx, cache_resource, next) {
2982                 if (resource->port_id == cache_resource->port_id) {
2983                         DRV_LOG(DEBUG, "port id action resource resource %p: "
2984                                 "refcnt %d++",
2985                                 (void *)cache_resource,
2986                                 __atomic_load_n(&cache_resource->refcnt,
2987                                                 __ATOMIC_RELAXED));
2988                         __atomic_fetch_add(&cache_resource->refcnt, 1,
2989                                            __ATOMIC_RELAXED);
2990                         dev_flow->handle->rix_port_id_action = idx;
2991                         dev_flow->dv.port_id_action = cache_resource;
2992                         return 0;
2993                 }
2994         }
2995         /* Register new port id action resource. */
2996         cache_resource = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_PORT_ID],
2997                                        &dev_flow->handle->rix_port_id_action);
2998         if (!cache_resource)
2999                 return rte_flow_error_set(error, ENOMEM,
3000                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
3001                                           "cannot allocate resource memory");
3002         *cache_resource = *resource;
3003         ret = mlx5_flow_os_create_flow_action_dest_port
3004                                 (priv->sh->fdb_domain, resource->port_id,
3005                                  &cache_resource->action);
3006         if (ret) {
3007                 mlx5_free(cache_resource);
3008                 return rte_flow_error_set(error, ENOMEM,
3009                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3010                                           NULL, "cannot create action");
3011         }
3012         __atomic_store_n(&cache_resource->refcnt, 1, __ATOMIC_RELAXED);
3013         ILIST_INSERT(sh->ipool[MLX5_IPOOL_PORT_ID], &sh->port_id_action_list,
3014                      dev_flow->handle->rix_port_id_action, cache_resource,
3015                      next);
3016         dev_flow->dv.port_id_action = cache_resource;
3017         DRV_LOG(DEBUG, "new port id action resource %p: refcnt %d++",
3018                 (void *)cache_resource,
3019                 __atomic_load_n(&cache_resource->refcnt, __ATOMIC_RELAXED));
3020         return 0;
3021 }
3022
3023 /**
3024  * Find existing push vlan resource or create and register a new one.
3025  *
3026  * @param [in, out] dev
3027  *   Pointer to rte_eth_dev structure.
3028  * @param[in, out] resource
3029  *   Pointer to port ID action resource.
3030  * @parm[in, out] dev_flow
3031  *   Pointer to the dev_flow.
3032  * @param[out] error
3033  *   pointer to error structure.
3034  *
3035  * @return
3036  *   0 on success otherwise -errno and errno is set.
3037  */
3038 static int
3039 flow_dv_push_vlan_action_resource_register
3040                        (struct rte_eth_dev *dev,
3041                         struct mlx5_flow_dv_push_vlan_action_resource *resource,
3042                         struct mlx5_flow *dev_flow,
3043                         struct rte_flow_error *error)
3044 {
3045         struct mlx5_priv *priv = dev->data->dev_private;
3046         struct mlx5_dev_ctx_shared *sh = priv->sh;
3047         struct mlx5_flow_dv_push_vlan_action_resource *cache_resource;
3048         struct mlx5dv_dr_domain *domain;
3049         uint32_t idx = 0;
3050         int ret;
3051
3052         /* Lookup a matching resource from cache. */
3053         ILIST_FOREACH(sh->ipool[MLX5_IPOOL_PUSH_VLAN],
3054                       sh->push_vlan_action_list, idx, cache_resource, next) {
3055                 if (resource->vlan_tag == cache_resource->vlan_tag &&
3056                     resource->ft_type == cache_resource->ft_type) {
3057                         DRV_LOG(DEBUG, "push-VLAN action resource resource %p: "
3058                                 "refcnt %d++",
3059                                 (void *)cache_resource,
3060                                 __atomic_load_n(&cache_resource->refcnt,
3061                                                 __ATOMIC_RELAXED));
3062                         __atomic_fetch_add(&cache_resource->refcnt, 1,
3063                                            __ATOMIC_RELAXED);
3064                         dev_flow->handle->dvh.rix_push_vlan = idx;
3065                         dev_flow->dv.push_vlan_res = cache_resource;
3066                         return 0;
3067                 }
3068         }
3069         /* Register new push_vlan action resource. */
3070         cache_resource = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_PUSH_VLAN],
3071                                        &dev_flow->handle->dvh.rix_push_vlan);
3072         if (!cache_resource)
3073                 return rte_flow_error_set(error, ENOMEM,
3074                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
3075                                           "cannot allocate resource memory");
3076         *cache_resource = *resource;
3077         if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB)
3078                 domain = sh->fdb_domain;
3079         else if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_NIC_RX)
3080                 domain = sh->rx_domain;
3081         else
3082                 domain = sh->tx_domain;
3083         ret = mlx5_flow_os_create_flow_action_push_vlan
3084                                         (domain, resource->vlan_tag,
3085                                          &cache_resource->action);
3086         if (ret) {
3087                 mlx5_free(cache_resource);
3088                 return rte_flow_error_set(error, ENOMEM,
3089                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3090                                           NULL, "cannot create action");
3091         }
3092         __atomic_store_n(&cache_resource->refcnt, 1, __ATOMIC_RELAXED);
3093         ILIST_INSERT(sh->ipool[MLX5_IPOOL_PUSH_VLAN],
3094                      &sh->push_vlan_action_list,
3095                      dev_flow->handle->dvh.rix_push_vlan,
3096                      cache_resource, next);
3097         dev_flow->dv.push_vlan_res = cache_resource;
3098         DRV_LOG(DEBUG, "new push vlan action resource %p: refcnt %d++",
3099                 (void *)cache_resource,
3100                 __atomic_load_n(&cache_resource->refcnt, __ATOMIC_RELAXED));
3101         return 0;
3102 }
3103 /**
3104  * Get the size of specific rte_flow_item_type hdr size
3105  *
3106  * @param[in] item_type
3107  *   Tested rte_flow_item_type.
3108  *
3109  * @return
3110  *   sizeof struct item_type, 0 if void or irrelevant.
3111  */
3112 static size_t
3113 flow_dv_get_item_hdr_len(const enum rte_flow_item_type item_type)
3114 {
3115         size_t retval;
3116
3117         switch (item_type) {
3118         case RTE_FLOW_ITEM_TYPE_ETH:
3119                 retval = sizeof(struct rte_ether_hdr);
3120                 break;
3121         case RTE_FLOW_ITEM_TYPE_VLAN:
3122                 retval = sizeof(struct rte_vlan_hdr);
3123                 break;
3124         case RTE_FLOW_ITEM_TYPE_IPV4:
3125                 retval = sizeof(struct rte_ipv4_hdr);
3126                 break;
3127         case RTE_FLOW_ITEM_TYPE_IPV6:
3128                 retval = sizeof(struct rte_ipv6_hdr);
3129                 break;
3130         case RTE_FLOW_ITEM_TYPE_UDP:
3131                 retval = sizeof(struct rte_udp_hdr);
3132                 break;
3133         case RTE_FLOW_ITEM_TYPE_TCP:
3134                 retval = sizeof(struct rte_tcp_hdr);
3135                 break;
3136         case RTE_FLOW_ITEM_TYPE_VXLAN:
3137         case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
3138                 retval = sizeof(struct rte_vxlan_hdr);
3139                 break;
3140         case RTE_FLOW_ITEM_TYPE_GRE:
3141         case RTE_FLOW_ITEM_TYPE_NVGRE:
3142                 retval = sizeof(struct rte_gre_hdr);
3143                 break;
3144         case RTE_FLOW_ITEM_TYPE_MPLS:
3145                 retval = sizeof(struct rte_mpls_hdr);
3146                 break;
3147         case RTE_FLOW_ITEM_TYPE_VOID: /* Fall through. */
3148         default:
3149                 retval = 0;
3150                 break;
3151         }
3152         return retval;
3153 }
3154
3155 #define MLX5_ENCAP_IPV4_VERSION         0x40
3156 #define MLX5_ENCAP_IPV4_IHL_MIN         0x05
3157 #define MLX5_ENCAP_IPV4_TTL_DEF         0x40
3158 #define MLX5_ENCAP_IPV6_VTC_FLOW        0x60000000
3159 #define MLX5_ENCAP_IPV6_HOP_LIMIT       0xff
3160 #define MLX5_ENCAP_VXLAN_FLAGS          0x08000000
3161 #define MLX5_ENCAP_VXLAN_GPE_FLAGS      0x04
3162
3163 /**
3164  * Convert the encap action data from list of rte_flow_item to raw buffer
3165  *
3166  * @param[in] items
3167  *   Pointer to rte_flow_item objects list.
3168  * @param[out] buf
3169  *   Pointer to the output buffer.
3170  * @param[out] size
3171  *   Pointer to the output buffer size.
3172  * @param[out] error
3173  *   Pointer to the error structure.
3174  *
3175  * @return
3176  *   0 on success, a negative errno value otherwise and rte_errno is set.
3177  */
3178 static int
3179 flow_dv_convert_encap_data(const struct rte_flow_item *items, uint8_t *buf,
3180                            size_t *size, struct rte_flow_error *error)
3181 {
3182         struct rte_ether_hdr *eth = NULL;
3183         struct rte_vlan_hdr *vlan = NULL;
3184         struct rte_ipv4_hdr *ipv4 = NULL;
3185         struct rte_ipv6_hdr *ipv6 = NULL;
3186         struct rte_udp_hdr *udp = NULL;
3187         struct rte_vxlan_hdr *vxlan = NULL;
3188         struct rte_vxlan_gpe_hdr *vxlan_gpe = NULL;
3189         struct rte_gre_hdr *gre = NULL;
3190         size_t len;
3191         size_t temp_size = 0;
3192
3193         if (!items)
3194                 return rte_flow_error_set(error, EINVAL,
3195                                           RTE_FLOW_ERROR_TYPE_ACTION,
3196                                           NULL, "invalid empty data");
3197         for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
3198                 len = flow_dv_get_item_hdr_len(items->type);
3199                 if (len + temp_size > MLX5_ENCAP_MAX_LEN)
3200                         return rte_flow_error_set(error, EINVAL,
3201                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3202                                                   (void *)items->type,
3203                                                   "items total size is too big"
3204                                                   " for encap action");
3205                 rte_memcpy((void *)&buf[temp_size], items->spec, len);
3206                 switch (items->type) {
3207                 case RTE_FLOW_ITEM_TYPE_ETH:
3208                         eth = (struct rte_ether_hdr *)&buf[temp_size];
3209                         break;
3210                 case RTE_FLOW_ITEM_TYPE_VLAN:
3211                         vlan = (struct rte_vlan_hdr *)&buf[temp_size];
3212                         if (!eth)
3213                                 return rte_flow_error_set(error, EINVAL,
3214                                                 RTE_FLOW_ERROR_TYPE_ACTION,
3215                                                 (void *)items->type,
3216                                                 "eth header not found");
3217                         if (!eth->ether_type)
3218                                 eth->ether_type = RTE_BE16(RTE_ETHER_TYPE_VLAN);
3219                         break;
3220                 case RTE_FLOW_ITEM_TYPE_IPV4:
3221                         ipv4 = (struct rte_ipv4_hdr *)&buf[temp_size];
3222                         if (!vlan && !eth)
3223                                 return rte_flow_error_set(error, EINVAL,
3224                                                 RTE_FLOW_ERROR_TYPE_ACTION,
3225                                                 (void *)items->type,
3226                                                 "neither eth nor vlan"
3227                                                 " header found");
3228                         if (vlan && !vlan->eth_proto)
3229                                 vlan->eth_proto = RTE_BE16(RTE_ETHER_TYPE_IPV4);
3230                         else if (eth && !eth->ether_type)
3231                                 eth->ether_type = RTE_BE16(RTE_ETHER_TYPE_IPV4);
3232                         if (!ipv4->version_ihl)
3233                                 ipv4->version_ihl = MLX5_ENCAP_IPV4_VERSION |
3234                                                     MLX5_ENCAP_IPV4_IHL_MIN;
3235                         if (!ipv4->time_to_live)
3236                                 ipv4->time_to_live = MLX5_ENCAP_IPV4_TTL_DEF;
3237                         break;
3238                 case RTE_FLOW_ITEM_TYPE_IPV6:
3239                         ipv6 = (struct rte_ipv6_hdr *)&buf[temp_size];
3240                         if (!vlan && !eth)
3241                                 return rte_flow_error_set(error, EINVAL,
3242                                                 RTE_FLOW_ERROR_TYPE_ACTION,
3243                                                 (void *)items->type,
3244                                                 "neither eth nor vlan"
3245                                                 " header found");
3246                         if (vlan && !vlan->eth_proto)
3247                                 vlan->eth_proto = RTE_BE16(RTE_ETHER_TYPE_IPV6);
3248                         else if (eth && !eth->ether_type)
3249                                 eth->ether_type = RTE_BE16(RTE_ETHER_TYPE_IPV6);
3250                         if (!ipv6->vtc_flow)
3251                                 ipv6->vtc_flow =
3252                                         RTE_BE32(MLX5_ENCAP_IPV6_VTC_FLOW);
3253                         if (!ipv6->hop_limits)
3254                                 ipv6->hop_limits = MLX5_ENCAP_IPV6_HOP_LIMIT;
3255                         break;
3256                 case RTE_FLOW_ITEM_TYPE_UDP:
3257                         udp = (struct rte_udp_hdr *)&buf[temp_size];
3258                         if (!ipv4 && !ipv6)
3259                                 return rte_flow_error_set(error, EINVAL,
3260                                                 RTE_FLOW_ERROR_TYPE_ACTION,
3261                                                 (void *)items->type,
3262                                                 "ip header not found");
3263                         if (ipv4 && !ipv4->next_proto_id)
3264                                 ipv4->next_proto_id = IPPROTO_UDP;
3265                         else if (ipv6 && !ipv6->proto)
3266                                 ipv6->proto = IPPROTO_UDP;
3267                         break;
3268                 case RTE_FLOW_ITEM_TYPE_VXLAN:
3269                         vxlan = (struct rte_vxlan_hdr *)&buf[temp_size];
3270                         if (!udp)
3271                                 return rte_flow_error_set(error, EINVAL,
3272                                                 RTE_FLOW_ERROR_TYPE_ACTION,
3273                                                 (void *)items->type,
3274                                                 "udp header not found");
3275                         if (!udp->dst_port)
3276                                 udp->dst_port = RTE_BE16(MLX5_UDP_PORT_VXLAN);
3277                         if (!vxlan->vx_flags)
3278                                 vxlan->vx_flags =
3279                                         RTE_BE32(MLX5_ENCAP_VXLAN_FLAGS);
3280                         break;
3281                 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
3282                         vxlan_gpe = (struct rte_vxlan_gpe_hdr *)&buf[temp_size];
3283                         if (!udp)
3284                                 return rte_flow_error_set(error, EINVAL,
3285                                                 RTE_FLOW_ERROR_TYPE_ACTION,
3286                                                 (void *)items->type,
3287                                                 "udp header not found");
3288                         if (!vxlan_gpe->proto)
3289                                 return rte_flow_error_set(error, EINVAL,
3290                                                 RTE_FLOW_ERROR_TYPE_ACTION,
3291                                                 (void *)items->type,
3292                                                 "next protocol not found");
3293                         if (!udp->dst_port)
3294                                 udp->dst_port =
3295                                         RTE_BE16(MLX5_UDP_PORT_VXLAN_GPE);
3296                         if (!vxlan_gpe->vx_flags)
3297                                 vxlan_gpe->vx_flags =
3298                                                 MLX5_ENCAP_VXLAN_GPE_FLAGS;
3299                         break;
3300                 case RTE_FLOW_ITEM_TYPE_GRE:
3301                 case RTE_FLOW_ITEM_TYPE_NVGRE:
3302                         gre = (struct rte_gre_hdr *)&buf[temp_size];
3303                         if (!gre->proto)
3304                                 return rte_flow_error_set(error, EINVAL,
3305                                                 RTE_FLOW_ERROR_TYPE_ACTION,
3306                                                 (void *)items->type,
3307                                                 "next protocol not found");
3308                         if (!ipv4 && !ipv6)
3309                                 return rte_flow_error_set(error, EINVAL,
3310                                                 RTE_FLOW_ERROR_TYPE_ACTION,
3311                                                 (void *)items->type,
3312                                                 "ip header not found");
3313                         if (ipv4 && !ipv4->next_proto_id)
3314                                 ipv4->next_proto_id = IPPROTO_GRE;
3315                         else if (ipv6 && !ipv6->proto)
3316                                 ipv6->proto = IPPROTO_GRE;
3317                         break;
3318                 case RTE_FLOW_ITEM_TYPE_VOID:
3319                         break;
3320                 default:
3321                         return rte_flow_error_set(error, EINVAL,
3322                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3323                                                   (void *)items->type,
3324                                                   "unsupported item type");
3325                         break;
3326                 }
3327                 temp_size += len;
3328         }
3329         *size = temp_size;
3330         return 0;
3331 }
3332
3333 static int
3334 flow_dv_zero_encap_udp_csum(void *data, struct rte_flow_error *error)
3335 {
3336         struct rte_ether_hdr *eth = NULL;
3337         struct rte_vlan_hdr *vlan = NULL;
3338         struct rte_ipv6_hdr *ipv6 = NULL;
3339         struct rte_udp_hdr *udp = NULL;
3340         char *next_hdr;
3341         uint16_t proto;
3342
3343         eth = (struct rte_ether_hdr *)data;
3344         next_hdr = (char *)(eth + 1);
3345         proto = RTE_BE16(eth->ether_type);
3346
3347         /* VLAN skipping */
3348         while (proto == RTE_ETHER_TYPE_VLAN || proto == RTE_ETHER_TYPE_QINQ) {
3349                 vlan = (struct rte_vlan_hdr *)next_hdr;
3350                 proto = RTE_BE16(vlan->eth_proto);
3351                 next_hdr += sizeof(struct rte_vlan_hdr);
3352         }
3353
3354         /* HW calculates IPv4 csum. no need to proceed */
3355         if (proto == RTE_ETHER_TYPE_IPV4)
3356                 return 0;
3357
3358         /* non IPv4/IPv6 header. not supported */
3359         if (proto != RTE_ETHER_TYPE_IPV6) {
3360                 return rte_flow_error_set(error, ENOTSUP,
3361                                           RTE_FLOW_ERROR_TYPE_ACTION,
3362                                           NULL, "Cannot offload non IPv4/IPv6");
3363         }
3364
3365         ipv6 = (struct rte_ipv6_hdr *)next_hdr;
3366
3367         /* ignore non UDP */
3368         if (ipv6->proto != IPPROTO_UDP)
3369                 return 0;
3370
3371         udp = (struct rte_udp_hdr *)(ipv6 + 1);
3372         udp->dgram_cksum = 0;
3373
3374         return 0;
3375 }
3376
3377 /**
3378  * Convert L2 encap action to DV specification.
3379  *
3380  * @param[in] dev
3381  *   Pointer to rte_eth_dev structure.
3382  * @param[in] action
3383  *   Pointer to action structure.
3384  * @param[in, out] dev_flow
3385  *   Pointer to the mlx5_flow.
3386  * @param[in] transfer
3387  *   Mark if the flow is E-Switch flow.
3388  * @param[out] error
3389  *   Pointer to the error structure.
3390  *
3391  * @return
3392  *   0 on success, a negative errno value otherwise and rte_errno is set.
3393  */
3394 static int
3395 flow_dv_create_action_l2_encap(struct rte_eth_dev *dev,
3396                                const struct rte_flow_action *action,
3397                                struct mlx5_flow *dev_flow,
3398                                uint8_t transfer,
3399                                struct rte_flow_error *error)
3400 {
3401         const struct rte_flow_item *encap_data;
3402         const struct rte_flow_action_raw_encap *raw_encap_data;
3403         struct mlx5_flow_dv_encap_decap_resource res = {
3404                 .reformat_type =
3405                         MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TO_L2_TUNNEL,
3406                 .ft_type = transfer ? MLX5DV_FLOW_TABLE_TYPE_FDB :
3407                                       MLX5DV_FLOW_TABLE_TYPE_NIC_TX,
3408         };
3409
3410         if (action->type == RTE_FLOW_ACTION_TYPE_RAW_ENCAP) {
3411                 raw_encap_data =
3412                         (const struct rte_flow_action_raw_encap *)action->conf;
3413                 res.size = raw_encap_data->size;
3414                 memcpy(res.buf, raw_encap_data->data, res.size);
3415         } else {
3416                 if (action->type == RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP)
3417                         encap_data =
3418                                 ((const struct rte_flow_action_vxlan_encap *)
3419                                                 action->conf)->definition;
3420                 else
3421                         encap_data =
3422                                 ((const struct rte_flow_action_nvgre_encap *)
3423                                                 action->conf)->definition;
3424                 if (flow_dv_convert_encap_data(encap_data, res.buf,
3425                                                &res.size, error))
3426                         return -rte_errno;
3427         }
3428         if (flow_dv_zero_encap_udp_csum(res.buf, error))
3429                 return -rte_errno;
3430         if (flow_dv_encap_decap_resource_register(dev, &res, dev_flow, error))
3431                 return rte_flow_error_set(error, EINVAL,
3432                                           RTE_FLOW_ERROR_TYPE_ACTION,
3433                                           NULL, "can't create L2 encap action");
3434         return 0;
3435 }
3436
3437 /**
3438  * Convert L2 decap action to DV specification.
3439  *
3440  * @param[in] dev
3441  *   Pointer to rte_eth_dev structure.
3442  * @param[in, out] dev_flow
3443  *   Pointer to the mlx5_flow.
3444  * @param[in] transfer
3445  *   Mark if the flow is E-Switch flow.
3446  * @param[out] error
3447  *   Pointer to the error structure.
3448  *
3449  * @return
3450  *   0 on success, a negative errno value otherwise and rte_errno is set.
3451  */
3452 static int
3453 flow_dv_create_action_l2_decap(struct rte_eth_dev *dev,
3454                                struct mlx5_flow *dev_flow,
3455                                uint8_t transfer,
3456                                struct rte_flow_error *error)
3457 {
3458         struct mlx5_flow_dv_encap_decap_resource res = {
3459                 .size = 0,
3460                 .reformat_type =
3461                         MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TUNNEL_TO_L2,
3462                 .ft_type = transfer ? MLX5DV_FLOW_TABLE_TYPE_FDB :
3463                                       MLX5DV_FLOW_TABLE_TYPE_NIC_RX,
3464         };
3465
3466         if (flow_dv_encap_decap_resource_register(dev, &res, dev_flow, error))
3467                 return rte_flow_error_set(error, EINVAL,
3468                                           RTE_FLOW_ERROR_TYPE_ACTION,
3469                                           NULL, "can't create L2 decap action");
3470         return 0;
3471 }
3472
3473 /**
3474  * Convert raw decap/encap (L3 tunnel) action to DV specification.
3475  *
3476  * @param[in] dev
3477  *   Pointer to rte_eth_dev structure.
3478  * @param[in] action
3479  *   Pointer to action structure.
3480  * @param[in, out] dev_flow
3481  *   Pointer to the mlx5_flow.
3482  * @param[in] attr
3483  *   Pointer to the flow attributes.
3484  * @param[out] error
3485  *   Pointer to the error structure.
3486  *
3487  * @return
3488  *   0 on success, a negative errno value otherwise and rte_errno is set.
3489  */
3490 static int
3491 flow_dv_create_action_raw_encap(struct rte_eth_dev *dev,
3492                                 const struct rte_flow_action *action,
3493                                 struct mlx5_flow *dev_flow,
3494                                 const struct rte_flow_attr *attr,
3495                                 struct rte_flow_error *error)
3496 {
3497         const struct rte_flow_action_raw_encap *encap_data;
3498         struct mlx5_flow_dv_encap_decap_resource res;
3499
3500         memset(&res, 0, sizeof(res));
3501         encap_data = (const struct rte_flow_action_raw_encap *)action->conf;
3502         res.size = encap_data->size;
3503         memcpy(res.buf, encap_data->data, res.size);
3504         res.reformat_type = res.size < MLX5_ENCAPSULATION_DECISION_SIZE ?
3505                 MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L3_TUNNEL_TO_L2 :
3506                 MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TO_L3_TUNNEL;
3507         if (attr->transfer)
3508                 res.ft_type = MLX5DV_FLOW_TABLE_TYPE_FDB;
3509         else
3510                 res.ft_type = attr->egress ? MLX5DV_FLOW_TABLE_TYPE_NIC_TX :
3511                                              MLX5DV_FLOW_TABLE_TYPE_NIC_RX;
3512         if (flow_dv_encap_decap_resource_register(dev, &res, dev_flow, error))
3513                 return rte_flow_error_set(error, EINVAL,
3514                                           RTE_FLOW_ERROR_TYPE_ACTION,
3515                                           NULL, "can't create encap action");
3516         return 0;
3517 }
3518
3519 /**
3520  * Create action push VLAN.
3521  *
3522  * @param[in] dev
3523  *   Pointer to rte_eth_dev structure.
3524  * @param[in] attr
3525  *   Pointer to the flow attributes.
3526  * @param[in] vlan
3527  *   Pointer to the vlan to push to the Ethernet header.
3528  * @param[in, out] dev_flow
3529  *   Pointer to the mlx5_flow.
3530  * @param[out] error
3531  *   Pointer to the error structure.
3532  *
3533  * @return
3534  *   0 on success, a negative errno value otherwise and rte_errno is set.
3535  */
3536 static int
3537 flow_dv_create_action_push_vlan(struct rte_eth_dev *dev,
3538                                 const struct rte_flow_attr *attr,
3539                                 const struct rte_vlan_hdr *vlan,
3540                                 struct mlx5_flow *dev_flow,
3541                                 struct rte_flow_error *error)
3542 {
3543         struct mlx5_flow_dv_push_vlan_action_resource res;
3544
3545         memset(&res, 0, sizeof(res));
3546         res.vlan_tag =
3547                 rte_cpu_to_be_32(((uint32_t)vlan->eth_proto) << 16 |
3548                                  vlan->vlan_tci);
3549         if (attr->transfer)
3550                 res.ft_type = MLX5DV_FLOW_TABLE_TYPE_FDB;
3551         else
3552                 res.ft_type = attr->egress ? MLX5DV_FLOW_TABLE_TYPE_NIC_TX :
3553                                              MLX5DV_FLOW_TABLE_TYPE_NIC_RX;
3554         return flow_dv_push_vlan_action_resource_register
3555                                             (dev, &res, dev_flow, error);
3556 }
3557
3558 static int fdb_mirror;
3559
3560 /**
3561  * Validate the modify-header actions.
3562  *
3563  * @param[in] action_flags
3564  *   Holds the actions detected until now.
3565  * @param[in] action
3566  *   Pointer to the modify action.
3567  * @param[out] error
3568  *   Pointer to error structure.
3569  *
3570  * @return
3571  *   0 on success, a negative errno value otherwise and rte_errno is set.
3572  */
3573 static int
3574 flow_dv_validate_action_modify_hdr(const uint64_t action_flags,
3575                                    const struct rte_flow_action *action,
3576                                    struct rte_flow_error *error)
3577 {
3578         if (action->type != RTE_FLOW_ACTION_TYPE_DEC_TTL && !action->conf)
3579                 return rte_flow_error_set(error, EINVAL,
3580                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
3581                                           NULL, "action configuration not set");
3582         if (action_flags & MLX5_FLOW_ACTION_ENCAP)
3583                 return rte_flow_error_set(error, EINVAL,
3584                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3585                                           "can't have encap action before"
3586                                           " modify action");
3587         if ((action_flags & MLX5_FLOW_ACTION_SAMPLE) && fdb_mirror)
3588                 return rte_flow_error_set(error, EINVAL,
3589                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3590                                           "can't support sample action before"
3591                                           " modify action for E-Switch"
3592                                           " mirroring");
3593         return 0;
3594 }
3595
3596 /**
3597  * Validate the modify-header MAC address actions.
3598  *
3599  * @param[in] action_flags
3600  *   Holds the actions detected until now.
3601  * @param[in] action
3602  *   Pointer to the modify action.
3603  * @param[in] item_flags
3604  *   Holds the items detected.
3605  * @param[out] error
3606  *   Pointer to error structure.
3607  *
3608  * @return
3609  *   0 on success, a negative errno value otherwise and rte_errno is set.
3610  */
3611 static int
3612 flow_dv_validate_action_modify_mac(const uint64_t action_flags,
3613                                    const struct rte_flow_action *action,
3614                                    const uint64_t item_flags,
3615                                    struct rte_flow_error *error)
3616 {
3617         int ret = 0;
3618
3619         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3620         if (!ret) {
3621                 if (!(item_flags & MLX5_FLOW_LAYER_L2))
3622                         return rte_flow_error_set(error, EINVAL,
3623                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3624                                                   NULL,
3625                                                   "no L2 item in pattern");
3626         }
3627         return ret;
3628 }
3629
3630 /**
3631  * Validate the modify-header IPv4 address actions.
3632  *
3633  * @param[in] action_flags
3634  *   Holds the actions detected until now.
3635  * @param[in] action
3636  *   Pointer to the modify action.
3637  * @param[in] item_flags
3638  *   Holds the items detected.
3639  * @param[out] error
3640  *   Pointer to error structure.
3641  *
3642  * @return
3643  *   0 on success, a negative errno value otherwise and rte_errno is set.
3644  */
3645 static int
3646 flow_dv_validate_action_modify_ipv4(const uint64_t action_flags,
3647                                     const struct rte_flow_action *action,
3648                                     const uint64_t item_flags,
3649                                     struct rte_flow_error *error)
3650 {
3651         int ret = 0;
3652         uint64_t layer;
3653
3654         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3655         if (!ret) {
3656                 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
3657                                  MLX5_FLOW_LAYER_INNER_L3_IPV4 :
3658                                  MLX5_FLOW_LAYER_OUTER_L3_IPV4;
3659                 if (!(item_flags & layer))
3660                         return rte_flow_error_set(error, EINVAL,
3661                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3662                                                   NULL,
3663                                                   "no ipv4 item in pattern");
3664         }
3665         return ret;
3666 }
3667
3668 /**
3669  * Validate the modify-header IPv6 address actions.
3670  *
3671  * @param[in] action_flags
3672  *   Holds the actions detected until now.
3673  * @param[in] action
3674  *   Pointer to the modify action.
3675  * @param[in] item_flags
3676  *   Holds the items detected.
3677  * @param[out] error
3678  *   Pointer to error structure.
3679  *
3680  * @return
3681  *   0 on success, a negative errno value otherwise and rte_errno is set.
3682  */
3683 static int
3684 flow_dv_validate_action_modify_ipv6(const uint64_t action_flags,
3685                                     const struct rte_flow_action *action,
3686                                     const uint64_t item_flags,
3687                                     struct rte_flow_error *error)
3688 {
3689         int ret = 0;
3690         uint64_t layer;
3691
3692         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3693         if (!ret) {
3694                 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
3695                                  MLX5_FLOW_LAYER_INNER_L3_IPV6 :
3696                                  MLX5_FLOW_LAYER_OUTER_L3_IPV6;
3697                 if (!(item_flags & layer))
3698                         return rte_flow_error_set(error, EINVAL,
3699                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3700                                                   NULL,
3701                                                   "no ipv6 item in pattern");
3702         }
3703         return ret;
3704 }
3705
3706 /**
3707  * Validate the modify-header TP actions.
3708  *
3709  * @param[in] action_flags
3710  *   Holds the actions detected until now.
3711  * @param[in] action
3712  *   Pointer to the modify action.
3713  * @param[in] item_flags
3714  *   Holds the items detected.
3715  * @param[out] error
3716  *   Pointer to error structure.
3717  *
3718  * @return
3719  *   0 on success, a negative errno value otherwise and rte_errno is set.
3720  */
3721 static int
3722 flow_dv_validate_action_modify_tp(const uint64_t action_flags,
3723                                   const struct rte_flow_action *action,
3724                                   const uint64_t item_flags,
3725                                   struct rte_flow_error *error)
3726 {
3727         int ret = 0;
3728         uint64_t layer;
3729
3730         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3731         if (!ret) {
3732                 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
3733                                  MLX5_FLOW_LAYER_INNER_L4 :
3734                                  MLX5_FLOW_LAYER_OUTER_L4;
3735                 if (!(item_flags & layer))
3736                         return rte_flow_error_set(error, EINVAL,
3737                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3738                                                   NULL, "no transport layer "
3739                                                   "in pattern");
3740         }
3741         return ret;
3742 }
3743
3744 /**
3745  * Validate the modify-header actions of increment/decrement
3746  * TCP Sequence-number.
3747  *
3748  * @param[in] action_flags
3749  *   Holds the actions detected until now.
3750  * @param[in] action
3751  *   Pointer to the modify action.
3752  * @param[in] item_flags
3753  *   Holds the items detected.
3754  * @param[out] error
3755  *   Pointer to error structure.
3756  *
3757  * @return
3758  *   0 on success, a negative errno value otherwise and rte_errno is set.
3759  */
3760 static int
3761 flow_dv_validate_action_modify_tcp_seq(const uint64_t action_flags,
3762                                        const struct rte_flow_action *action,
3763                                        const uint64_t item_flags,
3764                                        struct rte_flow_error *error)
3765 {
3766         int ret = 0;
3767         uint64_t layer;
3768
3769         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3770         if (!ret) {
3771                 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
3772                                  MLX5_FLOW_LAYER_INNER_L4_TCP :
3773                                  MLX5_FLOW_LAYER_OUTER_L4_TCP;
3774                 if (!(item_flags & layer))
3775                         return rte_flow_error_set(error, EINVAL,
3776                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3777                                                   NULL, "no TCP item in"
3778                                                   " pattern");
3779                 if ((action->type == RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ &&
3780                         (action_flags & MLX5_FLOW_ACTION_DEC_TCP_SEQ)) ||
3781                     (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ &&
3782                         (action_flags & MLX5_FLOW_ACTION_INC_TCP_SEQ)))
3783                         return rte_flow_error_set(error, EINVAL,
3784                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3785                                                   NULL,
3786                                                   "cannot decrease and increase"
3787                                                   " TCP sequence number"
3788                                                   " at the same time");
3789         }
3790         return ret;
3791 }
3792
3793 /**
3794  * Validate the modify-header actions of increment/decrement
3795  * TCP Acknowledgment number.
3796  *
3797  * @param[in] action_flags
3798  *   Holds the actions detected until now.
3799  * @param[in] action
3800  *   Pointer to the modify action.
3801  * @param[in] item_flags
3802  *   Holds the items detected.
3803  * @param[out] error
3804  *   Pointer to error structure.
3805  *
3806  * @return
3807  *   0 on success, a negative errno value otherwise and rte_errno is set.
3808  */
3809 static int
3810 flow_dv_validate_action_modify_tcp_ack(const uint64_t action_flags,
3811                                        const struct rte_flow_action *action,
3812                                        const uint64_t item_flags,
3813                                        struct rte_flow_error *error)
3814 {
3815         int ret = 0;
3816         uint64_t layer;
3817
3818         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3819         if (!ret) {
3820                 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
3821                                  MLX5_FLOW_LAYER_INNER_L4_TCP :
3822                                  MLX5_FLOW_LAYER_OUTER_L4_TCP;
3823                 if (!(item_flags & layer))
3824                         return rte_flow_error_set(error, EINVAL,
3825                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3826                                                   NULL, "no TCP item in"
3827                                                   " pattern");
3828                 if ((action->type == RTE_FLOW_ACTION_TYPE_INC_TCP_ACK &&
3829                         (action_flags & MLX5_FLOW_ACTION_DEC_TCP_ACK)) ||
3830                     (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK &&
3831                         (action_flags & MLX5_FLOW_ACTION_INC_TCP_ACK)))
3832                         return rte_flow_error_set(error, EINVAL,
3833                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3834                                                   NULL,
3835                                                   "cannot decrease and increase"
3836                                                   " TCP acknowledgment number"
3837                                                   " at the same time");
3838         }
3839         return ret;
3840 }
3841
3842 /**
3843  * Validate the modify-header TTL actions.
3844  *
3845  * @param[in] action_flags
3846  *   Holds the actions detected until now.
3847  * @param[in] action
3848  *   Pointer to the modify action.
3849  * @param[in] item_flags
3850  *   Holds the items detected.
3851  * @param[out] error
3852  *   Pointer to error structure.
3853  *
3854  * @return
3855  *   0 on success, a negative errno value otherwise and rte_errno is set.
3856  */
3857 static int
3858 flow_dv_validate_action_modify_ttl(const uint64_t action_flags,
3859                                    const struct rte_flow_action *action,
3860                                    const uint64_t item_flags,
3861                                    struct rte_flow_error *error)
3862 {
3863         int ret = 0;
3864         uint64_t layer;
3865
3866         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3867         if (!ret) {
3868                 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
3869                                  MLX5_FLOW_LAYER_INNER_L3 :
3870                                  MLX5_FLOW_LAYER_OUTER_L3;
3871                 if (!(item_flags & layer))
3872                         return rte_flow_error_set(error, EINVAL,
3873                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3874                                                   NULL,
3875                                                   "no IP protocol in pattern");
3876         }
3877         return ret;
3878 }
3879
3880 /**
3881  * Validate jump action.
3882  *
3883  * @param[in] action
3884  *   Pointer to the jump action.
3885  * @param[in] action_flags
3886  *   Holds the actions detected until now.
3887  * @param[in] attributes
3888  *   Pointer to flow attributes
3889  * @param[in] external
3890  *   Action belongs to flow rule created by request external to PMD.
3891  * @param[out] error
3892  *   Pointer to error structure.
3893  *
3894  * @return
3895  *   0 on success, a negative errno value otherwise and rte_errno is set.
3896  */
3897 static int
3898 flow_dv_validate_action_jump(struct rte_eth_dev *dev,
3899                              const struct mlx5_flow_tunnel *tunnel,
3900                              const struct rte_flow_action *action,
3901                              uint64_t action_flags,
3902                              const struct rte_flow_attr *attributes,
3903                              bool external, struct rte_flow_error *error)
3904 {
3905         uint32_t target_group, table;
3906         int ret = 0;
3907         struct flow_grp_info grp_info = {
3908                 .external = !!external,
3909                 .transfer = !!attributes->transfer,
3910                 .fdb_def_rule = 1,
3911                 .std_tbl_fix = 0
3912         };
3913         if (action_flags & (MLX5_FLOW_FATE_ACTIONS |
3914                             MLX5_FLOW_FATE_ESWITCH_ACTIONS))
3915                 return rte_flow_error_set(error, EINVAL,
3916                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3917                                           "can't have 2 fate actions in"
3918                                           " same flow");
3919         if (action_flags & MLX5_FLOW_ACTION_METER)
3920                 return rte_flow_error_set(error, ENOTSUP,
3921                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3922                                           "jump with meter not support");
3923         if ((action_flags & MLX5_FLOW_ACTION_SAMPLE) && fdb_mirror)
3924                 return rte_flow_error_set(error, EINVAL,
3925                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3926                                           "E-Switch mirroring can't support"
3927                                           " Sample action and jump action in"
3928                                           " same flow now");
3929         if (!action->conf)
3930                 return rte_flow_error_set(error, EINVAL,
3931                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
3932                                           NULL, "action configuration not set");
3933         target_group =
3934                 ((const struct rte_flow_action_jump *)action->conf)->group;
3935         ret = mlx5_flow_group_to_table(dev, tunnel, target_group, &table,
3936                                        grp_info, error);
3937         if (ret)
3938                 return ret;
3939         if (attributes->group == target_group &&
3940             !(action_flags & (MLX5_FLOW_ACTION_TUNNEL_SET |
3941                               MLX5_FLOW_ACTION_TUNNEL_MATCH)))
3942                 return rte_flow_error_set(error, EINVAL,
3943                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3944                                           "target group must be other than"
3945                                           " the current flow group");
3946         return 0;
3947 }
3948
3949 /*
3950  * Validate the port_id action.
3951  *
3952  * @param[in] dev
3953  *   Pointer to rte_eth_dev structure.
3954  * @param[in] action_flags
3955  *   Bit-fields that holds the actions detected until now.
3956  * @param[in] action
3957  *   Port_id RTE action structure.
3958  * @param[in] attr
3959  *   Attributes of flow that includes this action.
3960  * @param[out] error
3961  *   Pointer to error structure.
3962  *
3963  * @return
3964  *   0 on success, a negative errno value otherwise and rte_errno is set.
3965  */
3966 static int
3967 flow_dv_validate_action_port_id(struct rte_eth_dev *dev,
3968                                 uint64_t action_flags,
3969                                 const struct rte_flow_action *action,
3970                                 const struct rte_flow_attr *attr,
3971                                 struct rte_flow_error *error)
3972 {
3973         const struct rte_flow_action_port_id *port_id;
3974         struct mlx5_priv *act_priv;
3975         struct mlx5_priv *dev_priv;
3976         uint16_t port;
3977
3978         if (!attr->transfer)
3979                 return rte_flow_error_set(error, ENOTSUP,
3980                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3981                                           NULL,
3982                                           "port id action is valid in transfer"
3983                                           " mode only");
3984         if (!action || !action->conf)
3985                 return rte_flow_error_set(error, ENOTSUP,
3986                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
3987                                           NULL,
3988                                           "port id action parameters must be"
3989                                           " specified");
3990         if (action_flags & (MLX5_FLOW_FATE_ACTIONS |
3991                             MLX5_FLOW_FATE_ESWITCH_ACTIONS))
3992                 return rte_flow_error_set(error, EINVAL,
3993                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3994                                           "can have only one fate actions in"
3995                                           " a flow");
3996         dev_priv = mlx5_dev_to_eswitch_info(dev);
3997         if (!dev_priv)
3998                 return rte_flow_error_set(error, rte_errno,
3999                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
4000                                           NULL,
4001                                           "failed to obtain E-Switch info");
4002         port_id = action->conf;
4003         port = port_id->original ? dev->data->port_id : port_id->id;
4004         act_priv = mlx5_port_to_eswitch_info(port, false);
4005         if (!act_priv)
4006                 return rte_flow_error_set
4007                                 (error, rte_errno,
4008                                  RTE_FLOW_ERROR_TYPE_ACTION_CONF, port_id,
4009                                  "failed to obtain E-Switch port id for port");
4010         if (act_priv->domain_id != dev_priv->domain_id)
4011                 return rte_flow_error_set
4012                                 (error, EINVAL,
4013                                  RTE_FLOW_ERROR_TYPE_ACTION, NULL,
4014                                  "port does not belong to"
4015                                  " E-Switch being configured");
4016         return 0;
4017 }
4018
4019 /**
4020  * Get the maximum number of modify header actions.
4021  *
4022  * @param dev
4023  *   Pointer to rte_eth_dev structure.
4024  * @param flags
4025  *   Flags bits to check if root level.
4026  *
4027  * @return
4028  *   Max number of modify header actions device can support.
4029  */
4030 static inline unsigned int
4031 flow_dv_modify_hdr_action_max(struct rte_eth_dev *dev __rte_unused,
4032                               uint64_t flags)
4033 {
4034         /*
4035          * There's no way to directly query the max capacity from FW.
4036          * The maximal value on root table should be assumed to be supported.
4037          */
4038         if (!(flags & MLX5DV_DR_ACTION_FLAGS_ROOT_LEVEL))
4039                 return MLX5_MAX_MODIFY_NUM;
4040         else
4041                 return MLX5_ROOT_TBL_MODIFY_NUM;
4042 }
4043
4044 /**
4045  * Validate the meter action.
4046  *
4047  * @param[in] dev
4048  *   Pointer to rte_eth_dev structure.
4049  * @param[in] action_flags
4050  *   Bit-fields that holds the actions detected until now.
4051  * @param[in] action
4052  *   Pointer to the meter action.
4053  * @param[in] attr
4054  *   Attributes of flow that includes this action.
4055  * @param[out] error
4056  *   Pointer to error structure.
4057  *
4058  * @return
4059  *   0 on success, a negative errno value otherwise and rte_ernno is set.
4060  */
4061 static int
4062 mlx5_flow_validate_action_meter(struct rte_eth_dev *dev,
4063                                 uint64_t action_flags,
4064                                 const struct rte_flow_action *action,
4065                                 const struct rte_flow_attr *attr,
4066                                 struct rte_flow_error *error)
4067 {
4068         struct mlx5_priv *priv = dev->data->dev_private;
4069         const struct rte_flow_action_meter *am = action->conf;
4070         struct mlx5_flow_meter *fm;
4071
4072         if (!am)
4073                 return rte_flow_error_set(error, EINVAL,
4074                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
4075                                           "meter action conf is NULL");
4076
4077         if (action_flags & MLX5_FLOW_ACTION_METER)
4078                 return rte_flow_error_set(error, ENOTSUP,
4079                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
4080                                           "meter chaining not support");
4081         if (action_flags & MLX5_FLOW_ACTION_JUMP)
4082                 return rte_flow_error_set(error, ENOTSUP,
4083                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
4084                                           "meter with jump not support");
4085         if (!priv->mtr_en)
4086                 return rte_flow_error_set(error, ENOTSUP,
4087                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
4088                                           NULL,
4089                                           "meter action not supported");
4090         fm = mlx5_flow_meter_find(priv, am->mtr_id);
4091         if (!fm)
4092                 return rte_flow_error_set(error, EINVAL,
4093                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
4094                                           "Meter not found");
4095         if (fm->ref_cnt && (!(fm->transfer == attr->transfer ||
4096               (!fm->ingress && !attr->ingress && attr->egress) ||
4097               (!fm->egress && !attr->egress && attr->ingress))))
4098                 return rte_flow_error_set(error, EINVAL,
4099                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
4100                                           "Flow attributes are either invalid "
4101                                           "or have a conflict with current "
4102                                           "meter attributes");
4103         return 0;
4104 }
4105
4106 /**
4107  * Validate the age action.
4108  *
4109  * @param[in] action_flags
4110  *   Holds the actions detected until now.
4111  * @param[in] action
4112  *   Pointer to the age action.
4113  * @param[in] dev
4114  *   Pointer to the Ethernet device structure.
4115  * @param[out] error
4116  *   Pointer to error structure.
4117  *
4118  * @return
4119  *   0 on success, a negative errno value otherwise and rte_errno is set.
4120  */
4121 static int
4122 flow_dv_validate_action_age(uint64_t action_flags,
4123                             const struct rte_flow_action *action,
4124                             struct rte_eth_dev *dev,
4125                             struct rte_flow_error *error)
4126 {
4127         struct mlx5_priv *priv = dev->data->dev_private;
4128         const struct rte_flow_action_age *age = action->conf;
4129
4130         if (!priv->config.devx || priv->sh->cmng.counter_fallback)
4131                 return rte_flow_error_set(error, ENOTSUP,
4132                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
4133                                           NULL,
4134                                           "age action not supported");
4135         if (!(action->conf))
4136                 return rte_flow_error_set(error, EINVAL,
4137                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
4138                                           "configuration cannot be null");
4139         if (!(age->timeout))
4140                 return rte_flow_error_set(error, EINVAL,
4141                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
4142                                           "invalid timeout value 0");
4143         if (action_flags & MLX5_FLOW_ACTION_AGE)
4144                 return rte_flow_error_set(error, EINVAL,
4145                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
4146                                           "duplicate age actions set");
4147         return 0;
4148 }
4149
4150 /**
4151  * Validate the modify-header IPv4 DSCP actions.
4152  *
4153  * @param[in] action_flags
4154  *   Holds the actions detected until now.
4155  * @param[in] action
4156  *   Pointer to the modify action.
4157  * @param[in] item_flags
4158  *   Holds the items detected.
4159  * @param[out] error
4160  *   Pointer to error structure.
4161  *
4162  * @return
4163  *   0 on success, a negative errno value otherwise and rte_errno is set.
4164  */
4165 static int
4166 flow_dv_validate_action_modify_ipv4_dscp(const uint64_t action_flags,
4167                                          const struct rte_flow_action *action,
4168                                          const uint64_t item_flags,
4169                                          struct rte_flow_error *error)
4170 {
4171         int ret = 0;
4172
4173         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
4174         if (!ret) {
4175                 if (!(item_flags & MLX5_FLOW_LAYER_L3_IPV4))
4176                         return rte_flow_error_set(error, EINVAL,
4177                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4178                                                   NULL,
4179                                                   "no ipv4 item in pattern");
4180         }
4181         return ret;
4182 }
4183
4184 /**
4185  * Validate the modify-header IPv6 DSCP actions.
4186  *
4187  * @param[in] action_flags
4188  *   Holds the actions detected until now.
4189  * @param[in] action
4190  *   Pointer to the modify action.
4191  * @param[in] item_flags
4192  *   Holds the items detected.
4193  * @param[out] error
4194  *   Pointer to error structure.
4195  *
4196  * @return
4197  *   0 on success, a negative errno value otherwise and rte_errno is set.
4198  */
4199 static int
4200 flow_dv_validate_action_modify_ipv6_dscp(const uint64_t action_flags,
4201                                          const struct rte_flow_action *action,
4202                                          const uint64_t item_flags,
4203                                          struct rte_flow_error *error)
4204 {
4205         int ret = 0;
4206
4207         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
4208         if (!ret) {
4209                 if (!(item_flags & MLX5_FLOW_LAYER_L3_IPV6))
4210                         return rte_flow_error_set(error, EINVAL,
4211                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4212                                                   NULL,
4213                                                   "no ipv6 item in pattern");
4214         }
4215         return ret;
4216 }
4217
4218 /**
4219  * Match modify-header resource.
4220  *
4221  * @param entry
4222  *   Pointer to exist resource entry object.
4223  * @param ctx
4224  *   Pointer to new modify-header resource.
4225  *
4226  * @return
4227  *   0 on matching, -1 otherwise.
4228  */
4229 static int
4230 flow_dv_modify_hdr_resource_match(struct mlx5_hlist_entry *entry, void *ctx)
4231 {
4232         struct mlx5_flow_dv_modify_hdr_resource *resource;
4233         struct mlx5_flow_dv_modify_hdr_resource *cache_resource;
4234         uint32_t actions_len;
4235
4236         resource = (struct mlx5_flow_dv_modify_hdr_resource *)ctx;
4237         cache_resource = container_of(entry,
4238                                       struct mlx5_flow_dv_modify_hdr_resource,
4239                                       entry);
4240         actions_len = resource->actions_num * sizeof(resource->actions[0]);
4241         if (resource->entry.key == cache_resource->entry.key &&
4242             resource->ft_type == cache_resource->ft_type &&
4243             resource->actions_num == cache_resource->actions_num &&
4244             resource->flags == cache_resource->flags &&
4245             !memcmp((const void *)resource->actions,
4246                     (const void *)cache_resource->actions,
4247                     actions_len))
4248                 return 0;
4249         return -1;
4250 }
4251
4252 /**
4253  * Validate the sample action.
4254  *
4255  * @param[in] action_flags
4256  *   Holds the actions detected until now.
4257  * @param[in] action
4258  *   Pointer to the sample action.
4259  * @param[in] dev
4260  *   Pointer to the Ethernet device structure.
4261  * @param[in] attr
4262  *   Attributes of flow that includes this action.
4263  * @param[out] error
4264  *   Pointer to error structure.
4265  *
4266  * @return
4267  *   0 on success, a negative errno value otherwise and rte_errno is set.
4268  */
4269 static int
4270 flow_dv_validate_action_sample(uint64_t action_flags,
4271                                const struct rte_flow_action *action,
4272                                struct rte_eth_dev *dev,
4273                                const struct rte_flow_attr *attr,
4274                                struct rte_flow_error *error)
4275 {
4276         struct mlx5_priv *priv = dev->data->dev_private;
4277         struct mlx5_dev_config *dev_conf = &priv->config;
4278         const struct rte_flow_action_sample *sample = action->conf;
4279         const struct rte_flow_action *act;
4280         uint64_t sub_action_flags = 0;
4281         uint16_t queue_index = 0xFFFF;
4282         int actions_n = 0;
4283         int ret;
4284         fdb_mirror = 0;
4285
4286         if (!sample)
4287                 return rte_flow_error_set(error, EINVAL,
4288                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
4289                                           "configuration cannot be NULL");
4290         if (sample->ratio == 0)
4291                 return rte_flow_error_set(error, EINVAL,
4292                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
4293                                           "ratio value starts from 1");
4294         if (!priv->config.devx || (sample->ratio > 0 && !priv->sampler_en))
4295                 return rte_flow_error_set(error, ENOTSUP,
4296                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
4297                                           NULL,
4298                                           "sample action not supported");
4299         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
4300                 return rte_flow_error_set(error, EINVAL,
4301                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
4302                                           "Multiple sample actions not "
4303                                           "supported");
4304         if (action_flags & MLX5_FLOW_ACTION_METER)
4305                 return rte_flow_error_set(error, EINVAL,
4306                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
4307                                           "wrong action order, meter should "
4308                                           "be after sample action");
4309         if (action_flags & MLX5_FLOW_ACTION_JUMP)
4310                 return rte_flow_error_set(error, EINVAL,
4311                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
4312                                           "wrong action order, jump should "
4313                                           "be after sample action");
4314         act = sample->actions;
4315         for (; act->type != RTE_FLOW_ACTION_TYPE_END; act++) {
4316                 if (actions_n == MLX5_DV_MAX_NUMBER_OF_ACTIONS)
4317                         return rte_flow_error_set(error, ENOTSUP,
4318                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4319                                                   act, "too many actions");
4320                 switch (act->type) {
4321                 case RTE_FLOW_ACTION_TYPE_QUEUE:
4322                         ret = mlx5_flow_validate_action_queue(act,
4323                                                               sub_action_flags,
4324                                                               dev,
4325                                                               attr, error);
4326                         if (ret < 0)
4327                                 return ret;
4328                         queue_index = ((const struct rte_flow_action_queue *)
4329                                                         (act->conf))->index;
4330                         sub_action_flags |= MLX5_FLOW_ACTION_QUEUE;
4331                         ++actions_n;
4332                         break;
4333                 case RTE_FLOW_ACTION_TYPE_MARK:
4334                         ret = flow_dv_validate_action_mark(dev, act,
4335                                                            sub_action_flags,
4336                                                            attr, error);
4337                         if (ret < 0)
4338                                 return ret;
4339                         if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY)
4340                                 sub_action_flags |= MLX5_FLOW_ACTION_MARK |
4341                                                 MLX5_FLOW_ACTION_MARK_EXT;
4342                         else
4343                                 sub_action_flags |= MLX5_FLOW_ACTION_MARK;
4344                         ++actions_n;
4345                         break;
4346                 case RTE_FLOW_ACTION_TYPE_COUNT:
4347                         ret = flow_dv_validate_action_count(dev, error);
4348                         if (ret < 0)
4349                                 return ret;
4350                         sub_action_flags |= MLX5_FLOW_ACTION_COUNT;
4351                         ++actions_n;
4352                         break;
4353                 case RTE_FLOW_ACTION_TYPE_PORT_ID:
4354                         ret = flow_dv_validate_action_port_id(dev,
4355                                                               sub_action_flags,
4356                                                               act,
4357                                                               attr,
4358                                                               error);
4359                         if (ret)
4360                                 return ret;
4361                         sub_action_flags |= MLX5_FLOW_ACTION_PORT_ID;
4362                         ++actions_n;
4363                         break;
4364                 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
4365                         ret = flow_dv_validate_action_raw_encap_decap
4366                                 (dev, NULL, act->conf, attr, &sub_action_flags,
4367                                  &actions_n, error);
4368                         if (ret < 0)
4369                                 return ret;
4370                         ++actions_n;
4371                         break;
4372                 default:
4373                         return rte_flow_error_set(error, ENOTSUP,
4374                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4375                                                   NULL,
4376                                                   "Doesn't support optional "
4377                                                   "action");
4378                 }
4379         }
4380         if (attr->ingress && !attr->transfer) {
4381                 if (!(sub_action_flags & MLX5_FLOW_ACTION_QUEUE))
4382                         return rte_flow_error_set(error, EINVAL,
4383                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4384                                                   NULL,
4385                                                   "Ingress must has a dest "
4386                                                   "QUEUE for Sample");
4387         } else if (attr->egress && !attr->transfer) {
4388                 return rte_flow_error_set(error, ENOTSUP,
4389                                           RTE_FLOW_ERROR_TYPE_ACTION,
4390                                           NULL,
4391                                           "Sample Only support Ingress "
4392                                           "or E-Switch");
4393         } else if (sample->actions->type != RTE_FLOW_ACTION_TYPE_END) {
4394                 MLX5_ASSERT(attr->transfer);
4395                 if (sample->ratio > 1)
4396                         return rte_flow_error_set(error, ENOTSUP,
4397                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4398                                                   NULL,
4399                                                   "E-Switch doesn't support "
4400                                                   "any optional action "
4401                                                   "for sampling");
4402                 fdb_mirror = 1;
4403                 if (sub_action_flags & MLX5_FLOW_ACTION_QUEUE)
4404                         return rte_flow_error_set(error, ENOTSUP,
4405                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4406                                                   NULL,
4407                                                   "unsupported action QUEUE");
4408                 if (!(sub_action_flags & MLX5_FLOW_ACTION_PORT_ID))
4409                         return rte_flow_error_set(error, EINVAL,
4410                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4411                                                   NULL,
4412                                                   "E-Switch must has a dest "
4413                                                   "port for mirroring");
4414         }
4415         /* Continue validation for Xcap actions.*/
4416         if ((sub_action_flags & MLX5_FLOW_XCAP_ACTIONS) &&
4417             (queue_index == 0xFFFF ||
4418              mlx5_rxq_get_type(dev, queue_index) != MLX5_RXQ_TYPE_HAIRPIN)) {
4419                 if ((sub_action_flags & MLX5_FLOW_XCAP_ACTIONS) ==
4420                      MLX5_FLOW_XCAP_ACTIONS)
4421                         return rte_flow_error_set(error, ENOTSUP,
4422                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4423                                                   NULL, "encap and decap "
4424                                                   "combination aren't "
4425                                                   "supported");
4426                 if (!attr->transfer && attr->ingress && (sub_action_flags &
4427                                                         MLX5_FLOW_ACTION_ENCAP))
4428                         return rte_flow_error_set(error, ENOTSUP,
4429                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4430                                                   NULL, "encap is not supported"
4431                                                   " for ingress traffic");
4432         }
4433         return 0;
4434 }
4435
4436 /**
4437  * Find existing modify-header resource or create and register a new one.
4438  *
4439  * @param dev[in, out]
4440  *   Pointer to rte_eth_dev structure.
4441  * @param[in, out] resource
4442  *   Pointer to modify-header resource.
4443  * @parm[in, out] dev_flow
4444  *   Pointer to the dev_flow.
4445  * @param[out] error
4446  *   pointer to error structure.
4447  *
4448  * @return
4449  *   0 on success otherwise -errno and errno is set.
4450  */
4451 static int
4452 flow_dv_modify_hdr_resource_register
4453                         (struct rte_eth_dev *dev,
4454                          struct mlx5_flow_dv_modify_hdr_resource *resource,
4455                          struct mlx5_flow *dev_flow,
4456                          struct rte_flow_error *error)
4457 {
4458         struct mlx5_priv *priv = dev->data->dev_private;
4459         struct mlx5_dev_ctx_shared *sh = priv->sh;
4460         struct mlx5_flow_dv_modify_hdr_resource *cache_resource;
4461         struct mlx5dv_dr_domain *ns;
4462         uint32_t actions_len;
4463         struct mlx5_hlist_entry *entry;
4464         union mlx5_flow_modify_hdr_key hdr_mod_key = {
4465                 {
4466                         .ft_type = resource->ft_type,
4467                         .actions_num = resource->actions_num,
4468                         .group = dev_flow->dv.group,
4469                         .cksum = 0,
4470                 }
4471         };
4472         int ret;
4473
4474         resource->flags = dev_flow->dv.group ? 0 :
4475                           MLX5DV_DR_ACTION_FLAGS_ROOT_LEVEL;
4476         if (resource->actions_num > flow_dv_modify_hdr_action_max(dev,
4477                                     resource->flags))
4478                 return rte_flow_error_set(error, EOVERFLOW,
4479                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
4480                                           "too many modify header items");
4481         if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB)
4482                 ns = sh->fdb_domain;
4483         else if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_NIC_TX)
4484                 ns = sh->tx_domain;
4485         else
4486                 ns = sh->rx_domain;
4487         /* Lookup a matching resource from cache. */
4488         actions_len = resource->actions_num * sizeof(resource->actions[0]);
4489         hdr_mod_key.cksum = __rte_raw_cksum(resource->actions, actions_len, 0);
4490         resource->entry.key = hdr_mod_key.v64;
4491         entry = mlx5_hlist_lookup_ex(sh->modify_cmds, resource->entry.key,
4492                                      flow_dv_modify_hdr_resource_match,
4493                                      (void *)resource);
4494         if (entry) {
4495                 cache_resource = container_of(entry,
4496                                         struct mlx5_flow_dv_modify_hdr_resource,
4497                                         entry);
4498                 DRV_LOG(DEBUG, "modify-header resource %p: refcnt %d++",
4499                         (void *)cache_resource,
4500                         __atomic_load_n(&cache_resource->refcnt,
4501                                         __ATOMIC_RELAXED));
4502                 __atomic_fetch_add(&cache_resource->refcnt, 1,
4503                                    __ATOMIC_RELAXED);
4504                 dev_flow->handle->dvh.modify_hdr = cache_resource;
4505                 return 0;
4506
4507         }
4508         /* Register new modify-header resource. */
4509         cache_resource = mlx5_malloc(MLX5_MEM_ZERO,
4510                                     sizeof(*cache_resource) + actions_len, 0,
4511                                     SOCKET_ID_ANY);
4512         if (!cache_resource)
4513                 return rte_flow_error_set(error, ENOMEM,
4514                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
4515                                           "cannot allocate resource memory");
4516         *cache_resource = *resource;
4517         rte_memcpy(cache_resource->actions, resource->actions, actions_len);
4518         ret = mlx5_flow_os_create_flow_action_modify_header
4519                                         (sh->ctx, ns, cache_resource,
4520                                          actions_len, &cache_resource->action);
4521         if (ret) {
4522                 mlx5_free(cache_resource);
4523                 return rte_flow_error_set(error, ENOMEM,
4524                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
4525                                           NULL, "cannot create action");
4526         }
4527         __atomic_store_n(&cache_resource->refcnt, 1, __ATOMIC_RELAXED);
4528         if (mlx5_hlist_insert_ex(sh->modify_cmds, &cache_resource->entry,
4529                                  flow_dv_modify_hdr_resource_match,
4530                                  (void *)cache_resource)) {
4531                 claim_zero(mlx5_flow_os_destroy_flow_action
4532                                                 (cache_resource->action));
4533                 mlx5_free(cache_resource);
4534                 return rte_flow_error_set(error, EEXIST,
4535                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
4536                                           NULL, "action exist");
4537         }
4538         dev_flow->handle->dvh.modify_hdr = cache_resource;
4539         DRV_LOG(DEBUG, "new modify-header resource %p: refcnt %d++",
4540                 (void *)cache_resource,
4541                 __atomic_load_n(&cache_resource->refcnt, __ATOMIC_RELAXED));
4542         return 0;
4543 }
4544
4545 /**
4546  * Get DV flow counter by index.
4547  *
4548  * @param[in] dev
4549  *   Pointer to the Ethernet device structure.
4550  * @param[in] idx
4551  *   mlx5 flow counter index in the container.
4552  * @param[out] ppool
4553  *   mlx5 flow counter pool in the container,
4554  *
4555  * @return
4556  *   Pointer to the counter, NULL otherwise.
4557  */
4558 static struct mlx5_flow_counter *
4559 flow_dv_counter_get_by_idx(struct rte_eth_dev *dev,
4560                            uint32_t idx,
4561                            struct mlx5_flow_counter_pool **ppool)
4562 {
4563         struct mlx5_priv *priv = dev->data->dev_private;
4564         struct mlx5_flow_counter_mng *cmng = &priv->sh->cmng;
4565         struct mlx5_flow_counter_pool *pool;
4566
4567         /* Decrease to original index and clear shared bit. */
4568         idx = (idx - 1) & (MLX5_CNT_SHARED_OFFSET - 1);
4569         MLX5_ASSERT(idx / MLX5_COUNTERS_PER_POOL < cmng->n);
4570         pool = cmng->pools[idx / MLX5_COUNTERS_PER_POOL];
4571         MLX5_ASSERT(pool);
4572         if (ppool)
4573                 *ppool = pool;
4574         return MLX5_POOL_GET_CNT(pool, idx % MLX5_COUNTERS_PER_POOL);
4575 }
4576
4577 /**
4578  * Check the devx counter belongs to the pool.
4579  *
4580  * @param[in] pool
4581  *   Pointer to the counter pool.
4582  * @param[in] id
4583  *   The counter devx ID.
4584  *
4585  * @return
4586  *   True if counter belongs to the pool, false otherwise.
4587  */
4588 static bool
4589 flow_dv_is_counter_in_pool(struct mlx5_flow_counter_pool *pool, int id)
4590 {
4591         int base = (pool->min_dcs->id / MLX5_COUNTERS_PER_POOL) *
4592                    MLX5_COUNTERS_PER_POOL;
4593
4594         if (id >= base && id < base + MLX5_COUNTERS_PER_POOL)
4595                 return true;
4596         return false;
4597 }
4598
4599 /**
4600  * Get a pool by devx counter ID.
4601  *
4602  * @param[in] cmng
4603  *   Pointer to the counter management.
4604  * @param[in] id
4605  *   The counter devx ID.
4606  *
4607  * @return
4608  *   The counter pool pointer if exists, NULL otherwise,
4609  */
4610 static struct mlx5_flow_counter_pool *
4611 flow_dv_find_pool_by_id(struct mlx5_flow_counter_mng *cmng, int id)
4612 {
4613         uint32_t i;
4614         struct mlx5_flow_counter_pool *pool = NULL;
4615
4616         rte_spinlock_lock(&cmng->pool_update_sl);
4617         /* Check last used pool. */
4618         if (cmng->last_pool_idx != POOL_IDX_INVALID &&
4619             flow_dv_is_counter_in_pool(cmng->pools[cmng->last_pool_idx], id)) {
4620                 pool = cmng->pools[cmng->last_pool_idx];
4621                 goto out;
4622         }
4623         /* ID out of range means no suitable pool in the container. */
4624         if (id > cmng->max_id || id < cmng->min_id)
4625                 goto out;
4626         /*
4627          * Find the pool from the end of the container, since mostly counter
4628          * ID is sequence increasing, and the last pool should be the needed
4629          * one.
4630          */
4631         i = cmng->n_valid;
4632         while (i--) {
4633                 struct mlx5_flow_counter_pool *pool_tmp = cmng->pools[i];
4634
4635                 if (flow_dv_is_counter_in_pool(pool_tmp, id)) {
4636                         pool = pool_tmp;
4637                         break;
4638                 }
4639         }
4640 out:
4641         rte_spinlock_unlock(&cmng->pool_update_sl);
4642         return pool;
4643 }
4644
4645 /**
4646  * Resize a counter container.
4647  *
4648  * @param[in] dev
4649  *   Pointer to the Ethernet device structure.
4650  *
4651  * @return
4652  *   0 on success, otherwise negative errno value and rte_errno is set.
4653  */
4654 static int
4655 flow_dv_container_resize(struct rte_eth_dev *dev)
4656 {
4657         struct mlx5_priv *priv = dev->data->dev_private;
4658         struct mlx5_flow_counter_mng *cmng = &priv->sh->cmng;
4659         void *old_pools = cmng->pools;
4660         uint32_t resize = cmng->n + MLX5_CNT_CONTAINER_RESIZE;
4661         uint32_t mem_size = sizeof(struct mlx5_flow_counter_pool *) * resize;
4662         void *pools = mlx5_malloc(MLX5_MEM_ZERO, mem_size, 0, SOCKET_ID_ANY);
4663
4664         if (!pools) {
4665                 rte_errno = ENOMEM;
4666                 return -ENOMEM;
4667         }
4668         if (old_pools)
4669                 memcpy(pools, old_pools, cmng->n *
4670                                        sizeof(struct mlx5_flow_counter_pool *));
4671         cmng->n = resize;
4672         cmng->pools = pools;
4673         if (old_pools)
4674                 mlx5_free(old_pools);
4675         return 0;
4676 }
4677
4678 /**
4679  * Query a devx flow counter.
4680  *
4681  * @param[in] dev
4682  *   Pointer to the Ethernet device structure.
4683  * @param[in] cnt
4684  *   Index to the flow counter.
4685  * @param[out] pkts
4686  *   The statistics value of packets.
4687  * @param[out] bytes
4688  *   The statistics value of bytes.
4689  *
4690  * @return
4691  *   0 on success, otherwise a negative errno value and rte_errno is set.
4692  */
4693 static inline int
4694 _flow_dv_query_count(struct rte_eth_dev *dev, uint32_t counter, uint64_t *pkts,
4695                      uint64_t *bytes)
4696 {
4697         struct mlx5_priv *priv = dev->data->dev_private;
4698         struct mlx5_flow_counter_pool *pool = NULL;
4699         struct mlx5_flow_counter *cnt;
4700         int offset;
4701
4702         cnt = flow_dv_counter_get_by_idx(dev, counter, &pool);
4703         MLX5_ASSERT(pool);
4704         if (priv->sh->cmng.counter_fallback)
4705                 return mlx5_devx_cmd_flow_counter_query(cnt->dcs_when_active, 0,
4706                                         0, pkts, bytes, 0, NULL, NULL, 0);
4707         rte_spinlock_lock(&pool->sl);
4708         if (!pool->raw) {
4709                 *pkts = 0;
4710                 *bytes = 0;
4711         } else {
4712                 offset = MLX5_CNT_ARRAY_IDX(pool, cnt);
4713                 *pkts = rte_be_to_cpu_64(pool->raw->data[offset].hits);
4714                 *bytes = rte_be_to_cpu_64(pool->raw->data[offset].bytes);
4715         }
4716         rte_spinlock_unlock(&pool->sl);
4717         return 0;
4718 }
4719
4720 /**
4721  * Create and initialize a new counter pool.
4722  *
4723  * @param[in] dev
4724  *   Pointer to the Ethernet device structure.
4725  * @param[out] dcs
4726  *   The devX counter handle.
4727  * @param[in] age
4728  *   Whether the pool is for counter that was allocated for aging.
4729  * @param[in/out] cont_cur
4730  *   Pointer to the container pointer, it will be update in pool resize.
4731  *
4732  * @return
4733  *   The pool container pointer on success, NULL otherwise and rte_errno is set.
4734  */
4735 static struct mlx5_flow_counter_pool *
4736 flow_dv_pool_create(struct rte_eth_dev *dev, struct mlx5_devx_obj *dcs,
4737                     uint32_t age)
4738 {
4739         struct mlx5_priv *priv = dev->data->dev_private;
4740         struct mlx5_flow_counter_pool *pool;
4741         struct mlx5_flow_counter_mng *cmng = &priv->sh->cmng;
4742         bool fallback = priv->sh->cmng.counter_fallback;
4743         uint32_t size = sizeof(*pool);
4744
4745         size += MLX5_COUNTERS_PER_POOL * MLX5_CNT_SIZE;
4746         size += (!age ? 0 : MLX5_COUNTERS_PER_POOL * MLX5_AGE_SIZE);
4747         pool = mlx5_malloc(MLX5_MEM_ZERO, size, 0, SOCKET_ID_ANY);
4748         if (!pool) {
4749                 rte_errno = ENOMEM;
4750                 return NULL;
4751         }
4752         pool->raw = NULL;
4753         pool->is_aged = !!age;
4754         pool->query_gen = 0;
4755         pool->min_dcs = dcs;
4756         rte_spinlock_init(&pool->sl);
4757         rte_spinlock_init(&pool->csl);
4758         TAILQ_INIT(&pool->counters[0]);
4759         TAILQ_INIT(&pool->counters[1]);
4760         pool->time_of_last_age_check = MLX5_CURR_TIME_SEC;
4761         rte_spinlock_lock(&cmng->pool_update_sl);
4762         pool->index = cmng->n_valid;
4763         if (pool->index == cmng->n && flow_dv_container_resize(dev)) {
4764                 mlx5_free(pool);
4765                 rte_spinlock_unlock(&cmng->pool_update_sl);
4766                 return NULL;
4767         }
4768         cmng->pools[pool->index] = pool;
4769         cmng->n_valid++;
4770         if (unlikely(fallback)) {
4771                 int base = RTE_ALIGN_FLOOR(dcs->id, MLX5_COUNTERS_PER_POOL);
4772
4773                 if (base < cmng->min_id)
4774                         cmng->min_id = base;
4775                 if (base > cmng->max_id)
4776                         cmng->max_id = base + MLX5_COUNTERS_PER_POOL - 1;
4777                 cmng->last_pool_idx = pool->index;
4778         }
4779         rte_spinlock_unlock(&cmng->pool_update_sl);
4780         return pool;
4781 }
4782
4783 /**
4784  * Prepare a new counter and/or a new counter pool.
4785  *
4786  * @param[in] dev
4787  *   Pointer to the Ethernet device structure.
4788  * @param[out] cnt_free
4789  *   Where to put the pointer of a new counter.
4790  * @param[in] age
4791  *   Whether the pool is for counter that was allocated for aging.
4792  *
4793  * @return
4794  *   The counter pool pointer and @p cnt_free is set on success,
4795  *   NULL otherwise and rte_errno is set.
4796  */
4797 static struct mlx5_flow_counter_pool *
4798 flow_dv_counter_pool_prepare(struct rte_eth_dev *dev,
4799                              struct mlx5_flow_counter **cnt_free,
4800                              uint32_t age)
4801 {
4802         struct mlx5_priv *priv = dev->data->dev_private;
4803         struct mlx5_flow_counter_mng *cmng = &priv->sh->cmng;
4804         struct mlx5_flow_counter_pool *pool;
4805         struct mlx5_counters tmp_tq;
4806         struct mlx5_devx_obj *dcs = NULL;
4807         struct mlx5_flow_counter *cnt;
4808         enum mlx5_counter_type cnt_type =
4809                         age ? MLX5_COUNTER_TYPE_AGE : MLX5_COUNTER_TYPE_ORIGIN;
4810         bool fallback = priv->sh->cmng.counter_fallback;
4811         uint32_t i;
4812
4813         if (fallback) {
4814                 /* bulk_bitmap must be 0 for single counter allocation. */
4815                 dcs = mlx5_devx_cmd_flow_counter_alloc(priv->sh->ctx, 0);
4816                 if (!dcs)
4817                         return NULL;
4818                 pool = flow_dv_find_pool_by_id(cmng, dcs->id);
4819                 if (!pool) {
4820                         pool = flow_dv_pool_create(dev, dcs, age);
4821                         if (!pool) {
4822                                 mlx5_devx_cmd_destroy(dcs);
4823                                 return NULL;
4824                         }
4825                 }
4826                 i = dcs->id % MLX5_COUNTERS_PER_POOL;
4827                 cnt = MLX5_POOL_GET_CNT(pool, i);
4828                 cnt->pool = pool;
4829                 cnt->dcs_when_free = dcs;
4830                 *cnt_free = cnt;
4831                 return pool;
4832         }
4833         dcs = mlx5_devx_cmd_flow_counter_alloc(priv->sh->ctx, 0x4);
4834         if (!dcs) {
4835                 rte_errno = ENODATA;
4836                 return NULL;
4837         }
4838         pool = flow_dv_pool_create(dev, dcs, age);
4839         if (!pool) {
4840                 mlx5_devx_cmd_destroy(dcs);
4841                 return NULL;
4842         }
4843         TAILQ_INIT(&tmp_tq);
4844         for (i = 1; i < MLX5_COUNTERS_PER_POOL; ++i) {
4845                 cnt = MLX5_POOL_GET_CNT(pool, i);
4846                 cnt->pool = pool;
4847                 TAILQ_INSERT_HEAD(&tmp_tq, cnt, next);
4848         }
4849         rte_spinlock_lock(&cmng->csl[cnt_type]);
4850         TAILQ_CONCAT(&cmng->counters[cnt_type], &tmp_tq, next);
4851         rte_spinlock_unlock(&cmng->csl[cnt_type]);
4852         *cnt_free = MLX5_POOL_GET_CNT(pool, 0);
4853         (*cnt_free)->pool = pool;
4854         return pool;
4855 }
4856
4857 /**
4858  * Allocate a flow counter.
4859  *
4860  * @param[in] dev
4861  *   Pointer to the Ethernet device structure.
4862  * @param[in] age
4863  *   Whether the counter was allocated for aging.
4864  *
4865  * @return
4866  *   Index to flow counter on success, 0 otherwise and rte_errno is set.
4867  */
4868 static uint32_t
4869 flow_dv_counter_alloc(struct rte_eth_dev *dev, uint32_t age)
4870 {
4871         struct mlx5_priv *priv = dev->data->dev_private;
4872         struct mlx5_flow_counter_pool *pool = NULL;
4873         struct mlx5_flow_counter *cnt_free = NULL;
4874         bool fallback = priv->sh->cmng.counter_fallback;
4875         struct mlx5_flow_counter_mng *cmng = &priv->sh->cmng;
4876         enum mlx5_counter_type cnt_type =
4877                         age ? MLX5_COUNTER_TYPE_AGE : MLX5_COUNTER_TYPE_ORIGIN;
4878         uint32_t cnt_idx;
4879
4880         if (!priv->config.devx) {
4881                 rte_errno = ENOTSUP;
4882                 return 0;
4883         }
4884         /* Get free counters from container. */
4885         rte_spinlock_lock(&cmng->csl[cnt_type]);
4886         cnt_free = TAILQ_FIRST(&cmng->counters[cnt_type]);
4887         if (cnt_free)
4888                 TAILQ_REMOVE(&cmng->counters[cnt_type], cnt_free, next);
4889         rte_spinlock_unlock(&cmng->csl[cnt_type]);
4890         if (!cnt_free && !flow_dv_counter_pool_prepare(dev, &cnt_free, age))
4891                 goto err;
4892         pool = cnt_free->pool;
4893         if (fallback)
4894                 cnt_free->dcs_when_active = cnt_free->dcs_when_free;
4895         /* Create a DV counter action only in the first time usage. */
4896         if (!cnt_free->action) {
4897                 uint16_t offset;
4898                 struct mlx5_devx_obj *dcs;
4899                 int ret;
4900
4901                 if (!fallback) {
4902                         offset = MLX5_CNT_ARRAY_IDX(pool, cnt_free);
4903                         dcs = pool->min_dcs;
4904                 } else {
4905                         offset = 0;
4906                         dcs = cnt_free->dcs_when_free;
4907                 }
4908                 ret = mlx5_flow_os_create_flow_action_count(dcs->obj, offset,
4909                                                             &cnt_free->action);
4910                 if (ret) {
4911                         rte_errno = errno;
4912                         goto err;
4913                 }
4914         }
4915         cnt_idx = MLX5_MAKE_CNT_IDX(pool->index,
4916                                 MLX5_CNT_ARRAY_IDX(pool, cnt_free));
4917         /* Update the counter reset values. */
4918         if (_flow_dv_query_count(dev, cnt_idx, &cnt_free->hits,
4919                                  &cnt_free->bytes))
4920                 goto err;
4921         if (!fallback && !priv->sh->cmng.query_thread_on)
4922                 /* Start the asynchronous batch query by the host thread. */
4923                 mlx5_set_query_alarm(priv->sh);
4924         return cnt_idx;
4925 err:
4926         if (cnt_free) {
4927                 cnt_free->pool = pool;
4928                 if (fallback)
4929                         cnt_free->dcs_when_free = cnt_free->dcs_when_active;
4930                 rte_spinlock_lock(&cmng->csl[cnt_type]);
4931                 TAILQ_INSERT_TAIL(&cmng->counters[cnt_type], cnt_free, next);
4932                 rte_spinlock_unlock(&cmng->csl[cnt_type]);
4933         }
4934         return 0;
4935 }
4936
4937 /**
4938  * Allocate a shared flow counter.
4939  *
4940  * @param[in] ctx
4941  *   Pointer to the shared counter configuration.
4942  * @param[in] data
4943  *   Pointer to save the allocated counter index.
4944  *
4945  * @return
4946  *   Index to flow counter on success, 0 otherwise and rte_errno is set.
4947  */
4948
4949 static int32_t
4950 flow_dv_counter_alloc_shared_cb(void *ctx, union mlx5_l3t_data *data)
4951 {
4952         struct mlx5_shared_counter_conf *conf = ctx;
4953         struct rte_eth_dev *dev = conf->dev;
4954         struct mlx5_flow_counter *cnt;
4955
4956         data->dword = flow_dv_counter_alloc(dev, 0);
4957         data->dword |= MLX5_CNT_SHARED_OFFSET;
4958         cnt = flow_dv_counter_get_by_idx(dev, data->dword, NULL);
4959         cnt->shared_info.id = conf->id;
4960         return 0;
4961 }
4962
4963 /**
4964  * Get a shared flow counter.
4965  *
4966  * @param[in] dev
4967  *   Pointer to the Ethernet device structure.
4968  * @param[in] id
4969  *   Counter identifier.
4970  *
4971  * @return
4972  *   Index to flow counter on success, 0 otherwise and rte_errno is set.
4973  */
4974 static uint32_t
4975 flow_dv_counter_get_shared(struct rte_eth_dev *dev, uint32_t id)
4976 {
4977         struct mlx5_priv *priv = dev->data->dev_private;
4978         struct mlx5_shared_counter_conf conf = {
4979                 .dev = dev,
4980                 .id = id,
4981         };
4982         union mlx5_l3t_data data = {
4983                 .dword = 0,
4984         };
4985
4986         mlx5_l3t_prepare_entry(priv->sh->cnt_id_tbl, id, &data,
4987                                flow_dv_counter_alloc_shared_cb, &conf);
4988         return data.dword;
4989 }
4990
4991 /**
4992  * Get age param from counter index.
4993  *
4994  * @param[in] dev
4995  *   Pointer to the Ethernet device structure.
4996  * @param[in] counter
4997  *   Index to the counter handler.
4998  *
4999  * @return
5000  *   The aging parameter specified for the counter index.
5001  */
5002 static struct mlx5_age_param*
5003 flow_dv_counter_idx_get_age(struct rte_eth_dev *dev,
5004                                 uint32_t counter)
5005 {
5006         struct mlx5_flow_counter *cnt;
5007         struct mlx5_flow_counter_pool *pool = NULL;
5008
5009         flow_dv_counter_get_by_idx(dev, counter, &pool);
5010         counter = (counter - 1) % MLX5_COUNTERS_PER_POOL;
5011         cnt = MLX5_POOL_GET_CNT(pool, counter);
5012         return MLX5_CNT_TO_AGE(cnt);
5013 }
5014
5015 /**
5016  * Remove a flow counter from aged counter list.
5017  *
5018  * @param[in] dev
5019  *   Pointer to the Ethernet device structure.
5020  * @param[in] counter
5021  *   Index to the counter handler.
5022  * @param[in] cnt
5023  *   Pointer to the counter handler.
5024  */
5025 static void
5026 flow_dv_counter_remove_from_age(struct rte_eth_dev *dev,
5027                                 uint32_t counter, struct mlx5_flow_counter *cnt)
5028 {
5029         struct mlx5_age_info *age_info;
5030         struct mlx5_age_param *age_param;
5031         struct mlx5_priv *priv = dev->data->dev_private;
5032         uint16_t expected = AGE_CANDIDATE;
5033
5034         age_info = GET_PORT_AGE_INFO(priv);
5035         age_param = flow_dv_counter_idx_get_age(dev, counter);
5036         if (!__atomic_compare_exchange_n(&age_param->state, &expected,
5037                                          AGE_FREE, false, __ATOMIC_RELAXED,
5038                                          __ATOMIC_RELAXED)) {
5039                 /**
5040                  * We need the lock even it is age timeout,
5041                  * since counter may still in process.
5042                  */
5043                 rte_spinlock_lock(&age_info->aged_sl);
5044                 TAILQ_REMOVE(&age_info->aged_counters, cnt, next);
5045                 rte_spinlock_unlock(&age_info->aged_sl);
5046                 __atomic_store_n(&age_param->state, AGE_FREE, __ATOMIC_RELAXED);
5047         }
5048 }
5049
5050 /**
5051  * Release a flow counter.
5052  *
5053  * @param[in] dev
5054  *   Pointer to the Ethernet device structure.
5055  * @param[in] counter
5056  *   Index to the counter handler.
5057  */
5058 static void
5059 flow_dv_counter_release(struct rte_eth_dev *dev, uint32_t counter)
5060 {
5061         struct mlx5_priv *priv = dev->data->dev_private;
5062         struct mlx5_flow_counter_pool *pool = NULL;
5063         struct mlx5_flow_counter *cnt;
5064         enum mlx5_counter_type cnt_type;
5065
5066         if (!counter)
5067                 return;
5068         cnt = flow_dv_counter_get_by_idx(dev, counter, &pool);
5069         MLX5_ASSERT(pool);
5070         if (IS_SHARED_CNT(counter) &&
5071             mlx5_l3t_clear_entry(priv->sh->cnt_id_tbl, cnt->shared_info.id))
5072                 return;
5073         if (pool->is_aged)
5074                 flow_dv_counter_remove_from_age(dev, counter, cnt);
5075         cnt->pool = pool;
5076         /*
5077          * Put the counter back to list to be updated in none fallback mode.
5078          * Currently, we are using two list alternately, while one is in query,
5079          * add the freed counter to the other list based on the pool query_gen
5080          * value. After query finishes, add counter the list to the global
5081          * container counter list. The list changes while query starts. In
5082          * this case, lock will not be needed as query callback and release
5083          * function both operate with the different list.
5084          *
5085          */
5086         if (!priv->sh->cmng.counter_fallback) {
5087                 rte_spinlock_lock(&pool->csl);
5088                 TAILQ_INSERT_TAIL(&pool->counters[pool->query_gen], cnt, next);
5089                 rte_spinlock_unlock(&pool->csl);
5090         } else {
5091                 cnt->dcs_when_free = cnt->dcs_when_active;
5092                 cnt_type = pool->is_aged ? MLX5_COUNTER_TYPE_AGE :
5093                                            MLX5_COUNTER_TYPE_ORIGIN;
5094                 rte_spinlock_lock(&priv->sh->cmng.csl[cnt_type]);
5095                 TAILQ_INSERT_TAIL(&priv->sh->cmng.counters[cnt_type],
5096                                   cnt, next);
5097                 rte_spinlock_unlock(&priv->sh->cmng.csl[cnt_type]);
5098         }
5099 }
5100
5101 /**
5102  * Verify the @p attributes will be correctly understood by the NIC and store
5103  * them in the @p flow if everything is correct.
5104  *
5105  * @param[in] dev
5106  *   Pointer to dev struct.
5107  * @param[in] attributes
5108  *   Pointer to flow attributes
5109  * @param[in] external
5110  *   This flow rule is created by request external to PMD.
5111  * @param[out] error
5112  *   Pointer to error structure.
5113  *
5114  * @return
5115  *   - 0 on success and non root table.
5116  *   - 1 on success and root table.
5117  *   - a negative errno value otherwise and rte_errno is set.
5118  */
5119 static int
5120 flow_dv_validate_attributes(struct rte_eth_dev *dev,
5121                             const struct mlx5_flow_tunnel *tunnel,
5122                             const struct rte_flow_attr *attributes,
5123                             struct flow_grp_info grp_info,
5124                             struct rte_flow_error *error)
5125 {
5126         struct mlx5_priv *priv = dev->data->dev_private;
5127         uint32_t priority_max = priv->config.flow_prio - 1;
5128         int ret = 0;
5129
5130 #ifndef HAVE_MLX5DV_DR
5131         RTE_SET_USED(tunnel);
5132         RTE_SET_USED(grp_info);
5133         if (attributes->group)
5134                 return rte_flow_error_set(error, ENOTSUP,
5135                                           RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
5136                                           NULL,
5137                                           "groups are not supported");
5138 #else
5139         uint32_t table = 0;
5140
5141         ret = mlx5_flow_group_to_table(dev, tunnel, attributes->group, &table,
5142                                        grp_info, error);
5143         if (ret)
5144                 return ret;
5145         if (!table)
5146                 ret = MLX5DV_DR_ACTION_FLAGS_ROOT_LEVEL;
5147 #endif
5148         if (attributes->priority != MLX5_FLOW_PRIO_RSVD &&
5149             attributes->priority >= priority_max)
5150                 return rte_flow_error_set(error, ENOTSUP,
5151                                           RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
5152                                           NULL,
5153                                           "priority out of range");
5154         if (attributes->transfer) {
5155                 if (!priv->config.dv_esw_en)
5156                         return rte_flow_error_set
5157                                 (error, ENOTSUP,
5158                                  RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
5159                                  "E-Switch dr is not supported");
5160                 if (!(priv->representor || priv->master))
5161                         return rte_flow_error_set
5162                                 (error, EINVAL, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
5163                                  NULL, "E-Switch configuration can only be"
5164                                  " done by a master or a representor device");
5165                 if (attributes->egress)
5166                         return rte_flow_error_set
5167                                 (error, ENOTSUP,
5168                                  RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, attributes,
5169                                  "egress is not supported");
5170         }
5171         if (!(attributes->egress ^ attributes->ingress))
5172                 return rte_flow_error_set(error, ENOTSUP,
5173                                           RTE_FLOW_ERROR_TYPE_ATTR, NULL,
5174                                           "must specify exactly one of "
5175                                           "ingress or egress");
5176         return ret;
5177 }
5178
5179 /**
5180  * Internal validation function. For validating both actions and items.
5181  *
5182  * @param[in] dev
5183  *   Pointer to the rte_eth_dev structure.
5184  * @param[in] attr
5185  *   Pointer to the flow attributes.
5186  * @param[in] items
5187  *   Pointer to the list of items.
5188  * @param[in] actions
5189  *   Pointer to the list of actions.
5190  * @param[in] external
5191  *   This flow rule is created by request external to PMD.
5192  * @param[in] hairpin
5193  *   Number of hairpin TX actions, 0 means classic flow.
5194  * @param[out] error
5195  *   Pointer to the error structure.
5196  *
5197  * @return
5198  *   0 on success, a negative errno value otherwise and rte_errno is set.
5199  */
5200 static int
5201 flow_dv_validate(struct rte_eth_dev *dev, const struct rte_flow_attr *attr,
5202                  const struct rte_flow_item items[],
5203                  const struct rte_flow_action actions[],
5204                  bool external, int hairpin, struct rte_flow_error *error)
5205 {
5206         int ret;
5207         uint64_t action_flags = 0;
5208         uint64_t item_flags = 0;
5209         uint64_t last_item = 0;
5210         uint8_t next_protocol = 0xff;
5211         uint16_t ether_type = 0;
5212         int actions_n = 0;
5213         uint8_t item_ipv6_proto = 0;
5214         const struct rte_flow_item *gre_item = NULL;
5215         const struct rte_flow_action_raw_decap *decap;
5216         const struct rte_flow_action_raw_encap *encap;
5217         const struct rte_flow_action_rss *rss;
5218         const struct rte_flow_item_tcp nic_tcp_mask = {
5219                 .hdr = {
5220                         .tcp_flags = 0xFF,
5221                         .src_port = RTE_BE16(UINT16_MAX),
5222                         .dst_port = RTE_BE16(UINT16_MAX),
5223                 }
5224         };
5225         const struct rte_flow_item_ipv6 nic_ipv6_mask = {
5226                 .hdr = {
5227                         .src_addr =
5228                         "\xff\xff\xff\xff\xff\xff\xff\xff"
5229                         "\xff\xff\xff\xff\xff\xff\xff\xff",
5230                         .dst_addr =
5231                         "\xff\xff\xff\xff\xff\xff\xff\xff"
5232                         "\xff\xff\xff\xff\xff\xff\xff\xff",
5233                         .vtc_flow = RTE_BE32(0xffffffff),
5234                         .proto = 0xff,
5235                         .hop_limits = 0xff,
5236                 },
5237                 .has_frag_ext = 1,
5238         };
5239         const struct rte_flow_item_ecpri nic_ecpri_mask = {
5240                 .hdr = {
5241                         .common = {
5242                                 .u32 =
5243                                 RTE_BE32(((const struct rte_ecpri_common_hdr) {
5244                                         .type = 0xFF,
5245                                         }).u32),
5246                         },
5247                         .dummy[0] = 0xffffffff,
5248                 },
5249         };
5250         struct mlx5_priv *priv = dev->data->dev_private;
5251         struct mlx5_dev_config *dev_conf = &priv->config;
5252         uint16_t queue_index = 0xFFFF;
5253         const struct rte_flow_item_vlan *vlan_m = NULL;
5254         int16_t rw_act_num = 0;
5255         uint64_t is_root;
5256         const struct mlx5_flow_tunnel *tunnel;
5257         struct flow_grp_info grp_info = {
5258                 .external = !!external,
5259                 .transfer = !!attr->transfer,
5260                 .fdb_def_rule = !!priv->fdb_def_rule,
5261         };
5262         const struct rte_eth_hairpin_conf *conf;
5263
5264         if (items == NULL)
5265                 return -1;
5266         if (is_flow_tunnel_match_rule(dev, attr, items, actions)) {
5267                 tunnel = flow_items_to_tunnel(items);
5268                 action_flags |= MLX5_FLOW_ACTION_TUNNEL_MATCH |
5269                                 MLX5_FLOW_ACTION_DECAP;
5270         } else if (is_flow_tunnel_steer_rule(dev, attr, items, actions)) {
5271                 tunnel = flow_actions_to_tunnel(actions);
5272                 action_flags |= MLX5_FLOW_ACTION_TUNNEL_SET;
5273         } else {
5274                 tunnel = NULL;
5275         }
5276         grp_info.std_tbl_fix = tunnel_use_standard_attr_group_translate
5277                                 (dev, tunnel, attr, items, actions);
5278         ret = flow_dv_validate_attributes(dev, tunnel, attr, grp_info, error);
5279         if (ret < 0)
5280                 return ret;
5281         is_root = (uint64_t)ret;
5282         for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
5283                 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
5284                 int type = items->type;
5285
5286                 if (!mlx5_flow_os_item_supported(type))
5287                         return rte_flow_error_set(error, ENOTSUP,
5288                                                   RTE_FLOW_ERROR_TYPE_ITEM,
5289                                                   NULL, "item not supported");
5290                 switch (type) {
5291                 case MLX5_RTE_FLOW_ITEM_TYPE_TUNNEL:
5292                         if (items[0].type != (typeof(items[0].type))
5293                                                 MLX5_RTE_FLOW_ITEM_TYPE_TUNNEL)
5294                                 return rte_flow_error_set
5295                                                 (error, EINVAL,
5296                                                 RTE_FLOW_ERROR_TYPE_ITEM,
5297                                                 NULL, "MLX5 private items "
5298                                                 "must be the first");
5299                         break;
5300                 case RTE_FLOW_ITEM_TYPE_VOID:
5301                         break;
5302                 case RTE_FLOW_ITEM_TYPE_PORT_ID:
5303                         ret = flow_dv_validate_item_port_id
5304                                         (dev, items, attr, item_flags, error);
5305                         if (ret < 0)
5306                                 return ret;
5307                         last_item = MLX5_FLOW_ITEM_PORT_ID;
5308                         break;
5309                 case RTE_FLOW_ITEM_TYPE_ETH:
5310                         ret = mlx5_flow_validate_item_eth(items, item_flags,
5311                                                           true, error);
5312                         if (ret < 0)
5313                                 return ret;
5314                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
5315                                              MLX5_FLOW_LAYER_OUTER_L2;
5316                         if (items->mask != NULL && items->spec != NULL) {
5317                                 ether_type =
5318                                         ((const struct rte_flow_item_eth *)
5319                                          items->spec)->type;
5320                                 ether_type &=
5321                                         ((const struct rte_flow_item_eth *)
5322                                          items->mask)->type;
5323                                 ether_type = rte_be_to_cpu_16(ether_type);
5324                         } else {
5325                                 ether_type = 0;
5326                         }
5327                         break;
5328                 case RTE_FLOW_ITEM_TYPE_VLAN:
5329                         ret = flow_dv_validate_item_vlan(items, item_flags,
5330                                                          dev, error);
5331                         if (ret < 0)
5332                                 return ret;
5333                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_VLAN :
5334                                              MLX5_FLOW_LAYER_OUTER_VLAN;
5335                         if (items->mask != NULL && items->spec != NULL) {
5336                                 ether_type =
5337                                         ((const struct rte_flow_item_vlan *)
5338                                          items->spec)->inner_type;
5339                                 ether_type &=
5340                                         ((const struct rte_flow_item_vlan *)
5341                                          items->mask)->inner_type;
5342                                 ether_type = rte_be_to_cpu_16(ether_type);
5343                         } else {
5344                                 ether_type = 0;
5345                         }
5346                         /* Store outer VLAN mask for of_push_vlan action. */
5347                         if (!tunnel)
5348                                 vlan_m = items->mask;
5349                         break;
5350                 case RTE_FLOW_ITEM_TYPE_IPV4:
5351                         mlx5_flow_tunnel_ip_check(items, next_protocol,
5352                                                   &item_flags, &tunnel);
5353                         ret = flow_dv_validate_item_ipv4(items, item_flags,
5354                                                          last_item, ether_type,
5355                                                          error);
5356                         if (ret < 0)
5357                                 return ret;
5358                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
5359                                              MLX5_FLOW_LAYER_OUTER_L3_IPV4;
5360                         if (items->mask != NULL &&
5361                             ((const struct rte_flow_item_ipv4 *)
5362                              items->mask)->hdr.next_proto_id) {
5363                                 next_protocol =
5364                                         ((const struct rte_flow_item_ipv4 *)
5365                                          (items->spec))->hdr.next_proto_id;
5366                                 next_protocol &=
5367                                         ((const struct rte_flow_item_ipv4 *)
5368                                          (items->mask))->hdr.next_proto_id;
5369                         } else {
5370                                 /* Reset for inner layer. */
5371                                 next_protocol = 0xff;
5372                         }
5373                         break;
5374                 case RTE_FLOW_ITEM_TYPE_IPV6:
5375                         mlx5_flow_tunnel_ip_check(items, next_protocol,
5376                                                   &item_flags, &tunnel);
5377                         ret = mlx5_flow_validate_item_ipv6(items, item_flags,
5378                                                            last_item,
5379                                                            ether_type,
5380                                                            &nic_ipv6_mask,
5381                                                            error);
5382                         if (ret < 0)
5383                                 return ret;
5384                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
5385                                              MLX5_FLOW_LAYER_OUTER_L3_IPV6;
5386                         if (items->mask != NULL &&
5387                             ((const struct rte_flow_item_ipv6 *)
5388                              items->mask)->hdr.proto) {
5389                                 item_ipv6_proto =
5390                                         ((const struct rte_flow_item_ipv6 *)
5391                                          items->spec)->hdr.proto;
5392                                 next_protocol =
5393                                         ((const struct rte_flow_item_ipv6 *)
5394                                          items->spec)->hdr.proto;
5395                                 next_protocol &=
5396                                         ((const struct rte_flow_item_ipv6 *)
5397                                          items->mask)->hdr.proto;
5398                         } else {
5399                                 /* Reset for inner layer. */
5400                                 next_protocol = 0xff;
5401                         }
5402                         break;
5403                 case RTE_FLOW_ITEM_TYPE_IPV6_FRAG_EXT:
5404                         ret = flow_dv_validate_item_ipv6_frag_ext(items,
5405                                                                   item_flags,
5406                                                                   error);
5407                         if (ret < 0)
5408                                 return ret;
5409                         last_item = tunnel ?
5410                                         MLX5_FLOW_LAYER_INNER_L3_IPV6_FRAG_EXT :
5411                                         MLX5_FLOW_LAYER_OUTER_L3_IPV6_FRAG_EXT;
5412                         if (items->mask != NULL &&
5413                             ((const struct rte_flow_item_ipv6_frag_ext *)
5414                              items->mask)->hdr.next_header) {
5415                                 next_protocol =
5416                                 ((const struct rte_flow_item_ipv6_frag_ext *)
5417                                  items->spec)->hdr.next_header;
5418                                 next_protocol &=
5419                                 ((const struct rte_flow_item_ipv6_frag_ext *)
5420                                  items->mask)->hdr.next_header;
5421                         } else {
5422                                 /* Reset for inner layer. */
5423                                 next_protocol = 0xff;
5424                         }
5425                         break;
5426                 case RTE_FLOW_ITEM_TYPE_TCP:
5427                         ret = mlx5_flow_validate_item_tcp
5428                                                 (items, item_flags,
5429                                                  next_protocol,
5430                                                  &nic_tcp_mask,
5431                                                  error);
5432                         if (ret < 0)
5433                                 return ret;
5434                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
5435                                              MLX5_FLOW_LAYER_OUTER_L4_TCP;
5436                         break;
5437                 case RTE_FLOW_ITEM_TYPE_UDP:
5438                         ret = mlx5_flow_validate_item_udp(items, item_flags,
5439                                                           next_protocol,
5440                                                           error);
5441                         if (ret < 0)
5442                                 return ret;
5443                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
5444                                              MLX5_FLOW_LAYER_OUTER_L4_UDP;
5445                         break;
5446                 case RTE_FLOW_ITEM_TYPE_GRE:
5447                         ret = mlx5_flow_validate_item_gre(items, item_flags,
5448                                                           next_protocol, error);
5449                         if (ret < 0)
5450                                 return ret;
5451                         gre_item = items;
5452                         last_item = MLX5_FLOW_LAYER_GRE;
5453                         break;
5454                 case RTE_FLOW_ITEM_TYPE_NVGRE:
5455                         ret = mlx5_flow_validate_item_nvgre(items, item_flags,
5456                                                             next_protocol,
5457                                                             error);
5458                         if (ret < 0)
5459                                 return ret;
5460                         last_item = MLX5_FLOW_LAYER_NVGRE;
5461                         break;
5462                 case RTE_FLOW_ITEM_TYPE_GRE_KEY:
5463                         ret = mlx5_flow_validate_item_gre_key
5464                                 (items, item_flags, gre_item, error);
5465                         if (ret < 0)
5466                                 return ret;
5467                         last_item = MLX5_FLOW_LAYER_GRE_KEY;
5468                         break;
5469                 case RTE_FLOW_ITEM_TYPE_VXLAN:
5470                         ret = mlx5_flow_validate_item_vxlan(items, item_flags,
5471                                                             error);
5472                         if (ret < 0)
5473                                 return ret;
5474                         last_item = MLX5_FLOW_LAYER_VXLAN;
5475                         break;
5476                 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
5477                         ret = mlx5_flow_validate_item_vxlan_gpe(items,
5478                                                                 item_flags, dev,
5479                                                                 error);
5480                         if (ret < 0)
5481                                 return ret;
5482                         last_item = MLX5_FLOW_LAYER_VXLAN_GPE;
5483                         break;
5484                 case RTE_FLOW_ITEM_TYPE_GENEVE:
5485                         ret = mlx5_flow_validate_item_geneve(items,
5486                                                              item_flags, dev,
5487                                                              error);
5488                         if (ret < 0)
5489                                 return ret;
5490                         last_item = MLX5_FLOW_LAYER_GENEVE;
5491                         break;
5492                 case RTE_FLOW_ITEM_TYPE_MPLS:
5493                         ret = mlx5_flow_validate_item_mpls(dev, items,
5494                                                            item_flags,
5495                                                            last_item, error);
5496                         if (ret < 0)
5497                                 return ret;
5498                         last_item = MLX5_FLOW_LAYER_MPLS;
5499                         break;
5500
5501                 case RTE_FLOW_ITEM_TYPE_MARK:
5502                         ret = flow_dv_validate_item_mark(dev, items, attr,
5503                                                          error);
5504                         if (ret < 0)
5505                                 return ret;
5506                         last_item = MLX5_FLOW_ITEM_MARK;
5507                         break;
5508                 case RTE_FLOW_ITEM_TYPE_META:
5509                         ret = flow_dv_validate_item_meta(dev, items, attr,
5510                                                          error);
5511                         if (ret < 0)
5512                                 return ret;
5513                         last_item = MLX5_FLOW_ITEM_METADATA;
5514                         break;
5515                 case RTE_FLOW_ITEM_TYPE_ICMP:
5516                         ret = mlx5_flow_validate_item_icmp(items, item_flags,
5517                                                            next_protocol,
5518                                                            error);
5519                         if (ret < 0)
5520                                 return ret;
5521                         last_item = MLX5_FLOW_LAYER_ICMP;
5522                         break;
5523                 case RTE_FLOW_ITEM_TYPE_ICMP6:
5524                         ret = mlx5_flow_validate_item_icmp6(items, item_flags,
5525                                                             next_protocol,
5526                                                             error);
5527                         if (ret < 0)
5528                                 return ret;
5529                         item_ipv6_proto = IPPROTO_ICMPV6;
5530                         last_item = MLX5_FLOW_LAYER_ICMP6;
5531                         break;
5532                 case RTE_FLOW_ITEM_TYPE_TAG:
5533                         ret = flow_dv_validate_item_tag(dev, items,
5534                                                         attr, error);
5535                         if (ret < 0)
5536                                 return ret;
5537                         last_item = MLX5_FLOW_ITEM_TAG;
5538                         break;
5539                 case MLX5_RTE_FLOW_ITEM_TYPE_TAG:
5540                 case MLX5_RTE_FLOW_ITEM_TYPE_TX_QUEUE:
5541                         break;
5542                 case RTE_FLOW_ITEM_TYPE_GTP:
5543                         ret = flow_dv_validate_item_gtp(dev, items, item_flags,
5544                                                         error);
5545                         if (ret < 0)
5546                                 return ret;
5547                         last_item = MLX5_FLOW_LAYER_GTP;
5548                         break;
5549                 case RTE_FLOW_ITEM_TYPE_ECPRI:
5550                         /* Capacity will be checked in the translate stage. */
5551                         ret = mlx5_flow_validate_item_ecpri(items, item_flags,
5552                                                             last_item,
5553                                                             ether_type,
5554                                                             &nic_ecpri_mask,
5555                                                             error);
5556                         if (ret < 0)
5557                                 return ret;
5558                         last_item = MLX5_FLOW_LAYER_ECPRI;
5559                         break;
5560                 default:
5561                         return rte_flow_error_set(error, ENOTSUP,
5562                                                   RTE_FLOW_ERROR_TYPE_ITEM,
5563                                                   NULL, "item not supported");
5564                 }
5565                 item_flags |= last_item;
5566         }
5567         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
5568                 int type = actions->type;
5569
5570                 if (!mlx5_flow_os_action_supported(type))
5571                         return rte_flow_error_set(error, ENOTSUP,
5572                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5573                                                   actions,
5574                                                   "action not supported");
5575                 if (actions_n == MLX5_DV_MAX_NUMBER_OF_ACTIONS)
5576                         return rte_flow_error_set(error, ENOTSUP,
5577                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5578                                                   actions, "too many actions");
5579                 switch (type) {
5580                 case RTE_FLOW_ACTION_TYPE_VOID:
5581                         break;
5582                 case RTE_FLOW_ACTION_TYPE_PORT_ID:
5583                         ret = flow_dv_validate_action_port_id(dev,
5584                                                               action_flags,
5585                                                               actions,
5586                                                               attr,
5587                                                               error);
5588                         if (ret)
5589                                 return ret;
5590                         action_flags |= MLX5_FLOW_ACTION_PORT_ID;
5591                         ++actions_n;
5592                         break;
5593                 case RTE_FLOW_ACTION_TYPE_FLAG:
5594                         ret = flow_dv_validate_action_flag(dev, action_flags,
5595                                                            attr, error);
5596                         if (ret < 0)
5597                                 return ret;
5598                         if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
5599                                 /* Count all modify-header actions as one. */
5600                                 if (!(action_flags &
5601                                       MLX5_FLOW_MODIFY_HDR_ACTIONS))
5602                                         ++actions_n;
5603                                 action_flags |= MLX5_FLOW_ACTION_FLAG |
5604                                                 MLX5_FLOW_ACTION_MARK_EXT;
5605                         } else {
5606                                 action_flags |= MLX5_FLOW_ACTION_FLAG;
5607                                 ++actions_n;
5608                         }
5609                         rw_act_num += MLX5_ACT_NUM_SET_MARK;
5610                         break;
5611                 case RTE_FLOW_ACTION_TYPE_MARK:
5612                         ret = flow_dv_validate_action_mark(dev, actions,
5613                                                            action_flags,
5614                                                            attr, error);
5615                         if (ret < 0)
5616                                 return ret;
5617                         if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
5618                                 /* Count all modify-header actions as one. */
5619                                 if (!(action_flags &
5620                                       MLX5_FLOW_MODIFY_HDR_ACTIONS))
5621                                         ++actions_n;
5622                                 action_flags |= MLX5_FLOW_ACTION_MARK |
5623                                                 MLX5_FLOW_ACTION_MARK_EXT;
5624                         } else {
5625                                 action_flags |= MLX5_FLOW_ACTION_MARK;
5626                                 ++actions_n;
5627                         }
5628                         rw_act_num += MLX5_ACT_NUM_SET_MARK;
5629                         break;
5630                 case RTE_FLOW_ACTION_TYPE_SET_META:
5631                         ret = flow_dv_validate_action_set_meta(dev, actions,
5632                                                                action_flags,
5633                                                                attr, error);
5634                         if (ret < 0)
5635                                 return ret;
5636                         /* Count all modify-header actions as one action. */
5637                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5638                                 ++actions_n;
5639                         action_flags |= MLX5_FLOW_ACTION_SET_META;
5640                         rw_act_num += MLX5_ACT_NUM_SET_META;
5641                         break;
5642                 case RTE_FLOW_ACTION_TYPE_SET_TAG:
5643                         ret = flow_dv_validate_action_set_tag(dev, actions,
5644                                                               action_flags,
5645                                                               attr, error);
5646                         if (ret < 0)
5647                                 return ret;
5648                         /* Count all modify-header actions as one action. */
5649                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5650                                 ++actions_n;
5651                         action_flags |= MLX5_FLOW_ACTION_SET_TAG;
5652                         rw_act_num += MLX5_ACT_NUM_SET_TAG;
5653                         break;
5654                 case RTE_FLOW_ACTION_TYPE_DROP:
5655                         ret = mlx5_flow_validate_action_drop(action_flags,
5656                                                              attr, error);
5657                         if (ret < 0)
5658                                 return ret;
5659                         action_flags |= MLX5_FLOW_ACTION_DROP;
5660                         ++actions_n;
5661                         break;
5662                 case RTE_FLOW_ACTION_TYPE_QUEUE:
5663                         ret = mlx5_flow_validate_action_queue(actions,
5664                                                               action_flags, dev,
5665                                                               attr, error);
5666                         if (ret < 0)
5667                                 return ret;
5668                         queue_index = ((const struct rte_flow_action_queue *)
5669                                                         (actions->conf))->index;
5670                         action_flags |= MLX5_FLOW_ACTION_QUEUE;
5671                         ++actions_n;
5672                         break;
5673                 case RTE_FLOW_ACTION_TYPE_RSS:
5674                         rss = actions->conf;
5675                         ret = mlx5_flow_validate_action_rss(actions,
5676                                                             action_flags, dev,
5677                                                             attr, item_flags,
5678                                                             error);
5679                         if (ret < 0)
5680                                 return ret;
5681                         if (rss != NULL && rss->queue_num)
5682                                 queue_index = rss->queue[0];
5683                         action_flags |= MLX5_FLOW_ACTION_RSS;
5684                         ++actions_n;
5685                         break;
5686                 case MLX5_RTE_FLOW_ACTION_TYPE_DEFAULT_MISS:
5687                         ret =
5688                         mlx5_flow_validate_action_default_miss(action_flags,
5689                                         attr, error);
5690                         if (ret < 0)
5691                                 return ret;
5692                         action_flags |= MLX5_FLOW_ACTION_DEFAULT_MISS;
5693                         ++actions_n;
5694                         break;
5695                 case RTE_FLOW_ACTION_TYPE_COUNT:
5696                         ret = flow_dv_validate_action_count(dev, error);
5697                         if (ret < 0)
5698                                 return ret;
5699                         action_flags |= MLX5_FLOW_ACTION_COUNT;
5700                         ++actions_n;
5701                         break;
5702                 case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
5703                         if (flow_dv_validate_action_pop_vlan(dev,
5704                                                              action_flags,
5705                                                              actions,
5706                                                              item_flags, attr,
5707                                                              error))
5708                                 return -rte_errno;
5709                         action_flags |= MLX5_FLOW_ACTION_OF_POP_VLAN;
5710                         ++actions_n;
5711                         break;
5712                 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
5713                         ret = flow_dv_validate_action_push_vlan(dev,
5714                                                                 action_flags,
5715                                                                 vlan_m,
5716                                                                 actions, attr,
5717                                                                 error);
5718                         if (ret < 0)
5719                                 return ret;
5720                         action_flags |= MLX5_FLOW_ACTION_OF_PUSH_VLAN;
5721                         ++actions_n;
5722                         break;
5723                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
5724                         ret = flow_dv_validate_action_set_vlan_pcp
5725                                                 (action_flags, actions, error);
5726                         if (ret < 0)
5727                                 return ret;
5728                         /* Count PCP with push_vlan command. */
5729                         action_flags |= MLX5_FLOW_ACTION_OF_SET_VLAN_PCP;
5730                         break;
5731                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
5732                         ret = flow_dv_validate_action_set_vlan_vid
5733                                                 (item_flags, action_flags,
5734                                                  actions, error);
5735                         if (ret < 0)
5736                                 return ret;
5737                         /* Count VID with push_vlan command. */
5738                         action_flags |= MLX5_FLOW_ACTION_OF_SET_VLAN_VID;
5739                         rw_act_num += MLX5_ACT_NUM_MDF_VID;
5740                         break;
5741                 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
5742                 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
5743                         ret = flow_dv_validate_action_l2_encap(dev,
5744                                                                action_flags,
5745                                                                actions, attr,
5746                                                                error);
5747                         if (ret < 0)
5748                                 return ret;
5749                         action_flags |= MLX5_FLOW_ACTION_ENCAP;
5750                         ++actions_n;
5751                         break;
5752                 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
5753                 case RTE_FLOW_ACTION_TYPE_NVGRE_DECAP:
5754                         ret = flow_dv_validate_action_decap(dev, action_flags,
5755                                                             attr, error);
5756                         if (ret < 0)
5757                                 return ret;
5758                         action_flags |= MLX5_FLOW_ACTION_DECAP;
5759                         ++actions_n;
5760                         break;
5761                 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
5762                         ret = flow_dv_validate_action_raw_encap_decap
5763                                 (dev, NULL, actions->conf, attr, &action_flags,
5764                                  &actions_n, error);
5765                         if (ret < 0)
5766                                 return ret;
5767                         break;
5768                 case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
5769                         decap = actions->conf;
5770                         while ((++actions)->type == RTE_FLOW_ACTION_TYPE_VOID)
5771                                 ;
5772                         if (actions->type != RTE_FLOW_ACTION_TYPE_RAW_ENCAP) {
5773                                 encap = NULL;
5774                                 actions--;
5775                         } else {
5776                                 encap = actions->conf;
5777                         }
5778                         ret = flow_dv_validate_action_raw_encap_decap
5779                                            (dev,
5780                                             decap ? decap : &empty_decap, encap,
5781                                             attr, &action_flags, &actions_n,
5782                                             error);
5783                         if (ret < 0)
5784                                 return ret;
5785                         break;
5786                 case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
5787                 case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
5788                         ret = flow_dv_validate_action_modify_mac(action_flags,
5789                                                                  actions,
5790                                                                  item_flags,
5791                                                                  error);
5792                         if (ret < 0)
5793                                 return ret;
5794                         /* Count all modify-header actions as one action. */
5795                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5796                                 ++actions_n;
5797                         action_flags |= actions->type ==
5798                                         RTE_FLOW_ACTION_TYPE_SET_MAC_SRC ?
5799                                                 MLX5_FLOW_ACTION_SET_MAC_SRC :
5800                                                 MLX5_FLOW_ACTION_SET_MAC_DST;
5801                         /*
5802                          * Even if the source and destination MAC addresses have
5803                          * overlap in the header with 4B alignment, the convert
5804                          * function will handle them separately and 4 SW actions
5805                          * will be created. And 2 actions will be added each
5806                          * time no matter how many bytes of address will be set.
5807                          */
5808                         rw_act_num += MLX5_ACT_NUM_MDF_MAC;
5809                         break;
5810                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
5811                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
5812                         ret = flow_dv_validate_action_modify_ipv4(action_flags,
5813                                                                   actions,
5814                                                                   item_flags,
5815                                                                   error);
5816                         if (ret < 0)
5817                                 return ret;
5818                         /* Count all modify-header actions as one action. */
5819                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5820                                 ++actions_n;
5821                         action_flags |= actions->type ==
5822                                         RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC ?
5823                                                 MLX5_FLOW_ACTION_SET_IPV4_SRC :
5824                                                 MLX5_FLOW_ACTION_SET_IPV4_DST;
5825                         rw_act_num += MLX5_ACT_NUM_MDF_IPV4;
5826                         break;
5827                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
5828                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
5829                         ret = flow_dv_validate_action_modify_ipv6(action_flags,
5830                                                                   actions,
5831                                                                   item_flags,
5832                                                                   error);
5833                         if (ret < 0)
5834                                 return ret;
5835                         if (item_ipv6_proto == IPPROTO_ICMPV6)
5836                                 return rte_flow_error_set(error, ENOTSUP,
5837                                         RTE_FLOW_ERROR_TYPE_ACTION,
5838                                         actions,
5839                                         "Can't change header "
5840                                         "with ICMPv6 proto");
5841                         /* Count all modify-header actions as one action. */
5842                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5843                                 ++actions_n;
5844                         action_flags |= actions->type ==
5845                                         RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC ?
5846                                                 MLX5_FLOW_ACTION_SET_IPV6_SRC :
5847                                                 MLX5_FLOW_ACTION_SET_IPV6_DST;
5848                         rw_act_num += MLX5_ACT_NUM_MDF_IPV6;
5849                         break;
5850                 case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
5851                 case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
5852                         ret = flow_dv_validate_action_modify_tp(action_flags,
5853                                                                 actions,
5854                                                                 item_flags,
5855                                                                 error);
5856                         if (ret < 0)
5857                                 return ret;
5858                         /* Count all modify-header actions as one action. */
5859                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5860                                 ++actions_n;
5861                         action_flags |= actions->type ==
5862                                         RTE_FLOW_ACTION_TYPE_SET_TP_SRC ?
5863                                                 MLX5_FLOW_ACTION_SET_TP_SRC :
5864                                                 MLX5_FLOW_ACTION_SET_TP_DST;
5865                         rw_act_num += MLX5_ACT_NUM_MDF_PORT;
5866                         break;
5867                 case RTE_FLOW_ACTION_TYPE_DEC_TTL:
5868                 case RTE_FLOW_ACTION_TYPE_SET_TTL:
5869                         ret = flow_dv_validate_action_modify_ttl(action_flags,
5870                                                                  actions,
5871                                                                  item_flags,
5872                                                                  error);
5873                         if (ret < 0)
5874                                 return ret;
5875                         /* Count all modify-header actions as one action. */
5876                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5877                                 ++actions_n;
5878                         action_flags |= actions->type ==
5879                                         RTE_FLOW_ACTION_TYPE_SET_TTL ?
5880                                                 MLX5_FLOW_ACTION_SET_TTL :
5881                                                 MLX5_FLOW_ACTION_DEC_TTL;
5882                         rw_act_num += MLX5_ACT_NUM_MDF_TTL;
5883                         break;
5884                 case RTE_FLOW_ACTION_TYPE_JUMP:
5885                         ret = flow_dv_validate_action_jump(dev, tunnel, actions,
5886                                                            action_flags,
5887                                                            attr, external,
5888                                                            error);
5889                         if (ret)
5890                                 return ret;
5891                         ++actions_n;
5892                         action_flags |= MLX5_FLOW_ACTION_JUMP;
5893                         break;
5894                 case RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ:
5895                 case RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ:
5896                         ret = flow_dv_validate_action_modify_tcp_seq
5897                                                                 (action_flags,
5898                                                                  actions,
5899                                                                  item_flags,
5900                                                                  error);
5901                         if (ret < 0)
5902                                 return ret;
5903                         /* Count all modify-header actions as one action. */
5904                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5905                                 ++actions_n;
5906                         action_flags |= actions->type ==
5907                                         RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ ?
5908                                                 MLX5_FLOW_ACTION_INC_TCP_SEQ :
5909                                                 MLX5_FLOW_ACTION_DEC_TCP_SEQ;
5910                         rw_act_num += MLX5_ACT_NUM_MDF_TCPSEQ;
5911                         break;
5912                 case RTE_FLOW_ACTION_TYPE_INC_TCP_ACK:
5913                 case RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK:
5914                         ret = flow_dv_validate_action_modify_tcp_ack
5915                                                                 (action_flags,
5916                                                                  actions,
5917                                                                  item_flags,
5918                                                                  error);
5919                         if (ret < 0)
5920                                 return ret;
5921                         /* Count all modify-header actions as one action. */
5922                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5923                                 ++actions_n;
5924                         action_flags |= actions->type ==
5925                                         RTE_FLOW_ACTION_TYPE_INC_TCP_ACK ?
5926                                                 MLX5_FLOW_ACTION_INC_TCP_ACK :
5927                                                 MLX5_FLOW_ACTION_DEC_TCP_ACK;
5928                         rw_act_num += MLX5_ACT_NUM_MDF_TCPACK;
5929                         break;
5930                 case MLX5_RTE_FLOW_ACTION_TYPE_MARK:
5931                         break;
5932                 case MLX5_RTE_FLOW_ACTION_TYPE_TAG:
5933                 case MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG:
5934                         rw_act_num += MLX5_ACT_NUM_SET_TAG;
5935                         break;
5936                 case RTE_FLOW_ACTION_TYPE_METER:
5937                         ret = mlx5_flow_validate_action_meter(dev,
5938                                                               action_flags,
5939                                                               actions, attr,
5940                                                               error);
5941                         if (ret < 0)
5942                                 return ret;
5943                         action_flags |= MLX5_FLOW_ACTION_METER;
5944                         ++actions_n;
5945                         /* Meter action will add one more TAG action. */
5946                         rw_act_num += MLX5_ACT_NUM_SET_TAG;
5947                         break;
5948                 case RTE_FLOW_ACTION_TYPE_AGE:
5949                         ret = flow_dv_validate_action_age(action_flags,
5950                                                           actions, dev,
5951                                                           error);
5952                         if (ret < 0)
5953                                 return ret;
5954                         action_flags |= MLX5_FLOW_ACTION_AGE;
5955                         ++actions_n;
5956                         break;
5957                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DSCP:
5958                         ret = flow_dv_validate_action_modify_ipv4_dscp
5959                                                          (action_flags,
5960                                                           actions,
5961                                                           item_flags,
5962                                                           error);
5963                         if (ret < 0)
5964                                 return ret;
5965                         /* Count all modify-header actions as one action. */
5966                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5967                                 ++actions_n;
5968                         action_flags |= MLX5_FLOW_ACTION_SET_IPV4_DSCP;
5969                         rw_act_num += MLX5_ACT_NUM_SET_DSCP;
5970                         break;
5971                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DSCP:
5972                         ret = flow_dv_validate_action_modify_ipv6_dscp
5973                                                                 (action_flags,
5974                                                                  actions,
5975                                                                  item_flags,
5976                                                                  error);
5977                         if (ret < 0)
5978                                 return ret;
5979                         /* Count all modify-header actions as one action. */
5980                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5981                                 ++actions_n;
5982                         action_flags |= MLX5_FLOW_ACTION_SET_IPV6_DSCP;
5983                         rw_act_num += MLX5_ACT_NUM_SET_DSCP;
5984                         break;
5985                 case RTE_FLOW_ACTION_TYPE_SAMPLE:
5986                         ret = flow_dv_validate_action_sample(action_flags,
5987                                                              actions, dev,
5988                                                              attr, error);
5989                         if (ret < 0)
5990                                 return ret;
5991                         action_flags |= MLX5_FLOW_ACTION_SAMPLE;
5992                         ++actions_n;
5993                         break;
5994                 case MLX5_RTE_FLOW_ACTION_TYPE_TUNNEL_SET:
5995                         if (actions[0].type != (typeof(actions[0].type))
5996                                 MLX5_RTE_FLOW_ACTION_TYPE_TUNNEL_SET)
5997                                 return rte_flow_error_set
5998                                                 (error, EINVAL,
5999                                                 RTE_FLOW_ERROR_TYPE_ACTION,
6000                                                 NULL, "MLX5 private action "
6001                                                 "must be the first");
6002
6003                         action_flags |= MLX5_FLOW_ACTION_TUNNEL_SET;
6004                         break;
6005                 default:
6006                         return rte_flow_error_set(error, ENOTSUP,
6007                                                   RTE_FLOW_ERROR_TYPE_ACTION,
6008                                                   actions,
6009                                                   "action not supported");
6010                 }
6011         }
6012         /*
6013          * Validate actions in flow rules
6014          * - Explicit decap action is prohibited by the tunnel offload API.
6015          * - Drop action in tunnel steer rule is prohibited by the API.
6016          * - Application cannot use MARK action because it's value can mask
6017          *   tunnel default miss nitification.
6018          * - JUMP in tunnel match rule has no support in current PMD
6019          *   implementation.
6020          * - TAG & META are reserved for future uses.
6021          */
6022         if (action_flags & MLX5_FLOW_ACTION_TUNNEL_SET) {
6023                 uint64_t bad_actions_mask = MLX5_FLOW_ACTION_DECAP    |
6024                                             MLX5_FLOW_ACTION_MARK     |
6025                                             MLX5_FLOW_ACTION_SET_TAG  |
6026                                             MLX5_FLOW_ACTION_SET_META |
6027                                             MLX5_FLOW_ACTION_DROP;
6028
6029                 if (action_flags & bad_actions_mask)
6030                         return rte_flow_error_set
6031                                         (error, EINVAL,
6032                                         RTE_FLOW_ERROR_TYPE_ACTION, NULL,
6033                                         "Invalid RTE action in tunnel "
6034                                         "set decap rule");
6035                 if (!(action_flags & MLX5_FLOW_ACTION_JUMP))
6036                         return rte_flow_error_set
6037                                         (error, EINVAL,
6038                                         RTE_FLOW_ERROR_TYPE_ACTION, NULL,
6039                                         "tunnel set decap rule must terminate "
6040                                         "with JUMP");
6041                 if (!attr->ingress)
6042                         return rte_flow_error_set
6043                                         (error, EINVAL,
6044                                         RTE_FLOW_ERROR_TYPE_ACTION, NULL,
6045                                         "tunnel flows for ingress traffic only");
6046         }
6047         if (action_flags & MLX5_FLOW_ACTION_TUNNEL_MATCH) {
6048                 uint64_t bad_actions_mask = MLX5_FLOW_ACTION_JUMP    |
6049                                             MLX5_FLOW_ACTION_MARK    |
6050                                             MLX5_FLOW_ACTION_SET_TAG |
6051                                             MLX5_FLOW_ACTION_SET_META;
6052
6053                 if (action_flags & bad_actions_mask)
6054                         return rte_flow_error_set
6055                                         (error, EINVAL,
6056                                         RTE_FLOW_ERROR_TYPE_ACTION, NULL,
6057                                         "Invalid RTE action in tunnel "
6058                                         "set match rule");
6059         }
6060         /*
6061          * Validate the drop action mutual exclusion with other actions.
6062          * Drop action is mutually-exclusive with any other action, except for
6063          * Count action.
6064          */
6065         if ((action_flags & MLX5_FLOW_ACTION_DROP) &&
6066             (action_flags & ~(MLX5_FLOW_ACTION_DROP | MLX5_FLOW_ACTION_COUNT)))
6067                 return rte_flow_error_set(error, EINVAL,
6068                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
6069                                           "Drop action is mutually-exclusive "
6070                                           "with any other action, except for "
6071                                           "Count action");
6072         /* Eswitch has few restrictions on using items and actions */
6073         if (attr->transfer) {
6074                 if (!mlx5_flow_ext_mreg_supported(dev) &&
6075                     action_flags & MLX5_FLOW_ACTION_FLAG)
6076                         return rte_flow_error_set(error, ENOTSUP,
6077                                                   RTE_FLOW_ERROR_TYPE_ACTION,
6078                                                   NULL,
6079                                                   "unsupported action FLAG");
6080                 if (!mlx5_flow_ext_mreg_supported(dev) &&
6081                     action_flags & MLX5_FLOW_ACTION_MARK)
6082                         return rte_flow_error_set(error, ENOTSUP,
6083                                                   RTE_FLOW_ERROR_TYPE_ACTION,
6084                                                   NULL,
6085                                                   "unsupported action MARK");
6086                 if (action_flags & MLX5_FLOW_ACTION_QUEUE)
6087                         return rte_flow_error_set(error, ENOTSUP,
6088                                                   RTE_FLOW_ERROR_TYPE_ACTION,
6089                                                   NULL,
6090                                                   "unsupported action QUEUE");
6091                 if (action_flags & MLX5_FLOW_ACTION_RSS)
6092                         return rte_flow_error_set(error, ENOTSUP,
6093                                                   RTE_FLOW_ERROR_TYPE_ACTION,
6094                                                   NULL,
6095                                                   "unsupported action RSS");
6096                 if (!(action_flags & MLX5_FLOW_FATE_ESWITCH_ACTIONS))
6097                         return rte_flow_error_set(error, EINVAL,
6098                                                   RTE_FLOW_ERROR_TYPE_ACTION,
6099                                                   actions,
6100                                                   "no fate action is found");
6101         } else {
6102                 if (!(action_flags & MLX5_FLOW_FATE_ACTIONS) && attr->ingress)
6103                         return rte_flow_error_set(error, EINVAL,
6104                                                   RTE_FLOW_ERROR_TYPE_ACTION,
6105                                                   actions,
6106                                                   "no fate action is found");
6107         }
6108         /*
6109          * Continue validation for Xcap and VLAN actions.
6110          * If hairpin is working in explicit TX rule mode, there is no actions
6111          * splitting and the validation of hairpin ingress flow should be the
6112          * same as other standard flows.
6113          */
6114         if ((action_flags & (MLX5_FLOW_XCAP_ACTIONS |
6115                              MLX5_FLOW_VLAN_ACTIONS)) &&
6116             (queue_index == 0xFFFF ||
6117              mlx5_rxq_get_type(dev, queue_index) != MLX5_RXQ_TYPE_HAIRPIN ||
6118              ((conf = mlx5_rxq_get_hairpin_conf(dev, queue_index)) != NULL &&
6119              conf->tx_explicit != 0))) {
6120                 if ((action_flags & MLX5_FLOW_XCAP_ACTIONS) ==
6121                     MLX5_FLOW_XCAP_ACTIONS)
6122                         return rte_flow_error_set(error, ENOTSUP,
6123                                                   RTE_FLOW_ERROR_TYPE_ACTION,
6124                                                   NULL, "encap and decap "
6125                                                   "combination aren't supported");
6126                 if (!attr->transfer && attr->ingress) {
6127                         if (action_flags & MLX5_FLOW_ACTION_ENCAP)
6128                                 return rte_flow_error_set
6129                                                 (error, ENOTSUP,
6130                                                  RTE_FLOW_ERROR_TYPE_ACTION,
6131                                                  NULL, "encap is not supported"
6132                                                  " for ingress traffic");
6133                         else if (action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN)
6134                                 return rte_flow_error_set
6135                                                 (error, ENOTSUP,
6136                                                  RTE_FLOW_ERROR_TYPE_ACTION,
6137                                                  NULL, "push VLAN action not "
6138                                                  "supported for ingress");
6139                         else if ((action_flags & MLX5_FLOW_VLAN_ACTIONS) ==
6140                                         MLX5_FLOW_VLAN_ACTIONS)
6141                                 return rte_flow_error_set
6142                                                 (error, ENOTSUP,
6143                                                  RTE_FLOW_ERROR_TYPE_ACTION,
6144                                                  NULL, "no support for "
6145                                                  "multiple VLAN actions");
6146                 }
6147         }
6148         /*
6149          * Hairpin flow will add one more TAG action in TX implicit mode.
6150          * In TX explicit mode, there will be no hairpin flow ID.
6151          */
6152         if (hairpin > 0)
6153                 rw_act_num += MLX5_ACT_NUM_SET_TAG;
6154         /* extra metadata enabled: one more TAG action will be add. */
6155         if (dev_conf->dv_flow_en &&
6156             dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY &&
6157             mlx5_flow_ext_mreg_supported(dev))
6158                 rw_act_num += MLX5_ACT_NUM_SET_TAG;
6159         if ((uint32_t)rw_act_num >
6160                         flow_dv_modify_hdr_action_max(dev, is_root)) {
6161                 return rte_flow_error_set(error, ENOTSUP,
6162                                           RTE_FLOW_ERROR_TYPE_ACTION,
6163                                           NULL, "too many header modify"
6164                                           " actions to support");
6165         }
6166         return 0;
6167 }
6168
6169 /**
6170  * Internal preparation function. Allocates the DV flow size,
6171  * this size is constant.
6172  *
6173  * @param[in] dev
6174  *   Pointer to the rte_eth_dev structure.
6175  * @param[in] attr
6176  *   Pointer to the flow attributes.
6177  * @param[in] items
6178  *   Pointer to the list of items.
6179  * @param[in] actions
6180  *   Pointer to the list of actions.
6181  * @param[out] error
6182  *   Pointer to the error structure.
6183  *
6184  * @return
6185  *   Pointer to mlx5_flow object on success,
6186  *   otherwise NULL and rte_errno is set.
6187  */
6188 static struct mlx5_flow *
6189 flow_dv_prepare(struct rte_eth_dev *dev,
6190                 const struct rte_flow_attr *attr __rte_unused,
6191                 const struct rte_flow_item items[] __rte_unused,
6192                 const struct rte_flow_action actions[] __rte_unused,
6193                 struct rte_flow_error *error)
6194 {
6195         uint32_t handle_idx = 0;
6196         struct mlx5_flow *dev_flow;
6197         struct mlx5_flow_handle *dev_handle;
6198         struct mlx5_priv *priv = dev->data->dev_private;
6199         struct mlx5_flow_workspace *wks = mlx5_flow_get_thread_workspace();
6200
6201         MLX5_ASSERT(wks);
6202         /* In case of corrupting the memory. */
6203         if (wks->flow_idx >= MLX5_NUM_MAX_DEV_FLOWS) {
6204                 rte_flow_error_set(error, ENOSPC,
6205                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
6206                                    "not free temporary device flow");
6207                 return NULL;
6208         }
6209         dev_handle = mlx5_ipool_zmalloc(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
6210                                    &handle_idx);
6211         if (!dev_handle) {
6212                 rte_flow_error_set(error, ENOMEM,
6213                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
6214                                    "not enough memory to create flow handle");
6215                 return NULL;
6216         }
6217         MLX5_ASSERT(wks->flow_idx + 1 < RTE_DIM(wks->flows));
6218         dev_flow = &wks->flows[wks->flow_idx++];
6219         dev_flow->handle = dev_handle;
6220         dev_flow->handle_idx = handle_idx;
6221         /*
6222          * In some old rdma-core releases, before continuing, a check of the
6223          * length of matching parameter will be done at first. It needs to use
6224          * the length without misc4 param. If the flow has misc4 support, then
6225          * the length needs to be adjusted accordingly. Each param member is
6226          * aligned with a 64B boundary naturally.
6227          */
6228         dev_flow->dv.value.size = MLX5_ST_SZ_BYTES(fte_match_param) -
6229                                   MLX5_ST_SZ_BYTES(fte_match_set_misc4);
6230         /*
6231          * The matching value needs to be cleared to 0 before using. In the
6232          * past, it will be automatically cleared when using rte_*alloc
6233          * API. The time consumption will be almost the same as before.
6234          */
6235         memset(dev_flow->dv.value.buf, 0, MLX5_ST_SZ_BYTES(fte_match_param));
6236         dev_flow->ingress = attr->ingress;
6237         dev_flow->dv.transfer = attr->transfer;
6238         return dev_flow;
6239 }
6240
6241 #ifdef RTE_LIBRTE_MLX5_DEBUG
6242 /**
6243  * Sanity check for match mask and value. Similar to check_valid_spec() in
6244  * kernel driver. If unmasked bit is present in value, it returns failure.
6245  *
6246  * @param match_mask
6247  *   pointer to match mask buffer.
6248  * @param match_value
6249  *   pointer to match value buffer.
6250  *
6251  * @return
6252  *   0 if valid, -EINVAL otherwise.
6253  */
6254 static int
6255 flow_dv_check_valid_spec(void *match_mask, void *match_value)
6256 {
6257         uint8_t *m = match_mask;
6258         uint8_t *v = match_value;
6259         unsigned int i;
6260
6261         for (i = 0; i < MLX5_ST_SZ_BYTES(fte_match_param); ++i) {
6262                 if (v[i] & ~m[i]) {
6263                         DRV_LOG(ERR,
6264                                 "match_value differs from match_criteria"
6265                                 " %p[%u] != %p[%u]",
6266                                 match_value, i, match_mask, i);
6267                         return -EINVAL;
6268                 }
6269         }
6270         return 0;
6271 }
6272 #endif
6273
6274 /**
6275  * Add match of ip_version.
6276  *
6277  * @param[in] group
6278  *   Flow group.
6279  * @param[in] headers_v
6280  *   Values header pointer.
6281  * @param[in] headers_m
6282  *   Masks header pointer.
6283  * @param[in] ip_version
6284  *   The IP version to set.
6285  */
6286 static inline void
6287 flow_dv_set_match_ip_version(uint32_t group,
6288                              void *headers_v,
6289                              void *headers_m,
6290                              uint8_t ip_version)
6291 {
6292         if (group == 0)
6293                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version, 0xf);
6294         else
6295                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version,
6296                          ip_version);
6297         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_version, ip_version);
6298         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype, 0);
6299         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ethertype, 0);
6300 }
6301
6302 /**
6303  * Add Ethernet item to matcher and to the value.
6304  *
6305  * @param[in, out] matcher
6306  *   Flow matcher.
6307  * @param[in, out] key
6308  *   Flow matcher value.
6309  * @param[in] item
6310  *   Flow pattern to translate.
6311  * @param[in] inner
6312  *   Item is inner pattern.
6313  */
6314 static void
6315 flow_dv_translate_item_eth(void *matcher, void *key,
6316                            const struct rte_flow_item *item, int inner,
6317                            uint32_t group)
6318 {
6319         const struct rte_flow_item_eth *eth_m = item->mask;
6320         const struct rte_flow_item_eth *eth_v = item->spec;
6321         const struct rte_flow_item_eth nic_mask = {
6322                 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
6323                 .src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
6324                 .type = RTE_BE16(0xffff),
6325                 .has_vlan = 0,
6326         };
6327         void *hdrs_m;
6328         void *hdrs_v;
6329         char *l24_v;
6330         unsigned int i;
6331
6332         if (!eth_v)
6333                 return;
6334         if (!eth_m)
6335                 eth_m = &nic_mask;
6336         if (inner) {
6337                 hdrs_m = MLX5_ADDR_OF(fte_match_param, matcher,
6338                                          inner_headers);
6339                 hdrs_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
6340         } else {
6341                 hdrs_m = MLX5_ADDR_OF(fte_match_param, matcher,
6342                                          outer_headers);
6343                 hdrs_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6344         }
6345         memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, hdrs_m, dmac_47_16),
6346                &eth_m->dst, sizeof(eth_m->dst));
6347         /* The value must be in the range of the mask. */
6348         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, hdrs_v, dmac_47_16);
6349         for (i = 0; i < sizeof(eth_m->dst); ++i)
6350                 l24_v[i] = eth_m->dst.addr_bytes[i] & eth_v->dst.addr_bytes[i];
6351         memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, hdrs_m, smac_47_16),
6352                &eth_m->src, sizeof(eth_m->src));
6353         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, hdrs_v, smac_47_16);
6354         /* The value must be in the range of the mask. */
6355         for (i = 0; i < sizeof(eth_m->dst); ++i)
6356                 l24_v[i] = eth_m->src.addr_bytes[i] & eth_v->src.addr_bytes[i];
6357         /*
6358          * HW supports match on one Ethertype, the Ethertype following the last
6359          * VLAN tag of the packet (see PRM).
6360          * Set match on ethertype only if ETH header is not followed by VLAN.
6361          * HW is optimized for IPv4/IPv6. In such cases, avoid setting
6362          * ethertype, and use ip_version field instead.
6363          * eCPRI over Ether layer will use type value 0xAEFE.
6364          */
6365         if (eth_m->type == 0xFFFF) {
6366                 /* Set cvlan_tag mask for any single\multi\un-tagged case. */
6367                 MLX5_SET(fte_match_set_lyr_2_4, hdrs_m, cvlan_tag, 1);
6368                 switch (eth_v->type) {
6369                 case RTE_BE16(RTE_ETHER_TYPE_VLAN):
6370                         MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, cvlan_tag, 1);
6371                         return;
6372                 case RTE_BE16(RTE_ETHER_TYPE_QINQ):
6373                         MLX5_SET(fte_match_set_lyr_2_4, hdrs_m, svlan_tag, 1);
6374                         MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, svlan_tag, 1);
6375                         return;
6376                 case RTE_BE16(RTE_ETHER_TYPE_IPV4):
6377                         flow_dv_set_match_ip_version(group, hdrs_v, hdrs_m, 4);
6378                         return;
6379                 case RTE_BE16(RTE_ETHER_TYPE_IPV6):
6380                         flow_dv_set_match_ip_version(group, hdrs_v, hdrs_m, 6);
6381                         return;
6382                 default:
6383                         break;
6384                 }
6385         }
6386         if (eth_m->has_vlan) {
6387                 MLX5_SET(fte_match_set_lyr_2_4, hdrs_m, cvlan_tag, 1);
6388                 if (eth_v->has_vlan) {
6389                         /*
6390                          * Here, when also has_more_vlan field in VLAN item is
6391                          * not set, only single-tagged packets will be matched.
6392                          */
6393                         MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, cvlan_tag, 1);
6394                         return;
6395                 }
6396         }
6397         MLX5_SET(fte_match_set_lyr_2_4, hdrs_m, ethertype,
6398                  rte_be_to_cpu_16(eth_m->type));
6399         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, hdrs_v, ethertype);
6400         *(uint16_t *)(l24_v) = eth_m->type & eth_v->type;
6401 }
6402
6403 /**
6404  * Add VLAN item to matcher and to the value.
6405  *
6406  * @param[in, out] dev_flow
6407  *   Flow descriptor.
6408  * @param[in, out] matcher
6409  *   Flow matcher.
6410  * @param[in, out] key
6411  *   Flow matcher value.
6412  * @param[in] item
6413  *   Flow pattern to translate.
6414  * @param[in] inner
6415  *   Item is inner pattern.
6416  */
6417 static void
6418 flow_dv_translate_item_vlan(struct mlx5_flow *dev_flow,
6419                             void *matcher, void *key,
6420                             const struct rte_flow_item *item,
6421                             int inner, uint32_t group)
6422 {
6423         const struct rte_flow_item_vlan *vlan_m = item->mask;
6424         const struct rte_flow_item_vlan *vlan_v = item->spec;
6425         void *hdrs_m;
6426         void *hdrs_v;
6427         uint16_t tci_m;
6428         uint16_t tci_v;
6429
6430         if (inner) {
6431                 hdrs_m = MLX5_ADDR_OF(fte_match_param, matcher,
6432                                          inner_headers);
6433                 hdrs_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
6434         } else {
6435                 hdrs_m = MLX5_ADDR_OF(fte_match_param, matcher,
6436                                          outer_headers);
6437                 hdrs_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6438                 /*
6439                  * This is workaround, masks are not supported,
6440                  * and pre-validated.
6441                  */
6442                 if (vlan_v)
6443                         dev_flow->handle->vf_vlan.tag =
6444                                         rte_be_to_cpu_16(vlan_v->tci) & 0x0fff;
6445         }
6446         /*
6447          * When VLAN item exists in flow, mark packet as tagged,
6448          * even if TCI is not specified.
6449          */
6450         if (!MLX5_GET(fte_match_set_lyr_2_4, hdrs_v, svlan_tag)) {
6451                 MLX5_SET(fte_match_set_lyr_2_4, hdrs_m, cvlan_tag, 1);
6452                 MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, cvlan_tag, 1);
6453         }
6454         if (!vlan_v)
6455                 return;
6456         if (!vlan_m)
6457                 vlan_m = &rte_flow_item_vlan_mask;
6458         tci_m = rte_be_to_cpu_16(vlan_m->tci);
6459         tci_v = rte_be_to_cpu_16(vlan_m->tci & vlan_v->tci);
6460         MLX5_SET(fte_match_set_lyr_2_4, hdrs_m, first_vid, tci_m);
6461         MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, first_vid, tci_v);
6462         MLX5_SET(fte_match_set_lyr_2_4, hdrs_m, first_cfi, tci_m >> 12);
6463         MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, first_cfi, tci_v >> 12);
6464         MLX5_SET(fte_match_set_lyr_2_4, hdrs_m, first_prio, tci_m >> 13);
6465         MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, first_prio, tci_v >> 13);
6466         /*
6467          * HW is optimized for IPv4/IPv6. In such cases, avoid setting
6468          * ethertype, and use ip_version field instead.
6469          */
6470         if (vlan_m->inner_type == 0xFFFF) {
6471                 switch (vlan_v->inner_type) {
6472                 case RTE_BE16(RTE_ETHER_TYPE_VLAN):
6473                         MLX5_SET(fte_match_set_lyr_2_4, hdrs_m, svlan_tag, 1);
6474                         MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, svlan_tag, 1);
6475                         MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, cvlan_tag, 0);
6476                         return;
6477                 case RTE_BE16(RTE_ETHER_TYPE_IPV4):
6478                         flow_dv_set_match_ip_version(group, hdrs_v, hdrs_m, 4);
6479                         return;
6480                 case RTE_BE16(RTE_ETHER_TYPE_IPV6):
6481                         flow_dv_set_match_ip_version(group, hdrs_v, hdrs_m, 6);
6482                         return;
6483                 default:
6484                         break;
6485                 }
6486         }
6487         if (vlan_m->has_more_vlan && vlan_v->has_more_vlan) {
6488                 MLX5_SET(fte_match_set_lyr_2_4, hdrs_m, svlan_tag, 1);
6489                 MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, svlan_tag, 1);
6490                 /* Only one vlan_tag bit can be set. */
6491                 MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, cvlan_tag, 0);
6492                 return;
6493         }
6494         MLX5_SET(fte_match_set_lyr_2_4, hdrs_m, ethertype,
6495                  rte_be_to_cpu_16(vlan_m->inner_type));
6496         MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, ethertype,
6497                  rte_be_to_cpu_16(vlan_m->inner_type & vlan_v->inner_type));
6498 }
6499
6500 /**
6501  * Add IPV4 item to matcher and to the value.
6502  *
6503  * @param[in, out] matcher
6504  *   Flow matcher.
6505  * @param[in, out] key
6506  *   Flow matcher value.
6507  * @param[in] item
6508  *   Flow pattern to translate.
6509  * @param[in] inner
6510  *   Item is inner pattern.
6511  * @param[in] group
6512  *   The group to insert the rule.
6513  */
6514 static void
6515 flow_dv_translate_item_ipv4(void *matcher, void *key,
6516                             const struct rte_flow_item *item,
6517                             int inner, uint32_t group)
6518 {
6519         const struct rte_flow_item_ipv4 *ipv4_m = item->mask;
6520         const struct rte_flow_item_ipv4 *ipv4_v = item->spec;
6521         const struct rte_flow_item_ipv4 nic_mask = {
6522                 .hdr = {
6523                         .src_addr = RTE_BE32(0xffffffff),
6524                         .dst_addr = RTE_BE32(0xffffffff),
6525                         .type_of_service = 0xff,
6526                         .next_proto_id = 0xff,
6527                         .time_to_live = 0xff,
6528                 },
6529         };
6530         void *headers_m;
6531         void *headers_v;
6532         char *l24_m;
6533         char *l24_v;
6534         uint8_t tos;
6535
6536         if (inner) {
6537                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6538                                          inner_headers);
6539                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
6540         } else {
6541                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6542                                          outer_headers);
6543                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6544         }
6545         flow_dv_set_match_ip_version(group, headers_v, headers_m, 4);
6546         if (!ipv4_v)
6547                 return;
6548         if (!ipv4_m)
6549                 ipv4_m = &nic_mask;
6550         l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
6551                              dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
6552         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
6553                              dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
6554         *(uint32_t *)l24_m = ipv4_m->hdr.dst_addr;
6555         *(uint32_t *)l24_v = ipv4_m->hdr.dst_addr & ipv4_v->hdr.dst_addr;
6556         l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
6557                           src_ipv4_src_ipv6.ipv4_layout.ipv4);
6558         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
6559                           src_ipv4_src_ipv6.ipv4_layout.ipv4);
6560         *(uint32_t *)l24_m = ipv4_m->hdr.src_addr;
6561         *(uint32_t *)l24_v = ipv4_m->hdr.src_addr & ipv4_v->hdr.src_addr;
6562         tos = ipv4_m->hdr.type_of_service & ipv4_v->hdr.type_of_service;
6563         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ecn,
6564                  ipv4_m->hdr.type_of_service);
6565         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn, tos);
6566         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_dscp,
6567                  ipv4_m->hdr.type_of_service >> 2);
6568         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp, tos >> 2);
6569         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol,
6570                  ipv4_m->hdr.next_proto_id);
6571         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
6572                  ipv4_v->hdr.next_proto_id & ipv4_m->hdr.next_proto_id);
6573         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ttl_hoplimit,
6574                  ipv4_m->hdr.time_to_live);
6575         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ttl_hoplimit,
6576                  ipv4_v->hdr.time_to_live & ipv4_m->hdr.time_to_live);
6577         MLX5_SET(fte_match_set_lyr_2_4, headers_m, frag,
6578                  !!(ipv4_m->hdr.fragment_offset));
6579         MLX5_SET(fte_match_set_lyr_2_4, headers_v, frag,
6580                  !!(ipv4_v->hdr.fragment_offset & ipv4_m->hdr.fragment_offset));
6581 }
6582
6583 /**
6584  * Add IPV6 item to matcher and to the value.
6585  *
6586  * @param[in, out] matcher
6587  *   Flow matcher.
6588  * @param[in, out] key
6589  *   Flow matcher value.
6590  * @param[in] item
6591  *   Flow pattern to translate.
6592  * @param[in] inner
6593  *   Item is inner pattern.
6594  * @param[in] group
6595  *   The group to insert the rule.
6596  */
6597 static void
6598 flow_dv_translate_item_ipv6(void *matcher, void *key,
6599                             const struct rte_flow_item *item,
6600                             int inner, uint32_t group)
6601 {
6602         const struct rte_flow_item_ipv6 *ipv6_m = item->mask;
6603         const struct rte_flow_item_ipv6 *ipv6_v = item->spec;
6604         const struct rte_flow_item_ipv6 nic_mask = {
6605                 .hdr = {
6606                         .src_addr =
6607                                 "\xff\xff\xff\xff\xff\xff\xff\xff"
6608                                 "\xff\xff\xff\xff\xff\xff\xff\xff",
6609                         .dst_addr =
6610                                 "\xff\xff\xff\xff\xff\xff\xff\xff"
6611                                 "\xff\xff\xff\xff\xff\xff\xff\xff",
6612                         .vtc_flow = RTE_BE32(0xffffffff),
6613                         .proto = 0xff,
6614                         .hop_limits = 0xff,
6615                 },
6616         };
6617         void *headers_m;
6618         void *headers_v;
6619         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
6620         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
6621         char *l24_m;
6622         char *l24_v;
6623         uint32_t vtc_m;
6624         uint32_t vtc_v;
6625         int i;
6626         int size;
6627
6628         if (inner) {
6629                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6630                                          inner_headers);
6631                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
6632         } else {
6633                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6634                                          outer_headers);
6635                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6636         }
6637         flow_dv_set_match_ip_version(group, headers_v, headers_m, 6);
6638         if (!ipv6_v)
6639                 return;
6640         if (!ipv6_m)
6641                 ipv6_m = &nic_mask;
6642         size = sizeof(ipv6_m->hdr.dst_addr);
6643         l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
6644                              dst_ipv4_dst_ipv6.ipv6_layout.ipv6);
6645         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
6646                              dst_ipv4_dst_ipv6.ipv6_layout.ipv6);
6647         memcpy(l24_m, ipv6_m->hdr.dst_addr, size);
6648         for (i = 0; i < size; ++i)
6649                 l24_v[i] = l24_m[i] & ipv6_v->hdr.dst_addr[i];
6650         l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
6651                              src_ipv4_src_ipv6.ipv6_layout.ipv6);
6652         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
6653                              src_ipv4_src_ipv6.ipv6_layout.ipv6);
6654         memcpy(l24_m, ipv6_m->hdr.src_addr, size);
6655         for (i = 0; i < size; ++i)
6656                 l24_v[i] = l24_m[i] & ipv6_v->hdr.src_addr[i];
6657         /* TOS. */
6658         vtc_m = rte_be_to_cpu_32(ipv6_m->hdr.vtc_flow);
6659         vtc_v = rte_be_to_cpu_32(ipv6_m->hdr.vtc_flow & ipv6_v->hdr.vtc_flow);
6660         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ecn, vtc_m >> 20);
6661         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn, vtc_v >> 20);
6662         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_dscp, vtc_m >> 22);
6663         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp, vtc_v >> 22);
6664         /* Label. */
6665         if (inner) {
6666                 MLX5_SET(fte_match_set_misc, misc_m, inner_ipv6_flow_label,
6667                          vtc_m);
6668                 MLX5_SET(fte_match_set_misc, misc_v, inner_ipv6_flow_label,
6669                          vtc_v);
6670         } else {
6671                 MLX5_SET(fte_match_set_misc, misc_m, outer_ipv6_flow_label,
6672                          vtc_m);
6673                 MLX5_SET(fte_match_set_misc, misc_v, outer_ipv6_flow_label,
6674                          vtc_v);
6675         }
6676         /* Protocol. */
6677         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol,
6678                  ipv6_m->hdr.proto);
6679         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
6680                  ipv6_v->hdr.proto & ipv6_m->hdr.proto);
6681         /* Hop limit. */
6682         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ttl_hoplimit,
6683                  ipv6_m->hdr.hop_limits);
6684         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ttl_hoplimit,
6685                  ipv6_v->hdr.hop_limits & ipv6_m->hdr.hop_limits);
6686         MLX5_SET(fte_match_set_lyr_2_4, headers_m, frag,
6687                  !!(ipv6_m->has_frag_ext));
6688         MLX5_SET(fte_match_set_lyr_2_4, headers_v, frag,
6689                  !!(ipv6_v->has_frag_ext & ipv6_m->has_frag_ext));
6690 }
6691
6692 /**
6693  * Add IPV6 fragment extension item to matcher and to the value.
6694  *
6695  * @param[in, out] matcher
6696  *   Flow matcher.
6697  * @param[in, out] key
6698  *   Flow matcher value.
6699  * @param[in] item
6700  *   Flow pattern to translate.
6701  * @param[in] inner
6702  *   Item is inner pattern.
6703  */
6704 static void
6705 flow_dv_translate_item_ipv6_frag_ext(void *matcher, void *key,
6706                                      const struct rte_flow_item *item,
6707                                      int inner)
6708 {
6709         const struct rte_flow_item_ipv6_frag_ext *ipv6_frag_ext_m = item->mask;
6710         const struct rte_flow_item_ipv6_frag_ext *ipv6_frag_ext_v = item->spec;
6711         const struct rte_flow_item_ipv6_frag_ext nic_mask = {
6712                 .hdr = {
6713                         .next_header = 0xff,
6714                         .frag_data = RTE_BE16(0xffff),
6715                 },
6716         };
6717         void *headers_m;
6718         void *headers_v;
6719
6720         if (inner) {
6721                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6722                                          inner_headers);
6723                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
6724         } else {
6725                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6726                                          outer_headers);
6727                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6728         }
6729         /* IPv6 fragment extension item exists, so packet is IP fragment. */
6730         MLX5_SET(fte_match_set_lyr_2_4, headers_m, frag, 1);
6731         MLX5_SET(fte_match_set_lyr_2_4, headers_v, frag, 1);
6732         if (!ipv6_frag_ext_v)
6733                 return;
6734         if (!ipv6_frag_ext_m)
6735                 ipv6_frag_ext_m = &nic_mask;
6736         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol,
6737                  ipv6_frag_ext_m->hdr.next_header);
6738         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
6739                  ipv6_frag_ext_v->hdr.next_header &
6740                  ipv6_frag_ext_m->hdr.next_header);
6741 }
6742
6743 /**
6744  * Add TCP item to matcher and to the value.
6745  *
6746  * @param[in, out] matcher
6747  *   Flow matcher.
6748  * @param[in, out] key
6749  *   Flow matcher value.
6750  * @param[in] item
6751  *   Flow pattern to translate.
6752  * @param[in] inner
6753  *   Item is inner pattern.
6754  */
6755 static void
6756 flow_dv_translate_item_tcp(void *matcher, void *key,
6757                            const struct rte_flow_item *item,
6758                            int inner)
6759 {
6760         const struct rte_flow_item_tcp *tcp_m = item->mask;
6761         const struct rte_flow_item_tcp *tcp_v = item->spec;
6762         void *headers_m;
6763         void *headers_v;
6764
6765         if (inner) {
6766                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6767                                          inner_headers);
6768                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
6769         } else {
6770                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6771                                          outer_headers);
6772                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6773         }
6774         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
6775         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_TCP);
6776         if (!tcp_v)
6777                 return;
6778         if (!tcp_m)
6779                 tcp_m = &rte_flow_item_tcp_mask;
6780         MLX5_SET(fte_match_set_lyr_2_4, headers_m, tcp_sport,
6781                  rte_be_to_cpu_16(tcp_m->hdr.src_port));
6782         MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
6783                  rte_be_to_cpu_16(tcp_v->hdr.src_port & tcp_m->hdr.src_port));
6784         MLX5_SET(fte_match_set_lyr_2_4, headers_m, tcp_dport,
6785                  rte_be_to_cpu_16(tcp_m->hdr.dst_port));
6786         MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
6787                  rte_be_to_cpu_16(tcp_v->hdr.dst_port & tcp_m->hdr.dst_port));
6788         MLX5_SET(fte_match_set_lyr_2_4, headers_m, tcp_flags,
6789                  tcp_m->hdr.tcp_flags);
6790         MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_flags,
6791                  (tcp_v->hdr.tcp_flags & tcp_m->hdr.tcp_flags));
6792 }
6793
6794 /**
6795  * Add UDP item to matcher and to the value.
6796  *
6797  * @param[in, out] matcher
6798  *   Flow matcher.
6799  * @param[in, out] key
6800  *   Flow matcher value.
6801  * @param[in] item
6802  *   Flow pattern to translate.
6803  * @param[in] inner
6804  *   Item is inner pattern.
6805  */
6806 static void
6807 flow_dv_translate_item_udp(void *matcher, void *key,
6808                            const struct rte_flow_item *item,
6809                            int inner)
6810 {
6811         const struct rte_flow_item_udp *udp_m = item->mask;
6812         const struct rte_flow_item_udp *udp_v = item->spec;
6813         void *headers_m;
6814         void *headers_v;
6815
6816         if (inner) {
6817                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6818                                          inner_headers);
6819                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
6820         } else {
6821                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6822                                          outer_headers);
6823                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6824         }
6825         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
6826         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_UDP);
6827         if (!udp_v)
6828                 return;
6829         if (!udp_m)
6830                 udp_m = &rte_flow_item_udp_mask;
6831         MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_sport,
6832                  rte_be_to_cpu_16(udp_m->hdr.src_port));
6833         MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
6834                  rte_be_to_cpu_16(udp_v->hdr.src_port & udp_m->hdr.src_port));
6835         MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport,
6836                  rte_be_to_cpu_16(udp_m->hdr.dst_port));
6837         MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
6838                  rte_be_to_cpu_16(udp_v->hdr.dst_port & udp_m->hdr.dst_port));
6839 }
6840
6841 /**
6842  * Add GRE optional Key item to matcher and to the value.
6843  *
6844  * @param[in, out] matcher
6845  *   Flow matcher.
6846  * @param[in, out] key
6847  *   Flow matcher value.
6848  * @param[in] item
6849  *   Flow pattern to translate.
6850  * @param[in] inner
6851  *   Item is inner pattern.
6852  */
6853 static void
6854 flow_dv_translate_item_gre_key(void *matcher, void *key,
6855                                    const struct rte_flow_item *item)
6856 {
6857         const rte_be32_t *key_m = item->mask;
6858         const rte_be32_t *key_v = item->spec;
6859         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
6860         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
6861         rte_be32_t gre_key_default_mask = RTE_BE32(UINT32_MAX);
6862
6863         /* GRE K bit must be on and should already be validated */
6864         MLX5_SET(fte_match_set_misc, misc_m, gre_k_present, 1);
6865         MLX5_SET(fte_match_set_misc, misc_v, gre_k_present, 1);
6866         if (!key_v)
6867                 return;
6868         if (!key_m)
6869                 key_m = &gre_key_default_mask;
6870         MLX5_SET(fte_match_set_misc, misc_m, gre_key_h,
6871                  rte_be_to_cpu_32(*key_m) >> 8);
6872         MLX5_SET(fte_match_set_misc, misc_v, gre_key_h,
6873                  rte_be_to_cpu_32((*key_v) & (*key_m)) >> 8);
6874         MLX5_SET(fte_match_set_misc, misc_m, gre_key_l,
6875                  rte_be_to_cpu_32(*key_m) & 0xFF);
6876         MLX5_SET(fte_match_set_misc, misc_v, gre_key_l,
6877                  rte_be_to_cpu_32((*key_v) & (*key_m)) & 0xFF);
6878 }
6879
6880 /**
6881  * Add GRE item to matcher and to the value.
6882  *
6883  * @param[in, out] matcher
6884  *   Flow matcher.
6885  * @param[in, out] key
6886  *   Flow matcher value.
6887  * @param[in] item
6888  *   Flow pattern to translate.
6889  * @param[in] inner
6890  *   Item is inner pattern.
6891  */
6892 static void
6893 flow_dv_translate_item_gre(void *matcher, void *key,
6894                            const struct rte_flow_item *item,
6895                            int inner)
6896 {
6897         const struct rte_flow_item_gre *gre_m = item->mask;
6898         const struct rte_flow_item_gre *gre_v = item->spec;
6899         void *headers_m;
6900         void *headers_v;
6901         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
6902         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
6903         struct {
6904                 union {
6905                         __extension__
6906                         struct {
6907                                 uint16_t version:3;
6908                                 uint16_t rsvd0:9;
6909                                 uint16_t s_present:1;
6910                                 uint16_t k_present:1;
6911                                 uint16_t rsvd_bit1:1;
6912                                 uint16_t c_present:1;
6913                         };
6914                         uint16_t value;
6915                 };
6916         } gre_crks_rsvd0_ver_m, gre_crks_rsvd0_ver_v;
6917
6918         if (inner) {
6919                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6920                                          inner_headers);
6921                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
6922         } else {
6923                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6924                                          outer_headers);
6925                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6926         }
6927         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
6928         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_GRE);
6929         if (!gre_v)
6930                 return;
6931         if (!gre_m)
6932                 gre_m = &rte_flow_item_gre_mask;
6933         MLX5_SET(fte_match_set_misc, misc_m, gre_protocol,
6934                  rte_be_to_cpu_16(gre_m->protocol));
6935         MLX5_SET(fte_match_set_misc, misc_v, gre_protocol,
6936                  rte_be_to_cpu_16(gre_v->protocol & gre_m->protocol));
6937         gre_crks_rsvd0_ver_m.value = rte_be_to_cpu_16(gre_m->c_rsvd0_ver);
6938         gre_crks_rsvd0_ver_v.value = rte_be_to_cpu_16(gre_v->c_rsvd0_ver);
6939         MLX5_SET(fte_match_set_misc, misc_m, gre_c_present,
6940                  gre_crks_rsvd0_ver_m.c_present);
6941         MLX5_SET(fte_match_set_misc, misc_v, gre_c_present,
6942                  gre_crks_rsvd0_ver_v.c_present &
6943                  gre_crks_rsvd0_ver_m.c_present);
6944         MLX5_SET(fte_match_set_misc, misc_m, gre_k_present,
6945                  gre_crks_rsvd0_ver_m.k_present);
6946         MLX5_SET(fte_match_set_misc, misc_v, gre_k_present,
6947                  gre_crks_rsvd0_ver_v.k_present &
6948                  gre_crks_rsvd0_ver_m.k_present);
6949         MLX5_SET(fte_match_set_misc, misc_m, gre_s_present,
6950                  gre_crks_rsvd0_ver_m.s_present);
6951         MLX5_SET(fte_match_set_misc, misc_v, gre_s_present,
6952                  gre_crks_rsvd0_ver_v.s_present &
6953                  gre_crks_rsvd0_ver_m.s_present);
6954 }
6955
6956 /**
6957  * Add NVGRE item to matcher and to the value.
6958  *
6959  * @param[in, out] matcher
6960  *   Flow matcher.
6961  * @param[in, out] key
6962  *   Flow matcher value.
6963  * @param[in] item
6964  *   Flow pattern to translate.
6965  * @param[in] inner
6966  *   Item is inner pattern.
6967  */
6968 static void
6969 flow_dv_translate_item_nvgre(void *matcher, void *key,
6970                              const struct rte_flow_item *item,
6971                              int inner)
6972 {
6973         const struct rte_flow_item_nvgre *nvgre_m = item->mask;
6974         const struct rte_flow_item_nvgre *nvgre_v = item->spec;
6975         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
6976         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
6977         const char *tni_flow_id_m;
6978         const char *tni_flow_id_v;
6979         char *gre_key_m;
6980         char *gre_key_v;
6981         int size;
6982         int i;
6983
6984         /* For NVGRE, GRE header fields must be set with defined values. */
6985         const struct rte_flow_item_gre gre_spec = {
6986                 .c_rsvd0_ver = RTE_BE16(0x2000),
6987                 .protocol = RTE_BE16(RTE_ETHER_TYPE_TEB)
6988         };
6989         const struct rte_flow_item_gre gre_mask = {
6990                 .c_rsvd0_ver = RTE_BE16(0xB000),
6991                 .protocol = RTE_BE16(UINT16_MAX),
6992         };
6993         const struct rte_flow_item gre_item = {
6994                 .spec = &gre_spec,
6995                 .mask = &gre_mask,
6996                 .last = NULL,
6997         };
6998         flow_dv_translate_item_gre(matcher, key, &gre_item, inner);
6999         if (!nvgre_v)
7000                 return;
7001         if (!nvgre_m)
7002                 nvgre_m = &rte_flow_item_nvgre_mask;
7003         tni_flow_id_m = (const char *)nvgre_m->tni;
7004         tni_flow_id_v = (const char *)nvgre_v->tni;
7005         size = sizeof(nvgre_m->tni) + sizeof(nvgre_m->flow_id);
7006         gre_key_m = MLX5_ADDR_OF(fte_match_set_misc, misc_m, gre_key_h);
7007         gre_key_v = MLX5_ADDR_OF(fte_match_set_misc, misc_v, gre_key_h);
7008         memcpy(gre_key_m, tni_flow_id_m, size);
7009         for (i = 0; i < size; ++i)
7010                 gre_key_v[i] = gre_key_m[i] & tni_flow_id_v[i];
7011 }
7012
7013 /**
7014  * Add VXLAN item to matcher and to the value.
7015  *
7016  * @param[in, out] matcher
7017  *   Flow matcher.
7018  * @param[in, out] key
7019  *   Flow matcher value.
7020  * @param[in] item
7021  *   Flow pattern to translate.
7022  * @param[in] inner
7023  *   Item is inner pattern.
7024  */
7025 static void
7026 flow_dv_translate_item_vxlan(void *matcher, void *key,
7027                              const struct rte_flow_item *item,
7028                              int inner)
7029 {
7030         const struct rte_flow_item_vxlan *vxlan_m = item->mask;
7031         const struct rte_flow_item_vxlan *vxlan_v = item->spec;
7032         void *headers_m;
7033         void *headers_v;
7034         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
7035         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
7036         char *vni_m;
7037         char *vni_v;
7038         uint16_t dport;
7039         int size;
7040         int i;
7041
7042         if (inner) {
7043                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
7044                                          inner_headers);
7045                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
7046         } else {
7047                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
7048                                          outer_headers);
7049                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
7050         }
7051         dport = item->type == RTE_FLOW_ITEM_TYPE_VXLAN ?
7052                 MLX5_UDP_PORT_VXLAN : MLX5_UDP_PORT_VXLAN_GPE;
7053         if (!MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport)) {
7054                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xFFFF);
7055                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, dport);
7056         }
7057         if (!vxlan_v)
7058                 return;
7059         if (!vxlan_m)
7060                 vxlan_m = &rte_flow_item_vxlan_mask;
7061         size = sizeof(vxlan_m->vni);
7062         vni_m = MLX5_ADDR_OF(fte_match_set_misc, misc_m, vxlan_vni);
7063         vni_v = MLX5_ADDR_OF(fte_match_set_misc, misc_v, vxlan_vni);
7064         memcpy(vni_m, vxlan_m->vni, size);
7065         for (i = 0; i < size; ++i)
7066                 vni_v[i] = vni_m[i] & vxlan_v->vni[i];
7067 }
7068
7069 /**
7070  * Add VXLAN-GPE item to matcher and to the value.
7071  *
7072  * @param[in, out] matcher
7073  *   Flow matcher.
7074  * @param[in, out] key
7075  *   Flow matcher value.
7076  * @param[in] item
7077  *   Flow pattern to translate.
7078  * @param[in] inner
7079  *   Item is inner pattern.
7080  */
7081
7082 static void
7083 flow_dv_translate_item_vxlan_gpe(void *matcher, void *key,
7084                                  const struct rte_flow_item *item, int inner)
7085 {
7086         const struct rte_flow_item_vxlan_gpe *vxlan_m = item->mask;
7087         const struct rte_flow_item_vxlan_gpe *vxlan_v = item->spec;
7088         void *headers_m;
7089         void *headers_v;
7090         void *misc_m =
7091                 MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters_3);
7092         void *misc_v =
7093                 MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
7094         char *vni_m;
7095         char *vni_v;
7096         uint16_t dport;
7097         int size;
7098         int i;
7099         uint8_t flags_m = 0xff;
7100         uint8_t flags_v = 0xc;
7101
7102         if (inner) {
7103                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
7104                                          inner_headers);
7105                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
7106         } else {
7107                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
7108                                          outer_headers);
7109                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
7110         }
7111         dport = item->type == RTE_FLOW_ITEM_TYPE_VXLAN ?
7112                 MLX5_UDP_PORT_VXLAN : MLX5_UDP_PORT_VXLAN_GPE;
7113         if (!MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport)) {
7114                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xFFFF);
7115                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, dport);
7116         }
7117         if (!vxlan_v)
7118                 return;
7119         if (!vxlan_m)
7120                 vxlan_m = &rte_flow_item_vxlan_gpe_mask;
7121         size = sizeof(vxlan_m->vni);
7122         vni_m = MLX5_ADDR_OF(fte_match_set_misc3, misc_m, outer_vxlan_gpe_vni);
7123         vni_v = MLX5_ADDR_OF(fte_match_set_misc3, misc_v, outer_vxlan_gpe_vni);
7124         memcpy(vni_m, vxlan_m->vni, size);
7125         for (i = 0; i < size; ++i)
7126                 vni_v[i] = vni_m[i] & vxlan_v->vni[i];
7127         if (vxlan_m->flags) {
7128                 flags_m = vxlan_m->flags;
7129                 flags_v = vxlan_v->flags;
7130         }
7131         MLX5_SET(fte_match_set_misc3, misc_m, outer_vxlan_gpe_flags, flags_m);
7132         MLX5_SET(fte_match_set_misc3, misc_v, outer_vxlan_gpe_flags, flags_v);
7133         MLX5_SET(fte_match_set_misc3, misc_m, outer_vxlan_gpe_next_protocol,
7134                  vxlan_m->protocol);
7135         MLX5_SET(fte_match_set_misc3, misc_v, outer_vxlan_gpe_next_protocol,
7136                  vxlan_v->protocol);
7137 }
7138
7139 /**
7140  * Add Geneve item to matcher and to the value.
7141  *
7142  * @param[in, out] matcher
7143  *   Flow matcher.
7144  * @param[in, out] key
7145  *   Flow matcher value.
7146  * @param[in] item
7147  *   Flow pattern to translate.
7148  * @param[in] inner
7149  *   Item is inner pattern.
7150  */
7151
7152 static void
7153 flow_dv_translate_item_geneve(void *matcher, void *key,
7154                               const struct rte_flow_item *item, int inner)
7155 {
7156         const struct rte_flow_item_geneve *geneve_m = item->mask;
7157         const struct rte_flow_item_geneve *geneve_v = item->spec;
7158         void *headers_m;
7159         void *headers_v;
7160         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
7161         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
7162         uint16_t dport;
7163         uint16_t gbhdr_m;
7164         uint16_t gbhdr_v;
7165         char *vni_m;
7166         char *vni_v;
7167         size_t size, i;
7168
7169         if (inner) {
7170                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
7171                                          inner_headers);
7172                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
7173         } else {
7174                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
7175                                          outer_headers);
7176                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
7177         }
7178         dport = MLX5_UDP_PORT_GENEVE;
7179         if (!MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport)) {
7180                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xFFFF);
7181                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, dport);
7182         }
7183         if (!geneve_v)
7184                 return;
7185         if (!geneve_m)
7186                 geneve_m = &rte_flow_item_geneve_mask;
7187         size = sizeof(geneve_m->vni);
7188         vni_m = MLX5_ADDR_OF(fte_match_set_misc, misc_m, geneve_vni);
7189         vni_v = MLX5_ADDR_OF(fte_match_set_misc, misc_v, geneve_vni);
7190         memcpy(vni_m, geneve_m->vni, size);
7191         for (i = 0; i < size; ++i)
7192                 vni_v[i] = vni_m[i] & geneve_v->vni[i];
7193         MLX5_SET(fte_match_set_misc, misc_m, geneve_protocol_type,
7194                  rte_be_to_cpu_16(geneve_m->protocol));
7195         MLX5_SET(fte_match_set_misc, misc_v, geneve_protocol_type,
7196                  rte_be_to_cpu_16(geneve_v->protocol & geneve_m->protocol));
7197         gbhdr_m = rte_be_to_cpu_16(geneve_m->ver_opt_len_o_c_rsvd0);
7198         gbhdr_v = rte_be_to_cpu_16(geneve_v->ver_opt_len_o_c_rsvd0);
7199         MLX5_SET(fte_match_set_misc, misc_m, geneve_oam,
7200                  MLX5_GENEVE_OAMF_VAL(gbhdr_m));
7201         MLX5_SET(fte_match_set_misc, misc_v, geneve_oam,
7202                  MLX5_GENEVE_OAMF_VAL(gbhdr_v) & MLX5_GENEVE_OAMF_VAL(gbhdr_m));
7203         MLX5_SET(fte_match_set_misc, misc_m, geneve_opt_len,
7204                  MLX5_GENEVE_OPTLEN_VAL(gbhdr_m));
7205         MLX5_SET(fte_match_set_misc, misc_v, geneve_opt_len,
7206                  MLX5_GENEVE_OPTLEN_VAL(gbhdr_v) &
7207                  MLX5_GENEVE_OPTLEN_VAL(gbhdr_m));
7208 }
7209
7210 /**
7211  * Add MPLS item to matcher and to the value.
7212  *
7213  * @param[in, out] matcher
7214  *   Flow matcher.
7215  * @param[in, out] key
7216  *   Flow matcher value.
7217  * @param[in] item
7218  *   Flow pattern to translate.
7219  * @param[in] prev_layer
7220  *   The protocol layer indicated in previous item.
7221  * @param[in] inner
7222  *   Item is inner pattern.
7223  */
7224 static void
7225 flow_dv_translate_item_mpls(void *matcher, void *key,
7226                             const struct rte_flow_item *item,
7227                             uint64_t prev_layer,
7228                             int inner)
7229 {
7230         const uint32_t *in_mpls_m = item->mask;
7231         const uint32_t *in_mpls_v = item->spec;
7232         uint32_t *out_mpls_m = 0;
7233         uint32_t *out_mpls_v = 0;
7234         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
7235         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
7236         void *misc2_m = MLX5_ADDR_OF(fte_match_param, matcher,
7237                                      misc_parameters_2);
7238         void *misc2_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_2);
7239         void *headers_m = MLX5_ADDR_OF(fte_match_param, matcher, outer_headers);
7240         void *headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
7241
7242         switch (prev_layer) {
7243         case MLX5_FLOW_LAYER_OUTER_L4_UDP:
7244                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xffff);
7245                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
7246                          MLX5_UDP_PORT_MPLS);
7247                 break;
7248         case MLX5_FLOW_LAYER_GRE:
7249                 MLX5_SET(fte_match_set_misc, misc_m, gre_protocol, 0xffff);
7250                 MLX5_SET(fte_match_set_misc, misc_v, gre_protocol,
7251                          RTE_ETHER_TYPE_MPLS);
7252                 break;
7253         default:
7254                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
7255                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
7256                          IPPROTO_MPLS);
7257                 break;
7258         }
7259         if (!in_mpls_v)
7260                 return;
7261         if (!in_mpls_m)
7262                 in_mpls_m = (const uint32_t *)&rte_flow_item_mpls_mask;
7263         switch (prev_layer) {
7264         case MLX5_FLOW_LAYER_OUTER_L4_UDP:
7265                 out_mpls_m =
7266                         (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_m,
7267                                                  outer_first_mpls_over_udp);
7268                 out_mpls_v =
7269                         (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_v,
7270                                                  outer_first_mpls_over_udp);
7271                 break;
7272         case MLX5_FLOW_LAYER_GRE:
7273                 out_mpls_m =
7274                         (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_m,
7275                                                  outer_first_mpls_over_gre);
7276                 out_mpls_v =
7277                         (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_v,
7278                                                  outer_first_mpls_over_gre);
7279                 break;
7280         default:
7281                 /* Inner MPLS not over GRE is not supported. */
7282                 if (!inner) {
7283                         out_mpls_m =
7284                                 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2,
7285                                                          misc2_m,
7286                                                          outer_first_mpls);
7287                         out_mpls_v =
7288                                 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2,
7289                                                          misc2_v,
7290                                                          outer_first_mpls);
7291                 }
7292                 break;
7293         }
7294         if (out_mpls_m && out_mpls_v) {
7295                 *out_mpls_m = *in_mpls_m;
7296                 *out_mpls_v = *in_mpls_v & *in_mpls_m;
7297         }
7298 }
7299
7300 /**
7301  * Add metadata register item to matcher
7302  *
7303  * @param[in, out] matcher
7304  *   Flow matcher.
7305  * @param[in, out] key
7306  *   Flow matcher value.
7307  * @param[in] reg_type
7308  *   Type of device metadata register
7309  * @param[in] value
7310  *   Register value
7311  * @param[in] mask
7312  *   Register mask
7313  */
7314 static void
7315 flow_dv_match_meta_reg(void *matcher, void *key,
7316                        enum modify_reg reg_type,
7317                        uint32_t data, uint32_t mask)
7318 {
7319         void *misc2_m =
7320                 MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters_2);
7321         void *misc2_v =
7322                 MLX5_ADDR_OF(fte_match_param, key, misc_parameters_2);
7323         uint32_t temp;
7324
7325         data &= mask;
7326         switch (reg_type) {
7327         case REG_A:
7328                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_a, mask);
7329                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_a, data);
7330                 break;
7331         case REG_B:
7332                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_b, mask);
7333                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_b, data);
7334                 break;
7335         case REG_C_0:
7336                 /*
7337                  * The metadata register C0 field might be divided into
7338                  * source vport index and META item value, we should set
7339                  * this field according to specified mask, not as whole one.
7340                  */
7341                 temp = MLX5_GET(fte_match_set_misc2, misc2_m, metadata_reg_c_0);
7342                 temp |= mask;
7343                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_0, temp);
7344                 temp = MLX5_GET(fte_match_set_misc2, misc2_v, metadata_reg_c_0);
7345                 temp &= ~mask;
7346                 temp |= data;
7347                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_0, temp);
7348                 break;
7349         case REG_C_1:
7350                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_1, mask);
7351                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_1, data);
7352                 break;
7353         case REG_C_2:
7354                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_2, mask);
7355                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_2, data);
7356                 break;
7357         case REG_C_3:
7358                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_3, mask);
7359                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_3, data);
7360                 break;
7361         case REG_C_4:
7362                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_4, mask);
7363                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_4, data);
7364                 break;
7365         case REG_C_5:
7366                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_5, mask);
7367                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_5, data);
7368                 break;
7369         case REG_C_6:
7370                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_6, mask);
7371                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_6, data);
7372                 break;
7373         case REG_C_7:
7374                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_7, mask);
7375                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_7, data);
7376                 break;
7377         default:
7378                 MLX5_ASSERT(false);
7379                 break;
7380         }
7381 }
7382
7383 /**
7384  * Add MARK item to matcher
7385  *
7386  * @param[in] dev
7387  *   The device to configure through.
7388  * @param[in, out] matcher
7389  *   Flow matcher.
7390  * @param[in, out] key
7391  *   Flow matcher value.
7392  * @param[in] item
7393  *   Flow pattern to translate.
7394  */
7395 static void
7396 flow_dv_translate_item_mark(struct rte_eth_dev *dev,
7397                             void *matcher, void *key,
7398                             const struct rte_flow_item *item)
7399 {
7400         struct mlx5_priv *priv = dev->data->dev_private;
7401         const struct rte_flow_item_mark *mark;
7402         uint32_t value;
7403         uint32_t mask;
7404
7405         mark = item->mask ? (const void *)item->mask :
7406                             &rte_flow_item_mark_mask;
7407         mask = mark->id & priv->sh->dv_mark_mask;
7408         mark = (const void *)item->spec;
7409         MLX5_ASSERT(mark);
7410         value = mark->id & priv->sh->dv_mark_mask & mask;
7411         if (mask) {
7412                 enum modify_reg reg;
7413
7414                 /* Get the metadata register index for the mark. */
7415                 reg = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, NULL);
7416                 MLX5_ASSERT(reg > 0);
7417                 if (reg == REG_C_0) {
7418                         struct mlx5_priv *priv = dev->data->dev_private;
7419                         uint32_t msk_c0 = priv->sh->dv_regc0_mask;
7420                         uint32_t shl_c0 = rte_bsf32(msk_c0);
7421
7422                         mask &= msk_c0;
7423                         mask <<= shl_c0;
7424                         value <<= shl_c0;
7425                 }
7426                 flow_dv_match_meta_reg(matcher, key, reg, value, mask);
7427         }
7428 }
7429
7430 /**
7431  * Add META item to matcher
7432  *
7433  * @param[in] dev
7434  *   The devich to configure through.
7435  * @param[in, out] matcher
7436  *   Flow matcher.
7437  * @param[in, out] key
7438  *   Flow matcher value.
7439  * @param[in] attr
7440  *   Attributes of flow that includes this item.
7441  * @param[in] item
7442  *   Flow pattern to translate.
7443  */
7444 static void
7445 flow_dv_translate_item_meta(struct rte_eth_dev *dev,
7446                             void *matcher, void *key,
7447                             const struct rte_flow_attr *attr,
7448                             const struct rte_flow_item *item)
7449 {
7450         const struct rte_flow_item_meta *meta_m;
7451         const struct rte_flow_item_meta *meta_v;
7452
7453         meta_m = (const void *)item->mask;
7454         if (!meta_m)
7455                 meta_m = &rte_flow_item_meta_mask;
7456         meta_v = (const void *)item->spec;
7457         if (meta_v) {
7458                 int reg;
7459                 uint32_t value = meta_v->data;
7460                 uint32_t mask = meta_m->data;
7461
7462                 reg = flow_dv_get_metadata_reg(dev, attr, NULL);
7463                 if (reg < 0)
7464                         return;
7465                 /*
7466                  * In datapath code there is no endianness
7467                  * coversions for perfromance reasons, all
7468                  * pattern conversions are done in rte_flow.
7469                  */
7470                 value = rte_cpu_to_be_32(value);
7471                 mask = rte_cpu_to_be_32(mask);
7472                 if (reg == REG_C_0) {
7473                         struct mlx5_priv *priv = dev->data->dev_private;
7474                         uint32_t msk_c0 = priv->sh->dv_regc0_mask;
7475                         uint32_t shl_c0 = rte_bsf32(msk_c0);
7476 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
7477                         uint32_t shr_c0 = __builtin_clz(priv->sh->dv_meta_mask);
7478
7479                         value >>= shr_c0;
7480                         mask >>= shr_c0;
7481 #endif
7482                         value <<= shl_c0;
7483                         mask <<= shl_c0;
7484                         MLX5_ASSERT(msk_c0);
7485                         MLX5_ASSERT(!(~msk_c0 & mask));
7486                 }
7487                 flow_dv_match_meta_reg(matcher, key, reg, value, mask);
7488         }
7489 }
7490
7491 /**
7492  * Add vport metadata Reg C0 item to matcher
7493  *
7494  * @param[in, out] matcher
7495  *   Flow matcher.
7496  * @param[in, out] key
7497  *   Flow matcher value.
7498  * @param[in] reg
7499  *   Flow pattern to translate.
7500  */
7501 static void
7502 flow_dv_translate_item_meta_vport(void *matcher, void *key,
7503                                   uint32_t value, uint32_t mask)
7504 {
7505         flow_dv_match_meta_reg(matcher, key, REG_C_0, value, mask);
7506 }
7507
7508 /**
7509  * Add tag item to matcher
7510  *
7511  * @param[in] dev
7512  *   The devich to configure through.
7513  * @param[in, out] matcher
7514  *   Flow matcher.
7515  * @param[in, out] key
7516  *   Flow matcher value.
7517  * @param[in] item
7518  *   Flow pattern to translate.
7519  */
7520 static void
7521 flow_dv_translate_mlx5_item_tag(struct rte_eth_dev *dev,
7522                                 void *matcher, void *key,
7523                                 const struct rte_flow_item *item)
7524 {
7525         const struct mlx5_rte_flow_item_tag *tag_v = item->spec;
7526         const struct mlx5_rte_flow_item_tag *tag_m = item->mask;
7527         uint32_t mask, value;
7528
7529         MLX5_ASSERT(tag_v);
7530         value = tag_v->data;
7531         mask = tag_m ? tag_m->data : UINT32_MAX;
7532         if (tag_v->id == REG_C_0) {
7533                 struct mlx5_priv *priv = dev->data->dev_private;
7534                 uint32_t msk_c0 = priv->sh->dv_regc0_mask;
7535                 uint32_t shl_c0 = rte_bsf32(msk_c0);
7536
7537                 mask &= msk_c0;
7538                 mask <<= shl_c0;
7539                 value <<= shl_c0;
7540         }
7541         flow_dv_match_meta_reg(matcher, key, tag_v->id, value, mask);
7542 }
7543
7544 /**
7545  * Add TAG item to matcher
7546  *
7547  * @param[in] dev
7548  *   The devich to configure through.
7549  * @param[in, out] matcher
7550  *   Flow matcher.
7551  * @param[in, out] key
7552  *   Flow matcher value.
7553  * @param[in] item
7554  *   Flow pattern to translate.
7555  */
7556 static void
7557 flow_dv_translate_item_tag(struct rte_eth_dev *dev,
7558                            void *matcher, void *key,
7559                            const struct rte_flow_item *item)
7560 {
7561         const struct rte_flow_item_tag *tag_v = item->spec;
7562         const struct rte_flow_item_tag *tag_m = item->mask;
7563         enum modify_reg reg;
7564
7565         MLX5_ASSERT(tag_v);
7566         tag_m = tag_m ? tag_m : &rte_flow_item_tag_mask;
7567         /* Get the metadata register index for the tag. */
7568         reg = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, tag_v->index, NULL);
7569         MLX5_ASSERT(reg > 0);
7570         flow_dv_match_meta_reg(matcher, key, reg, tag_v->data, tag_m->data);
7571 }
7572
7573 /**
7574  * Add source vport match to the specified matcher.
7575  *
7576  * @param[in, out] matcher
7577  *   Flow matcher.
7578  * @param[in, out] key
7579  *   Flow matcher value.
7580  * @param[in] port
7581  *   Source vport value to match
7582  * @param[in] mask
7583  *   Mask
7584  */
7585 static void
7586 flow_dv_translate_item_source_vport(void *matcher, void *key,
7587                                     int16_t port, uint16_t mask)
7588 {
7589         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
7590         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
7591
7592         MLX5_SET(fte_match_set_misc, misc_m, source_port, mask);
7593         MLX5_SET(fte_match_set_misc, misc_v, source_port, port);
7594 }
7595
7596 /**
7597  * Translate port-id item to eswitch match on  port-id.
7598  *
7599  * @param[in] dev
7600  *   The devich to configure through.
7601  * @param[in, out] matcher
7602  *   Flow matcher.
7603  * @param[in, out] key
7604  *   Flow matcher value.
7605  * @param[in] item
7606  *   Flow pattern to translate.
7607  *
7608  * @return
7609  *   0 on success, a negative errno value otherwise.
7610  */
7611 static int
7612 flow_dv_translate_item_port_id(struct rte_eth_dev *dev, void *matcher,
7613                                void *key, const struct rte_flow_item *item)
7614 {
7615         const struct rte_flow_item_port_id *pid_m = item ? item->mask : NULL;
7616         const struct rte_flow_item_port_id *pid_v = item ? item->spec : NULL;
7617         struct mlx5_priv *priv;
7618         uint16_t mask, id;
7619
7620         mask = pid_m ? pid_m->id : 0xffff;
7621         id = pid_v ? pid_v->id : dev->data->port_id;
7622         priv = mlx5_port_to_eswitch_info(id, item == NULL);
7623         if (!priv)
7624                 return -rte_errno;
7625         /* Translate to vport field or to metadata, depending on mode. */
7626         if (priv->vport_meta_mask)
7627                 flow_dv_translate_item_meta_vport(matcher, key,
7628                                                   priv->vport_meta_tag,
7629                                                   priv->vport_meta_mask);
7630         else
7631                 flow_dv_translate_item_source_vport(matcher, key,
7632                                                     priv->vport_id, mask);
7633         return 0;
7634 }
7635
7636 /**
7637  * Add ICMP6 item to matcher and to the value.
7638  *
7639  * @param[in, out] matcher
7640  *   Flow matcher.
7641  * @param[in, out] key
7642  *   Flow matcher value.
7643  * @param[in] item
7644  *   Flow pattern to translate.
7645  * @param[in] inner
7646  *   Item is inner pattern.
7647  */
7648 static void
7649 flow_dv_translate_item_icmp6(void *matcher, void *key,
7650                               const struct rte_flow_item *item,
7651                               int inner)
7652 {
7653         const struct rte_flow_item_icmp6 *icmp6_m = item->mask;
7654         const struct rte_flow_item_icmp6 *icmp6_v = item->spec;
7655         void *headers_m;
7656         void *headers_v;
7657         void *misc3_m = MLX5_ADDR_OF(fte_match_param, matcher,
7658                                      misc_parameters_3);
7659         void *misc3_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
7660         if (inner) {
7661                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
7662                                          inner_headers);
7663                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
7664         } else {
7665                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
7666                                          outer_headers);
7667                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
7668         }
7669         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xFF);
7670         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_ICMPV6);
7671         if (!icmp6_v)
7672                 return;
7673         if (!icmp6_m)
7674                 icmp6_m = &rte_flow_item_icmp6_mask;
7675         MLX5_SET(fte_match_set_misc3, misc3_m, icmpv6_type, icmp6_m->type);
7676         MLX5_SET(fte_match_set_misc3, misc3_v, icmpv6_type,
7677                  icmp6_v->type & icmp6_m->type);
7678         MLX5_SET(fte_match_set_misc3, misc3_m, icmpv6_code, icmp6_m->code);
7679         MLX5_SET(fte_match_set_misc3, misc3_v, icmpv6_code,
7680                  icmp6_v->code & icmp6_m->code);
7681 }
7682
7683 /**
7684  * Add ICMP item to matcher and to the value.
7685  *
7686  * @param[in, out] matcher
7687  *   Flow matcher.
7688  * @param[in, out] key
7689  *   Flow matcher value.
7690  * @param[in] item
7691  *   Flow pattern to translate.
7692  * @param[in] inner
7693  *   Item is inner pattern.
7694  */
7695 static void
7696 flow_dv_translate_item_icmp(void *matcher, void *key,
7697                             const struct rte_flow_item *item,
7698                             int inner)
7699 {
7700         const struct rte_flow_item_icmp *icmp_m = item->mask;
7701         const struct rte_flow_item_icmp *icmp_v = item->spec;
7702         uint32_t icmp_header_data_m = 0;
7703         uint32_t icmp_header_data_v = 0;
7704         void *headers_m;
7705         void *headers_v;
7706         void *misc3_m = MLX5_ADDR_OF(fte_match_param, matcher,
7707                                      misc_parameters_3);
7708         void *misc3_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
7709         if (inner) {
7710                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
7711                                          inner_headers);
7712                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
7713         } else {
7714                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
7715                                          outer_headers);
7716                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
7717         }
7718         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xFF);
7719         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_ICMP);
7720         if (!icmp_v)
7721                 return;
7722         if (!icmp_m)
7723                 icmp_m = &rte_flow_item_icmp_mask;
7724         MLX5_SET(fte_match_set_misc3, misc3_m, icmp_type,
7725                  icmp_m->hdr.icmp_type);
7726         MLX5_SET(fte_match_set_misc3, misc3_v, icmp_type,
7727                  icmp_v->hdr.icmp_type & icmp_m->hdr.icmp_type);
7728         MLX5_SET(fte_match_set_misc3, misc3_m, icmp_code,
7729                  icmp_m->hdr.icmp_code);
7730         MLX5_SET(fte_match_set_misc3, misc3_v, icmp_code,
7731                  icmp_v->hdr.icmp_code & icmp_m->hdr.icmp_code);
7732         icmp_header_data_m = rte_be_to_cpu_16(icmp_m->hdr.icmp_seq_nb);
7733         icmp_header_data_m |= rte_be_to_cpu_16(icmp_m->hdr.icmp_ident) << 16;
7734         if (icmp_header_data_m) {
7735                 icmp_header_data_v = rte_be_to_cpu_16(icmp_v->hdr.icmp_seq_nb);
7736                 icmp_header_data_v |=
7737                          rte_be_to_cpu_16(icmp_v->hdr.icmp_ident) << 16;
7738                 MLX5_SET(fte_match_set_misc3, misc3_m, icmp_header_data,
7739                          icmp_header_data_m);
7740                 MLX5_SET(fte_match_set_misc3, misc3_v, icmp_header_data,
7741                          icmp_header_data_v & icmp_header_data_m);
7742         }
7743 }
7744
7745 /**
7746  * Add GTP item to matcher and to the value.
7747  *
7748  * @param[in, out] matcher
7749  *   Flow matcher.
7750  * @param[in, out] key
7751  *   Flow matcher value.
7752  * @param[in] item
7753  *   Flow pattern to translate.
7754  * @param[in] inner
7755  *   Item is inner pattern.
7756  */
7757 static void
7758 flow_dv_translate_item_gtp(void *matcher, void *key,
7759                            const struct rte_flow_item *item, int inner)
7760 {
7761         const struct rte_flow_item_gtp *gtp_m = item->mask;
7762         const struct rte_flow_item_gtp *gtp_v = item->spec;
7763         void *headers_m;
7764         void *headers_v;
7765         void *misc3_m = MLX5_ADDR_OF(fte_match_param, matcher,
7766                                      misc_parameters_3);
7767         void *misc3_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
7768         uint16_t dport = RTE_GTPU_UDP_PORT;
7769
7770         if (inner) {
7771                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
7772                                          inner_headers);
7773                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
7774         } else {
7775                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
7776                                          outer_headers);
7777                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
7778         }
7779         if (!MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport)) {
7780                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xFFFF);
7781                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, dport);
7782         }
7783         if (!gtp_v)
7784                 return;
7785         if (!gtp_m)
7786                 gtp_m = &rte_flow_item_gtp_mask;
7787         MLX5_SET(fte_match_set_misc3, misc3_m, gtpu_msg_flags,
7788                  gtp_m->v_pt_rsv_flags);
7789         MLX5_SET(fte_match_set_misc3, misc3_v, gtpu_msg_flags,
7790                  gtp_v->v_pt_rsv_flags & gtp_m->v_pt_rsv_flags);
7791         MLX5_SET(fte_match_set_misc3, misc3_m, gtpu_msg_type, gtp_m->msg_type);
7792         MLX5_SET(fte_match_set_misc3, misc3_v, gtpu_msg_type,
7793                  gtp_v->msg_type & gtp_m->msg_type);
7794         MLX5_SET(fte_match_set_misc3, misc3_m, gtpu_teid,
7795                  rte_be_to_cpu_32(gtp_m->teid));
7796         MLX5_SET(fte_match_set_misc3, misc3_v, gtpu_teid,
7797                  rte_be_to_cpu_32(gtp_v->teid & gtp_m->teid));
7798 }
7799
7800 /**
7801  * Add eCPRI item to matcher and to the value.
7802  *
7803  * @param[in] dev
7804  *   The devich to configure through.
7805  * @param[in, out] matcher
7806  *   Flow matcher.
7807  * @param[in, out] key
7808  *   Flow matcher value.
7809  * @param[in] item
7810  *   Flow pattern to translate.
7811  * @param[in] samples
7812  *   Sample IDs to be used in the matching.
7813  */
7814 static void
7815 flow_dv_translate_item_ecpri(struct rte_eth_dev *dev, void *matcher,
7816                              void *key, const struct rte_flow_item *item)
7817 {
7818         struct mlx5_priv *priv = dev->data->dev_private;
7819         const struct rte_flow_item_ecpri *ecpri_m = item->mask;
7820         const struct rte_flow_item_ecpri *ecpri_v = item->spec;
7821         void *misc4_m = MLX5_ADDR_OF(fte_match_param, matcher,
7822                                      misc_parameters_4);
7823         void *misc4_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_4);
7824         uint32_t *samples;
7825         void *dw_m;
7826         void *dw_v;
7827
7828         if (!ecpri_v)
7829                 return;
7830         if (!ecpri_m)
7831                 ecpri_m = &rte_flow_item_ecpri_mask;
7832         /*
7833          * Maximal four DW samples are supported in a single matching now.
7834          * Two are used now for a eCPRI matching:
7835          * 1. Type: one byte, mask should be 0x00ff0000 in network order
7836          * 2. ID of a message: one or two bytes, mask 0xffff0000 or 0xff000000
7837          *    if any.
7838          */
7839         if (!ecpri_m->hdr.common.u32)
7840                 return;
7841         samples = priv->sh->fp[MLX5_FLEX_PARSER_ECPRI_0].ids;
7842         /* Need to take the whole DW as the mask to fill the entry. */
7843         dw_m = MLX5_ADDR_OF(fte_match_set_misc4, misc4_m,
7844                             prog_sample_field_value_0);
7845         dw_v = MLX5_ADDR_OF(fte_match_set_misc4, misc4_v,
7846                             prog_sample_field_value_0);
7847         /* Already big endian (network order) in the header. */
7848         *(uint32_t *)dw_m = ecpri_m->hdr.common.u32;
7849         *(uint32_t *)dw_v = ecpri_v->hdr.common.u32;
7850         /* Sample#0, used for matching type, offset 0. */
7851         MLX5_SET(fte_match_set_misc4, misc4_m,
7852                  prog_sample_field_id_0, samples[0]);
7853         /* It makes no sense to set the sample ID in the mask field. */
7854         MLX5_SET(fte_match_set_misc4, misc4_v,
7855                  prog_sample_field_id_0, samples[0]);
7856         /*
7857          * Checking if message body part needs to be matched.
7858          * Some wildcard rules only matching type field should be supported.
7859          */
7860         if (ecpri_m->hdr.dummy[0]) {
7861                 switch (ecpri_v->hdr.common.type) {
7862                 case RTE_ECPRI_MSG_TYPE_IQ_DATA:
7863                 case RTE_ECPRI_MSG_TYPE_RTC_CTRL:
7864                 case RTE_ECPRI_MSG_TYPE_DLY_MSR:
7865                         dw_m = MLX5_ADDR_OF(fte_match_set_misc4, misc4_m,
7866                                             prog_sample_field_value_1);
7867                         dw_v = MLX5_ADDR_OF(fte_match_set_misc4, misc4_v,
7868                                             prog_sample_field_value_1);
7869                         *(uint32_t *)dw_m = ecpri_m->hdr.dummy[0];
7870                         *(uint32_t *)dw_v = ecpri_v->hdr.dummy[0];
7871                         /* Sample#1, to match message body, offset 4. */
7872                         MLX5_SET(fte_match_set_misc4, misc4_m,
7873                                  prog_sample_field_id_1, samples[1]);
7874                         MLX5_SET(fte_match_set_misc4, misc4_v,
7875                                  prog_sample_field_id_1, samples[1]);
7876                         break;
7877                 default:
7878                         /* Others, do not match any sample ID. */
7879                         break;
7880                 }
7881         }
7882 }
7883
7884 static uint32_t matcher_zero[MLX5_ST_SZ_DW(fte_match_param)] = { 0 };
7885
7886 #define HEADER_IS_ZERO(match_criteria, headers)                              \
7887         !(memcmp(MLX5_ADDR_OF(fte_match_param, match_criteria, headers),     \
7888                  matcher_zero, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
7889
7890 /**
7891  * Calculate flow matcher enable bitmap.
7892  *
7893  * @param match_criteria
7894  *   Pointer to flow matcher criteria.
7895  *
7896  * @return
7897  *   Bitmap of enabled fields.
7898  */
7899 static uint8_t
7900 flow_dv_matcher_enable(uint32_t *match_criteria)
7901 {
7902         uint8_t match_criteria_enable;
7903
7904         match_criteria_enable =
7905                 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
7906                 MLX5_MATCH_CRITERIA_ENABLE_OUTER_BIT;
7907         match_criteria_enable |=
7908                 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
7909                 MLX5_MATCH_CRITERIA_ENABLE_MISC_BIT;
7910         match_criteria_enable |=
7911                 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
7912                 MLX5_MATCH_CRITERIA_ENABLE_INNER_BIT;
7913         match_criteria_enable |=
7914                 (!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
7915                 MLX5_MATCH_CRITERIA_ENABLE_MISC2_BIT;
7916         match_criteria_enable |=
7917                 (!HEADER_IS_ZERO(match_criteria, misc_parameters_3)) <<
7918                 MLX5_MATCH_CRITERIA_ENABLE_MISC3_BIT;
7919         match_criteria_enable |=
7920                 (!HEADER_IS_ZERO(match_criteria, misc_parameters_4)) <<
7921                 MLX5_MATCH_CRITERIA_ENABLE_MISC4_BIT;
7922         return match_criteria_enable;
7923 }
7924
7925 struct mlx5_hlist_entry *
7926 flow_dv_tbl_create_cb(struct mlx5_hlist *list, uint64_t key64, void *cb_ctx)
7927 {
7928         struct mlx5_dev_ctx_shared *sh = list->ctx;
7929         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
7930         struct rte_eth_dev *dev = ctx->dev;
7931         struct mlx5_flow_tbl_data_entry *tbl_data;
7932         struct mlx5_flow_tbl_tunnel_prm *tt_prm = ctx->data;
7933         struct rte_flow_error *error = ctx->error;
7934         union mlx5_flow_tbl_key key = { .v64 = key64 };
7935         struct mlx5_flow_tbl_resource *tbl;
7936         void *domain;
7937         uint32_t idx = 0;
7938         int ret;
7939
7940         tbl_data = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_JUMP], &idx);
7941         if (!tbl_data) {
7942                 rte_flow_error_set(error, ENOMEM,
7943                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
7944                                    NULL,
7945                                    "cannot allocate flow table data entry");
7946                 return NULL;
7947         }
7948         tbl_data->idx = idx;
7949         tbl_data->tunnel = tt_prm->tunnel;
7950         tbl_data->group_id = tt_prm->group_id;
7951         tbl_data->external = tt_prm->external;
7952         tbl_data->tunnel_offload = is_tunnel_offload_active(dev);
7953         tbl = &tbl_data->tbl;
7954         if (key.dummy)
7955                 return &tbl_data->entry;
7956         if (key.domain)
7957                 domain = sh->fdb_domain;
7958         else if (key.direction)
7959                 domain = sh->tx_domain;
7960         else
7961                 domain = sh->rx_domain;
7962         ret = mlx5_flow_os_create_flow_tbl(domain, key.table_id, &tbl->obj);
7963         if (ret) {
7964                 rte_flow_error_set(error, ENOMEM,
7965                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
7966                                    NULL, "cannot create flow table object");
7967                 mlx5_ipool_free(sh->ipool[MLX5_IPOOL_JUMP], idx);
7968                 return NULL;
7969         }
7970         if (key.table_id) {
7971                 ret = mlx5_flow_os_create_flow_action_dest_flow_tbl
7972                                         (tbl->obj, &tbl_data->jump.action);
7973                 if (ret) {
7974                         rte_flow_error_set(error, ENOMEM,
7975                                            RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
7976                                            NULL,
7977                                            "cannot create flow jump action");
7978                         mlx5_flow_os_destroy_flow_tbl(tbl->obj);
7979                         mlx5_ipool_free(sh->ipool[MLX5_IPOOL_JUMP], idx);
7980                         return NULL;
7981                 }
7982         }
7983         return &tbl_data->entry;
7984 }
7985
7986 /**
7987  * Get a flow table.
7988  *
7989  * @param[in, out] dev
7990  *   Pointer to rte_eth_dev structure.
7991  * @param[in] table_id
7992  *   Table id to use.
7993  * @param[in] egress
7994  *   Direction of the table.
7995  * @param[in] transfer
7996  *   E-Switch or NIC flow.
7997  * @param[in] dummy
7998  *   Dummy entry for dv API.
7999  * @param[out] error
8000  *   pointer to error structure.
8001  *
8002  * @return
8003  *   Returns tables resource based on the index, NULL in case of failed.
8004  */
8005 struct mlx5_flow_tbl_resource *
8006 flow_dv_tbl_resource_get(struct rte_eth_dev *dev,
8007                          uint32_t table_id, uint8_t egress,
8008                          uint8_t transfer,
8009                          bool external,
8010                          const struct mlx5_flow_tunnel *tunnel,
8011                          uint32_t group_id, uint8_t dummy,
8012                          struct rte_flow_error *error)
8013 {
8014         struct mlx5_priv *priv = dev->data->dev_private;
8015         union mlx5_flow_tbl_key table_key = {
8016                 {
8017                         .table_id = table_id,
8018                         .dummy = dummy,
8019                         .domain = !!transfer,
8020                         .direction = !!egress,
8021                 }
8022         };
8023         struct mlx5_flow_tbl_tunnel_prm tt_prm = {
8024                 .tunnel = tunnel,
8025                 .group_id = group_id,
8026                 .external = external,
8027         };
8028         struct mlx5_flow_cb_ctx ctx = {
8029                 .dev = dev,
8030                 .error = error,
8031                 .data = &tt_prm,
8032         };
8033         struct mlx5_hlist_entry *entry;
8034         struct mlx5_flow_tbl_data_entry *tbl_data;
8035
8036         entry = mlx5_hlist_register(priv->sh->flow_tbls, table_key.v64, &ctx);
8037         if (!entry) {
8038                 rte_flow_error_set(error, ENOMEM,
8039                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
8040                                    "cannot get table");
8041                 return NULL;
8042         }
8043         tbl_data = container_of(entry, struct mlx5_flow_tbl_data_entry, entry);
8044         return &tbl_data->tbl;
8045 }
8046
8047 void
8048 flow_dv_tbl_remove_cb(struct mlx5_hlist *list,
8049                       struct mlx5_hlist_entry *entry)
8050 {
8051         struct mlx5_dev_ctx_shared *sh = list->ctx;
8052         struct mlx5_flow_tbl_data_entry *tbl_data =
8053                 container_of(entry, struct mlx5_flow_tbl_data_entry, entry);
8054
8055         MLX5_ASSERT(entry && sh);
8056         if (tbl_data->jump.action)
8057                 mlx5_flow_os_destroy_flow_action(tbl_data->jump.action);
8058         if (tbl_data->tbl.obj)
8059                 mlx5_flow_os_destroy_flow_tbl(tbl_data->tbl.obj);
8060         if (tbl_data->tunnel_offload && tbl_data->external) {
8061                 struct mlx5_hlist_entry *he;
8062                 struct mlx5_hlist *tunnel_grp_hash;
8063                 struct mlx5_flow_tunnel_hub *thub = sh->tunnel_hub;
8064                 union tunnel_tbl_key tunnel_key = {
8065                         .tunnel_id = tbl_data->tunnel ?
8066                                         tbl_data->tunnel->tunnel_id : 0,
8067                         .group = tbl_data->group_id
8068                 };
8069                 union mlx5_flow_tbl_key table_key = {
8070                         .v64 = entry->key
8071                 };
8072                 uint32_t table_id = table_key.table_id;
8073
8074                 tunnel_grp_hash = tbl_data->tunnel ?
8075                                         tbl_data->tunnel->groups :
8076                                         thub->groups;
8077                 he = mlx5_hlist_lookup(tunnel_grp_hash, tunnel_key.val, NULL);
8078                 if (he) {
8079                         struct tunnel_tbl_entry *tte;
8080                         tte = container_of(he, typeof(*tte), hash);
8081                         MLX5_ASSERT(tte->flow_table == table_id);
8082                         mlx5_hlist_remove(tunnel_grp_hash, he);
8083                         mlx5_free(tte);
8084                 }
8085                 mlx5_ipool_free(sh->ipool[MLX5_IPOOL_TNL_TBL_ID],
8086                                 tunnel_flow_tbl_to_id(table_id));
8087                 DRV_LOG(DEBUG,
8088                         "Table_id %#x tunnel %u group %u released.",
8089                         table_id,
8090                         tbl_data->tunnel ?
8091                         tbl_data->tunnel->tunnel_id : 0,
8092                         tbl_data->group_id);
8093         }
8094         mlx5_ipool_free(sh->ipool[MLX5_IPOOL_JUMP], tbl_data->idx);
8095 }
8096
8097 /**
8098  * Release a flow table.
8099  *
8100  * @param[in] dev
8101  *   Pointer to rte_eth_dev structure.
8102  * @param[in] tbl
8103  *   Table resource to be released.
8104  *
8105  * @return
8106  *   Returns 0 if table was released, else return 1;
8107  */
8108 static int
8109 flow_dv_tbl_resource_release(struct rte_eth_dev *dev,
8110                              struct mlx5_flow_tbl_resource *tbl)
8111 {
8112         struct mlx5_priv *priv = dev->data->dev_private;
8113         struct mlx5_dev_ctx_shared *sh = priv->sh;
8114         struct mlx5_flow_tbl_data_entry *tbl_data =
8115                 container_of(tbl, struct mlx5_flow_tbl_data_entry, tbl);
8116
8117         if (!tbl)
8118                 return 0;
8119         return mlx5_hlist_unregister(sh->flow_tbls, &tbl_data->entry);
8120 }
8121
8122 /**
8123  * Register the flow matcher.
8124  *
8125  * @param[in, out] dev
8126  *   Pointer to rte_eth_dev structure.
8127  * @param[in, out] matcher
8128  *   Pointer to flow matcher.
8129  * @param[in, out] key
8130  *   Pointer to flow table key.
8131  * @parm[in, out] dev_flow
8132  *   Pointer to the dev_flow.
8133  * @param[out] error
8134  *   pointer to error structure.
8135  *
8136  * @return
8137  *   0 on success otherwise -errno and errno is set.
8138  */
8139 static int
8140 flow_dv_matcher_register(struct rte_eth_dev *dev,
8141                          struct mlx5_flow_dv_matcher *matcher,
8142                          union mlx5_flow_tbl_key *key,
8143                          struct mlx5_flow *dev_flow,
8144                          struct rte_flow_error *error)
8145 {
8146         struct mlx5_priv *priv = dev->data->dev_private;
8147         struct mlx5_dev_ctx_shared *sh = priv->sh;
8148         struct mlx5_flow_dv_matcher *cache_matcher;
8149         struct mlx5dv_flow_matcher_attr dv_attr = {
8150                 .type = IBV_FLOW_ATTR_NORMAL,
8151                 .match_mask = (void *)&matcher->mask,
8152         };
8153         struct mlx5_flow_tbl_resource *tbl;
8154         struct mlx5_flow_tbl_data_entry *tbl_data;
8155         int ret;
8156
8157         tbl = flow_dv_tbl_resource_get(dev, key->table_id, key->direction,
8158                                        key->domain, false, NULL, 0, 0, error);
8159         if (!tbl)
8160                 return -rte_errno;      /* No need to refill the error info */
8161         tbl_data = container_of(tbl, struct mlx5_flow_tbl_data_entry, tbl);
8162         /* Lookup from cache. */
8163         LIST_FOREACH(cache_matcher, &tbl_data->matchers, next) {
8164                 if (matcher->crc == cache_matcher->crc &&
8165                     matcher->priority == cache_matcher->priority &&
8166                     !memcmp((const void *)matcher->mask.buf,
8167                             (const void *)cache_matcher->mask.buf,
8168                             cache_matcher->mask.size)) {
8169                         DRV_LOG(DEBUG,
8170                                 "%s group %u priority %hd use %s "
8171                                 "matcher %p: refcnt %d++",
8172                                 key->domain ? "FDB" : "NIC", key->table_id,
8173                                 cache_matcher->priority,
8174                                 key->direction ? "tx" : "rx",
8175                                 (void *)cache_matcher,
8176                                 __atomic_load_n(&cache_matcher->refcnt,
8177                                                 __ATOMIC_RELAXED));
8178                         __atomic_fetch_add(&cache_matcher->refcnt, 1,
8179                                            __ATOMIC_RELAXED);
8180                         dev_flow->handle->dvh.matcher = cache_matcher;
8181                         /* old matcher should not make the table ref++. */
8182                         flow_dv_tbl_resource_release(dev, tbl);
8183                         return 0;
8184                 }
8185         }
8186         /* Register new matcher. */
8187         cache_matcher = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*cache_matcher), 0,
8188                                     SOCKET_ID_ANY);
8189         if (!cache_matcher) {
8190                 flow_dv_tbl_resource_release(dev, tbl);
8191                 return rte_flow_error_set(error, ENOMEM,
8192                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
8193                                           "cannot allocate matcher memory");
8194         }
8195         *cache_matcher = *matcher;
8196         dv_attr.match_criteria_enable =
8197                 flow_dv_matcher_enable(cache_matcher->mask.buf);
8198         dv_attr.priority = matcher->priority;
8199         if (key->direction)
8200                 dv_attr.flags |= IBV_FLOW_ATTR_FLAGS_EGRESS;
8201         ret = mlx5_flow_os_create_flow_matcher(sh->ctx, &dv_attr, tbl->obj,
8202                                                &cache_matcher->matcher_object);
8203         if (ret) {
8204                 mlx5_free(cache_matcher);
8205 #ifdef HAVE_MLX5DV_DR
8206                 flow_dv_tbl_resource_release(dev, tbl);
8207 #endif
8208                 return rte_flow_error_set(error, ENOMEM,
8209                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
8210                                           NULL, "cannot create matcher");
8211         }
8212         /* Save the table information */
8213         cache_matcher->tbl = tbl;
8214         /* only matcher ref++, table ref++ already done above in get API. */
8215         __atomic_store_n(&cache_matcher->refcnt, 1, __ATOMIC_RELAXED);
8216         LIST_INSERT_HEAD(&tbl_data->matchers, cache_matcher, next);
8217         dev_flow->handle->dvh.matcher = cache_matcher;
8218         DRV_LOG(DEBUG, "%s group %u priority %hd new %s matcher %p: refcnt %d",
8219                 key->domain ? "FDB" : "NIC", key->table_id,
8220                 cache_matcher->priority,
8221                 key->direction ? "tx" : "rx", (void *)cache_matcher,
8222                 __atomic_load_n(&cache_matcher->refcnt, __ATOMIC_RELAXED));
8223         return 0;
8224 }
8225
8226 /**
8227  * Find existing tag resource or create and register a new one.
8228  *
8229  * @param dev[in, out]
8230  *   Pointer to rte_eth_dev structure.
8231  * @param[in, out] tag_be24
8232  *   Tag value in big endian then R-shift 8.
8233  * @parm[in, out] dev_flow
8234  *   Pointer to the dev_flow.
8235  * @param[out] error
8236  *   pointer to error structure.
8237  *
8238  * @return
8239  *   0 on success otherwise -errno and errno is set.
8240  */
8241 static int
8242 flow_dv_tag_resource_register
8243                         (struct rte_eth_dev *dev,
8244                          uint32_t tag_be24,
8245                          struct mlx5_flow *dev_flow,
8246                          struct rte_flow_error *error)
8247 {
8248         struct mlx5_priv *priv = dev->data->dev_private;
8249         struct mlx5_dev_ctx_shared *sh = priv->sh;
8250         struct mlx5_flow_dv_tag_resource *cache_resource;
8251         struct mlx5_hlist_entry *entry;
8252         int ret;
8253
8254         /* Lookup a matching resource from cache. */
8255         entry = mlx5_hlist_lookup(sh->tag_table, (uint64_t)tag_be24, NULL);
8256         if (entry) {
8257                 cache_resource = container_of
8258                         (entry, struct mlx5_flow_dv_tag_resource, entry);
8259                 __atomic_fetch_add(&cache_resource->refcnt, 1,
8260                                    __ATOMIC_RELAXED);
8261                 dev_flow->handle->dvh.rix_tag = cache_resource->idx;
8262                 dev_flow->dv.tag_resource = cache_resource;
8263                 DRV_LOG(DEBUG, "cached tag resource %p: refcnt now %d++",
8264                         (void *)cache_resource,
8265                         __atomic_load_n(&cache_resource->refcnt,
8266                                         __ATOMIC_RELAXED));
8267                 return 0;
8268         }
8269         /* Register new resource. */
8270         cache_resource = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_TAG],
8271                                        &dev_flow->handle->dvh.rix_tag);
8272         if (!cache_resource)
8273                 return rte_flow_error_set(error, ENOMEM,
8274                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
8275                                           "cannot allocate resource memory");
8276         cache_resource->entry.key = (uint64_t)tag_be24;
8277         ret = mlx5_flow_os_create_flow_action_tag(tag_be24,
8278                                                   &cache_resource->action);
8279         if (ret) {
8280                 mlx5_free(cache_resource);
8281                 return rte_flow_error_set(error, ENOMEM,
8282                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
8283                                           NULL, "cannot create action");
8284         }
8285         __atomic_store_n(&cache_resource->refcnt, 1, __ATOMIC_RELAXED);
8286         if (mlx5_hlist_insert(sh->tag_table, &cache_resource->entry)) {
8287                 mlx5_flow_os_destroy_flow_action(cache_resource->action);
8288                 mlx5_free(cache_resource);
8289                 return rte_flow_error_set(error, EEXIST,
8290                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
8291                                           NULL, "cannot insert tag");
8292         }
8293         dev_flow->dv.tag_resource = cache_resource;
8294         DRV_LOG(DEBUG, "new tag resource %p: refcnt now %d++",
8295                 (void *)cache_resource,
8296                 __atomic_load_n(&cache_resource->refcnt, __ATOMIC_RELAXED));
8297         return 0;
8298 }
8299
8300 /**
8301  * Release the tag.
8302  *
8303  * @param dev
8304  *   Pointer to Ethernet device.
8305  * @param tag_idx
8306  *   Tag index.
8307  *
8308  * @return
8309  *   1 while a reference on it exists, 0 when freed.
8310  */
8311 static int
8312 flow_dv_tag_release(struct rte_eth_dev *dev,
8313                     uint32_t tag_idx)
8314 {
8315         struct mlx5_priv *priv = dev->data->dev_private;
8316         struct mlx5_dev_ctx_shared *sh = priv->sh;
8317         struct mlx5_flow_dv_tag_resource *tag;
8318
8319         tag = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_TAG], tag_idx);
8320         if (!tag)
8321                 return 0;
8322         DRV_LOG(DEBUG, "port %u tag %p: refcnt %d--",
8323                 dev->data->port_id, (void *)tag,
8324                 __atomic_load_n(&tag->refcnt, __ATOMIC_RELAXED));
8325         if (__atomic_sub_fetch(&tag->refcnt, 1, __ATOMIC_RELAXED) == 0) {
8326                 claim_zero(mlx5_flow_os_destroy_flow_action(tag->action));
8327                 mlx5_hlist_remove(sh->tag_table, &tag->entry);
8328                 DRV_LOG(DEBUG, "port %u tag %p: removed",
8329                         dev->data->port_id, (void *)tag);
8330                 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_TAG], tag_idx);
8331                 return 0;
8332         }
8333         return 1;
8334 }
8335
8336 /**
8337  * Translate port ID action to vport.
8338  *
8339  * @param[in] dev
8340  *   Pointer to rte_eth_dev structure.
8341  * @param[in] action
8342  *   Pointer to the port ID action.
8343  * @param[out] dst_port_id
8344  *   The target port ID.
8345  * @param[out] error
8346  *   Pointer to the error structure.
8347  *
8348  * @return
8349  *   0 on success, a negative errno value otherwise and rte_errno is set.
8350  */
8351 static int
8352 flow_dv_translate_action_port_id(struct rte_eth_dev *dev,
8353                                  const struct rte_flow_action *action,
8354                                  uint32_t *dst_port_id,
8355                                  struct rte_flow_error *error)
8356 {
8357         uint32_t port;
8358         struct mlx5_priv *priv;
8359         const struct rte_flow_action_port_id *conf =
8360                         (const struct rte_flow_action_port_id *)action->conf;
8361
8362         port = conf->original ? dev->data->port_id : conf->id;
8363         priv = mlx5_port_to_eswitch_info(port, false);
8364         if (!priv)
8365                 return rte_flow_error_set(error, -rte_errno,
8366                                           RTE_FLOW_ERROR_TYPE_ACTION,
8367                                           NULL,
8368                                           "No eswitch info was found for port");
8369 #ifdef HAVE_MLX5DV_DR_DEVX_PORT
8370         /*
8371          * This parameter is transferred to
8372          * mlx5dv_dr_action_create_dest_ib_port().
8373          */
8374         *dst_port_id = priv->dev_port;
8375 #else
8376         /*
8377          * Legacy mode, no LAG configurations is supported.
8378          * This parameter is transferred to
8379          * mlx5dv_dr_action_create_dest_vport().
8380          */
8381         *dst_port_id = priv->vport_id;
8382 #endif
8383         return 0;
8384 }
8385
8386 /**
8387  * Create a counter with aging configuration.
8388  *
8389  * @param[in] dev
8390  *   Pointer to rte_eth_dev structure.
8391  * @param[out] count
8392  *   Pointer to the counter action configuration.
8393  * @param[in] age
8394  *   Pointer to the aging action configuration.
8395  *
8396  * @return
8397  *   Index to flow counter on success, 0 otherwise.
8398  */
8399 static uint32_t
8400 flow_dv_translate_create_counter(struct rte_eth_dev *dev,
8401                                 struct mlx5_flow *dev_flow,
8402                                 const struct rte_flow_action_count *count,
8403                                 const struct rte_flow_action_age *age)
8404 {
8405         uint32_t counter;
8406         struct mlx5_age_param *age_param;
8407
8408         if (count && count->shared)
8409                 counter = flow_dv_counter_get_shared(dev, count->id);
8410         else
8411                 counter = flow_dv_counter_alloc(dev, !!age);
8412         if (!counter || age == NULL)
8413                 return counter;
8414         age_param  = flow_dv_counter_idx_get_age(dev, counter);
8415         age_param->context = age->context ? age->context :
8416                 (void *)(uintptr_t)(dev_flow->flow_idx);
8417         age_param->timeout = age->timeout;
8418         age_param->port_id = dev->data->port_id;
8419         __atomic_store_n(&age_param->sec_since_last_hit, 0, __ATOMIC_RELAXED);
8420         __atomic_store_n(&age_param->state, AGE_CANDIDATE, __ATOMIC_RELAXED);
8421         return counter;
8422 }
8423 /**
8424  * Add Tx queue matcher
8425  *
8426  * @param[in] dev
8427  *   Pointer to the dev struct.
8428  * @param[in, out] matcher
8429  *   Flow matcher.
8430  * @param[in, out] key
8431  *   Flow matcher value.
8432  * @param[in] item
8433  *   Flow pattern to translate.
8434  * @param[in] inner
8435  *   Item is inner pattern.
8436  */
8437 static void
8438 flow_dv_translate_item_tx_queue(struct rte_eth_dev *dev,
8439                                 void *matcher, void *key,
8440                                 const struct rte_flow_item *item)
8441 {
8442         const struct mlx5_rte_flow_item_tx_queue *queue_m;
8443         const struct mlx5_rte_flow_item_tx_queue *queue_v;
8444         void *misc_m =
8445                 MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
8446         void *misc_v =
8447                 MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
8448         struct mlx5_txq_ctrl *txq;
8449         uint32_t queue;
8450
8451
8452         queue_m = (const void *)item->mask;
8453         if (!queue_m)
8454                 return;
8455         queue_v = (const void *)item->spec;
8456         if (!queue_v)
8457                 return;
8458         txq = mlx5_txq_get(dev, queue_v->queue);
8459         if (!txq)
8460                 return;
8461         queue = txq->obj->sq->id;
8462         MLX5_SET(fte_match_set_misc, misc_m, source_sqn, queue_m->queue);
8463         MLX5_SET(fte_match_set_misc, misc_v, source_sqn,
8464                  queue & queue_m->queue);
8465         mlx5_txq_release(dev, queue_v->queue);
8466 }
8467
8468 /**
8469  * Set the hash fields according to the @p flow information.
8470  *
8471  * @param[in] dev_flow
8472  *   Pointer to the mlx5_flow.
8473  * @param[in] rss_desc
8474  *   Pointer to the mlx5_flow_rss_desc.
8475  */
8476 static void
8477 flow_dv_hashfields_set(struct mlx5_flow *dev_flow,
8478                        struct mlx5_flow_rss_desc *rss_desc)
8479 {
8480         uint64_t items = dev_flow->handle->layers;
8481         int rss_inner = 0;
8482         uint64_t rss_types = rte_eth_rss_hf_refine(rss_desc->types);
8483
8484         dev_flow->hash_fields = 0;
8485 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
8486         if (rss_desc->level >= 2) {
8487                 dev_flow->hash_fields |= IBV_RX_HASH_INNER;
8488                 rss_inner = 1;
8489         }
8490 #endif
8491         if ((rss_inner && (items & MLX5_FLOW_LAYER_INNER_L3_IPV4)) ||
8492             (!rss_inner && (items & MLX5_FLOW_LAYER_OUTER_L3_IPV4))) {
8493                 if (rss_types & MLX5_IPV4_LAYER_TYPES) {
8494                         if (rss_types & ETH_RSS_L3_SRC_ONLY)
8495                                 dev_flow->hash_fields |= IBV_RX_HASH_SRC_IPV4;
8496                         else if (rss_types & ETH_RSS_L3_DST_ONLY)
8497                                 dev_flow->hash_fields |= IBV_RX_HASH_DST_IPV4;
8498                         else
8499                                 dev_flow->hash_fields |= MLX5_IPV4_IBV_RX_HASH;
8500                 }
8501         } else if ((rss_inner && (items & MLX5_FLOW_LAYER_INNER_L3_IPV6)) ||
8502                    (!rss_inner && (items & MLX5_FLOW_LAYER_OUTER_L3_IPV6))) {
8503                 if (rss_types & MLX5_IPV6_LAYER_TYPES) {
8504                         if (rss_types & ETH_RSS_L3_SRC_ONLY)
8505                                 dev_flow->hash_fields |= IBV_RX_HASH_SRC_IPV6;
8506                         else if (rss_types & ETH_RSS_L3_DST_ONLY)
8507                                 dev_flow->hash_fields |= IBV_RX_HASH_DST_IPV6;
8508                         else
8509                                 dev_flow->hash_fields |= MLX5_IPV6_IBV_RX_HASH;
8510                 }
8511         }
8512         if ((rss_inner && (items & MLX5_FLOW_LAYER_INNER_L4_UDP)) ||
8513             (!rss_inner && (items & MLX5_FLOW_LAYER_OUTER_L4_UDP))) {
8514                 if (rss_types & ETH_RSS_UDP) {
8515                         if (rss_types & ETH_RSS_L4_SRC_ONLY)
8516                                 dev_flow->hash_fields |=
8517                                                 IBV_RX_HASH_SRC_PORT_UDP;
8518                         else if (rss_types & ETH_RSS_L4_DST_ONLY)
8519                                 dev_flow->hash_fields |=
8520                                                 IBV_RX_HASH_DST_PORT_UDP;
8521                         else
8522                                 dev_flow->hash_fields |= MLX5_UDP_IBV_RX_HASH;
8523                 }
8524         } else if ((rss_inner && (items & MLX5_FLOW_LAYER_INNER_L4_TCP)) ||
8525                    (!rss_inner && (items & MLX5_FLOW_LAYER_OUTER_L4_TCP))) {
8526                 if (rss_types & ETH_RSS_TCP) {
8527                         if (rss_types & ETH_RSS_L4_SRC_ONLY)
8528                                 dev_flow->hash_fields |=
8529                                                 IBV_RX_HASH_SRC_PORT_TCP;
8530                         else if (rss_types & ETH_RSS_L4_DST_ONLY)
8531                                 dev_flow->hash_fields |=
8532                                                 IBV_RX_HASH_DST_PORT_TCP;
8533                         else
8534                                 dev_flow->hash_fields |= MLX5_TCP_IBV_RX_HASH;
8535                 }
8536         }
8537 }
8538
8539 /**
8540  * Create an Rx Hash queue.
8541  *
8542  * @param dev
8543  *   Pointer to Ethernet device.
8544  * @param[in] dev_flow
8545  *   Pointer to the mlx5_flow.
8546  * @param[in] rss_desc
8547  *   Pointer to the mlx5_flow_rss_desc.
8548  * @param[out] hrxq_idx
8549  *   Hash Rx queue index.
8550  *
8551  * @return
8552  *   The Verbs/DevX object initialised, NULL otherwise and rte_errno is set.
8553  */
8554 static struct mlx5_hrxq *
8555 flow_dv_handle_rx_queue(struct rte_eth_dev *dev,
8556                         struct mlx5_flow *dev_flow,
8557                         struct mlx5_flow_rss_desc *rss_desc,
8558                         uint32_t *hrxq_idx)
8559 {
8560         struct mlx5_priv *priv = dev->data->dev_private;
8561         struct mlx5_flow_handle *dh = dev_flow->handle;
8562         struct mlx5_hrxq *hrxq;
8563
8564         MLX5_ASSERT(rss_desc->queue_num);
8565         *hrxq_idx = mlx5_hrxq_get(dev, rss_desc->key, MLX5_RSS_HASH_KEY_LEN,
8566                                   dev_flow->hash_fields,
8567                                   rss_desc->queue, rss_desc->queue_num);
8568         if (!*hrxq_idx) {
8569                 *hrxq_idx = mlx5_hrxq_new
8570                                 (dev, rss_desc->key, MLX5_RSS_HASH_KEY_LEN,
8571                                  dev_flow->hash_fields,
8572                                  rss_desc->queue, rss_desc->queue_num,
8573                                  !!(dh->layers & MLX5_FLOW_LAYER_TUNNEL),
8574                                  false);
8575                 if (!*hrxq_idx)
8576                         return NULL;
8577         }
8578         hrxq = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_HRXQ],
8579                               *hrxq_idx);
8580         return hrxq;
8581 }
8582
8583 /**
8584  * Find existing sample resource or create and register a new one.
8585  *
8586  * @param[in, out] dev
8587  *   Pointer to rte_eth_dev structure.
8588  * @param[in] attr
8589  *   Attributes of flow that includes this item.
8590  * @param[in] resource
8591  *   Pointer to sample resource.
8592  * @parm[in, out] dev_flow
8593  *   Pointer to the dev_flow.
8594  * @param[in, out] sample_dv_actions
8595  *   Pointer to sample actions list.
8596  * @param[out] error
8597  *   pointer to error structure.
8598  *
8599  * @return
8600  *   0 on success otherwise -errno and errno is set.
8601  */
8602 static int
8603 flow_dv_sample_resource_register(struct rte_eth_dev *dev,
8604                          const struct rte_flow_attr *attr,
8605                          struct mlx5_flow_dv_sample_resource *resource,
8606                          struct mlx5_flow *dev_flow,
8607                          void **sample_dv_actions,
8608                          struct rte_flow_error *error)
8609 {
8610         struct mlx5_flow_dv_sample_resource *cache_resource;
8611         struct mlx5dv_dr_flow_sampler_attr sampler_attr;
8612         struct mlx5_priv *priv = dev->data->dev_private;
8613         struct mlx5_dev_ctx_shared *sh = priv->sh;
8614         struct mlx5_flow_tbl_resource *tbl;
8615         uint32_t idx = 0;
8616         const uint32_t next_ft_step = 1;
8617         uint32_t next_ft_id = resource->ft_id + next_ft_step;
8618
8619         /* Lookup a matching resource from cache. */
8620         ILIST_FOREACH(sh->ipool[MLX5_IPOOL_SAMPLE], sh->sample_action_list,
8621                       idx, cache_resource, next) {
8622                 if (resource->ratio == cache_resource->ratio &&
8623                     resource->ft_type == cache_resource->ft_type &&
8624                     resource->ft_id == cache_resource->ft_id &&
8625                     resource->set_action == cache_resource->set_action &&
8626                     !memcmp((void *)&resource->sample_act,
8627                             (void *)&cache_resource->sample_act,
8628                             sizeof(struct mlx5_flow_sub_actions_list))) {
8629                         DRV_LOG(DEBUG, "sample resource %p: refcnt %d++",
8630                                 (void *)cache_resource,
8631                                 __atomic_load_n(&cache_resource->refcnt,
8632                                                 __ATOMIC_RELAXED));
8633                         __atomic_fetch_add(&cache_resource->refcnt, 1,
8634                                            __ATOMIC_RELAXED);
8635                         dev_flow->handle->dvh.rix_sample = idx;
8636                         dev_flow->dv.sample_res = cache_resource;
8637                         return 0;
8638                 }
8639         }
8640         /* Register new sample resource. */
8641         cache_resource = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_SAMPLE],
8642                                        &dev_flow->handle->dvh.rix_sample);
8643         if (!cache_resource)
8644                 return rte_flow_error_set(error, ENOMEM,
8645                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
8646                                           NULL,
8647                                           "cannot allocate resource memory");
8648         *cache_resource = *resource;
8649         /* Create normal path table level */
8650         tbl = flow_dv_tbl_resource_get(dev, next_ft_id,
8651                                         attr->egress, attr->transfer,
8652                                         dev_flow->external, NULL, 0, 0, error);
8653         if (!tbl) {
8654                 rte_flow_error_set(error, ENOMEM,
8655                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
8656                                           NULL,
8657                                           "fail to create normal path table "
8658                                           "for sample");
8659                 goto error;
8660         }
8661         cache_resource->normal_path_tbl = tbl;
8662         if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB) {
8663                 cache_resource->default_miss =
8664                                 mlx5_glue->dr_create_flow_action_default_miss();
8665                 if (!cache_resource->default_miss) {
8666                         rte_flow_error_set(error, ENOMEM,
8667                                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
8668                                                 NULL,
8669                                                 "cannot create default miss "
8670                                                 "action");
8671                         goto error;
8672                 }
8673                 sample_dv_actions[resource->sample_act.actions_num++] =
8674                                                 cache_resource->default_miss;
8675         }
8676         /* Create a DR sample action */
8677         sampler_attr.sample_ratio = cache_resource->ratio;
8678         sampler_attr.default_next_table = tbl->obj;
8679         sampler_attr.num_sample_actions = resource->sample_act.actions_num;
8680         sampler_attr.sample_actions = (struct mlx5dv_dr_action **)
8681                                                         &sample_dv_actions[0];
8682         sampler_attr.action = cache_resource->set_action;
8683         cache_resource->verbs_action =
8684                 mlx5_glue->dr_create_flow_action_sampler(&sampler_attr);
8685         if (!cache_resource->verbs_action) {
8686                 rte_flow_error_set(error, ENOMEM,
8687                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
8688                                         NULL, "cannot create sample action");
8689                 goto error;
8690         }
8691         __atomic_store_n(&cache_resource->refcnt, 1, __ATOMIC_RELAXED);
8692         ILIST_INSERT(sh->ipool[MLX5_IPOOL_SAMPLE], &sh->sample_action_list,
8693                      dev_flow->handle->dvh.rix_sample, cache_resource,
8694                      next);
8695         dev_flow->dv.sample_res = cache_resource;
8696         DRV_LOG(DEBUG, "new sample resource %p: refcnt %d++",
8697                 (void *)cache_resource,
8698                 __atomic_load_n(&cache_resource->refcnt, __ATOMIC_RELAXED));
8699         return 0;
8700 error:
8701         if (cache_resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB) {
8702                 if (cache_resource->default_miss)
8703                         claim_zero(mlx5_glue->destroy_flow_action
8704                                 (cache_resource->default_miss));
8705         } else {
8706                 if (cache_resource->sample_idx.rix_hrxq &&
8707                     !mlx5_hrxq_release(dev,
8708                                 cache_resource->sample_idx.rix_hrxq))
8709                         cache_resource->sample_idx.rix_hrxq = 0;
8710                 if (cache_resource->sample_idx.rix_tag &&
8711                     !flow_dv_tag_release(dev,
8712                                 cache_resource->sample_idx.rix_tag))
8713                         cache_resource->sample_idx.rix_tag = 0;
8714                 if (cache_resource->sample_idx.cnt) {
8715                         flow_dv_counter_release(dev,
8716                                 cache_resource->sample_idx.cnt);
8717                         cache_resource->sample_idx.cnt = 0;
8718                 }
8719         }
8720         if (cache_resource->normal_path_tbl)
8721                 flow_dv_tbl_resource_release(dev,
8722                                 cache_resource->normal_path_tbl);
8723         mlx5_ipool_free(sh->ipool[MLX5_IPOOL_SAMPLE],
8724                                 dev_flow->handle->dvh.rix_sample);
8725         dev_flow->handle->dvh.rix_sample = 0;
8726         return -rte_errno;
8727 }
8728
8729 /**
8730  * Find existing destination array resource or create and register a new one.
8731  *
8732  * @param[in, out] dev
8733  *   Pointer to rte_eth_dev structure.
8734  * @param[in] attr
8735  *   Attributes of flow that includes this item.
8736  * @param[in] resource
8737  *   Pointer to destination array resource.
8738  * @parm[in, out] dev_flow
8739  *   Pointer to the dev_flow.
8740  * @param[out] error
8741  *   pointer to error structure.
8742  *
8743  * @return
8744  *   0 on success otherwise -errno and errno is set.
8745  */
8746 static int
8747 flow_dv_dest_array_resource_register(struct rte_eth_dev *dev,
8748                          const struct rte_flow_attr *attr,
8749                          struct mlx5_flow_dv_dest_array_resource *resource,
8750                          struct mlx5_flow *dev_flow,
8751                          struct rte_flow_error *error)
8752 {
8753         struct mlx5_flow_dv_dest_array_resource *cache_resource;
8754         struct mlx5dv_dr_action_dest_attr *dest_attr[MLX5_MAX_DEST_NUM] = { 0 };
8755         struct mlx5dv_dr_action_dest_reformat dest_reformat[MLX5_MAX_DEST_NUM];
8756         struct mlx5_priv *priv = dev->data->dev_private;
8757         struct mlx5_dev_ctx_shared *sh = priv->sh;
8758         struct mlx5_flow_sub_actions_list *sample_act;
8759         struct mlx5dv_dr_domain *domain;
8760         uint32_t idx = 0;
8761
8762         /* Lookup a matching resource from cache. */
8763         ILIST_FOREACH(sh->ipool[MLX5_IPOOL_DEST_ARRAY],
8764                       sh->dest_array_list,
8765                       idx, cache_resource, next) {
8766                 if (resource->num_of_dest == cache_resource->num_of_dest &&
8767                     resource->ft_type == cache_resource->ft_type &&
8768                     !memcmp((void *)cache_resource->sample_act,
8769                             (void *)resource->sample_act,
8770                            (resource->num_of_dest *
8771                            sizeof(struct mlx5_flow_sub_actions_list)))) {
8772                         DRV_LOG(DEBUG, "dest array resource %p: refcnt %d++",
8773                                 (void *)cache_resource,
8774                                 __atomic_load_n(&cache_resource->refcnt,
8775                                                 __ATOMIC_RELAXED));
8776                         __atomic_fetch_add(&cache_resource->refcnt, 1,
8777                                            __ATOMIC_RELAXED);
8778                         dev_flow->handle->dvh.rix_dest_array = idx;
8779                         dev_flow->dv.dest_array_res = cache_resource;
8780                         return 0;
8781                 }
8782         }
8783         /* Register new destination array resource. */
8784         cache_resource = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_DEST_ARRAY],
8785                                        &dev_flow->handle->dvh.rix_dest_array);
8786         if (!cache_resource)
8787                 return rte_flow_error_set(error, ENOMEM,
8788                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
8789                                           NULL,
8790                                           "cannot allocate resource memory");
8791         *cache_resource = *resource;
8792         if (attr->transfer)
8793                 domain = sh->fdb_domain;
8794         else if (attr->ingress)
8795                 domain = sh->rx_domain;
8796         else
8797                 domain = sh->tx_domain;
8798         for (idx = 0; idx < resource->num_of_dest; idx++) {
8799                 dest_attr[idx] = (struct mlx5dv_dr_action_dest_attr *)
8800                                  mlx5_malloc(MLX5_MEM_ZERO,
8801                                  sizeof(struct mlx5dv_dr_action_dest_attr),
8802                                  0, SOCKET_ID_ANY);
8803                 if (!dest_attr[idx]) {
8804                         rte_flow_error_set(error, ENOMEM,
8805                                            RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
8806                                            NULL,
8807                                            "cannot allocate resource memory");
8808                         goto error;
8809                 }
8810                 dest_attr[idx]->type = MLX5DV_DR_ACTION_DEST;
8811                 sample_act = &resource->sample_act[idx];
8812                 if (sample_act->action_flags == MLX5_FLOW_ACTION_QUEUE) {
8813                         dest_attr[idx]->dest = sample_act->dr_queue_action;
8814                 } else if (sample_act->action_flags ==
8815                           (MLX5_FLOW_ACTION_PORT_ID | MLX5_FLOW_ACTION_ENCAP)) {
8816                         dest_attr[idx]->type = MLX5DV_DR_ACTION_DEST_REFORMAT;
8817                         dest_attr[idx]->dest_reformat = &dest_reformat[idx];
8818                         dest_attr[idx]->dest_reformat->reformat =
8819                                         sample_act->dr_encap_action;
8820                         dest_attr[idx]->dest_reformat->dest =
8821                                         sample_act->dr_port_id_action;
8822                 } else if (sample_act->action_flags ==
8823                            MLX5_FLOW_ACTION_PORT_ID) {
8824                         dest_attr[idx]->dest = sample_act->dr_port_id_action;
8825                 }
8826         }
8827         /* create a dest array actioin */
8828         cache_resource->action = mlx5_glue->dr_create_flow_action_dest_array
8829                                                 (domain,
8830                                                  cache_resource->num_of_dest,
8831                                                  dest_attr);
8832         if (!cache_resource->action) {
8833                 rte_flow_error_set(error, ENOMEM,
8834                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
8835                                    NULL,
8836                                    "cannot create destination array action");
8837                 goto error;
8838         }
8839         __atomic_store_n(&cache_resource->refcnt, 1, __ATOMIC_RELAXED);
8840         ILIST_INSERT(sh->ipool[MLX5_IPOOL_DEST_ARRAY],
8841                      &sh->dest_array_list,
8842                      dev_flow->handle->dvh.rix_dest_array, cache_resource,
8843                      next);
8844         dev_flow->dv.dest_array_res = cache_resource;
8845         DRV_LOG(DEBUG, "new destination array resource %p: refcnt %d++",
8846                 (void *)cache_resource,
8847                 __atomic_load_n(&cache_resource->refcnt, __ATOMIC_RELAXED));
8848         for (idx = 0; idx < resource->num_of_dest; idx++)
8849                 mlx5_free(dest_attr[idx]);
8850         return 0;
8851 error:
8852         for (idx = 0; idx < resource->num_of_dest; idx++) {
8853                 struct mlx5_flow_sub_actions_idx *act_res =
8854                                         &cache_resource->sample_idx[idx];
8855                 if (act_res->rix_hrxq &&
8856                     !mlx5_hrxq_release(dev,
8857                                 act_res->rix_hrxq))
8858                         act_res->rix_hrxq = 0;
8859                 if (act_res->rix_encap_decap &&
8860                         !flow_dv_encap_decap_resource_release(dev,
8861                                 act_res->rix_encap_decap))
8862                         act_res->rix_encap_decap = 0;
8863                 if (act_res->rix_port_id_action &&
8864                         !flow_dv_port_id_action_resource_release(dev,
8865                                 act_res->rix_port_id_action))
8866                         act_res->rix_port_id_action = 0;
8867                 if (dest_attr[idx])
8868                         mlx5_free(dest_attr[idx]);
8869         }
8870
8871         mlx5_ipool_free(sh->ipool[MLX5_IPOOL_DEST_ARRAY],
8872                                 dev_flow->handle->dvh.rix_dest_array);
8873         dev_flow->handle->dvh.rix_dest_array = 0;
8874         return -rte_errno;
8875 }
8876
8877 /**
8878  * Convert Sample action to DV specification.
8879  *
8880  * @param[in] dev
8881  *   Pointer to rte_eth_dev structure.
8882  * @param[in] action
8883  *   Pointer to action structure.
8884  * @param[in, out] dev_flow
8885  *   Pointer to the mlx5_flow.
8886  * @param[in] attr
8887  *   Pointer to the flow attributes.
8888  * @param[in, out] num_of_dest
8889  *   Pointer to the num of destination.
8890  * @param[in, out] sample_actions
8891  *   Pointer to sample actions list.
8892  * @param[in, out] res
8893  *   Pointer to sample resource.
8894  * @param[out] error
8895  *   Pointer to the error structure.
8896  *
8897  * @return
8898  *   0 on success, a negative errno value otherwise and rte_errno is set.
8899  */
8900 static int
8901 flow_dv_translate_action_sample(struct rte_eth_dev *dev,
8902                                 const struct rte_flow_action *action,
8903                                 struct mlx5_flow *dev_flow,
8904                                 const struct rte_flow_attr *attr,
8905                                 uint32_t *num_of_dest,
8906                                 void **sample_actions,
8907                                 struct mlx5_flow_dv_sample_resource *res,
8908                                 struct rte_flow_error *error)
8909 {
8910         struct mlx5_priv *priv = dev->data->dev_private;
8911         const struct rte_flow_action_sample *sample_action;
8912         const struct rte_flow_action *sub_actions;
8913         const struct rte_flow_action_queue *queue;
8914         struct mlx5_flow_sub_actions_list *sample_act;
8915         struct mlx5_flow_sub_actions_idx *sample_idx;
8916         struct mlx5_flow_workspace *wks = mlx5_flow_get_thread_workspace();
8917         struct mlx5_flow_rss_desc *rss_desc;
8918         uint64_t action_flags = 0;
8919
8920         MLX5_ASSERT(wks);
8921         rss_desc = &wks->rss_desc[!!wks->flow_nested_idx];
8922         sample_act = &res->sample_act;
8923         sample_idx = &res->sample_idx;
8924         sample_action = (const struct rte_flow_action_sample *)action->conf;
8925         res->ratio = sample_action->ratio;
8926         sub_actions = sample_action->actions;
8927         for (; sub_actions->type != RTE_FLOW_ACTION_TYPE_END; sub_actions++) {
8928                 int type = sub_actions->type;
8929                 uint32_t pre_rix = 0;
8930                 void *pre_r;
8931                 switch (type) {
8932                 case RTE_FLOW_ACTION_TYPE_QUEUE:
8933                 {
8934                         struct mlx5_hrxq *hrxq;
8935                         uint32_t hrxq_idx;
8936
8937                         queue = sub_actions->conf;
8938                         rss_desc->queue_num = 1;
8939                         rss_desc->queue[0] = queue->index;
8940                         hrxq = flow_dv_handle_rx_queue(dev, dev_flow,
8941                                         rss_desc, &hrxq_idx);
8942                         if (!hrxq)
8943                                 return rte_flow_error_set
8944                                         (error, rte_errno,
8945                                          RTE_FLOW_ERROR_TYPE_ACTION,
8946                                          NULL,
8947                                          "cannot create fate queue");
8948                         sample_act->dr_queue_action = hrxq->action;
8949                         sample_idx->rix_hrxq = hrxq_idx;
8950                         sample_actions[sample_act->actions_num++] =
8951                                                 hrxq->action;
8952                         (*num_of_dest)++;
8953                         action_flags |= MLX5_FLOW_ACTION_QUEUE;
8954                         if (action_flags & MLX5_FLOW_ACTION_MARK)
8955                                 dev_flow->handle->rix_hrxq = hrxq_idx;
8956                         dev_flow->handle->fate_action =
8957                                         MLX5_FLOW_FATE_QUEUE;
8958                         break;
8959                 }
8960                 case RTE_FLOW_ACTION_TYPE_MARK:
8961                 {
8962                         uint32_t tag_be = mlx5_flow_mark_set
8963                                 (((const struct rte_flow_action_mark *)
8964                                 (sub_actions->conf))->id);
8965
8966                         dev_flow->handle->mark = 1;
8967                         pre_rix = dev_flow->handle->dvh.rix_tag;
8968                         /* Save the mark resource before sample */
8969                         pre_r = dev_flow->dv.tag_resource;
8970                         if (flow_dv_tag_resource_register(dev, tag_be,
8971                                                   dev_flow, error))
8972                                 return -rte_errno;
8973                         MLX5_ASSERT(dev_flow->dv.tag_resource);
8974                         sample_act->dr_tag_action =
8975                                 dev_flow->dv.tag_resource->action;
8976                         sample_idx->rix_tag =
8977                                 dev_flow->handle->dvh.rix_tag;
8978                         sample_actions[sample_act->actions_num++] =
8979                                                 sample_act->dr_tag_action;
8980                         /* Recover the mark resource after sample */
8981                         dev_flow->dv.tag_resource = pre_r;
8982                         dev_flow->handle->dvh.rix_tag = pre_rix;
8983                         action_flags |= MLX5_FLOW_ACTION_MARK;
8984                         break;
8985                 }
8986                 case RTE_FLOW_ACTION_TYPE_COUNT:
8987                 {
8988                         uint32_t counter;
8989
8990                         counter = flow_dv_translate_create_counter(dev,
8991                                         dev_flow, sub_actions->conf, 0);
8992                         if (!counter)
8993                                 return rte_flow_error_set
8994                                                 (error, rte_errno,
8995                                                  RTE_FLOW_ERROR_TYPE_ACTION,
8996                                                  NULL,
8997                                                  "cannot create counter"
8998                                                  " object.");
8999                         sample_idx->cnt = counter;
9000                         sample_act->dr_cnt_action =
9001                                   (flow_dv_counter_get_by_idx(dev,
9002                                   counter, NULL))->action;
9003                         sample_actions[sample_act->actions_num++] =
9004                                                 sample_act->dr_cnt_action;
9005                         action_flags |= MLX5_FLOW_ACTION_COUNT;
9006                         break;
9007                 }
9008                 case RTE_FLOW_ACTION_TYPE_PORT_ID:
9009                 {
9010                         struct mlx5_flow_dv_port_id_action_resource
9011                                         port_id_resource;
9012                         uint32_t port_id = 0;
9013
9014                         memset(&port_id_resource, 0, sizeof(port_id_resource));
9015                         /* Save the port id resource before sample */
9016                         pre_rix = dev_flow->handle->rix_port_id_action;
9017                         pre_r = dev_flow->dv.port_id_action;
9018                         if (flow_dv_translate_action_port_id(dev, sub_actions,
9019                                                              &port_id, error))
9020                                 return -rte_errno;
9021                         port_id_resource.port_id = port_id;
9022                         if (flow_dv_port_id_action_resource_register
9023                             (dev, &port_id_resource, dev_flow, error))
9024                                 return -rte_errno;
9025                         sample_act->dr_port_id_action =
9026                                 dev_flow->dv.port_id_action->action;
9027                         sample_idx->rix_port_id_action =
9028                                 dev_flow->handle->rix_port_id_action;
9029                         sample_actions[sample_act->actions_num++] =
9030                                                 sample_act->dr_port_id_action;
9031                         /* Recover the port id resource after sample */
9032                         dev_flow->dv.port_id_action = pre_r;
9033                         dev_flow->handle->rix_port_id_action = pre_rix;
9034                         (*num_of_dest)++;
9035                         action_flags |= MLX5_FLOW_ACTION_PORT_ID;
9036                         break;
9037                 }
9038                 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
9039                         /* Save the encap resource before sample */
9040                         pre_rix = dev_flow->handle->dvh.rix_encap_decap;
9041                         pre_r = dev_flow->dv.encap_decap;
9042                         if (flow_dv_create_action_l2_encap(dev, sub_actions,
9043                                                            dev_flow,
9044                                                            attr->transfer,
9045                                                            error))
9046                                 return -rte_errno;
9047                         sample_act->dr_encap_action =
9048                                 dev_flow->dv.encap_decap->action;
9049                         sample_idx->rix_encap_decap =
9050                                 dev_flow->handle->dvh.rix_encap_decap;
9051                         sample_actions[sample_act->actions_num++] =
9052                                                 sample_act->dr_encap_action;
9053                         /* Recover the encap resource after sample */
9054                         dev_flow->dv.encap_decap = pre_r;
9055                         dev_flow->handle->dvh.rix_encap_decap = pre_rix;
9056                         action_flags |= MLX5_FLOW_ACTION_ENCAP;
9057                         break;
9058                 default:
9059                         return rte_flow_error_set(error, EINVAL,
9060                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
9061                                 NULL,
9062                                 "Not support for sampler action");
9063                 }
9064         }
9065         sample_act->action_flags = action_flags;
9066         res->ft_id = dev_flow->dv.group;
9067         if (attr->transfer) {
9068                 union {
9069                         uint32_t action_in[MLX5_ST_SZ_DW(set_action_in)];
9070                         uint64_t set_action;
9071                 } action_ctx = { .set_action = 0 };
9072
9073                 res->ft_type = MLX5DV_FLOW_TABLE_TYPE_FDB;
9074                 MLX5_SET(set_action_in, action_ctx.action_in, action_type,
9075                          MLX5_MODIFICATION_TYPE_SET);
9076                 MLX5_SET(set_action_in, action_ctx.action_in, field,
9077                          MLX5_MODI_META_REG_C_0);
9078                 MLX5_SET(set_action_in, action_ctx.action_in, data,
9079                          priv->vport_meta_tag);
9080                 res->set_action = action_ctx.set_action;
9081         } else if (attr->ingress) {
9082                 res->ft_type = MLX5DV_FLOW_TABLE_TYPE_NIC_RX;
9083         }
9084         return 0;
9085 }
9086
9087 /**
9088  * Convert Sample action to DV specification.
9089  *
9090  * @param[in] dev
9091  *   Pointer to rte_eth_dev structure.
9092  * @param[in, out] dev_flow
9093  *   Pointer to the mlx5_flow.
9094  * @param[in] attr
9095  *   Pointer to the flow attributes.
9096  * @param[in] num_of_dest
9097  *   The num of destination.
9098  * @param[in, out] res
9099  *   Pointer to sample resource.
9100  * @param[in, out] mdest_res
9101  *   Pointer to destination array resource.
9102  * @param[in] sample_actions
9103  *   Pointer to sample path actions list.
9104  * @param[in] action_flags
9105  *   Holds the actions detected until now.
9106  * @param[out] error
9107  *   Pointer to the error structure.
9108  *
9109  * @return
9110  *   0 on success, a negative errno value otherwise and rte_errno is set.
9111  */
9112 static int
9113 flow_dv_create_action_sample(struct rte_eth_dev *dev,
9114                              struct mlx5_flow *dev_flow,
9115                              const struct rte_flow_attr *attr,
9116                              uint32_t num_of_dest,
9117                              struct mlx5_flow_dv_sample_resource *res,
9118                              struct mlx5_flow_dv_dest_array_resource *mdest_res,
9119                              void **sample_actions,
9120                              uint64_t action_flags,
9121                              struct rte_flow_error *error)
9122 {
9123         /* update normal path action resource into last index of array */
9124         uint32_t dest_index = MLX5_MAX_DEST_NUM - 1;
9125         struct mlx5_flow_sub_actions_list *sample_act =
9126                                         &mdest_res->sample_act[dest_index];
9127         struct mlx5_flow_workspace *wks = mlx5_flow_get_thread_workspace();
9128         struct mlx5_flow_rss_desc *rss_desc;
9129         uint32_t normal_idx = 0;
9130         struct mlx5_hrxq *hrxq;
9131         uint32_t hrxq_idx;
9132
9133         MLX5_ASSERT(wks);
9134         rss_desc = &wks->rss_desc[!!wks->flow_nested_idx];
9135         if (num_of_dest > 1) {
9136                 if (sample_act->action_flags & MLX5_FLOW_ACTION_QUEUE) {
9137                         /* Handle QP action for mirroring */
9138                         hrxq = flow_dv_handle_rx_queue(dev, dev_flow,
9139                                                        rss_desc, &hrxq_idx);
9140                         if (!hrxq)
9141                                 return rte_flow_error_set
9142                                      (error, rte_errno,
9143                                       RTE_FLOW_ERROR_TYPE_ACTION,
9144                                       NULL,
9145                                       "cannot create rx queue");
9146                         normal_idx++;
9147                         mdest_res->sample_idx[dest_index].rix_hrxq = hrxq_idx;
9148                         sample_act->dr_queue_action = hrxq->action;
9149                         if (action_flags & MLX5_FLOW_ACTION_MARK)
9150                                 dev_flow->handle->rix_hrxq = hrxq_idx;
9151                         dev_flow->handle->fate_action = MLX5_FLOW_FATE_QUEUE;
9152                 }
9153                 if (sample_act->action_flags & MLX5_FLOW_ACTION_ENCAP) {
9154                         normal_idx++;
9155                         mdest_res->sample_idx[dest_index].rix_encap_decap =
9156                                 dev_flow->handle->dvh.rix_encap_decap;
9157                         sample_act->dr_encap_action =
9158                                 dev_flow->dv.encap_decap->action;
9159                 }
9160                 if (sample_act->action_flags & MLX5_FLOW_ACTION_PORT_ID) {
9161                         normal_idx++;
9162                         mdest_res->sample_idx[dest_index].rix_port_id_action =
9163                                 dev_flow->handle->rix_port_id_action;
9164                         sample_act->dr_port_id_action =
9165                                 dev_flow->dv.port_id_action->action;
9166                 }
9167                 sample_act->actions_num = normal_idx;
9168                 /* update sample action resource into first index of array */
9169                 mdest_res->ft_type = res->ft_type;
9170                 memcpy(&mdest_res->sample_idx[0], &res->sample_idx,
9171                                 sizeof(struct mlx5_flow_sub_actions_idx));
9172                 memcpy(&mdest_res->sample_act[0], &res->sample_act,
9173                                 sizeof(struct mlx5_flow_sub_actions_list));
9174                 mdest_res->num_of_dest = num_of_dest;
9175                 if (flow_dv_dest_array_resource_register(dev, attr, mdest_res,
9176                                                          dev_flow, error))
9177                         return rte_flow_error_set(error, EINVAL,
9178                                                   RTE_FLOW_ERROR_TYPE_ACTION,
9179                                                   NULL, "can't create sample "
9180                                                   "action");
9181         } else {
9182                 if (flow_dv_sample_resource_register(dev, attr, res, dev_flow,
9183                                                      sample_actions, error))
9184                         return rte_flow_error_set(error, EINVAL,
9185                                                   RTE_FLOW_ERROR_TYPE_ACTION,
9186                                                   NULL,
9187                                                   "can't create sample action");
9188         }
9189         return 0;
9190 }
9191
9192 /**
9193  * Fill the flow with DV spec, lock free
9194  * (mutex should be acquired by caller).
9195  *
9196  * @param[in] dev
9197  *   Pointer to rte_eth_dev structure.
9198  * @param[in, out] dev_flow
9199  *   Pointer to the sub flow.
9200  * @param[in] attr
9201  *   Pointer to the flow attributes.
9202  * @param[in] items
9203  *   Pointer to the list of items.
9204  * @param[in] actions
9205  *   Pointer to the list of actions.
9206  * @param[out] error
9207  *   Pointer to the error structure.
9208  *
9209  * @return
9210  *   0 on success, a negative errno value otherwise and rte_errno is set.
9211  */
9212 static int
9213 __flow_dv_translate(struct rte_eth_dev *dev,
9214                     struct mlx5_flow *dev_flow,
9215                     const struct rte_flow_attr *attr,
9216                     const struct rte_flow_item items[],
9217                     const struct rte_flow_action actions[],
9218                     struct rte_flow_error *error)
9219 {
9220         struct mlx5_priv *priv = dev->data->dev_private;
9221         struct mlx5_dev_config *dev_conf = &priv->config;
9222         struct rte_flow *flow = dev_flow->flow;
9223         struct mlx5_flow_handle *handle = dev_flow->handle;
9224         struct mlx5_flow_workspace *wks = mlx5_flow_get_thread_workspace();
9225         struct mlx5_flow_rss_desc *rss_desc;
9226         uint64_t item_flags = 0;
9227         uint64_t last_item = 0;
9228         uint64_t action_flags = 0;
9229         uint64_t priority = attr->priority;
9230         struct mlx5_flow_dv_matcher matcher = {
9231                 .mask = {
9232                         .size = sizeof(matcher.mask.buf) -
9233                                 MLX5_ST_SZ_BYTES(fte_match_set_misc4),
9234                 },
9235         };
9236         int actions_n = 0;
9237         bool actions_end = false;
9238         union {
9239                 struct mlx5_flow_dv_modify_hdr_resource res;
9240                 uint8_t len[sizeof(struct mlx5_flow_dv_modify_hdr_resource) +
9241                             sizeof(struct mlx5_modification_cmd) *
9242                             (MLX5_MAX_MODIFY_NUM + 1)];
9243         } mhdr_dummy;
9244         struct mlx5_flow_dv_modify_hdr_resource *mhdr_res = &mhdr_dummy.res;
9245         const struct rte_flow_action_count *count = NULL;
9246         const struct rte_flow_action_age *age = NULL;
9247         union flow_dv_attr flow_attr = { .attr = 0 };
9248         uint32_t tag_be;
9249         union mlx5_flow_tbl_key tbl_key;
9250         uint32_t modify_action_position = UINT32_MAX;
9251         void *match_mask = matcher.mask.buf;
9252         void *match_value = dev_flow->dv.value.buf;
9253         uint8_t next_protocol = 0xff;
9254         struct rte_vlan_hdr vlan = { 0 };
9255         struct mlx5_flow_dv_dest_array_resource mdest_res;
9256         struct mlx5_flow_dv_sample_resource sample_res;
9257         void *sample_actions[MLX5_DV_MAX_NUMBER_OF_ACTIONS] = {0};
9258         struct mlx5_flow_sub_actions_list *sample_act;
9259         uint32_t sample_act_pos = UINT32_MAX;
9260         uint32_t num_of_dest = 0;
9261         int tmp_actions_n = 0;
9262         uint32_t table;
9263         int ret = 0;
9264         const struct mlx5_flow_tunnel *tunnel;
9265         struct flow_grp_info grp_info = {
9266                 .external = !!dev_flow->external,
9267                 .transfer = !!attr->transfer,
9268                 .fdb_def_rule = !!priv->fdb_def_rule,
9269         };
9270
9271         MLX5_ASSERT(wks);
9272         rss_desc = &wks->rss_desc[!!wks->flow_nested_idx];
9273         memset(&mdest_res, 0, sizeof(struct mlx5_flow_dv_dest_array_resource));
9274         memset(&sample_res, 0, sizeof(struct mlx5_flow_dv_sample_resource));
9275         mhdr_res->ft_type = attr->egress ? MLX5DV_FLOW_TABLE_TYPE_NIC_TX :
9276                                            MLX5DV_FLOW_TABLE_TYPE_NIC_RX;
9277         /* update normal path action resource into last index of array */
9278         sample_act = &mdest_res.sample_act[MLX5_MAX_DEST_NUM - 1];
9279         tunnel = is_flow_tunnel_match_rule(dev, attr, items, actions) ?
9280                  flow_items_to_tunnel(items) :
9281                  is_flow_tunnel_steer_rule(dev, attr, items, actions) ?
9282                  flow_actions_to_tunnel(actions) :
9283                  dev_flow->tunnel ? dev_flow->tunnel : NULL;
9284         mhdr_res->ft_type = attr->egress ? MLX5DV_FLOW_TABLE_TYPE_NIC_TX :
9285                                            MLX5DV_FLOW_TABLE_TYPE_NIC_RX;
9286         grp_info.std_tbl_fix = tunnel_use_standard_attr_group_translate
9287                                 (dev, tunnel, attr, items, actions);
9288         ret = mlx5_flow_group_to_table(dev, tunnel, attr->group, &table,
9289                                        grp_info, error);
9290         if (ret)
9291                 return ret;
9292         dev_flow->dv.group = table;
9293         if (attr->transfer)
9294                 mhdr_res->ft_type = MLX5DV_FLOW_TABLE_TYPE_FDB;
9295         if (priority == MLX5_FLOW_PRIO_RSVD)
9296                 priority = dev_conf->flow_prio - 1;
9297         /* number of actions must be set to 0 in case of dirty stack. */
9298         mhdr_res->actions_num = 0;
9299         if (is_flow_tunnel_match_rule(dev, attr, items, actions)) {
9300                 /*
9301                  * do not add decap action if match rule drops packet
9302                  * HW rejects rules with decap & drop
9303                  */
9304                 bool add_decap = true;
9305                 const struct rte_flow_action *ptr = actions;
9306                 struct mlx5_flow_tbl_resource *tbl;
9307
9308                 for (; ptr->type != RTE_FLOW_ACTION_TYPE_END; ptr++) {
9309                         if (ptr->type == RTE_FLOW_ACTION_TYPE_DROP) {
9310                                 add_decap = false;
9311                                 break;
9312                         }
9313                 }
9314                 if (add_decap) {
9315                         if (flow_dv_create_action_l2_decap(dev, dev_flow,
9316                                                            attr->transfer,
9317                                                            error))
9318                                 return -rte_errno;
9319                         dev_flow->dv.actions[actions_n++] =
9320                                         dev_flow->dv.encap_decap->action;
9321                         action_flags |= MLX5_FLOW_ACTION_DECAP;
9322                 }
9323                 /*
9324                  * bind table_id with <group, table> for tunnel match rule.
9325                  * Tunnel set rule establishes that bind in JUMP action handler.
9326                  * Required for scenario when application creates tunnel match
9327                  * rule before tunnel set rule.
9328                  */
9329                 tbl = flow_dv_tbl_resource_get(dev, table, attr->egress,
9330                                                attr->transfer,
9331                                                !!dev_flow->external, tunnel,
9332                                                attr->group, 0, error);
9333                 if (!tbl)
9334                         return rte_flow_error_set
9335                                (error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
9336                                actions, "cannot register tunnel group");
9337         }
9338         for (; !actions_end ; actions++) {
9339                 const struct rte_flow_action_queue *queue;
9340                 const struct rte_flow_action_rss *rss;
9341                 const struct rte_flow_action *action = actions;
9342                 const uint8_t *rss_key;
9343                 const struct rte_flow_action_meter *mtr;
9344                 struct mlx5_flow_tbl_resource *tbl;
9345                 uint32_t port_id = 0;
9346                 struct mlx5_flow_dv_port_id_action_resource port_id_resource;
9347                 int action_type = actions->type;
9348                 const struct rte_flow_action *found_action = NULL;
9349                 struct mlx5_flow_meter *fm = NULL;
9350                 uint32_t jump_group = 0;
9351
9352                 if (!mlx5_flow_os_action_supported(action_type))
9353                         return rte_flow_error_set(error, ENOTSUP,
9354                                                   RTE_FLOW_ERROR_TYPE_ACTION,
9355                                                   actions,
9356                                                   "action not supported");
9357                 switch (action_type) {
9358                 case MLX5_RTE_FLOW_ACTION_TYPE_TUNNEL_SET:
9359                         action_flags |= MLX5_FLOW_ACTION_TUNNEL_SET;
9360                         break;
9361                 case RTE_FLOW_ACTION_TYPE_VOID:
9362                         break;
9363                 case RTE_FLOW_ACTION_TYPE_PORT_ID:
9364                         if (flow_dv_translate_action_port_id(dev, action,
9365                                                              &port_id, error))
9366                                 return -rte_errno;
9367                         port_id_resource.port_id = port_id;
9368                         MLX5_ASSERT(!handle->rix_port_id_action);
9369                         if (flow_dv_port_id_action_resource_register
9370                             (dev, &port_id_resource, dev_flow, error))
9371                                 return -rte_errno;
9372                         dev_flow->dv.actions[actions_n++] =
9373                                         dev_flow->dv.port_id_action->action;
9374                         action_flags |= MLX5_FLOW_ACTION_PORT_ID;
9375                         dev_flow->handle->fate_action = MLX5_FLOW_FATE_PORT_ID;
9376                         sample_act->action_flags |= MLX5_FLOW_ACTION_PORT_ID;
9377                         num_of_dest++;
9378                         break;
9379                 case RTE_FLOW_ACTION_TYPE_FLAG:
9380                         action_flags |= MLX5_FLOW_ACTION_FLAG;
9381                         dev_flow->handle->mark = 1;
9382                         if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
9383                                 struct rte_flow_action_mark mark = {
9384                                         .id = MLX5_FLOW_MARK_DEFAULT,
9385                                 };
9386
9387                                 if (flow_dv_convert_action_mark(dev, &mark,
9388                                                                 mhdr_res,
9389                                                                 error))
9390                                         return -rte_errno;
9391                                 action_flags |= MLX5_FLOW_ACTION_MARK_EXT;
9392                                 break;
9393                         }
9394                         tag_be = mlx5_flow_mark_set(MLX5_FLOW_MARK_DEFAULT);
9395                         /*
9396                          * Only one FLAG or MARK is supported per device flow
9397                          * right now. So the pointer to the tag resource must be
9398                          * zero before the register process.
9399                          */
9400                         MLX5_ASSERT(!handle->dvh.rix_tag);
9401                         if (flow_dv_tag_resource_register(dev, tag_be,
9402                                                           dev_flow, error))
9403                                 return -rte_errno;
9404                         MLX5_ASSERT(dev_flow->dv.tag_resource);
9405                         dev_flow->dv.actions[actions_n++] =
9406                                         dev_flow->dv.tag_resource->action;
9407                         break;
9408                 case RTE_FLOW_ACTION_TYPE_MARK:
9409                         action_flags |= MLX5_FLOW_ACTION_MARK;
9410                         dev_flow->handle->mark = 1;
9411                         if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
9412                                 const struct rte_flow_action_mark *mark =
9413                                         (const struct rte_flow_action_mark *)
9414                                                 actions->conf;
9415
9416                                 if (flow_dv_convert_action_mark(dev, mark,
9417                                                                 mhdr_res,
9418                                                                 error))
9419                                         return -rte_errno;
9420                                 action_flags |= MLX5_FLOW_ACTION_MARK_EXT;
9421                                 break;
9422                         }
9423                         /* Fall-through */
9424                 case MLX5_RTE_FLOW_ACTION_TYPE_MARK:
9425                         /* Legacy (non-extensive) MARK action. */
9426                         tag_be = mlx5_flow_mark_set
9427                               (((const struct rte_flow_action_mark *)
9428                                (actions->conf))->id);
9429                         MLX5_ASSERT(!handle->dvh.rix_tag);
9430                         if (flow_dv_tag_resource_register(dev, tag_be,
9431                                                           dev_flow, error))
9432                                 return -rte_errno;
9433                         MLX5_ASSERT(dev_flow->dv.tag_resource);
9434                         dev_flow->dv.actions[actions_n++] =
9435                                         dev_flow->dv.tag_resource->action;
9436                         break;
9437                 case RTE_FLOW_ACTION_TYPE_SET_META:
9438                         if (flow_dv_convert_action_set_meta
9439                                 (dev, mhdr_res, attr,
9440                                  (const struct rte_flow_action_set_meta *)
9441                                   actions->conf, error))
9442                                 return -rte_errno;
9443                         action_flags |= MLX5_FLOW_ACTION_SET_META;
9444                         break;
9445                 case RTE_FLOW_ACTION_TYPE_SET_TAG:
9446                         if (flow_dv_convert_action_set_tag
9447                                 (dev, mhdr_res,
9448                                  (const struct rte_flow_action_set_tag *)
9449                                   actions->conf, error))
9450                                 return -rte_errno;
9451                         action_flags |= MLX5_FLOW_ACTION_SET_TAG;
9452                         break;
9453                 case RTE_FLOW_ACTION_TYPE_DROP:
9454                         action_flags |= MLX5_FLOW_ACTION_DROP;
9455                         dev_flow->handle->fate_action = MLX5_FLOW_FATE_DROP;
9456                         break;
9457                 case RTE_FLOW_ACTION_TYPE_QUEUE:
9458                         queue = actions->conf;
9459                         rss_desc->queue_num = 1;
9460                         rss_desc->queue[0] = queue->index;
9461                         action_flags |= MLX5_FLOW_ACTION_QUEUE;
9462                         dev_flow->handle->fate_action = MLX5_FLOW_FATE_QUEUE;
9463                         sample_act->action_flags |= MLX5_FLOW_ACTION_QUEUE;
9464                         num_of_dest++;
9465                         break;
9466                 case RTE_FLOW_ACTION_TYPE_RSS:
9467                         rss = actions->conf;
9468                         memcpy(rss_desc->queue, rss->queue,
9469                                rss->queue_num * sizeof(uint16_t));
9470                         rss_desc->queue_num = rss->queue_num;
9471                         /* NULL RSS key indicates default RSS key. */
9472                         rss_key = !rss->key ? rss_hash_default_key : rss->key;
9473                         memcpy(rss_desc->key, rss_key, MLX5_RSS_HASH_KEY_LEN);
9474                         /*
9475                          * rss->level and rss.types should be set in advance
9476                          * when expanding items for RSS.
9477                          */
9478                         action_flags |= MLX5_FLOW_ACTION_RSS;
9479                         dev_flow->handle->fate_action = MLX5_FLOW_FATE_QUEUE;
9480                         break;
9481                 case RTE_FLOW_ACTION_TYPE_AGE:
9482                 case RTE_FLOW_ACTION_TYPE_COUNT:
9483                         if (!dev_conf->devx) {
9484                                 return rte_flow_error_set
9485                                               (error, ENOTSUP,
9486                                                RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
9487                                                NULL,
9488                                                "count action not supported");
9489                         }
9490                         /* Save information first, will apply later. */
9491                         if (actions->type == RTE_FLOW_ACTION_TYPE_COUNT)
9492                                 count = action->conf;
9493                         else
9494                                 age = action->conf;
9495                         action_flags |= MLX5_FLOW_ACTION_COUNT;
9496                         break;
9497                 case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
9498                         dev_flow->dv.actions[actions_n++] =
9499                                                 priv->sh->pop_vlan_action;
9500                         action_flags |= MLX5_FLOW_ACTION_OF_POP_VLAN;
9501                         break;
9502                 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
9503                         if (!(action_flags &
9504                               MLX5_FLOW_ACTION_OF_SET_VLAN_VID))
9505                                 flow_dev_get_vlan_info_from_items(items, &vlan);
9506                         vlan.eth_proto = rte_be_to_cpu_16
9507                              ((((const struct rte_flow_action_of_push_vlan *)
9508                                                    actions->conf)->ethertype));
9509                         found_action = mlx5_flow_find_action
9510                                         (actions + 1,
9511                                          RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID);
9512                         if (found_action)
9513                                 mlx5_update_vlan_vid_pcp(found_action, &vlan);
9514                         found_action = mlx5_flow_find_action
9515                                         (actions + 1,
9516                                          RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP);
9517                         if (found_action)
9518                                 mlx5_update_vlan_vid_pcp(found_action, &vlan);
9519                         if (flow_dv_create_action_push_vlan
9520                                             (dev, attr, &vlan, dev_flow, error))
9521                                 return -rte_errno;
9522                         dev_flow->dv.actions[actions_n++] =
9523                                         dev_flow->dv.push_vlan_res->action;
9524                         action_flags |= MLX5_FLOW_ACTION_OF_PUSH_VLAN;
9525                         break;
9526                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
9527                         /* of_vlan_push action handled this action */
9528                         MLX5_ASSERT(action_flags &
9529                                     MLX5_FLOW_ACTION_OF_PUSH_VLAN);
9530                         break;
9531                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
9532                         if (action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN)
9533                                 break;
9534                         flow_dev_get_vlan_info_from_items(items, &vlan);
9535                         mlx5_update_vlan_vid_pcp(actions, &vlan);
9536                         /* If no VLAN push - this is a modify header action */
9537                         if (flow_dv_convert_action_modify_vlan_vid
9538                                                 (mhdr_res, actions, error))
9539                                 return -rte_errno;
9540                         action_flags |= MLX5_FLOW_ACTION_OF_SET_VLAN_VID;
9541                         break;
9542                 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
9543                 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
9544                         if (flow_dv_create_action_l2_encap(dev, actions,
9545                                                            dev_flow,
9546                                                            attr->transfer,
9547                                                            error))
9548                                 return -rte_errno;
9549                         dev_flow->dv.actions[actions_n++] =
9550                                         dev_flow->dv.encap_decap->action;
9551                         action_flags |= MLX5_FLOW_ACTION_ENCAP;
9552                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
9553                                 sample_act->action_flags |=
9554                                                         MLX5_FLOW_ACTION_ENCAP;
9555                         break;
9556                 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
9557                 case RTE_FLOW_ACTION_TYPE_NVGRE_DECAP:
9558                         if (flow_dv_create_action_l2_decap(dev, dev_flow,
9559                                                            attr->transfer,
9560                                                            error))
9561                                 return -rte_errno;
9562                         dev_flow->dv.actions[actions_n++] =
9563                                         dev_flow->dv.encap_decap->action;
9564                         action_flags |= MLX5_FLOW_ACTION_DECAP;
9565                         break;
9566                 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
9567                         /* Handle encap with preceding decap. */
9568                         if (action_flags & MLX5_FLOW_ACTION_DECAP) {
9569                                 if (flow_dv_create_action_raw_encap
9570                                         (dev, actions, dev_flow, attr, error))
9571                                         return -rte_errno;
9572                                 dev_flow->dv.actions[actions_n++] =
9573                                         dev_flow->dv.encap_decap->action;
9574                         } else {
9575                                 /* Handle encap without preceding decap. */
9576                                 if (flow_dv_create_action_l2_encap
9577                                     (dev, actions, dev_flow, attr->transfer,
9578                                      error))
9579                                         return -rte_errno;
9580                                 dev_flow->dv.actions[actions_n++] =
9581                                         dev_flow->dv.encap_decap->action;
9582                         }
9583                         action_flags |= MLX5_FLOW_ACTION_ENCAP;
9584                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
9585                                 sample_act->action_flags |=
9586                                                         MLX5_FLOW_ACTION_ENCAP;
9587                         break;
9588                 case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
9589                         while ((++action)->type == RTE_FLOW_ACTION_TYPE_VOID)
9590                                 ;
9591                         if (action->type != RTE_FLOW_ACTION_TYPE_RAW_ENCAP) {
9592                                 if (flow_dv_create_action_l2_decap
9593                                     (dev, dev_flow, attr->transfer, error))
9594                                         return -rte_errno;
9595                                 dev_flow->dv.actions[actions_n++] =
9596                                         dev_flow->dv.encap_decap->action;
9597                         }
9598                         /* If decap is followed by encap, handle it at encap. */
9599                         action_flags |= MLX5_FLOW_ACTION_DECAP;
9600                         break;
9601                 case RTE_FLOW_ACTION_TYPE_JUMP:
9602                         jump_group = ((const struct rte_flow_action_jump *)
9603                                                         action->conf)->group;
9604                         grp_info.std_tbl_fix = 0;
9605                         ret = mlx5_flow_group_to_table(dev, tunnel,
9606                                                        jump_group,
9607                                                        &table,
9608                                                        grp_info, error);
9609                         if (ret)
9610                                 return ret;
9611                         tbl = flow_dv_tbl_resource_get(dev, table, attr->egress,
9612                                                        attr->transfer,
9613                                                        !!dev_flow->external,
9614                                                        tunnel, jump_group, 0,
9615                                                        error);
9616                         if (!tbl)
9617                                 return rte_flow_error_set
9618                                                 (error, errno,
9619                                                  RTE_FLOW_ERROR_TYPE_ACTION,
9620                                                  NULL,
9621                                                  "cannot create jump action.");
9622                         if (flow_dv_jump_tbl_resource_register
9623                             (dev, tbl, dev_flow, error)) {
9624                                 flow_dv_tbl_resource_release(dev, tbl);
9625                                 return rte_flow_error_set
9626                                                 (error, errno,
9627                                                  RTE_FLOW_ERROR_TYPE_ACTION,
9628                                                  NULL,
9629                                                  "cannot create jump action.");
9630                         }
9631                         dev_flow->dv.actions[actions_n++] =
9632                                         dev_flow->dv.jump->action;
9633                         action_flags |= MLX5_FLOW_ACTION_JUMP;
9634                         dev_flow->handle->fate_action = MLX5_FLOW_FATE_JUMP;
9635                         break;
9636                 case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
9637                 case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
9638                         if (flow_dv_convert_action_modify_mac
9639                                         (mhdr_res, actions, error))
9640                                 return -rte_errno;
9641                         action_flags |= actions->type ==
9642                                         RTE_FLOW_ACTION_TYPE_SET_MAC_SRC ?
9643                                         MLX5_FLOW_ACTION_SET_MAC_SRC :
9644                                         MLX5_FLOW_ACTION_SET_MAC_DST;
9645                         break;
9646                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
9647                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
9648                         if (flow_dv_convert_action_modify_ipv4
9649                                         (mhdr_res, actions, error))
9650                                 return -rte_errno;
9651                         action_flags |= actions->type ==
9652                                         RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC ?
9653                                         MLX5_FLOW_ACTION_SET_IPV4_SRC :
9654                                         MLX5_FLOW_ACTION_SET_IPV4_DST;
9655                         break;
9656                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
9657                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
9658                         if (flow_dv_convert_action_modify_ipv6
9659                                         (mhdr_res, actions, error))
9660                                 return -rte_errno;
9661                         action_flags |= actions->type ==
9662                                         RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC ?
9663                                         MLX5_FLOW_ACTION_SET_IPV6_SRC :
9664                                         MLX5_FLOW_ACTION_SET_IPV6_DST;
9665                         break;
9666                 case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
9667                 case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
9668                         if (flow_dv_convert_action_modify_tp
9669                                         (mhdr_res, actions, items,
9670                                          &flow_attr, dev_flow, !!(action_flags &
9671                                          MLX5_FLOW_ACTION_DECAP), error))
9672                                 return -rte_errno;
9673                         action_flags |= actions->type ==
9674                                         RTE_FLOW_ACTION_TYPE_SET_TP_SRC ?
9675                                         MLX5_FLOW_ACTION_SET_TP_SRC :
9676                                         MLX5_FLOW_ACTION_SET_TP_DST;
9677                         break;
9678                 case RTE_FLOW_ACTION_TYPE_DEC_TTL:
9679                         if (flow_dv_convert_action_modify_dec_ttl
9680                                         (mhdr_res, items, &flow_attr, dev_flow,
9681                                          !!(action_flags &
9682                                          MLX5_FLOW_ACTION_DECAP), error))
9683                                 return -rte_errno;
9684                         action_flags |= MLX5_FLOW_ACTION_DEC_TTL;
9685                         break;
9686                 case RTE_FLOW_ACTION_TYPE_SET_TTL:
9687                         if (flow_dv_convert_action_modify_ttl
9688                                         (mhdr_res, actions, items, &flow_attr,
9689                                          dev_flow, !!(action_flags &
9690                                          MLX5_FLOW_ACTION_DECAP), error))
9691                                 return -rte_errno;
9692                         action_flags |= MLX5_FLOW_ACTION_SET_TTL;
9693                         break;
9694                 case RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ:
9695                 case RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ:
9696                         if (flow_dv_convert_action_modify_tcp_seq
9697                                         (mhdr_res, actions, error))
9698                                 return -rte_errno;
9699                         action_flags |= actions->type ==
9700                                         RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ ?
9701                                         MLX5_FLOW_ACTION_INC_TCP_SEQ :
9702                                         MLX5_FLOW_ACTION_DEC_TCP_SEQ;
9703                         break;
9704
9705                 case RTE_FLOW_ACTION_TYPE_INC_TCP_ACK:
9706                 case RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK:
9707                         if (flow_dv_convert_action_modify_tcp_ack
9708                                         (mhdr_res, actions, error))
9709                                 return -rte_errno;
9710                         action_flags |= actions->type ==
9711                                         RTE_FLOW_ACTION_TYPE_INC_TCP_ACK ?
9712                                         MLX5_FLOW_ACTION_INC_TCP_ACK :
9713                                         MLX5_FLOW_ACTION_DEC_TCP_ACK;
9714                         break;
9715                 case MLX5_RTE_FLOW_ACTION_TYPE_TAG:
9716                         if (flow_dv_convert_action_set_reg
9717                                         (mhdr_res, actions, error))
9718                                 return -rte_errno;
9719                         action_flags |= MLX5_FLOW_ACTION_SET_TAG;
9720                         break;
9721                 case MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG:
9722                         if (flow_dv_convert_action_copy_mreg
9723                                         (dev, mhdr_res, actions, error))
9724                                 return -rte_errno;
9725                         action_flags |= MLX5_FLOW_ACTION_SET_TAG;
9726                         break;
9727                 case MLX5_RTE_FLOW_ACTION_TYPE_DEFAULT_MISS:
9728                         action_flags |= MLX5_FLOW_ACTION_DEFAULT_MISS;
9729                         dev_flow->handle->fate_action =
9730                                         MLX5_FLOW_FATE_DEFAULT_MISS;
9731                         break;
9732                 case RTE_FLOW_ACTION_TYPE_METER:
9733                         mtr = actions->conf;
9734                         if (!flow->meter) {
9735                                 fm = mlx5_flow_meter_attach(priv, mtr->mtr_id,
9736                                                             attr, error);
9737                                 if (!fm)
9738                                         return rte_flow_error_set(error,
9739                                                 rte_errno,
9740                                                 RTE_FLOW_ERROR_TYPE_ACTION,
9741                                                 NULL,
9742                                                 "meter not found "
9743                                                 "or invalid parameters");
9744                                 flow->meter = fm->idx;
9745                         }
9746                         /* Set the meter action. */
9747                         if (!fm) {
9748                                 fm = mlx5_ipool_get(priv->sh->ipool
9749                                                 [MLX5_IPOOL_MTR], flow->meter);
9750                                 if (!fm)
9751                                         return rte_flow_error_set(error,
9752                                                 rte_errno,
9753                                                 RTE_FLOW_ERROR_TYPE_ACTION,
9754                                                 NULL,
9755                                                 "meter not found "
9756                                                 "or invalid parameters");
9757                         }
9758                         dev_flow->dv.actions[actions_n++] =
9759                                 fm->mfts->meter_action;
9760                         action_flags |= MLX5_FLOW_ACTION_METER;
9761                         break;
9762                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DSCP:
9763                         if (flow_dv_convert_action_modify_ipv4_dscp(mhdr_res,
9764                                                               actions, error))
9765                                 return -rte_errno;
9766                         action_flags |= MLX5_FLOW_ACTION_SET_IPV4_DSCP;
9767                         break;
9768                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DSCP:
9769                         if (flow_dv_convert_action_modify_ipv6_dscp(mhdr_res,
9770                                                               actions, error))
9771                                 return -rte_errno;
9772                         action_flags |= MLX5_FLOW_ACTION_SET_IPV6_DSCP;
9773                         break;
9774                 case RTE_FLOW_ACTION_TYPE_SAMPLE:
9775                         sample_act_pos = actions_n;
9776                         ret = flow_dv_translate_action_sample(dev,
9777                                                               actions,
9778                                                               dev_flow, attr,
9779                                                               &num_of_dest,
9780                                                               sample_actions,
9781                                                               &sample_res,
9782                                                               error);
9783                         if (ret < 0)
9784                                 return ret;
9785                         actions_n++;
9786                         action_flags |= MLX5_FLOW_ACTION_SAMPLE;
9787                         /* put encap action into group if work with port id */
9788                         if ((action_flags & MLX5_FLOW_ACTION_ENCAP) &&
9789                             (action_flags & MLX5_FLOW_ACTION_PORT_ID))
9790                                 sample_act->action_flags |=
9791                                                         MLX5_FLOW_ACTION_ENCAP;
9792                         break;
9793                 case RTE_FLOW_ACTION_TYPE_END:
9794                         actions_end = true;
9795                         if (mhdr_res->actions_num) {
9796                                 /* create modify action if needed. */
9797                                 if (flow_dv_modify_hdr_resource_register
9798                                         (dev, mhdr_res, dev_flow, error))
9799                                         return -rte_errno;
9800                                 dev_flow->dv.actions[modify_action_position] =
9801                                         handle->dvh.modify_hdr->action;
9802                         }
9803                         if (action_flags & MLX5_FLOW_ACTION_COUNT) {
9804                                 flow->counter =
9805                                         flow_dv_translate_create_counter(dev,
9806                                                 dev_flow, count, age);
9807
9808                                 if (!flow->counter)
9809                                         return rte_flow_error_set
9810                                                 (error, rte_errno,
9811                                                 RTE_FLOW_ERROR_TYPE_ACTION,
9812                                                 NULL,
9813                                                 "cannot create counter"
9814                                                 " object.");
9815                                 dev_flow->dv.actions[actions_n] =
9816                                           (flow_dv_counter_get_by_idx(dev,
9817                                           flow->counter, NULL))->action;
9818                                 actions_n++;
9819                         }
9820                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE) {
9821                                 ret = flow_dv_create_action_sample(dev,
9822                                                           dev_flow, attr,
9823                                                           num_of_dest,
9824                                                           &sample_res,
9825                                                           &mdest_res,
9826                                                           sample_actions,
9827                                                           action_flags,
9828                                                           error);
9829                                 if (ret < 0)
9830                                         return rte_flow_error_set
9831                                                 (error, rte_errno,
9832                                                 RTE_FLOW_ERROR_TYPE_ACTION,
9833                                                 NULL,
9834                                                 "cannot create sample action");
9835                                 if (num_of_dest > 1) {
9836                                         dev_flow->dv.actions[sample_act_pos] =
9837                                         dev_flow->dv.dest_array_res->action;
9838                                 } else {
9839                                         dev_flow->dv.actions[sample_act_pos] =
9840                                         dev_flow->dv.sample_res->verbs_action;
9841                                 }
9842                         }
9843                         break;
9844                 default:
9845                         break;
9846                 }
9847                 if (mhdr_res->actions_num &&
9848                     modify_action_position == UINT32_MAX)
9849                         modify_action_position = actions_n++;
9850         }
9851         /*
9852          * For multiple destination (sample action with ratio=1), the encap
9853          * action and port id action will be combined into group action.
9854          * So need remove the original these actions in the flow and only
9855          * use the sample action instead of.
9856          */
9857         if (num_of_dest > 1 && sample_act->dr_port_id_action) {
9858                 int i;
9859                 void *temp_actions[MLX5_DV_MAX_NUMBER_OF_ACTIONS] = {0};
9860
9861                 for (i = 0; i < actions_n; i++) {
9862                         if ((sample_act->dr_encap_action &&
9863                                 sample_act->dr_encap_action ==
9864                                 dev_flow->dv.actions[i]) ||
9865                                 (sample_act->dr_port_id_action &&
9866                                 sample_act->dr_port_id_action ==
9867                                 dev_flow->dv.actions[i]))
9868                                 continue;
9869                         temp_actions[tmp_actions_n++] = dev_flow->dv.actions[i];
9870                 }
9871                 memcpy((void *)dev_flow->dv.actions,
9872                                 (void *)temp_actions,
9873                                 tmp_actions_n * sizeof(void *));
9874                 actions_n = tmp_actions_n;
9875         }
9876         dev_flow->dv.actions_n = actions_n;
9877         dev_flow->act_flags = action_flags;
9878         for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
9879                 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
9880                 int item_type = items->type;
9881
9882                 if (!mlx5_flow_os_item_supported(item_type))
9883                         return rte_flow_error_set(error, ENOTSUP,
9884                                                   RTE_FLOW_ERROR_TYPE_ITEM,
9885                                                   NULL, "item not supported");
9886                 switch (item_type) {
9887                 case RTE_FLOW_ITEM_TYPE_PORT_ID:
9888                         flow_dv_translate_item_port_id(dev, match_mask,
9889                                                        match_value, items);
9890                         last_item = MLX5_FLOW_ITEM_PORT_ID;
9891                         break;
9892                 case RTE_FLOW_ITEM_TYPE_ETH:
9893                         flow_dv_translate_item_eth(match_mask, match_value,
9894                                                    items, tunnel,
9895                                                    dev_flow->dv.group);
9896                         matcher.priority = action_flags &
9897                                         MLX5_FLOW_ACTION_DEFAULT_MISS &&
9898                                         !dev_flow->external ?
9899                                         MLX5_PRIORITY_MAP_L3 :
9900                                         MLX5_PRIORITY_MAP_L2;
9901                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
9902                                              MLX5_FLOW_LAYER_OUTER_L2;
9903                         break;
9904                 case RTE_FLOW_ITEM_TYPE_VLAN:
9905                         flow_dv_translate_item_vlan(dev_flow,
9906                                                     match_mask, match_value,
9907                                                     items, tunnel,
9908                                                     dev_flow->dv.group);
9909                         matcher.priority = MLX5_PRIORITY_MAP_L2;
9910                         last_item = tunnel ? (MLX5_FLOW_LAYER_INNER_L2 |
9911                                               MLX5_FLOW_LAYER_INNER_VLAN) :
9912                                              (MLX5_FLOW_LAYER_OUTER_L2 |
9913                                               MLX5_FLOW_LAYER_OUTER_VLAN);
9914                         break;
9915                 case RTE_FLOW_ITEM_TYPE_IPV4:
9916                         mlx5_flow_tunnel_ip_check(items, next_protocol,
9917                                                   &item_flags, &tunnel);
9918                         flow_dv_translate_item_ipv4(match_mask, match_value,
9919                                                     items, tunnel,
9920                                                     dev_flow->dv.group);
9921                         matcher.priority = MLX5_PRIORITY_MAP_L3;
9922                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
9923                                              MLX5_FLOW_LAYER_OUTER_L3_IPV4;
9924                         if (items->mask != NULL &&
9925                             ((const struct rte_flow_item_ipv4 *)
9926                              items->mask)->hdr.next_proto_id) {
9927                                 next_protocol =
9928                                         ((const struct rte_flow_item_ipv4 *)
9929                                          (items->spec))->hdr.next_proto_id;
9930                                 next_protocol &=
9931                                         ((const struct rte_flow_item_ipv4 *)
9932                                          (items->mask))->hdr.next_proto_id;
9933                         } else {
9934                                 /* Reset for inner layer. */
9935                                 next_protocol = 0xff;
9936                         }
9937                         break;
9938                 case RTE_FLOW_ITEM_TYPE_IPV6:
9939                         mlx5_flow_tunnel_ip_check(items, next_protocol,
9940                                                   &item_flags, &tunnel);
9941                         flow_dv_translate_item_ipv6(match_mask, match_value,
9942                                                     items, tunnel,
9943                                                     dev_flow->dv.group);
9944                         matcher.priority = MLX5_PRIORITY_MAP_L3;
9945                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
9946                                              MLX5_FLOW_LAYER_OUTER_L3_IPV6;
9947                         if (items->mask != NULL &&
9948                             ((const struct rte_flow_item_ipv6 *)
9949                              items->mask)->hdr.proto) {
9950                                 next_protocol =
9951                                         ((const struct rte_flow_item_ipv6 *)
9952                                          items->spec)->hdr.proto;
9953                                 next_protocol &=
9954                                         ((const struct rte_flow_item_ipv6 *)
9955                                          items->mask)->hdr.proto;
9956                         } else {
9957                                 /* Reset for inner layer. */
9958                                 next_protocol = 0xff;
9959                         }
9960                         break;
9961                 case RTE_FLOW_ITEM_TYPE_IPV6_FRAG_EXT:
9962                         flow_dv_translate_item_ipv6_frag_ext(match_mask,
9963                                                              match_value,
9964                                                              items, tunnel);
9965                         last_item = tunnel ?
9966                                         MLX5_FLOW_LAYER_INNER_L3_IPV6_FRAG_EXT :
9967                                         MLX5_FLOW_LAYER_OUTER_L3_IPV6_FRAG_EXT;
9968                         if (items->mask != NULL &&
9969                             ((const struct rte_flow_item_ipv6_frag_ext *)
9970                              items->mask)->hdr.next_header) {
9971                                 next_protocol =
9972                                 ((const struct rte_flow_item_ipv6_frag_ext *)
9973                                  items->spec)->hdr.next_header;
9974                                 next_protocol &=
9975                                 ((const struct rte_flow_item_ipv6_frag_ext *)
9976                                  items->mask)->hdr.next_header;
9977                         } else {
9978                                 /* Reset for inner layer. */
9979                                 next_protocol = 0xff;
9980                         }
9981                         break;
9982                 case RTE_FLOW_ITEM_TYPE_TCP:
9983                         flow_dv_translate_item_tcp(match_mask, match_value,
9984                                                    items, tunnel);
9985                         matcher.priority = MLX5_PRIORITY_MAP_L4;
9986                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
9987                                              MLX5_FLOW_LAYER_OUTER_L4_TCP;
9988                         break;
9989                 case RTE_FLOW_ITEM_TYPE_UDP:
9990                         flow_dv_translate_item_udp(match_mask, match_value,
9991                                                    items, tunnel);
9992                         matcher.priority = MLX5_PRIORITY_MAP_L4;
9993                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
9994                                              MLX5_FLOW_LAYER_OUTER_L4_UDP;
9995                         break;
9996                 case RTE_FLOW_ITEM_TYPE_GRE:
9997                         flow_dv_translate_item_gre(match_mask, match_value,
9998                                                    items, tunnel);
9999                         matcher.priority = MLX5_TUNNEL_PRIO_GET(rss_desc);
10000                         last_item = MLX5_FLOW_LAYER_GRE;
10001                         break;
10002                 case RTE_FLOW_ITEM_TYPE_GRE_KEY:
10003                         flow_dv_translate_item_gre_key(match_mask,
10004                                                        match_value, items);
10005                         last_item = MLX5_FLOW_LAYER_GRE_KEY;
10006                         break;
10007                 case RTE_FLOW_ITEM_TYPE_NVGRE:
10008                         flow_dv_translate_item_nvgre(match_mask, match_value,
10009                                                      items, tunnel);
10010                         matcher.priority = MLX5_TUNNEL_PRIO_GET(rss_desc);
10011                         last_item = MLX5_FLOW_LAYER_GRE;
10012                         break;
10013                 case RTE_FLOW_ITEM_TYPE_VXLAN:
10014                         flow_dv_translate_item_vxlan(match_mask, match_value,
10015                                                      items, tunnel);
10016                         matcher.priority = MLX5_TUNNEL_PRIO_GET(rss_desc);
10017                         last_item = MLX5_FLOW_LAYER_VXLAN;
10018                         break;
10019                 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
10020                         flow_dv_translate_item_vxlan_gpe(match_mask,
10021                                                          match_value, items,
10022                                                          tunnel);
10023                         matcher.priority = MLX5_TUNNEL_PRIO_GET(rss_desc);
10024                         last_item = MLX5_FLOW_LAYER_VXLAN_GPE;
10025                         break;
10026                 case RTE_FLOW_ITEM_TYPE_GENEVE:
10027                         flow_dv_translate_item_geneve(match_mask, match_value,
10028                                                       items, tunnel);
10029                         matcher.priority = MLX5_TUNNEL_PRIO_GET(rss_desc);
10030                         last_item = MLX5_FLOW_LAYER_GENEVE;
10031                         break;
10032                 case RTE_FLOW_ITEM_TYPE_MPLS:
10033                         flow_dv_translate_item_mpls(match_mask, match_value,
10034                                                     items, last_item, tunnel);
10035                         matcher.priority = MLX5_TUNNEL_PRIO_GET(rss_desc);
10036                         last_item = MLX5_FLOW_LAYER_MPLS;
10037                         break;
10038                 case RTE_FLOW_ITEM_TYPE_MARK:
10039                         flow_dv_translate_item_mark(dev, match_mask,
10040                                                     match_value, items);
10041                         last_item = MLX5_FLOW_ITEM_MARK;
10042                         break;
10043                 case RTE_FLOW_ITEM_TYPE_META:
10044                         flow_dv_translate_item_meta(dev, match_mask,
10045                                                     match_value, attr, items);
10046                         last_item = MLX5_FLOW_ITEM_METADATA;
10047                         break;
10048                 case RTE_FLOW_ITEM_TYPE_ICMP:
10049                         flow_dv_translate_item_icmp(match_mask, match_value,
10050                                                     items, tunnel);
10051                         last_item = MLX5_FLOW_LAYER_ICMP;
10052                         break;
10053                 case RTE_FLOW_ITEM_TYPE_ICMP6:
10054                         flow_dv_translate_item_icmp6(match_mask, match_value,
10055                                                       items, tunnel);
10056                         last_item = MLX5_FLOW_LAYER_ICMP6;
10057                         break;
10058                 case RTE_FLOW_ITEM_TYPE_TAG:
10059                         flow_dv_translate_item_tag(dev, match_mask,
10060                                                    match_value, items);
10061                         last_item = MLX5_FLOW_ITEM_TAG;
10062                         break;
10063                 case MLX5_RTE_FLOW_ITEM_TYPE_TAG:
10064                         flow_dv_translate_mlx5_item_tag(dev, match_mask,
10065                                                         match_value, items);
10066                         last_item = MLX5_FLOW_ITEM_TAG;
10067                         break;
10068                 case MLX5_RTE_FLOW_ITEM_TYPE_TX_QUEUE:
10069                         flow_dv_translate_item_tx_queue(dev, match_mask,
10070                                                         match_value,
10071                                                         items);
10072                         last_item = MLX5_FLOW_ITEM_TX_QUEUE;
10073                         break;
10074                 case RTE_FLOW_ITEM_TYPE_GTP:
10075                         flow_dv_translate_item_gtp(match_mask, match_value,
10076                                                    items, tunnel);
10077                         matcher.priority = MLX5_TUNNEL_PRIO_GET(rss_desc);
10078                         last_item = MLX5_FLOW_LAYER_GTP;
10079                         break;
10080                 case RTE_FLOW_ITEM_TYPE_ECPRI:
10081                         if (!mlx5_flex_parser_ecpri_exist(dev)) {
10082                                 /* Create it only the first time to be used. */
10083                                 ret = mlx5_flex_parser_ecpri_alloc(dev);
10084                                 if (ret)
10085                                         return rte_flow_error_set
10086                                                 (error, -ret,
10087                                                 RTE_FLOW_ERROR_TYPE_ITEM,
10088                                                 NULL,
10089                                                 "cannot create eCPRI parser");
10090                         }
10091                         /* Adjust the length matcher and device flow value. */
10092                         matcher.mask.size = MLX5_ST_SZ_BYTES(fte_match_param);
10093                         dev_flow->dv.value.size =
10094                                         MLX5_ST_SZ_BYTES(fte_match_param);
10095                         flow_dv_translate_item_ecpri(dev, match_mask,
10096                                                      match_value, items);
10097                         /* No other protocol should follow eCPRI layer. */
10098                         last_item = MLX5_FLOW_LAYER_ECPRI;
10099                         break;
10100                 default:
10101                         break;
10102                 }
10103                 item_flags |= last_item;
10104         }
10105         /*
10106          * When E-Switch mode is enabled, we have two cases where we need to
10107          * set the source port manually.
10108          * The first one, is in case of Nic steering rule, and the second is
10109          * E-Switch rule where no port_id item was found. In both cases
10110          * the source port is set according the current port in use.
10111          */
10112         if (!(item_flags & MLX5_FLOW_ITEM_PORT_ID) &&
10113             (priv->representor || priv->master)) {
10114                 if (flow_dv_translate_item_port_id(dev, match_mask,
10115                                                    match_value, NULL))
10116                         return -rte_errno;
10117         }
10118 #ifdef RTE_LIBRTE_MLX5_DEBUG
10119         MLX5_ASSERT(!flow_dv_check_valid_spec(matcher.mask.buf,
10120                                               dev_flow->dv.value.buf));
10121 #endif
10122         /*
10123          * Layers may be already initialized from prefix flow if this dev_flow
10124          * is the suffix flow.
10125          */
10126         handle->layers |= item_flags;
10127         if (action_flags & MLX5_FLOW_ACTION_RSS)
10128                 flow_dv_hashfields_set(dev_flow, rss_desc);
10129         /* Register matcher. */
10130         matcher.crc = rte_raw_cksum((const void *)matcher.mask.buf,
10131                                     matcher.mask.size);
10132         matcher.priority = mlx5_flow_adjust_priority(dev, priority,
10133                                                      matcher.priority);
10134         /* reserved field no needs to be set to 0 here. */
10135         tbl_key.domain = attr->transfer;
10136         tbl_key.direction = attr->egress;
10137         tbl_key.table_id = dev_flow->dv.group;
10138         if (flow_dv_matcher_register(dev, &matcher, &tbl_key, dev_flow, error))
10139                 return -rte_errno;
10140         return 0;
10141 }
10142
10143 /**
10144  * Set hash RX queue by hash fields (see enum ibv_rx_hash_fields)
10145  * and tunnel.
10146  *
10147  * @param[in, out] action
10148  *   Shred RSS action holding hash RX queue objects.
10149  * @param[in] hash_fields
10150  *   Defines combination of packet fields to participate in RX hash.
10151  * @param[in] tunnel
10152  *   Tunnel type
10153  * @param[in] hrxq_idx
10154  *   Hash RX queue index to set.
10155  *
10156  * @return
10157  *   0 on success, otherwise negative errno value.
10158  */
10159 static int
10160 __flow_dv_action_rss_hrxq_set(struct mlx5_shared_action_rss *action,
10161                               const uint64_t hash_fields,
10162                               const int tunnel,
10163                               uint32_t hrxq_idx)
10164 {
10165         uint32_t *hrxqs = tunnel ? action->hrxq : action->hrxq_tunnel;
10166
10167         switch (hash_fields & ~IBV_RX_HASH_INNER) {
10168         case MLX5_RSS_HASH_IPV4:
10169                 hrxqs[0] = hrxq_idx;
10170                 return 0;
10171         case MLX5_RSS_HASH_IPV4_TCP:
10172                 hrxqs[1] = hrxq_idx;
10173                 return 0;
10174         case MLX5_RSS_HASH_IPV4_UDP:
10175                 hrxqs[2] = hrxq_idx;
10176                 return 0;
10177         case MLX5_RSS_HASH_IPV6:
10178                 hrxqs[3] = hrxq_idx;
10179                 return 0;
10180         case MLX5_RSS_HASH_IPV6_TCP:
10181                 hrxqs[4] = hrxq_idx;
10182                 return 0;
10183         case MLX5_RSS_HASH_IPV6_UDP:
10184                 hrxqs[5] = hrxq_idx;
10185                 return 0;
10186         case MLX5_RSS_HASH_NONE:
10187                 hrxqs[6] = hrxq_idx;
10188                 return 0;
10189         default:
10190                 return -1;
10191         }
10192 }
10193
10194 /**
10195  * Look up for hash RX queue by hash fields (see enum ibv_rx_hash_fields)
10196  * and tunnel.
10197  *
10198  * @param[in] action
10199  *   Shred RSS action holding hash RX queue objects.
10200  * @param[in] hash_fields
10201  *   Defines combination of packet fields to participate in RX hash.
10202  * @param[in] tunnel
10203  *   Tunnel type
10204  *
10205  * @return
10206  *   Valid hash RX queue index, otherwise 0.
10207  */
10208 static uint32_t
10209 __flow_dv_action_rss_hrxq_lookup(const struct mlx5_shared_action_rss *action,
10210                                  const uint64_t hash_fields,
10211                                  const int tunnel)
10212 {
10213         const uint32_t *hrxqs = tunnel ? action->hrxq : action->hrxq_tunnel;
10214
10215         switch (hash_fields & ~IBV_RX_HASH_INNER) {
10216         case MLX5_RSS_HASH_IPV4:
10217                 return hrxqs[0];
10218         case MLX5_RSS_HASH_IPV4_TCP:
10219                 return hrxqs[1];
10220         case MLX5_RSS_HASH_IPV4_UDP:
10221                 return hrxqs[2];
10222         case MLX5_RSS_HASH_IPV6:
10223                 return hrxqs[3];
10224         case MLX5_RSS_HASH_IPV6_TCP:
10225                 return hrxqs[4];
10226         case MLX5_RSS_HASH_IPV6_UDP:
10227                 return hrxqs[5];
10228         case MLX5_RSS_HASH_NONE:
10229                 return hrxqs[6];
10230         default:
10231                 return 0;
10232         }
10233 }
10234
10235 /**
10236  * Retrieves hash RX queue suitable for the *flow*.
10237  * If shared action configured for *flow* suitable hash RX queue will be
10238  * retrieved from attached shared action.
10239  *
10240  * @param[in] flow
10241  *   Shred RSS action holding hash RX queue objects.
10242  * @param[in] dev_flow
10243  *   Pointer to the sub flow.
10244  * @param[out] hrxq
10245  *   Pointer to retrieved hash RX queue object.
10246  *
10247  * @return
10248  *   Valid hash RX queue index, otherwise 0 and rte_errno is set.
10249  */
10250 static uint32_t
10251 __flow_dv_rss_get_hrxq(struct rte_eth_dev *dev, struct rte_flow *flow,
10252                            struct mlx5_flow *dev_flow,
10253                            struct mlx5_hrxq **hrxq)
10254 {
10255         struct mlx5_priv *priv = dev->data->dev_private;
10256         struct mlx5_flow_workspace *wks = mlx5_flow_get_thread_workspace();
10257         uint32_t hrxq_idx;
10258
10259         if (flow->shared_rss) {
10260                 hrxq_idx = __flow_dv_action_rss_hrxq_lookup
10261                                 (flow->shared_rss, dev_flow->hash_fields,
10262                                  !!(dev_flow->handle->layers &
10263                                     MLX5_FLOW_LAYER_TUNNEL));
10264                 if (hrxq_idx) {
10265                         *hrxq = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_HRXQ],
10266                                                hrxq_idx);
10267                         __atomic_fetch_add(&(*hrxq)->refcnt, 1,
10268                                            __ATOMIC_RELAXED);
10269                 }
10270         } else {
10271                 struct mlx5_flow_rss_desc *rss_desc =
10272                                 &wks->rss_desc[!!wks->flow_nested_idx];
10273
10274                 MLX5_ASSERT(rss_desc->queue_num);
10275                 hrxq_idx = mlx5_hrxq_get(dev, rss_desc->key,
10276                                          MLX5_RSS_HASH_KEY_LEN,
10277                                          dev_flow->hash_fields,
10278                                          rss_desc->queue, rss_desc->queue_num);
10279                 if (!hrxq_idx) {
10280                         hrxq_idx = mlx5_hrxq_new(dev,
10281                                                  rss_desc->key,
10282                                                  MLX5_RSS_HASH_KEY_LEN,
10283                                                  dev_flow->hash_fields,
10284                                                  rss_desc->queue,
10285                                                  rss_desc->queue_num,
10286                                                  !!(dev_flow->handle->layers &
10287                                                  MLX5_FLOW_LAYER_TUNNEL),
10288                                                  false);
10289                 }
10290                 *hrxq = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_HRXQ],
10291                                        hrxq_idx);
10292         }
10293         return hrxq_idx;
10294 }
10295
10296 /**
10297  * Apply the flow to the NIC, lock free,
10298  * (mutex should be acquired by caller).
10299  *
10300  * @param[in] dev
10301  *   Pointer to the Ethernet device structure.
10302  * @param[in, out] flow
10303  *   Pointer to flow structure.
10304  * @param[out] error
10305  *   Pointer to error structure.
10306  *
10307  * @return
10308  *   0 on success, a negative errno value otherwise and rte_errno is set.
10309  */
10310 static int
10311 __flow_dv_apply(struct rte_eth_dev *dev, struct rte_flow *flow,
10312                 struct rte_flow_error *error)
10313 {
10314         struct mlx5_flow_dv_workspace *dv;
10315         struct mlx5_flow_handle *dh;
10316         struct mlx5_flow_handle_dv *dv_h;
10317         struct mlx5_flow *dev_flow;
10318         struct mlx5_priv *priv = dev->data->dev_private;
10319         uint32_t handle_idx;
10320         int n;
10321         int err;
10322         int idx;
10323         struct mlx5_flow_workspace *wks = mlx5_flow_get_thread_workspace();
10324
10325         MLX5_ASSERT(wks);
10326         for (idx = wks->flow_idx - 1; idx >= wks->flow_nested_idx; idx--) {
10327                 dev_flow = &wks->flows[idx];
10328                 dv = &dev_flow->dv;
10329                 dh = dev_flow->handle;
10330                 dv_h = &dh->dvh;
10331                 n = dv->actions_n;
10332                 if (dh->fate_action == MLX5_FLOW_FATE_DROP) {
10333                         if (dv->transfer) {
10334                                 dv->actions[n++] = priv->sh->esw_drop_action;
10335                         } else {
10336                                 MLX5_ASSERT(priv->drop_queue.hrxq);
10337                                 dv->actions[n++] =
10338                                                 priv->drop_queue.hrxq->action;
10339                         }
10340                 } else if (dh->fate_action == MLX5_FLOW_FATE_QUEUE &&
10341                            !dv_h->rix_sample && !dv_h->rix_dest_array) {
10342                         struct mlx5_hrxq *hrxq = NULL;
10343                         uint32_t hrxq_idx = __flow_dv_rss_get_hrxq
10344                                                 (dev, flow, dev_flow, &hrxq);
10345
10346                         if (!hrxq) {
10347                                 rte_flow_error_set
10348                                         (error, rte_errno,
10349                                          RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
10350                                          "cannot get hash queue");
10351                                 goto error;
10352                         }
10353                         dh->rix_hrxq = hrxq_idx;
10354                         dv->actions[n++] = hrxq->action;
10355                 } else if (dh->fate_action == MLX5_FLOW_FATE_DEFAULT_MISS) {
10356                         if (!priv->sh->default_miss_action) {
10357                                 rte_flow_error_set
10358                                         (error, rte_errno,
10359                                          RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
10360                                          "default miss action not be created.");
10361                                 goto error;
10362                         }
10363                         dv->actions[n++] = priv->sh->default_miss_action;
10364                 }
10365                 err = mlx5_flow_os_create_flow(dv_h->matcher->matcher_object,
10366                                                (void *)&dv->value, n,
10367                                                dv->actions, &dh->drv_flow);
10368                 if (err) {
10369                         rte_flow_error_set(error, errno,
10370                                            RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
10371                                            NULL,
10372                                            "hardware refuses to create flow");
10373                         goto error;
10374                 }
10375                 if (priv->vmwa_context &&
10376                     dh->vf_vlan.tag && !dh->vf_vlan.created) {
10377                         /*
10378                          * The rule contains the VLAN pattern.
10379                          * For VF we are going to create VLAN
10380                          * interface to make hypervisor set correct
10381                          * e-Switch vport context.
10382                          */
10383                         mlx5_vlan_vmwa_acquire(dev, &dh->vf_vlan);
10384                 }
10385         }
10386         return 0;
10387 error:
10388         err = rte_errno; /* Save rte_errno before cleanup. */
10389         SILIST_FOREACH(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW], flow->dev_handles,
10390                        handle_idx, dh, next) {
10391                 /* hrxq is union, don't clear it if the flag is not set. */
10392                 if (dh->fate_action == MLX5_FLOW_FATE_QUEUE && dh->rix_hrxq) {
10393                         mlx5_hrxq_release(dev, dh->rix_hrxq);
10394                         dh->rix_hrxq = 0;
10395                 }
10396                 if (dh->vf_vlan.tag && dh->vf_vlan.created)
10397                         mlx5_vlan_vmwa_release(dev, &dh->vf_vlan);
10398         }
10399         rte_errno = err; /* Restore rte_errno. */
10400         return -rte_errno;
10401 }
10402
10403 /**
10404  * Release the flow matcher.
10405  *
10406  * @param dev
10407  *   Pointer to Ethernet device.
10408  * @param handle
10409  *   Pointer to mlx5_flow_handle.
10410  *
10411  * @return
10412  *   1 while a reference on it exists, 0 when freed.
10413  */
10414 static int
10415 flow_dv_matcher_release(struct rte_eth_dev *dev,
10416                         struct mlx5_flow_handle *handle)
10417 {
10418         struct mlx5_flow_dv_matcher *matcher = handle->dvh.matcher;
10419
10420         MLX5_ASSERT(matcher->matcher_object);
10421         DRV_LOG(DEBUG, "port %u matcher %p: refcnt %d--",
10422                 dev->data->port_id, (void *)matcher,
10423                 __atomic_load_n(&matcher->refcnt, __ATOMIC_RELAXED));
10424         if (__atomic_sub_fetch(&matcher->refcnt, 1, __ATOMIC_RELAXED) == 0) {
10425                 claim_zero(mlx5_flow_os_destroy_flow_matcher
10426                            (matcher->matcher_object));
10427                 LIST_REMOVE(matcher, next);
10428                 /* table ref-- in release interface. */
10429                 flow_dv_tbl_resource_release(dev, matcher->tbl);
10430                 mlx5_free(matcher);
10431                 DRV_LOG(DEBUG, "port %u matcher %p: removed",
10432                         dev->data->port_id, (void *)matcher);
10433                 return 0;
10434         }
10435         return 1;
10436 }
10437
10438 /**
10439  * Release an encap/decap resource.
10440  *
10441  * @param dev
10442  *   Pointer to Ethernet device.
10443  * @param encap_decap_idx
10444  *   Index of encap decap resource.
10445  *
10446  * @return
10447  *   1 while a reference on it exists, 0 when freed.
10448  */
10449 static int
10450 flow_dv_encap_decap_resource_release(struct rte_eth_dev *dev,
10451                                      uint32_t encap_decap_idx)
10452 {
10453         struct mlx5_priv *priv = dev->data->dev_private;
10454         uint32_t idx = encap_decap_idx;
10455         struct mlx5_flow_dv_encap_decap_resource *cache_resource;
10456
10457         cache_resource = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_DECAP_ENCAP],
10458                          idx);
10459         if (!cache_resource)
10460                 return 0;
10461         MLX5_ASSERT(cache_resource->action);
10462         DRV_LOG(DEBUG, "encap/decap resource %p: refcnt %d--",
10463                 (void *)cache_resource,
10464                 __atomic_load_n(&cache_resource->refcnt, __ATOMIC_RELAXED));
10465         if (__atomic_sub_fetch(&cache_resource->refcnt, 1,
10466                                __ATOMIC_RELAXED) == 0) {
10467                 claim_zero(mlx5_flow_os_destroy_flow_action
10468                                                 (cache_resource->action));
10469                 mlx5_hlist_remove(priv->sh->encaps_decaps,
10470                                   &cache_resource->entry);
10471                 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_DECAP_ENCAP], idx);
10472                 DRV_LOG(DEBUG, "encap/decap resource %p: removed",
10473                         (void *)cache_resource);
10474                 return 0;
10475         }
10476         return 1;
10477 }
10478
10479 /**
10480  * Release an jump to table action resource.
10481  *
10482  * @param dev
10483  *   Pointer to Ethernet device.
10484  * @param handle
10485  *   Pointer to mlx5_flow_handle.
10486  *
10487  * @return
10488  *   1 while a reference on it exists, 0 when freed.
10489  */
10490 static int
10491 flow_dv_jump_tbl_resource_release(struct rte_eth_dev *dev,
10492                                   struct mlx5_flow_handle *handle)
10493 {
10494         struct mlx5_priv *priv = dev->data->dev_private;
10495         struct mlx5_flow_tbl_data_entry *tbl_data;
10496
10497         tbl_data = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_JUMP],
10498                              handle->rix_jump);
10499         if (!tbl_data)
10500                 return 0;
10501         return flow_dv_tbl_resource_release(dev, &tbl_data->tbl);
10502 }
10503
10504 /**
10505  * Release a modify-header resource.
10506  *
10507  * @param dev
10508  *   Pointer to Ethernet device.
10509  * @param handle
10510  *   Pointer to mlx5_flow_handle.
10511  *
10512  * @return
10513  *   1 while a reference on it exists, 0 when freed.
10514  */
10515 static int
10516 flow_dv_modify_hdr_resource_release(struct rte_eth_dev *dev,
10517                                     struct mlx5_flow_handle *handle)
10518 {
10519         struct mlx5_priv *priv = dev->data->dev_private;
10520         struct mlx5_flow_dv_modify_hdr_resource *cache_resource =
10521                                                         handle->dvh.modify_hdr;
10522
10523         MLX5_ASSERT(cache_resource->action);
10524         DRV_LOG(DEBUG, "modify-header resource %p: refcnt %d--",
10525                 (void *)cache_resource,
10526                 __atomic_load_n(&cache_resource->refcnt, __ATOMIC_RELAXED));
10527         if (__atomic_sub_fetch(&cache_resource->refcnt, 1,
10528                                 __ATOMIC_RELAXED) == 0) {
10529                 claim_zero(mlx5_flow_os_destroy_flow_action
10530                                                 (cache_resource->action));
10531                 mlx5_hlist_remove(priv->sh->modify_cmds,
10532                                   &cache_resource->entry);
10533                 mlx5_free(cache_resource);
10534                 DRV_LOG(DEBUG, "modify-header resource %p: removed",
10535                         (void *)cache_resource);
10536                 return 0;
10537         }
10538         return 1;
10539 }
10540
10541 /**
10542  * Release port ID action resource.
10543  *
10544  * @param dev
10545  *   Pointer to Ethernet device.
10546  * @param handle
10547  *   Pointer to mlx5_flow_handle.
10548  *
10549  * @return
10550  *   1 while a reference on it exists, 0 when freed.
10551  */
10552 static int
10553 flow_dv_port_id_action_resource_release(struct rte_eth_dev *dev,
10554                                         uint32_t port_id)
10555 {
10556         struct mlx5_priv *priv = dev->data->dev_private;
10557         struct mlx5_flow_dv_port_id_action_resource *cache_resource;
10558         uint32_t idx = port_id;
10559
10560         cache_resource = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_PORT_ID],
10561                                         idx);
10562         if (!cache_resource)
10563                 return 0;
10564         MLX5_ASSERT(cache_resource->action);
10565         DRV_LOG(DEBUG, "port ID action resource %p: refcnt %d--",
10566                 (void *)cache_resource,
10567                 __atomic_load_n(&cache_resource->refcnt, __ATOMIC_RELAXED));
10568         if (__atomic_sub_fetch(&cache_resource->refcnt, 1,
10569                                __ATOMIC_RELAXED) == 0) {
10570                 claim_zero(mlx5_flow_os_destroy_flow_action
10571                                                 (cache_resource->action));
10572                 ILIST_REMOVE(priv->sh->ipool[MLX5_IPOOL_PORT_ID],
10573                              &priv->sh->port_id_action_list, idx,
10574                              cache_resource, next);
10575                 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_PORT_ID], idx);
10576                 DRV_LOG(DEBUG, "port id action resource %p: removed",
10577                         (void *)cache_resource);
10578                 return 0;
10579         }
10580         return 1;
10581 }
10582
10583 /**
10584  * Release push vlan action resource.
10585  *
10586  * @param dev
10587  *   Pointer to Ethernet device.
10588  * @param handle
10589  *   Pointer to mlx5_flow_handle.
10590  *
10591  * @return
10592  *   1 while a reference on it exists, 0 when freed.
10593  */
10594 static int
10595 flow_dv_push_vlan_action_resource_release(struct rte_eth_dev *dev,
10596                                           struct mlx5_flow_handle *handle)
10597 {
10598         struct mlx5_priv *priv = dev->data->dev_private;
10599         uint32_t idx = handle->dvh.rix_push_vlan;
10600         struct mlx5_flow_dv_push_vlan_action_resource *cache_resource;
10601
10602         cache_resource = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_PUSH_VLAN],
10603                                         idx);
10604         if (!cache_resource)
10605                 return 0;
10606         MLX5_ASSERT(cache_resource->action);
10607         DRV_LOG(DEBUG, "push VLAN action resource %p: refcnt %d--",
10608                 (void *)cache_resource,
10609                 __atomic_load_n(&cache_resource->refcnt, __ATOMIC_RELAXED));
10610         if (__atomic_sub_fetch(&cache_resource->refcnt, 1,
10611                                __ATOMIC_RELAXED) == 0) {
10612                 claim_zero(mlx5_flow_os_destroy_flow_action
10613                                                 (cache_resource->action));
10614                 ILIST_REMOVE(priv->sh->ipool[MLX5_IPOOL_PUSH_VLAN],
10615                              &priv->sh->push_vlan_action_list, idx,
10616                              cache_resource, next);
10617                 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_PUSH_VLAN], idx);
10618                 DRV_LOG(DEBUG, "push vlan action resource %p: removed",
10619                         (void *)cache_resource);
10620                 return 0;
10621         }
10622         return 1;
10623 }
10624
10625 /**
10626  * Release the fate resource.
10627  *
10628  * @param dev
10629  *   Pointer to Ethernet device.
10630  * @param handle
10631  *   Pointer to mlx5_flow_handle.
10632  */
10633 static void
10634 flow_dv_fate_resource_release(struct rte_eth_dev *dev,
10635                                struct mlx5_flow_handle *handle)
10636 {
10637         if (!handle->rix_fate)
10638                 return;
10639         switch (handle->fate_action) {
10640         case MLX5_FLOW_FATE_QUEUE:
10641                 mlx5_hrxq_release(dev, handle->rix_hrxq);
10642                 break;
10643         case MLX5_FLOW_FATE_JUMP:
10644                 flow_dv_jump_tbl_resource_release(dev, handle);
10645                 break;
10646         case MLX5_FLOW_FATE_PORT_ID:
10647                 flow_dv_port_id_action_resource_release(dev,
10648                                 handle->rix_port_id_action);
10649                 break;
10650         default:
10651                 DRV_LOG(DEBUG, "Incorrect fate action:%d", handle->fate_action);
10652                 break;
10653         }
10654         handle->rix_fate = 0;
10655 }
10656
10657 /**
10658  * Release an sample resource.
10659  *
10660  * @param dev
10661  *   Pointer to Ethernet device.
10662  * @param handle
10663  *   Pointer to mlx5_flow_handle.
10664  *
10665  * @return
10666  *   1 while a reference on it exists, 0 when freed.
10667  */
10668 static int
10669 flow_dv_sample_resource_release(struct rte_eth_dev *dev,
10670                                      struct mlx5_flow_handle *handle)
10671 {
10672         struct mlx5_priv *priv = dev->data->dev_private;
10673         uint32_t idx = handle->dvh.rix_sample;
10674         struct mlx5_flow_dv_sample_resource *cache_resource;
10675
10676         cache_resource = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_SAMPLE],
10677                          idx);
10678         if (!cache_resource)
10679                 return 0;
10680         MLX5_ASSERT(cache_resource->verbs_action);
10681         DRV_LOG(DEBUG, "sample resource %p: refcnt %d--",
10682                 (void *)cache_resource,
10683                 __atomic_load_n(&cache_resource->refcnt, __ATOMIC_RELAXED));
10684         if (__atomic_sub_fetch(&cache_resource->refcnt, 1,
10685                                __ATOMIC_RELAXED) == 0) {
10686                 if (cache_resource->verbs_action)
10687                         claim_zero(mlx5_glue->destroy_flow_action
10688                                         (cache_resource->verbs_action));
10689                 if (cache_resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB) {
10690                         if (cache_resource->default_miss)
10691                                 claim_zero(mlx5_glue->destroy_flow_action
10692                                   (cache_resource->default_miss));
10693                 }
10694                 if (cache_resource->normal_path_tbl)
10695                         flow_dv_tbl_resource_release(dev,
10696                                 cache_resource->normal_path_tbl);
10697         }
10698         if (cache_resource->sample_idx.rix_hrxq &&
10699                 !mlx5_hrxq_release(dev,
10700                         cache_resource->sample_idx.rix_hrxq))
10701                 cache_resource->sample_idx.rix_hrxq = 0;
10702         if (cache_resource->sample_idx.rix_tag &&
10703                 !flow_dv_tag_release(dev,
10704                         cache_resource->sample_idx.rix_tag))
10705                 cache_resource->sample_idx.rix_tag = 0;
10706         if (cache_resource->sample_idx.cnt) {
10707                 flow_dv_counter_release(dev,
10708                         cache_resource->sample_idx.cnt);
10709                 cache_resource->sample_idx.cnt = 0;
10710         }
10711         if (!__atomic_load_n(&cache_resource->refcnt, __ATOMIC_RELAXED)) {
10712                 ILIST_REMOVE(priv->sh->ipool[MLX5_IPOOL_SAMPLE],
10713                              &priv->sh->sample_action_list, idx,
10714                              cache_resource, next);
10715                 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_SAMPLE], idx);
10716                 DRV_LOG(DEBUG, "sample resource %p: removed",
10717                         (void *)cache_resource);
10718                 return 0;
10719         }
10720         return 1;
10721 }
10722
10723 /**
10724  * Release an destination array resource.
10725  *
10726  * @param dev
10727  *   Pointer to Ethernet device.
10728  * @param handle
10729  *   Pointer to mlx5_flow_handle.
10730  *
10731  * @return
10732  *   1 while a reference on it exists, 0 when freed.
10733  */
10734 static int
10735 flow_dv_dest_array_resource_release(struct rte_eth_dev *dev,
10736                                      struct mlx5_flow_handle *handle)
10737 {
10738         struct mlx5_priv *priv = dev->data->dev_private;
10739         struct mlx5_flow_dv_dest_array_resource *cache_resource;
10740         struct mlx5_flow_sub_actions_idx *mdest_act_res;
10741         uint32_t idx = handle->dvh.rix_dest_array;
10742         uint32_t i = 0;
10743
10744         cache_resource = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_DEST_ARRAY],
10745                          idx);
10746         if (!cache_resource)
10747                 return 0;
10748         MLX5_ASSERT(cache_resource->action);
10749         DRV_LOG(DEBUG, "destination array resource %p: refcnt %d--",
10750                 (void *)cache_resource,
10751                 __atomic_load_n(&cache_resource->refcnt, __ATOMIC_RELAXED));
10752         if (__atomic_sub_fetch(&cache_resource->refcnt, 1,
10753                                __ATOMIC_RELAXED) == 0) {
10754                 if (cache_resource->action)
10755                         claim_zero(mlx5_glue->destroy_flow_action
10756                                                 (cache_resource->action));
10757                 for (; i < cache_resource->num_of_dest; i++) {
10758                         mdest_act_res = &cache_resource->sample_idx[i];
10759                         if (mdest_act_res->rix_hrxq) {
10760                                 mlx5_hrxq_release(dev,
10761                                         mdest_act_res->rix_hrxq);
10762                                 mdest_act_res->rix_hrxq = 0;
10763                         }
10764                         if (mdest_act_res->rix_encap_decap) {
10765                                 flow_dv_encap_decap_resource_release(dev,
10766                                         mdest_act_res->rix_encap_decap);
10767                                 mdest_act_res->rix_encap_decap = 0;
10768                         }
10769                         if (mdest_act_res->rix_port_id_action) {
10770                                 flow_dv_port_id_action_resource_release(dev,
10771                                         mdest_act_res->rix_port_id_action);
10772                                 mdest_act_res->rix_port_id_action = 0;
10773                         }
10774                         if (mdest_act_res->rix_tag) {
10775                                 flow_dv_tag_release(dev,
10776                                         mdest_act_res->rix_tag);
10777                                 mdest_act_res->rix_tag = 0;
10778                         }
10779                 }
10780                 ILIST_REMOVE(priv->sh->ipool[MLX5_IPOOL_DEST_ARRAY],
10781                              &priv->sh->dest_array_list, idx,
10782                              cache_resource, next);
10783                 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_DEST_ARRAY], idx);
10784                 DRV_LOG(DEBUG, "destination array resource %p: removed",
10785                         (void *)cache_resource);
10786                 return 0;
10787         }
10788         return 1;
10789 }
10790
10791 /**
10792  * Remove the flow from the NIC but keeps it in memory.
10793  * Lock free, (mutex should be acquired by caller).
10794  *
10795  * @param[in] dev
10796  *   Pointer to Ethernet device.
10797  * @param[in, out] flow
10798  *   Pointer to flow structure.
10799  */
10800 static void
10801 __flow_dv_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
10802 {
10803         struct mlx5_flow_handle *dh;
10804         uint32_t handle_idx;
10805         struct mlx5_priv *priv = dev->data->dev_private;
10806
10807         if (!flow)
10808                 return;
10809         handle_idx = flow->dev_handles;
10810         while (handle_idx) {
10811                 dh = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
10812                                     handle_idx);
10813                 if (!dh)
10814                         return;
10815                 if (dh->drv_flow) {
10816                         claim_zero(mlx5_flow_os_destroy_flow(dh->drv_flow));
10817                         dh->drv_flow = NULL;
10818                 }
10819                 if (dh->fate_action == MLX5_FLOW_FATE_QUEUE)
10820                         flow_dv_fate_resource_release(dev, dh);
10821                 if (dh->vf_vlan.tag && dh->vf_vlan.created)
10822                         mlx5_vlan_vmwa_release(dev, &dh->vf_vlan);
10823                 handle_idx = dh->next.next;
10824         }
10825 }
10826
10827 /**
10828  * Remove the flow from the NIC and the memory.
10829  * Lock free, (mutex should be acquired by caller).
10830  *
10831  * @param[in] dev
10832  *   Pointer to the Ethernet device structure.
10833  * @param[in, out] flow
10834  *   Pointer to flow structure.
10835  */
10836 static void
10837 __flow_dv_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
10838 {
10839         struct rte_flow_shared_action *shared;
10840         struct mlx5_flow_handle *dev_handle;
10841         struct mlx5_priv *priv = dev->data->dev_private;
10842
10843         if (!flow)
10844                 return;
10845         __flow_dv_remove(dev, flow);
10846         shared = mlx5_flow_get_shared_rss(flow);
10847         if (shared)
10848                 __atomic_sub_fetch(&shared->refcnt, 1, __ATOMIC_RELAXED);
10849         if (flow->counter) {
10850                 flow_dv_counter_release(dev, flow->counter);
10851                 flow->counter = 0;
10852         }
10853         if (flow->meter) {
10854                 struct mlx5_flow_meter *fm;
10855
10856                 fm = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_MTR],
10857                                     flow->meter);
10858                 if (fm)
10859                         mlx5_flow_meter_detach(fm);
10860                 flow->meter = 0;
10861         }
10862         while (flow->dev_handles) {
10863                 uint32_t tmp_idx = flow->dev_handles;
10864
10865                 dev_handle = mlx5_ipool_get(priv->sh->ipool
10866                                             [MLX5_IPOOL_MLX5_FLOW], tmp_idx);
10867                 if (!dev_handle)
10868                         return;
10869                 flow->dev_handles = dev_handle->next.next;
10870                 if (dev_handle->dvh.matcher)
10871                         flow_dv_matcher_release(dev, dev_handle);
10872                 if (dev_handle->dvh.rix_sample)
10873                         flow_dv_sample_resource_release(dev, dev_handle);
10874                 if (dev_handle->dvh.rix_dest_array)
10875                         flow_dv_dest_array_resource_release(dev, dev_handle);
10876                 if (dev_handle->dvh.rix_encap_decap)
10877                         flow_dv_encap_decap_resource_release(dev,
10878                                 dev_handle->dvh.rix_encap_decap);
10879                 if (dev_handle->dvh.modify_hdr)
10880                         flow_dv_modify_hdr_resource_release(dev, dev_handle);
10881                 if (dev_handle->dvh.rix_push_vlan)
10882                         flow_dv_push_vlan_action_resource_release(dev,
10883                                                                   dev_handle);
10884                 if (dev_handle->dvh.rix_tag)
10885                         flow_dv_tag_release(dev,
10886                                             dev_handle->dvh.rix_tag);
10887                 flow_dv_fate_resource_release(dev, dev_handle);
10888                 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
10889                            tmp_idx);
10890         }
10891 }
10892
10893 /**
10894  * Release array of hash RX queue objects.
10895  * Helper function.
10896  *
10897  * @param[in] dev
10898  *   Pointer to the Ethernet device structure.
10899  * @param[in, out] hrxqs
10900  *   Array of hash RX queue objects.
10901  *
10902  * @return
10903  *   Total number of references to hash RX queue objects in *hrxqs* array
10904  *   after this operation.
10905  */
10906 static int
10907 __flow_dv_hrxqs_release(struct rte_eth_dev *dev,
10908                         uint32_t (*hrxqs)[MLX5_RSS_HASH_FIELDS_LEN])
10909 {
10910         size_t i;
10911         int remaining = 0;
10912
10913         for (i = 0; i < RTE_DIM(*hrxqs); i++) {
10914                 int ret = mlx5_hrxq_release(dev, (*hrxqs)[i]);
10915
10916                 if (!ret)
10917                         (*hrxqs)[i] = 0;
10918                 remaining += ret;
10919         }
10920         return remaining;
10921 }
10922
10923 /**
10924  * Release all hash RX queue objects representing shared RSS action.
10925  *
10926  * @param[in] dev
10927  *   Pointer to the Ethernet device structure.
10928  * @param[in, out] action
10929  *   Shared RSS action to remove hash RX queue objects from.
10930  *
10931  * @return
10932  *   Total number of references to hash RX queue objects stored in *action*
10933  *   after this operation.
10934  *   Expected to be 0 if no external references held.
10935  */
10936 static int
10937 __flow_dv_action_rss_hrxqs_release(struct rte_eth_dev *dev,
10938                                  struct mlx5_shared_action_rss *action)
10939 {
10940         return __flow_dv_hrxqs_release(dev, &action->hrxq) +
10941                 __flow_dv_hrxqs_release(dev, &action->hrxq_tunnel);
10942 }
10943
10944 /**
10945  * Setup shared RSS action.
10946  * Prepare set of hash RX queue objects sufficient to handle all valid
10947  * hash_fields combinations (see enum ibv_rx_hash_fields).
10948  *
10949  * @param[in] dev
10950  *   Pointer to the Ethernet device structure.
10951  * @param[in, out] action
10952  *   Partially initialized shared RSS action.
10953  * @param[out] error
10954  *   Perform verbose error reporting if not NULL. Initialized in case of
10955  *   error only.
10956  *
10957  * @return
10958  *   0 on success, otherwise negative errno value.
10959  */
10960 static int
10961 __flow_dv_action_rss_setup(struct rte_eth_dev *dev,
10962                         struct mlx5_shared_action_rss *action,
10963                         struct rte_flow_error *error)
10964 {
10965         size_t i;
10966         int err;
10967
10968         for (i = 0; i < MLX5_RSS_HASH_FIELDS_LEN; i++) {
10969                 uint32_t hrxq_idx;
10970                 uint64_t hash_fields = mlx5_rss_hash_fields[i];
10971                 int tunnel;
10972
10973                 for (tunnel = 0; tunnel < 2; tunnel++) {
10974                         hrxq_idx = mlx5_hrxq_new(dev, action->origin.key,
10975                                         MLX5_RSS_HASH_KEY_LEN,
10976                                         hash_fields,
10977                                         action->origin.queue,
10978                                         action->origin.queue_num,
10979                                         tunnel, true);
10980                         if (!hrxq_idx) {
10981                                 rte_flow_error_set
10982                                         (error, rte_errno,
10983                                          RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
10984                                          "cannot get hash queue");
10985                                 goto error_hrxq_new;
10986                         }
10987                         err = __flow_dv_action_rss_hrxq_set
10988                                 (action, hash_fields, tunnel, hrxq_idx);
10989                         MLX5_ASSERT(!err);
10990                 }
10991         }
10992         return 0;
10993 error_hrxq_new:
10994         err = rte_errno;
10995         __flow_dv_action_rss_hrxqs_release(dev, action);
10996         rte_errno = err;
10997         return -rte_errno;
10998 }
10999
11000 /**
11001  * Create shared RSS action.
11002  *
11003  * @param[in] dev
11004  *   Pointer to the Ethernet device structure.
11005  * @param[in] conf
11006  *   Shared action configuration.
11007  * @param[in] rss
11008  *   RSS action specification used to create shared action.
11009  * @param[out] error
11010  *   Perform verbose error reporting if not NULL. Initialized in case of
11011  *   error only.
11012  *
11013  * @return
11014  *   A valid shared action handle in case of success, NULL otherwise and
11015  *   rte_errno is set.
11016  */
11017 static struct rte_flow_shared_action *
11018 __flow_dv_action_rss_create(struct rte_eth_dev *dev,
11019                             const struct rte_flow_shared_action_conf *conf,
11020                             const struct rte_flow_action_rss *rss,
11021                             struct rte_flow_error *error)
11022 {
11023         struct rte_flow_shared_action *shared_action = NULL;
11024         void *queue = NULL;
11025         struct mlx5_shared_action_rss *shared_rss;
11026         struct rte_flow_action_rss *origin;
11027         const uint8_t *rss_key;
11028         uint32_t queue_size = rss->queue_num * sizeof(uint16_t);
11029
11030         RTE_SET_USED(conf);
11031         queue = mlx5_malloc(0, RTE_ALIGN_CEIL(queue_size, sizeof(void *)),
11032                             0, SOCKET_ID_ANY);
11033         shared_action = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*shared_action), 0,
11034                                     SOCKET_ID_ANY);
11035         if (!shared_action || !queue) {
11036                 rte_flow_error_set(error, ENOMEM,
11037                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
11038                                    "cannot allocate resource memory");
11039                 goto error_rss_init;
11040         }
11041         shared_rss = &shared_action->rss;
11042         shared_rss->queue = queue;
11043         origin = &shared_rss->origin;
11044         origin->func = rss->func;
11045         origin->level = rss->level;
11046         /* RSS type 0 indicates default RSS type (ETH_RSS_IP). */
11047         origin->types = !rss->types ? ETH_RSS_IP : rss->types;
11048         /* NULL RSS key indicates default RSS key. */
11049         rss_key = !rss->key ? rss_hash_default_key : rss->key;
11050         memcpy(shared_rss->key, rss_key, MLX5_RSS_HASH_KEY_LEN);
11051         origin->key = &shared_rss->key[0];
11052         origin->key_len = MLX5_RSS_HASH_KEY_LEN;
11053         memcpy(shared_rss->queue, rss->queue, queue_size);
11054         origin->queue = shared_rss->queue;
11055         origin->queue_num = rss->queue_num;
11056         if (__flow_dv_action_rss_setup(dev, shared_rss, error))
11057                 goto error_rss_init;
11058         shared_action->type = MLX5_RTE_FLOW_ACTION_TYPE_SHARED_RSS;
11059         return shared_action;
11060 error_rss_init:
11061         mlx5_free(shared_action);
11062         mlx5_free(queue);
11063         return NULL;
11064 }
11065
11066 /**
11067  * Destroy the shared RSS action.
11068  * Release related hash RX queue objects.
11069  *
11070  * @param[in] dev
11071  *   Pointer to the Ethernet device structure.
11072  * @param[in] shared_rss
11073  *   The shared RSS action object to be removed.
11074  * @param[out] error
11075  *   Perform verbose error reporting if not NULL. Initialized in case of
11076  *   error only.
11077  *
11078  * @return
11079  *   0 on success, otherwise negative errno value.
11080  */
11081 static int
11082 __flow_dv_action_rss_release(struct rte_eth_dev *dev,
11083                          struct mlx5_shared_action_rss *shared_rss,
11084                          struct rte_flow_error *error)
11085 {
11086         struct rte_flow_shared_action *shared_action = NULL;
11087         uint32_t old_refcnt = 1;
11088         int remaining = __flow_dv_action_rss_hrxqs_release(dev, shared_rss);
11089
11090         if (remaining) {
11091                 return rte_flow_error_set(error, ETOOMANYREFS,
11092                                           RTE_FLOW_ERROR_TYPE_ACTION,
11093                                           NULL,
11094                                           "shared rss hrxq has references");
11095         }
11096         shared_action = container_of(shared_rss,
11097                                      struct rte_flow_shared_action, rss);
11098         if (!__atomic_compare_exchange_n(&shared_action->refcnt, &old_refcnt,
11099                                          0, 0,
11100                                          __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) {
11101                 return rte_flow_error_set(error, ETOOMANYREFS,
11102                                           RTE_FLOW_ERROR_TYPE_ACTION,
11103                                           NULL,
11104                                           "shared rss has references");
11105         }
11106         rte_free(shared_rss->queue);
11107         return 0;
11108 }
11109
11110 /**
11111  * Create shared action, lock free,
11112  * (mutex should be acquired by caller).
11113  * Dispatcher for action type specific call.
11114  *
11115  * @param[in] dev
11116  *   Pointer to the Ethernet device structure.
11117  * @param[in] conf
11118  *   Shared action configuration.
11119  * @param[in] action
11120  *   Action specification used to create shared action.
11121  * @param[out] error
11122  *   Perform verbose error reporting if not NULL. Initialized in case of
11123  *   error only.
11124  *
11125  * @return
11126  *   A valid shared action handle in case of success, NULL otherwise and
11127  *   rte_errno is set.
11128  */
11129 static struct rte_flow_shared_action *
11130 __flow_dv_action_create(struct rte_eth_dev *dev,
11131                         const struct rte_flow_shared_action_conf *conf,
11132                         const struct rte_flow_action *action,
11133                         struct rte_flow_error *error)
11134 {
11135         struct rte_flow_shared_action *shared_action = NULL;
11136         struct mlx5_priv *priv = dev->data->dev_private;
11137
11138         switch (action->type) {
11139         case RTE_FLOW_ACTION_TYPE_RSS:
11140                 shared_action = __flow_dv_action_rss_create(dev, conf,
11141                                                             action->conf,
11142                                                             error);
11143                 break;
11144         default:
11145                 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
11146                                    NULL, "action type not supported");
11147                 break;
11148         }
11149         if (shared_action) {
11150                 __atomic_add_fetch(&shared_action->refcnt, 1,
11151                                    __ATOMIC_RELAXED);
11152                 LIST_INSERT_HEAD(&priv->shared_actions, shared_action, next);
11153         }
11154         return shared_action;
11155 }
11156
11157 /**
11158  * Destroy the shared action.
11159  * Release action related resources on the NIC and the memory.
11160  * Lock free, (mutex should be acquired by caller).
11161  * Dispatcher for action type specific call.
11162  *
11163  * @param[in] dev
11164  *   Pointer to the Ethernet device structure.
11165  * @param[in] action
11166  *   The shared action object to be removed.
11167  * @param[out] error
11168  *   Perform verbose error reporting if not NULL. Initialized in case of
11169  *   error only.
11170  *
11171  * @return
11172  *   0 on success, otherwise negative errno value.
11173  */
11174 static int
11175 __flow_dv_action_destroy(struct rte_eth_dev *dev,
11176                          struct rte_flow_shared_action *action,
11177                          struct rte_flow_error *error)
11178 {
11179         int ret;
11180
11181         switch (action->type) {
11182         case MLX5_RTE_FLOW_ACTION_TYPE_SHARED_RSS:
11183                 ret = __flow_dv_action_rss_release(dev, &action->rss, error);
11184                 break;
11185         default:
11186                 return rte_flow_error_set(error, ENOTSUP,
11187                                           RTE_FLOW_ERROR_TYPE_ACTION,
11188                                           NULL,
11189                                           "action type not supported");
11190         }
11191         if (ret)
11192                 return ret;
11193         LIST_REMOVE(action, next);
11194         rte_free(action);
11195         return 0;
11196 }
11197
11198 /**
11199  * Updates in place shared RSS action configuration.
11200  *
11201  * @param[in] dev
11202  *   Pointer to the Ethernet device structure.
11203  * @param[in] shared_rss
11204  *   The shared RSS action object to be updated.
11205  * @param[in] action_conf
11206  *   RSS action specification used to modify *shared_rss*.
11207  * @param[out] error
11208  *   Perform verbose error reporting if not NULL. Initialized in case of
11209  *   error only.
11210  *
11211  * @return
11212  *   0 on success, otherwise negative errno value.
11213  * @note: currently only support update of RSS queues.
11214  */
11215 static int
11216 __flow_dv_action_rss_update(struct rte_eth_dev *dev,
11217                             struct mlx5_shared_action_rss *shared_rss,
11218                             const struct rte_flow_action_rss *action_conf,
11219                             struct rte_flow_error *error)
11220 {
11221         size_t i;
11222         int ret;
11223         void *queue = NULL;
11224         const uint8_t *rss_key;
11225         uint32_t rss_key_len;
11226         uint32_t queue_size = action_conf->queue_num * sizeof(uint16_t);
11227
11228         queue = mlx5_malloc(MLX5_MEM_ZERO,
11229                             RTE_ALIGN_CEIL(queue_size, sizeof(void *)),
11230                             0, SOCKET_ID_ANY);
11231         if (!queue)
11232                 return rte_flow_error_set(error, ENOMEM,
11233                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
11234                                           NULL,
11235                                           "cannot allocate resource memory");
11236         if (action_conf->key) {
11237                 rss_key = action_conf->key;
11238                 rss_key_len = action_conf->key_len;
11239         } else {
11240                 rss_key = rss_hash_default_key;
11241                 rss_key_len = MLX5_RSS_HASH_KEY_LEN;
11242         }
11243         for (i = 0; i < MLX5_RSS_HASH_FIELDS_LEN; i++) {
11244                 uint32_t hrxq_idx;
11245                 uint64_t hash_fields = mlx5_rss_hash_fields[i];
11246                 int tunnel;
11247
11248                 for (tunnel = 0; tunnel < 2; tunnel++) {
11249                         hrxq_idx = __flow_dv_action_rss_hrxq_lookup
11250                                         (shared_rss, hash_fields, tunnel);
11251                         MLX5_ASSERT(hrxq_idx);
11252                         ret = mlx5_hrxq_modify
11253                                 (dev, hrxq_idx,
11254                                  rss_key, rss_key_len,
11255                                  hash_fields,
11256                                  action_conf->queue, action_conf->queue_num);
11257                         if (ret) {
11258                                 mlx5_free(queue);
11259                                 return rte_flow_error_set
11260                                         (error, rte_errno,
11261                                          RTE_FLOW_ERROR_TYPE_ACTION, NULL,
11262                                          "cannot update hash queue");
11263                         }
11264                 }
11265         }
11266         mlx5_free(shared_rss->queue);
11267         shared_rss->queue = queue;
11268         memcpy(shared_rss->queue, action_conf->queue, queue_size);
11269         shared_rss->origin.queue = shared_rss->queue;
11270         shared_rss->origin.queue_num = action_conf->queue_num;
11271         return 0;
11272 }
11273
11274 /**
11275  * Updates in place shared action configuration, lock free,
11276  * (mutex should be acquired by caller).
11277  *
11278  * @param[in] dev
11279  *   Pointer to the Ethernet device structure.
11280  * @param[in] action
11281  *   The shared action object to be updated.
11282  * @param[in] action_conf
11283  *   Action specification used to modify *action*.
11284  *   *action_conf* should be of type correlating with type of the *action*,
11285  *   otherwise considered as invalid.
11286  * @param[out] error
11287  *   Perform verbose error reporting if not NULL. Initialized in case of
11288  *   error only.
11289  *
11290  * @return
11291  *   0 on success, otherwise negative errno value.
11292  */
11293 static int
11294 __flow_dv_action_update(struct rte_eth_dev *dev,
11295                         struct rte_flow_shared_action *action,
11296                         const void *action_conf,
11297                         struct rte_flow_error *error)
11298 {
11299         switch (action->type) {
11300         case MLX5_RTE_FLOW_ACTION_TYPE_SHARED_RSS:
11301                 return __flow_dv_action_rss_update(dev, &action->rss,
11302                                                    action_conf, error);
11303         default:
11304                 return rte_flow_error_set(error, ENOTSUP,
11305                                           RTE_FLOW_ERROR_TYPE_ACTION,
11306                                           NULL,
11307                                           "action type not supported");
11308         }
11309 }
11310 /**
11311  * Query a dv flow  rule for its statistics via devx.
11312  *
11313  * @param[in] dev
11314  *   Pointer to Ethernet device.
11315  * @param[in] flow
11316  *   Pointer to the sub flow.
11317  * @param[out] data
11318  *   data retrieved by the query.
11319  * @param[out] error
11320  *   Perform verbose error reporting if not NULL.
11321  *
11322  * @return
11323  *   0 on success, a negative errno value otherwise and rte_errno is set.
11324  */
11325 static int
11326 flow_dv_query_count(struct rte_eth_dev *dev, struct rte_flow *flow,
11327                     void *data, struct rte_flow_error *error)
11328 {
11329         struct mlx5_priv *priv = dev->data->dev_private;
11330         struct rte_flow_query_count *qc = data;
11331
11332         if (!priv->config.devx)
11333                 return rte_flow_error_set(error, ENOTSUP,
11334                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
11335                                           NULL,
11336                                           "counters are not supported");
11337         if (flow->counter) {
11338                 uint64_t pkts, bytes;
11339                 struct mlx5_flow_counter *cnt;
11340
11341                 cnt = flow_dv_counter_get_by_idx(dev, flow->counter,
11342                                                  NULL);
11343                 int err = _flow_dv_query_count(dev, flow->counter, &pkts,
11344                                                &bytes);
11345
11346                 if (err)
11347                         return rte_flow_error_set(error, -err,
11348                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
11349                                         NULL, "cannot read counters");
11350                 qc->hits_set = 1;
11351                 qc->bytes_set = 1;
11352                 qc->hits = pkts - cnt->hits;
11353                 qc->bytes = bytes - cnt->bytes;
11354                 if (qc->reset) {
11355                         cnt->hits = pkts;
11356                         cnt->bytes = bytes;
11357                 }
11358                 return 0;
11359         }
11360         return rte_flow_error_set(error, EINVAL,
11361                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
11362                                   NULL,
11363                                   "counters are not available");
11364 }
11365
11366 /**
11367  * Query a flow rule AGE action for aging information.
11368  *
11369  * @param[in] dev
11370  *   Pointer to Ethernet device.
11371  * @param[in] flow
11372  *   Pointer to the sub flow.
11373  * @param[out] data
11374  *   data retrieved by the query.
11375  * @param[out] error
11376  *   Perform verbose error reporting if not NULL.
11377  *
11378  * @return
11379  *   0 on success, a negative errno value otherwise and rte_errno is set.
11380  */
11381 static int
11382 flow_dv_query_age(struct rte_eth_dev *dev, struct rte_flow *flow,
11383                   void *data, struct rte_flow_error *error)
11384 {
11385         struct rte_flow_query_age *resp = data;
11386
11387         if (flow->counter) {
11388                 struct mlx5_age_param *age_param =
11389                                 flow_dv_counter_idx_get_age(dev, flow->counter);
11390
11391                 if (!age_param || !age_param->timeout)
11392                         return rte_flow_error_set
11393                                         (error, EINVAL,
11394                                          RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
11395                                          NULL, "cannot read age data");
11396                 resp->aged = __atomic_load_n(&age_param->state,
11397                                              __ATOMIC_RELAXED) ==
11398                                                         AGE_TMOUT ? 1 : 0;
11399                 resp->sec_since_last_hit_valid = !resp->aged;
11400                 if (resp->sec_since_last_hit_valid)
11401                         resp->sec_since_last_hit =
11402                                 __atomic_load_n(&age_param->sec_since_last_hit,
11403                                                 __ATOMIC_RELAXED);
11404                 return 0;
11405         }
11406         return rte_flow_error_set(error, EINVAL,
11407                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
11408                                   NULL,
11409                                   "age data not available");
11410 }
11411
11412 /**
11413  * Query a flow.
11414  *
11415  * @see rte_flow_query()
11416  * @see rte_flow_ops
11417  */
11418 static int
11419 flow_dv_query(struct rte_eth_dev *dev,
11420               struct rte_flow *flow __rte_unused,
11421               const struct rte_flow_action *actions __rte_unused,
11422               void *data __rte_unused,
11423               struct rte_flow_error *error __rte_unused)
11424 {
11425         int ret = -EINVAL;
11426
11427         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
11428                 switch (actions->type) {
11429                 case RTE_FLOW_ACTION_TYPE_VOID:
11430                         break;
11431                 case RTE_FLOW_ACTION_TYPE_COUNT:
11432                         ret = flow_dv_query_count(dev, flow, data, error);
11433                         break;
11434                 case RTE_FLOW_ACTION_TYPE_AGE:
11435                         ret = flow_dv_query_age(dev, flow, data, error);
11436                         break;
11437                 default:
11438                         return rte_flow_error_set(error, ENOTSUP,
11439                                                   RTE_FLOW_ERROR_TYPE_ACTION,
11440                                                   actions,
11441                                                   "action not supported");
11442                 }
11443         }
11444         return ret;
11445 }
11446
11447 /**
11448  * Destroy the meter table set.
11449  * Lock free, (mutex should be acquired by caller).
11450  *
11451  * @param[in] dev
11452  *   Pointer to Ethernet device.
11453  * @param[in] tbl
11454  *   Pointer to the meter table set.
11455  *
11456  * @return
11457  *   Always 0.
11458  */
11459 static int
11460 flow_dv_destroy_mtr_tbl(struct rte_eth_dev *dev,
11461                         struct mlx5_meter_domains_infos *tbl)
11462 {
11463         struct mlx5_priv *priv = dev->data->dev_private;
11464         struct mlx5_meter_domains_infos *mtd =
11465                                 (struct mlx5_meter_domains_infos *)tbl;
11466
11467         if (!mtd || !priv->config.dv_flow_en)
11468                 return 0;
11469         if (mtd->ingress.policer_rules[RTE_MTR_DROPPED])
11470                 claim_zero(mlx5_flow_os_destroy_flow
11471                            (mtd->ingress.policer_rules[RTE_MTR_DROPPED]));
11472         if (mtd->egress.policer_rules[RTE_MTR_DROPPED])
11473                 claim_zero(mlx5_flow_os_destroy_flow
11474                            (mtd->egress.policer_rules[RTE_MTR_DROPPED]));
11475         if (mtd->transfer.policer_rules[RTE_MTR_DROPPED])
11476                 claim_zero(mlx5_flow_os_destroy_flow
11477                            (mtd->transfer.policer_rules[RTE_MTR_DROPPED]));
11478         if (mtd->egress.color_matcher)
11479                 claim_zero(mlx5_flow_os_destroy_flow_matcher
11480                            (mtd->egress.color_matcher));
11481         if (mtd->egress.any_matcher)
11482                 claim_zero(mlx5_flow_os_destroy_flow_matcher
11483                            (mtd->egress.any_matcher));
11484         if (mtd->egress.tbl)
11485                 flow_dv_tbl_resource_release(dev, mtd->egress.tbl);
11486         if (mtd->egress.sfx_tbl)
11487                 flow_dv_tbl_resource_release(dev, mtd->egress.sfx_tbl);
11488         if (mtd->ingress.color_matcher)
11489                 claim_zero(mlx5_flow_os_destroy_flow_matcher
11490                            (mtd->ingress.color_matcher));
11491         if (mtd->ingress.any_matcher)
11492                 claim_zero(mlx5_flow_os_destroy_flow_matcher
11493                            (mtd->ingress.any_matcher));
11494         if (mtd->ingress.tbl)
11495                 flow_dv_tbl_resource_release(dev, mtd->ingress.tbl);
11496         if (mtd->ingress.sfx_tbl)
11497                 flow_dv_tbl_resource_release(dev, mtd->ingress.sfx_tbl);
11498         if (mtd->transfer.color_matcher)
11499                 claim_zero(mlx5_flow_os_destroy_flow_matcher
11500                            (mtd->transfer.color_matcher));
11501         if (mtd->transfer.any_matcher)
11502                 claim_zero(mlx5_flow_os_destroy_flow_matcher
11503                            (mtd->transfer.any_matcher));
11504         if (mtd->transfer.tbl)
11505                 flow_dv_tbl_resource_release(dev, mtd->transfer.tbl);
11506         if (mtd->transfer.sfx_tbl)
11507                 flow_dv_tbl_resource_release(dev, mtd->transfer.sfx_tbl);
11508         if (mtd->drop_actn)
11509                 claim_zero(mlx5_flow_os_destroy_flow_action(mtd->drop_actn));
11510         mlx5_free(mtd);
11511         return 0;
11512 }
11513
11514 /* Number of meter flow actions, count and jump or count and drop. */
11515 #define METER_ACTIONS 2
11516
11517 /**
11518  * Create specify domain meter table and suffix table.
11519  *
11520  * @param[in] dev
11521  *   Pointer to Ethernet device.
11522  * @param[in,out] mtb
11523  *   Pointer to DV meter table set.
11524  * @param[in] egress
11525  *   Table attribute.
11526  * @param[in] transfer
11527  *   Table attribute.
11528  * @param[in] color_reg_c_idx
11529  *   Reg C index for color match.
11530  *
11531  * @return
11532  *   0 on success, -1 otherwise and rte_errno is set.
11533  */
11534 static int
11535 flow_dv_prepare_mtr_tables(struct rte_eth_dev *dev,
11536                            struct mlx5_meter_domains_infos *mtb,
11537                            uint8_t egress, uint8_t transfer,
11538                            uint32_t color_reg_c_idx)
11539 {
11540         struct mlx5_priv *priv = dev->data->dev_private;
11541         struct mlx5_dev_ctx_shared *sh = priv->sh;
11542         struct mlx5_flow_dv_match_params mask = {
11543                 .size = sizeof(mask.buf),
11544         };
11545         struct mlx5_flow_dv_match_params value = {
11546                 .size = sizeof(value.buf),
11547         };
11548         struct mlx5dv_flow_matcher_attr dv_attr = {
11549                 .type = IBV_FLOW_ATTR_NORMAL,
11550                 .priority = 0,
11551                 .match_criteria_enable = 0,
11552                 .match_mask = (void *)&mask,
11553         };
11554         void *actions[METER_ACTIONS];
11555         struct mlx5_meter_domain_info *dtb;
11556         struct rte_flow_error error;
11557         int i = 0;
11558         int ret;
11559
11560         if (transfer)
11561                 dtb = &mtb->transfer;
11562         else if (egress)
11563                 dtb = &mtb->egress;
11564         else
11565                 dtb = &mtb->ingress;
11566         /* Create the meter table with METER level. */
11567         dtb->tbl = flow_dv_tbl_resource_get(dev, MLX5_FLOW_TABLE_LEVEL_METER,
11568                                             egress, transfer, false, NULL, 0,
11569                                             0, &error);
11570         if (!dtb->tbl) {
11571                 DRV_LOG(ERR, "Failed to create meter policer table.");
11572                 return -1;
11573         }
11574         /* Create the meter suffix table with SUFFIX level. */
11575         dtb->sfx_tbl = flow_dv_tbl_resource_get(dev,
11576                                             MLX5_FLOW_TABLE_LEVEL_SUFFIX,
11577                                             egress, transfer, false, NULL, 0,
11578                                             0, &error);
11579         if (!dtb->sfx_tbl) {
11580                 DRV_LOG(ERR, "Failed to create meter suffix table.");
11581                 return -1;
11582         }
11583         /* Create matchers, Any and Color. */
11584         dv_attr.priority = 3;
11585         dv_attr.match_criteria_enable = 0;
11586         ret = mlx5_flow_os_create_flow_matcher(sh->ctx, &dv_attr, dtb->tbl->obj,
11587                                                &dtb->any_matcher);
11588         if (ret) {
11589                 DRV_LOG(ERR, "Failed to create meter"
11590                              " policer default matcher.");
11591                 goto error_exit;
11592         }
11593         dv_attr.priority = 0;
11594         dv_attr.match_criteria_enable =
11595                                 1 << MLX5_MATCH_CRITERIA_ENABLE_MISC2_BIT;
11596         flow_dv_match_meta_reg(mask.buf, value.buf, color_reg_c_idx,
11597                                rte_col_2_mlx5_col(RTE_COLORS), UINT8_MAX);
11598         ret = mlx5_flow_os_create_flow_matcher(sh->ctx, &dv_attr, dtb->tbl->obj,
11599                                                &dtb->color_matcher);
11600         if (ret) {
11601                 DRV_LOG(ERR, "Failed to create meter policer color matcher.");
11602                 goto error_exit;
11603         }
11604         if (mtb->count_actns[RTE_MTR_DROPPED])
11605                 actions[i++] = mtb->count_actns[RTE_MTR_DROPPED];
11606         actions[i++] = mtb->drop_actn;
11607         /* Default rule: lowest priority, match any, actions: drop. */
11608         ret = mlx5_flow_os_create_flow(dtb->any_matcher, (void *)&value, i,
11609                                        actions,
11610                                        &dtb->policer_rules[RTE_MTR_DROPPED]);
11611         if (ret) {
11612                 DRV_LOG(ERR, "Failed to create meter policer drop rule.");
11613                 goto error_exit;
11614         }
11615         return 0;
11616 error_exit:
11617         return -1;
11618 }
11619
11620 /**
11621  * Create the needed meter and suffix tables.
11622  * Lock free, (mutex should be acquired by caller).
11623  *
11624  * @param[in] dev
11625  *   Pointer to Ethernet device.
11626  * @param[in] fm
11627  *   Pointer to the flow meter.
11628  *
11629  * @return
11630  *   Pointer to table set on success, NULL otherwise and rte_errno is set.
11631  */
11632 static struct mlx5_meter_domains_infos *
11633 flow_dv_create_mtr_tbl(struct rte_eth_dev *dev,
11634                        const struct mlx5_flow_meter *fm)
11635 {
11636         struct mlx5_priv *priv = dev->data->dev_private;
11637         struct mlx5_meter_domains_infos *mtb;
11638         int ret;
11639         int i;
11640
11641         if (!priv->mtr_en) {
11642                 rte_errno = ENOTSUP;
11643                 return NULL;
11644         }
11645         mtb = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*mtb), 0, SOCKET_ID_ANY);
11646         if (!mtb) {
11647                 DRV_LOG(ERR, "Failed to allocate memory for meter.");
11648                 return NULL;
11649         }
11650         /* Create meter count actions */
11651         for (i = 0; i <= RTE_MTR_DROPPED; i++) {
11652                 struct mlx5_flow_counter *cnt;
11653                 if (!fm->policer_stats.cnt[i])
11654                         continue;
11655                 cnt = flow_dv_counter_get_by_idx(dev,
11656                       fm->policer_stats.cnt[i], NULL);
11657                 mtb->count_actns[i] = cnt->action;
11658         }
11659         /* Create drop action. */
11660         ret = mlx5_flow_os_create_flow_action_drop(&mtb->drop_actn);
11661         if (ret) {
11662                 DRV_LOG(ERR, "Failed to create drop action.");
11663                 goto error_exit;
11664         }
11665         /* Egress meter table. */
11666         ret = flow_dv_prepare_mtr_tables(dev, mtb, 1, 0, priv->mtr_color_reg);
11667         if (ret) {
11668                 DRV_LOG(ERR, "Failed to prepare egress meter table.");
11669                 goto error_exit;
11670         }
11671         /* Ingress meter table. */
11672         ret = flow_dv_prepare_mtr_tables(dev, mtb, 0, 0, priv->mtr_color_reg);
11673         if (ret) {
11674                 DRV_LOG(ERR, "Failed to prepare ingress meter table.");
11675                 goto error_exit;
11676         }
11677         /* FDB meter table. */
11678         if (priv->config.dv_esw_en) {
11679                 ret = flow_dv_prepare_mtr_tables(dev, mtb, 0, 1,
11680                                                  priv->mtr_color_reg);
11681                 if (ret) {
11682                         DRV_LOG(ERR, "Failed to prepare fdb meter table.");
11683                         goto error_exit;
11684                 }
11685         }
11686         return mtb;
11687 error_exit:
11688         flow_dv_destroy_mtr_tbl(dev, mtb);
11689         return NULL;
11690 }
11691
11692 /**
11693  * Destroy domain policer rule.
11694  *
11695  * @param[in] dt
11696  *   Pointer to domain table.
11697  */
11698 static void
11699 flow_dv_destroy_domain_policer_rule(struct mlx5_meter_domain_info *dt)
11700 {
11701         int i;
11702
11703         for (i = 0; i < RTE_MTR_DROPPED; i++) {
11704                 if (dt->policer_rules[i]) {
11705                         claim_zero(mlx5_flow_os_destroy_flow
11706                                    (dt->policer_rules[i]));
11707                         dt->policer_rules[i] = NULL;
11708                 }
11709         }
11710         if (dt->jump_actn) {
11711                 claim_zero(mlx5_flow_os_destroy_flow_action(dt->jump_actn));
11712                 dt->jump_actn = NULL;
11713         }
11714 }
11715
11716 /**
11717  * Destroy policer rules.
11718  *
11719  * @param[in] dev
11720  *   Pointer to Ethernet device.
11721  * @param[in] fm
11722  *   Pointer to flow meter structure.
11723  * @param[in] attr
11724  *   Pointer to flow attributes.
11725  *
11726  * @return
11727  *   Always 0.
11728  */
11729 static int
11730 flow_dv_destroy_policer_rules(struct rte_eth_dev *dev __rte_unused,
11731                               const struct mlx5_flow_meter *fm,
11732                               const struct rte_flow_attr *attr)
11733 {
11734         struct mlx5_meter_domains_infos *mtb = fm ? fm->mfts : NULL;
11735
11736         if (!mtb)
11737                 return 0;
11738         if (attr->egress)
11739                 flow_dv_destroy_domain_policer_rule(&mtb->egress);
11740         if (attr->ingress)
11741                 flow_dv_destroy_domain_policer_rule(&mtb->ingress);
11742         if (attr->transfer)
11743                 flow_dv_destroy_domain_policer_rule(&mtb->transfer);
11744         return 0;
11745 }
11746
11747 /**
11748  * Create specify domain meter policer rule.
11749  *
11750  * @param[in] fm
11751  *   Pointer to flow meter structure.
11752  * @param[in] mtb
11753  *   Pointer to DV meter table set.
11754  * @param[in] mtr_reg_c
11755  *   Color match REG_C.
11756  *
11757  * @return
11758  *   0 on success, -1 otherwise.
11759  */
11760 static int
11761 flow_dv_create_policer_forward_rule(struct mlx5_flow_meter *fm,
11762                                     struct mlx5_meter_domain_info *dtb,
11763                                     uint8_t mtr_reg_c)
11764 {
11765         struct mlx5_flow_dv_match_params matcher = {
11766                 .size = sizeof(matcher.buf),
11767         };
11768         struct mlx5_flow_dv_match_params value = {
11769                 .size = sizeof(value.buf),
11770         };
11771         struct mlx5_meter_domains_infos *mtb = fm->mfts;
11772         void *actions[METER_ACTIONS];
11773         int i;
11774         int ret = 0;
11775
11776         /* Create jump action. */
11777         if (!dtb->jump_actn)
11778                 ret = mlx5_flow_os_create_flow_action_dest_flow_tbl
11779                                 (dtb->sfx_tbl->obj, &dtb->jump_actn);
11780         if (ret) {
11781                 DRV_LOG(ERR, "Failed to create policer jump action.");
11782                 goto error;
11783         }
11784         for (i = 0; i < RTE_MTR_DROPPED; i++) {
11785                 int j = 0;
11786
11787                 flow_dv_match_meta_reg(matcher.buf, value.buf, mtr_reg_c,
11788                                        rte_col_2_mlx5_col(i), UINT8_MAX);
11789                 if (mtb->count_actns[i])
11790                         actions[j++] = mtb->count_actns[i];
11791                 if (fm->action[i] == MTR_POLICER_ACTION_DROP)
11792                         actions[j++] = mtb->drop_actn;
11793                 else
11794                         actions[j++] = dtb->jump_actn;
11795                 ret = mlx5_flow_os_create_flow(dtb->color_matcher,
11796                                                (void *)&value, j, actions,
11797                                                &dtb->policer_rules[i]);
11798                 if (ret) {
11799                         DRV_LOG(ERR, "Failed to create policer rule.");
11800                         goto error;
11801                 }
11802         }
11803         return 0;
11804 error:
11805         rte_errno = errno;
11806         return -1;
11807 }
11808
11809 /**
11810  * Create policer rules.
11811  *
11812  * @param[in] dev
11813  *   Pointer to Ethernet device.
11814  * @param[in] fm
11815  *   Pointer to flow meter structure.
11816  * @param[in] attr
11817  *   Pointer to flow attributes.
11818  *
11819  * @return
11820  *   0 on success, -1 otherwise.
11821  */
11822 static int
11823 flow_dv_create_policer_rules(struct rte_eth_dev *dev,
11824                              struct mlx5_flow_meter *fm,
11825                              const struct rte_flow_attr *attr)
11826 {
11827         struct mlx5_priv *priv = dev->data->dev_private;
11828         struct mlx5_meter_domains_infos *mtb = fm->mfts;
11829         int ret;
11830
11831         if (attr->egress) {
11832                 ret = flow_dv_create_policer_forward_rule(fm, &mtb->egress,
11833                                                 priv->mtr_color_reg);
11834                 if (ret) {
11835                         DRV_LOG(ERR, "Failed to create egress policer.");
11836                         goto error;
11837                 }
11838         }
11839         if (attr->ingress) {
11840                 ret = flow_dv_create_policer_forward_rule(fm, &mtb->ingress,
11841                                                 priv->mtr_color_reg);
11842                 if (ret) {
11843                         DRV_LOG(ERR, "Failed to create ingress policer.");
11844                         goto error;
11845                 }
11846         }
11847         if (attr->transfer) {
11848                 ret = flow_dv_create_policer_forward_rule(fm, &mtb->transfer,
11849                                                 priv->mtr_color_reg);
11850                 if (ret) {
11851                         DRV_LOG(ERR, "Failed to create transfer policer.");
11852                         goto error;
11853                 }
11854         }
11855         return 0;
11856 error:
11857         flow_dv_destroy_policer_rules(dev, fm, attr);
11858         return -1;
11859 }
11860
11861 /**
11862  * Validate the batch counter support in root table.
11863  *
11864  * Create a simple flow with invalid counter and drop action on root table to
11865  * validate if batch counter with offset on root table is supported or not.
11866  *
11867  * @param[in] dev
11868  *   Pointer to rte_eth_dev structure.
11869  *
11870  * @return
11871  *   0 on success, a negative errno value otherwise and rte_errno is set.
11872  */
11873 int
11874 mlx5_flow_dv_discover_counter_offset_support(struct rte_eth_dev *dev)
11875 {
11876         struct mlx5_priv *priv = dev->data->dev_private;
11877         struct mlx5_dev_ctx_shared *sh = priv->sh;
11878         struct mlx5_flow_dv_match_params mask = {
11879                 .size = sizeof(mask.buf),
11880         };
11881         struct mlx5_flow_dv_match_params value = {
11882                 .size = sizeof(value.buf),
11883         };
11884         struct mlx5dv_flow_matcher_attr dv_attr = {
11885                 .type = IBV_FLOW_ATTR_NORMAL,
11886                 .priority = 0,
11887                 .match_criteria_enable = 0,
11888                 .match_mask = (void *)&mask,
11889         };
11890         void *actions[2] = { 0 };
11891         struct mlx5_flow_tbl_resource *tbl = NULL, *dest_tbl = NULL;
11892         struct mlx5_devx_obj *dcs = NULL;
11893         void *matcher = NULL;
11894         void *flow = NULL;
11895         int i, ret = -1;
11896
11897         tbl = flow_dv_tbl_resource_get(dev, 0, 0, 0, false, NULL, 0, 0, NULL);
11898         if (!tbl)
11899                 goto err;
11900         dest_tbl = flow_dv_tbl_resource_get(dev, 1, 0, 0, false,
11901                                             NULL, 0, 0, NULL);
11902         if (!dest_tbl)
11903                 goto err;
11904         dcs = mlx5_devx_cmd_flow_counter_alloc(priv->sh->ctx, 0x4);
11905         if (!dcs)
11906                 goto err;
11907         ret = mlx5_flow_os_create_flow_action_count(dcs->obj, UINT16_MAX,
11908                                                     &actions[0]);
11909         if (ret)
11910                 goto err;
11911         ret = mlx5_flow_os_create_flow_action_dest_flow_tbl
11912                                 (dest_tbl->obj, &actions[1]);
11913         if (ret)
11914                 goto err;
11915         dv_attr.match_criteria_enable = flow_dv_matcher_enable(mask.buf);
11916         ret = mlx5_flow_os_create_flow_matcher(sh->ctx, &dv_attr, tbl->obj,
11917                                                &matcher);
11918         if (ret)
11919                 goto err;
11920         ret = mlx5_flow_os_create_flow(matcher, (void *)&value, 2,
11921                                        actions, &flow);
11922 err:
11923         /*
11924          * If batch counter with offset is not supported, the driver will not
11925          * validate the invalid offset value, flow create should success.
11926          * In this case, it means batch counter is not supported in root table.
11927          *
11928          * Otherwise, if flow create is failed, counter offset is supported.
11929          */
11930         if (flow) {
11931                 DRV_LOG(INFO, "Batch counter is not supported in root "
11932                               "table. Switch to fallback mode.");
11933                 rte_errno = ENOTSUP;
11934                 ret = -rte_errno;
11935                 claim_zero(mlx5_flow_os_destroy_flow(flow));
11936         } else {
11937                 /* Check matcher to make sure validate fail at flow create. */
11938                 if (!matcher || (matcher && errno != EINVAL))
11939                         DRV_LOG(ERR, "Unexpected error in counter offset "
11940                                      "support detection");
11941                 ret = 0;
11942         }
11943         for (i = 0; i < 2; i++) {
11944                 if (actions[i])
11945                         claim_zero(mlx5_flow_os_destroy_flow_action
11946                                    (actions[i]));
11947         }
11948         if (matcher)
11949                 claim_zero(mlx5_flow_os_destroy_flow_matcher(matcher));
11950         if (tbl)
11951                 flow_dv_tbl_resource_release(dev, tbl);
11952         if (dest_tbl)
11953                 flow_dv_tbl_resource_release(dev, dest_tbl);
11954         if (dcs)
11955                 claim_zero(mlx5_devx_cmd_destroy(dcs));
11956         return ret;
11957 }
11958
11959 /**
11960  * Query a devx counter.
11961  *
11962  * @param[in] dev
11963  *   Pointer to the Ethernet device structure.
11964  * @param[in] cnt
11965  *   Index to the flow counter.
11966  * @param[in] clear
11967  *   Set to clear the counter statistics.
11968  * @param[out] pkts
11969  *   The statistics value of packets.
11970  * @param[out] bytes
11971  *   The statistics value of bytes.
11972  *
11973  * @return
11974  *   0 on success, otherwise return -1.
11975  */
11976 static int
11977 flow_dv_counter_query(struct rte_eth_dev *dev, uint32_t counter, bool clear,
11978                       uint64_t *pkts, uint64_t *bytes)
11979 {
11980         struct mlx5_priv *priv = dev->data->dev_private;
11981         struct mlx5_flow_counter *cnt;
11982         uint64_t inn_pkts, inn_bytes;
11983         int ret;
11984
11985         if (!priv->config.devx)
11986                 return -1;
11987
11988         ret = _flow_dv_query_count(dev, counter, &inn_pkts, &inn_bytes);
11989         if (ret)
11990                 return -1;
11991         cnt = flow_dv_counter_get_by_idx(dev, counter, NULL);
11992         *pkts = inn_pkts - cnt->hits;
11993         *bytes = inn_bytes - cnt->bytes;
11994         if (clear) {
11995                 cnt->hits = inn_pkts;
11996                 cnt->bytes = inn_bytes;
11997         }
11998         return 0;
11999 }
12000
12001 /**
12002  * Get aged-out flows.
12003  *
12004  * @param[in] dev
12005  *   Pointer to the Ethernet device structure.
12006  * @param[in] context
12007  *   The address of an array of pointers to the aged-out flows contexts.
12008  * @param[in] nb_contexts
12009  *   The length of context array pointers.
12010  * @param[out] error
12011  *   Perform verbose error reporting if not NULL. Initialized in case of
12012  *   error only.
12013  *
12014  * @return
12015  *   how many contexts get in success, otherwise negative errno value.
12016  *   if nb_contexts is 0, return the amount of all aged contexts.
12017  *   if nb_contexts is not 0 , return the amount of aged flows reported
12018  *   in the context array.
12019  * @note: only stub for now
12020  */
12021 static int
12022 flow_get_aged_flows(struct rte_eth_dev *dev,
12023                     void **context,
12024                     uint32_t nb_contexts,
12025                     struct rte_flow_error *error)
12026 {
12027         struct mlx5_priv *priv = dev->data->dev_private;
12028         struct mlx5_age_info *age_info;
12029         struct mlx5_age_param *age_param;
12030         struct mlx5_flow_counter *counter;
12031         int nb_flows = 0;
12032
12033         if (nb_contexts && !context)
12034                 return rte_flow_error_set(error, EINVAL,
12035                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
12036                                           NULL,
12037                                           "Should assign at least one flow or"
12038                                           " context to get if nb_contexts != 0");
12039         age_info = GET_PORT_AGE_INFO(priv);
12040         rte_spinlock_lock(&age_info->aged_sl);
12041         TAILQ_FOREACH(counter, &age_info->aged_counters, next) {
12042                 nb_flows++;
12043                 if (nb_contexts) {
12044                         age_param = MLX5_CNT_TO_AGE(counter);
12045                         context[nb_flows - 1] = age_param->context;
12046                         if (!(--nb_contexts))
12047                                 break;
12048                 }
12049         }
12050         rte_spinlock_unlock(&age_info->aged_sl);
12051         MLX5_AGE_SET(age_info, MLX5_AGE_TRIGGER);
12052         return nb_flows;
12053 }
12054
12055 /*
12056  * Mutex-protected thunk to lock-free  __flow_dv_translate().
12057  */
12058 static int
12059 flow_dv_translate(struct rte_eth_dev *dev,
12060                   struct mlx5_flow *dev_flow,
12061                   const struct rte_flow_attr *attr,
12062                   const struct rte_flow_item items[],
12063                   const struct rte_flow_action actions[],
12064                   struct rte_flow_error *error)
12065 {
12066         int ret;
12067
12068         flow_dv_shared_lock(dev);
12069         ret = __flow_dv_translate(dev, dev_flow, attr, items, actions, error);
12070         flow_dv_shared_unlock(dev);
12071         return ret;
12072 }
12073
12074 /*
12075  * Mutex-protected thunk to lock-free  __flow_dv_apply().
12076  */
12077 static int
12078 flow_dv_apply(struct rte_eth_dev *dev,
12079               struct rte_flow *flow,
12080               struct rte_flow_error *error)
12081 {
12082         int ret;
12083
12084         flow_dv_shared_lock(dev);
12085         ret = __flow_dv_apply(dev, flow, error);
12086         flow_dv_shared_unlock(dev);
12087         return ret;
12088 }
12089
12090 /*
12091  * Mutex-protected thunk to lock-free __flow_dv_remove().
12092  */
12093 static void
12094 flow_dv_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
12095 {
12096         flow_dv_shared_lock(dev);
12097         __flow_dv_remove(dev, flow);
12098         flow_dv_shared_unlock(dev);
12099 }
12100
12101 /*
12102  * Mutex-protected thunk to lock-free __flow_dv_destroy().
12103  */
12104 static void
12105 flow_dv_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
12106 {
12107         flow_dv_shared_lock(dev);
12108         __flow_dv_destroy(dev, flow);
12109         flow_dv_shared_unlock(dev);
12110 }
12111
12112 /*
12113  * Mutex-protected thunk to lock-free flow_dv_counter_alloc().
12114  */
12115 static uint32_t
12116 flow_dv_counter_allocate(struct rte_eth_dev *dev)
12117 {
12118         uint32_t cnt;
12119
12120         flow_dv_shared_lock(dev);
12121         cnt = flow_dv_counter_alloc(dev, 0);
12122         flow_dv_shared_unlock(dev);
12123         return cnt;
12124 }
12125
12126 /*
12127  * Mutex-protected thunk to lock-free flow_dv_counter_release().
12128  */
12129 static void
12130 flow_dv_counter_free(struct rte_eth_dev *dev, uint32_t cnt)
12131 {
12132         flow_dv_shared_lock(dev);
12133         flow_dv_counter_release(dev, cnt);
12134         flow_dv_shared_unlock(dev);
12135 }
12136
12137 /**
12138  * Validate shared action.
12139  * Dispatcher for action type specific validation.
12140  *
12141  * @param[in] dev
12142  *   Pointer to the Ethernet device structure.
12143  * @param[in] conf
12144  *   Shared action configuration.
12145  * @param[in] action
12146  *   The shared action object to validate.
12147  * @param[out] error
12148  *   Perform verbose error reporting if not NULL. Initialized in case of
12149  *   error only.
12150  *
12151  * @return
12152  *   0 on success, otherwise negative errno value.
12153  */
12154 static int
12155 flow_dv_action_validate(struct rte_eth_dev *dev,
12156                         const struct rte_flow_shared_action_conf *conf,
12157                         const struct rte_flow_action *action,
12158                         struct rte_flow_error *error)
12159 {
12160         RTE_SET_USED(conf);
12161         switch (action->type) {
12162         case RTE_FLOW_ACTION_TYPE_RSS:
12163                 return mlx5_validate_action_rss(dev, action, error);
12164         default:
12165                 return rte_flow_error_set(error, ENOTSUP,
12166                                           RTE_FLOW_ERROR_TYPE_ACTION,
12167                                           NULL,
12168                                           "action type not supported");
12169         }
12170 }
12171
12172 /*
12173  * Mutex-protected thunk to lock-free  __flow_dv_action_create().
12174  */
12175 static struct rte_flow_shared_action *
12176 flow_dv_action_create(struct rte_eth_dev *dev,
12177                       const struct rte_flow_shared_action_conf *conf,
12178                       const struct rte_flow_action *action,
12179                       struct rte_flow_error *error)
12180 {
12181         struct rte_flow_shared_action *shared_action = NULL;
12182
12183         flow_dv_shared_lock(dev);
12184         shared_action = __flow_dv_action_create(dev, conf, action, error);
12185         flow_dv_shared_unlock(dev);
12186         return shared_action;
12187 }
12188
12189 /*
12190  * Mutex-protected thunk to lock-free  __flow_dv_action_destroy().
12191  */
12192 static int
12193 flow_dv_action_destroy(struct rte_eth_dev *dev,
12194                        struct rte_flow_shared_action *action,
12195                        struct rte_flow_error *error)
12196 {
12197         int ret;
12198
12199         flow_dv_shared_lock(dev);
12200         ret = __flow_dv_action_destroy(dev, action, error);
12201         flow_dv_shared_unlock(dev);
12202         return ret;
12203 }
12204
12205 /*
12206  * Mutex-protected thunk to lock-free  __flow_dv_action_update().
12207  */
12208 static int
12209 flow_dv_action_update(struct rte_eth_dev *dev,
12210                       struct rte_flow_shared_action *action,
12211                       const void *action_conf,
12212                       struct rte_flow_error *error)
12213 {
12214         int ret;
12215
12216         flow_dv_shared_lock(dev);
12217         ret = __flow_dv_action_update(dev, action, action_conf,
12218                                       error);
12219         flow_dv_shared_unlock(dev);
12220         return ret;
12221 }
12222
12223 static int
12224 flow_dv_sync_domain(struct rte_eth_dev *dev, uint32_t domains, uint32_t flags)
12225 {
12226         struct mlx5_priv *priv = dev->data->dev_private;
12227         int ret = 0;
12228
12229         if ((domains & MLX5_DOMAIN_BIT_NIC_RX) && priv->sh->rx_domain != NULL) {
12230                 ret = mlx5_glue->dr_sync_domain(priv->sh->rx_domain,
12231                                                 flags);
12232                 if (ret != 0)
12233                         return ret;
12234         }
12235         if ((domains & MLX5_DOMAIN_BIT_NIC_TX) && priv->sh->tx_domain != NULL) {
12236                 ret = mlx5_glue->dr_sync_domain(priv->sh->tx_domain, flags);
12237                 if (ret != 0)
12238                         return ret;
12239         }
12240         if ((domains & MLX5_DOMAIN_BIT_FDB) && priv->sh->fdb_domain != NULL) {
12241                 ret = mlx5_glue->dr_sync_domain(priv->sh->fdb_domain, flags);
12242                 if (ret != 0)
12243                         return ret;
12244         }
12245         return 0;
12246 }
12247
12248 const struct mlx5_flow_driver_ops mlx5_flow_dv_drv_ops = {
12249         .validate = flow_dv_validate,
12250         .prepare = flow_dv_prepare,
12251         .translate = flow_dv_translate,
12252         .apply = flow_dv_apply,
12253         .remove = flow_dv_remove,
12254         .destroy = flow_dv_destroy,
12255         .query = flow_dv_query,
12256         .create_mtr_tbls = flow_dv_create_mtr_tbl,
12257         .destroy_mtr_tbls = flow_dv_destroy_mtr_tbl,
12258         .create_policer_rules = flow_dv_create_policer_rules,
12259         .destroy_policer_rules = flow_dv_destroy_policer_rules,
12260         .counter_alloc = flow_dv_counter_allocate,
12261         .counter_free = flow_dv_counter_free,
12262         .counter_query = flow_dv_counter_query,
12263         .get_aged_flows = flow_get_aged_flows,
12264         .action_validate = flow_dv_action_validate,
12265         .action_create = flow_dv_action_create,
12266         .action_destroy = flow_dv_action_destroy,
12267         .action_update = flow_dv_action_update,
12268         .sync_domain = flow_dv_sync_domain,
12269 };
12270
12271 #endif /* HAVE_IBV_FLOW_DV_SUPPORT */
12272