net/mlx5: fix check for VLAN actions
[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 /* Verbs header. */
12 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
13 #ifdef PEDANTIC
14 #pragma GCC diagnostic ignored "-Wpedantic"
15 #endif
16 #include <infiniband/verbs.h>
17 #ifdef PEDANTIC
18 #pragma GCC diagnostic error "-Wpedantic"
19 #endif
20
21 #include <rte_common.h>
22 #include <rte_ether.h>
23 #include <rte_ethdev_driver.h>
24 #include <rte_flow.h>
25 #include <rte_flow_driver.h>
26 #include <rte_malloc.h>
27 #include <rte_ip.h>
28 #include <rte_gre.h>
29 #include <rte_vxlan.h>
30 #include <rte_gtp.h>
31
32 #include "mlx5.h"
33 #include "mlx5_defs.h"
34 #include "mlx5_glue.h"
35 #include "mlx5_flow.h"
36 #include "mlx5_prm.h"
37 #include "mlx5_rxtx.h"
38
39 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
40
41 #ifndef HAVE_IBV_FLOW_DEVX_COUNTERS
42 #define MLX5DV_FLOW_ACTION_COUNTERS_DEVX 0
43 #endif
44
45 #ifndef HAVE_MLX5DV_DR_ESWITCH
46 #ifndef MLX5DV_FLOW_TABLE_TYPE_FDB
47 #define MLX5DV_FLOW_TABLE_TYPE_FDB 0
48 #endif
49 #endif
50
51 #ifndef HAVE_MLX5DV_DR
52 #define MLX5DV_DR_ACTION_FLAGS_ROOT_LEVEL 1
53 #endif
54
55 #define MLX5_ENCAPSULATION_DECISION_SIZE (sizeof(struct rte_flow_item_eth) + \
56                                           sizeof(struct rte_flow_item_ipv4))
57 /* VLAN header definitions */
58 #define MLX5DV_FLOW_VLAN_PCP_SHIFT 13
59 #define MLX5DV_FLOW_VLAN_PCP_MASK (0x7 << MLX5DV_FLOW_VLAN_PCP_SHIFT)
60 #define MLX5DV_FLOW_VLAN_VID_MASK 0x0fff
61 #define MLX5DV_FLOW_VLAN_PCP_MASK_BE RTE_BE16(MLX5DV_FLOW_VLAN_PCP_MASK)
62 #define MLX5DV_FLOW_VLAN_VID_MASK_BE RTE_BE16(MLX5DV_FLOW_VLAN_VID_MASK)
63
64 union flow_dv_attr {
65         struct {
66                 uint32_t valid:1;
67                 uint32_t ipv4:1;
68                 uint32_t ipv6:1;
69                 uint32_t tcp:1;
70                 uint32_t udp:1;
71                 uint32_t reserved:27;
72         };
73         uint32_t attr;
74 };
75
76 /**
77  * Initialize flow attributes structure according to flow items' types.
78  *
79  * flow_dv_validate() avoids multiple L3/L4 layers cases other than tunnel
80  * mode. For tunnel mode, the items to be modified are the outermost ones.
81  *
82  * @param[in] item
83  *   Pointer to item specification.
84  * @param[out] attr
85  *   Pointer to flow attributes structure.
86  */
87 static void
88 flow_dv_attr_init(const struct rte_flow_item *item, union flow_dv_attr *attr)
89 {
90         for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
91                 switch (item->type) {
92                 case RTE_FLOW_ITEM_TYPE_IPV4:
93                         if (!attr->ipv6)
94                                 attr->ipv4 = 1;
95                         break;
96                 case RTE_FLOW_ITEM_TYPE_IPV6:
97                         if (!attr->ipv4)
98                                 attr->ipv6 = 1;
99                         break;
100                 case RTE_FLOW_ITEM_TYPE_UDP:
101                         if (!attr->tcp)
102                                 attr->udp = 1;
103                         break;
104                 case RTE_FLOW_ITEM_TYPE_TCP:
105                         if (!attr->udp)
106                                 attr->tcp = 1;
107                         break;
108                 default:
109                         break;
110                 }
111         }
112         attr->valid = 1;
113 }
114
115 /**
116  * Convert rte_mtr_color to mlx5 color.
117  *
118  * @param[in] rcol
119  *   rte_mtr_color.
120  *
121  * @return
122  *   mlx5 color.
123  */
124 static int
125 rte_col_2_mlx5_col(enum rte_color rcol)
126 {
127         switch (rcol) {
128         case RTE_COLOR_GREEN:
129                 return MLX5_FLOW_COLOR_GREEN;
130         case RTE_COLOR_YELLOW:
131                 return MLX5_FLOW_COLOR_YELLOW;
132         case RTE_COLOR_RED:
133                 return MLX5_FLOW_COLOR_RED;
134         default:
135                 break;
136         }
137         return MLX5_FLOW_COLOR_UNDEFINED;
138 }
139
140 struct field_modify_info {
141         uint32_t size; /* Size of field in protocol header, in bytes. */
142         uint32_t offset; /* Offset of field in protocol header, in bytes. */
143         enum mlx5_modification_field id;
144 };
145
146 struct field_modify_info modify_eth[] = {
147         {4,  0, MLX5_MODI_OUT_DMAC_47_16},
148         {2,  4, MLX5_MODI_OUT_DMAC_15_0},
149         {4,  6, MLX5_MODI_OUT_SMAC_47_16},
150         {2, 10, MLX5_MODI_OUT_SMAC_15_0},
151         {0, 0, 0},
152 };
153
154 struct field_modify_info modify_vlan_out_first_vid[] = {
155         /* Size in bits !!! */
156         {12, 0, MLX5_MODI_OUT_FIRST_VID},
157         {0, 0, 0},
158 };
159
160 struct field_modify_info modify_ipv4[] = {
161         {1,  1, MLX5_MODI_OUT_IP_DSCP},
162         {1,  8, MLX5_MODI_OUT_IPV4_TTL},
163         {4, 12, MLX5_MODI_OUT_SIPV4},
164         {4, 16, MLX5_MODI_OUT_DIPV4},
165         {0, 0, 0},
166 };
167
168 struct field_modify_info modify_ipv6[] = {
169         {1,  0, MLX5_MODI_OUT_IP_DSCP},
170         {1,  7, MLX5_MODI_OUT_IPV6_HOPLIMIT},
171         {4,  8, MLX5_MODI_OUT_SIPV6_127_96},
172         {4, 12, MLX5_MODI_OUT_SIPV6_95_64},
173         {4, 16, MLX5_MODI_OUT_SIPV6_63_32},
174         {4, 20, MLX5_MODI_OUT_SIPV6_31_0},
175         {4, 24, MLX5_MODI_OUT_DIPV6_127_96},
176         {4, 28, MLX5_MODI_OUT_DIPV6_95_64},
177         {4, 32, MLX5_MODI_OUT_DIPV6_63_32},
178         {4, 36, MLX5_MODI_OUT_DIPV6_31_0},
179         {0, 0, 0},
180 };
181
182 struct field_modify_info modify_udp[] = {
183         {2, 0, MLX5_MODI_OUT_UDP_SPORT},
184         {2, 2, MLX5_MODI_OUT_UDP_DPORT},
185         {0, 0, 0},
186 };
187
188 struct field_modify_info modify_tcp[] = {
189         {2, 0, MLX5_MODI_OUT_TCP_SPORT},
190         {2, 2, MLX5_MODI_OUT_TCP_DPORT},
191         {4, 4, MLX5_MODI_OUT_TCP_SEQ_NUM},
192         {4, 8, MLX5_MODI_OUT_TCP_ACK_NUM},
193         {0, 0, 0},
194 };
195
196 static void
197 mlx5_flow_tunnel_ip_check(const struct rte_flow_item *item __rte_unused,
198                           uint8_t next_protocol, uint64_t *item_flags,
199                           int *tunnel)
200 {
201         assert(item->type == RTE_FLOW_ITEM_TYPE_IPV4 ||
202                item->type == RTE_FLOW_ITEM_TYPE_IPV6);
203         if (next_protocol == IPPROTO_IPIP) {
204                 *item_flags |= MLX5_FLOW_LAYER_IPIP;
205                 *tunnel = 1;
206         }
207         if (next_protocol == IPPROTO_IPV6) {
208                 *item_flags |= MLX5_FLOW_LAYER_IPV6_ENCAP;
209                 *tunnel = 1;
210         }
211 }
212
213 /**
214  * Acquire the synchronizing object to protect multithreaded access
215  * to shared dv context. Lock occurs only if context is actually
216  * shared, i.e. we have multiport IB device and representors are
217  * created.
218  *
219  * @param[in] dev
220  *   Pointer to the rte_eth_dev structure.
221  */
222 static void
223 flow_dv_shared_lock(struct rte_eth_dev *dev)
224 {
225         struct mlx5_priv *priv = dev->data->dev_private;
226         struct mlx5_ibv_shared *sh = priv->sh;
227
228         if (sh->dv_refcnt > 1) {
229                 int ret;
230
231                 ret = pthread_mutex_lock(&sh->dv_mutex);
232                 assert(!ret);
233                 (void)ret;
234         }
235 }
236
237 static void
238 flow_dv_shared_unlock(struct rte_eth_dev *dev)
239 {
240         struct mlx5_priv *priv = dev->data->dev_private;
241         struct mlx5_ibv_shared *sh = priv->sh;
242
243         if (sh->dv_refcnt > 1) {
244                 int ret;
245
246                 ret = pthread_mutex_unlock(&sh->dv_mutex);
247                 assert(!ret);
248                 (void)ret;
249         }
250 }
251
252 /* Update VLAN's VID/PCP based on input rte_flow_action.
253  *
254  * @param[in] action
255  *   Pointer to struct rte_flow_action.
256  * @param[out] vlan
257  *   Pointer to struct rte_vlan_hdr.
258  */
259 static void
260 mlx5_update_vlan_vid_pcp(const struct rte_flow_action *action,
261                          struct rte_vlan_hdr *vlan)
262 {
263         uint16_t vlan_tci;
264         if (action->type == RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP) {
265                 vlan_tci =
266                     ((const struct rte_flow_action_of_set_vlan_pcp *)
267                                                action->conf)->vlan_pcp;
268                 vlan_tci = vlan_tci << MLX5DV_FLOW_VLAN_PCP_SHIFT;
269                 vlan->vlan_tci &= ~MLX5DV_FLOW_VLAN_PCP_MASK;
270                 vlan->vlan_tci |= vlan_tci;
271         } else if (action->type == RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID) {
272                 vlan->vlan_tci &= ~MLX5DV_FLOW_VLAN_VID_MASK;
273                 vlan->vlan_tci |= rte_be_to_cpu_16
274                     (((const struct rte_flow_action_of_set_vlan_vid *)
275                                              action->conf)->vlan_vid);
276         }
277 }
278
279 /**
280  * Fetch 1, 2, 3 or 4 byte field from the byte array
281  * and return as unsigned integer in host-endian format.
282  *
283  * @param[in] data
284  *   Pointer to data array.
285  * @param[in] size
286  *   Size of field to extract.
287  *
288  * @return
289  *   converted field in host endian format.
290  */
291 static inline uint32_t
292 flow_dv_fetch_field(const uint8_t *data, uint32_t size)
293 {
294         uint32_t ret;
295
296         switch (size) {
297         case 1:
298                 ret = *data;
299                 break;
300         case 2:
301                 ret = rte_be_to_cpu_16(*(const unaligned_uint16_t *)data);
302                 break;
303         case 3:
304                 ret = rte_be_to_cpu_16(*(const unaligned_uint16_t *)data);
305                 ret = (ret << 8) | *(data + sizeof(uint16_t));
306                 break;
307         case 4:
308                 ret = rte_be_to_cpu_32(*(const unaligned_uint32_t *)data);
309                 break;
310         default:
311                 assert(false);
312                 ret = 0;
313                 break;
314         }
315         return ret;
316 }
317
318 /**
319  * Convert modify-header action to DV specification.
320  *
321  * Data length of each action is determined by provided field description
322  * and the item mask. Data bit offset and width of each action is determined
323  * by provided item mask.
324  *
325  * @param[in] item
326  *   Pointer to item specification.
327  * @param[in] field
328  *   Pointer to field modification information.
329  *     For MLX5_MODIFICATION_TYPE_SET specifies destination field.
330  *     For MLX5_MODIFICATION_TYPE_ADD specifies destination field.
331  *     For MLX5_MODIFICATION_TYPE_COPY specifies source field.
332  * @param[in] dcopy
333  *   Destination field info for MLX5_MODIFICATION_TYPE_COPY in @type.
334  *   Negative offset value sets the same offset as source offset.
335  *   size field is ignored, value is taken from source field.
336  * @param[in,out] resource
337  *   Pointer to the modify-header resource.
338  * @param[in] type
339  *   Type of modification.
340  * @param[out] error
341  *   Pointer to the error structure.
342  *
343  * @return
344  *   0 on success, a negative errno value otherwise and rte_errno is set.
345  */
346 static int
347 flow_dv_convert_modify_action(struct rte_flow_item *item,
348                               struct field_modify_info *field,
349                               struct field_modify_info *dcopy,
350                               struct mlx5_flow_dv_modify_hdr_resource *resource,
351                               uint32_t type, struct rte_flow_error *error)
352 {
353         uint32_t i = resource->actions_num;
354         struct mlx5_modification_cmd *actions = resource->actions;
355
356         /*
357          * The item and mask are provided in big-endian format.
358          * The fields should be presented as in big-endian format either.
359          * Mask must be always present, it defines the actual field width.
360          */
361         assert(item->mask);
362         assert(field->size);
363         do {
364                 unsigned int size_b;
365                 unsigned int off_b;
366                 uint32_t mask;
367                 uint32_t data;
368
369                 if (i >= MLX5_MAX_MODIFY_NUM)
370                         return rte_flow_error_set(error, EINVAL,
371                                  RTE_FLOW_ERROR_TYPE_ACTION, NULL,
372                                  "too many items to modify");
373                 /* Fetch variable byte size mask from the array. */
374                 mask = flow_dv_fetch_field((const uint8_t *)item->mask +
375                                            field->offset, field->size);
376                 if (!mask) {
377                         ++field;
378                         continue;
379                 }
380                 /* Deduce actual data width in bits from mask value. */
381                 off_b = rte_bsf32(mask);
382                 size_b = sizeof(uint32_t) * CHAR_BIT -
383                          off_b - __builtin_clz(mask);
384                 assert(size_b);
385                 size_b = size_b == sizeof(uint32_t) * CHAR_BIT ? 0 : size_b;
386                 actions[i].action_type = type;
387                 actions[i].field = field->id;
388                 actions[i].offset = off_b;
389                 actions[i].length = size_b;
390                 /* Convert entire record to expected big-endian format. */
391                 actions[i].data0 = rte_cpu_to_be_32(actions[i].data0);
392                 if (type == MLX5_MODIFICATION_TYPE_COPY) {
393                         assert(dcopy);
394                         actions[i].dst_field = dcopy->id;
395                         actions[i].dst_offset =
396                                 (int)dcopy->offset < 0 ? off_b : dcopy->offset;
397                         /* Convert entire record to big-endian format. */
398                         actions[i].data1 = rte_cpu_to_be_32(actions[i].data1);
399                 } else {
400                         assert(item->spec);
401                         data = flow_dv_fetch_field((const uint8_t *)item->spec +
402                                                    field->offset, field->size);
403                         /* Shift out the trailing masked bits from data. */
404                         data = (data & mask) >> off_b;
405                         actions[i].data1 = rte_cpu_to_be_32(data);
406                 }
407                 ++i;
408                 ++field;
409         } while (field->size);
410         if (resource->actions_num == i)
411                 return rte_flow_error_set(error, EINVAL,
412                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
413                                           "invalid modification flow item");
414         resource->actions_num = i;
415         return 0;
416 }
417
418 /**
419  * Convert modify-header set IPv4 address action to DV specification.
420  *
421  * @param[in,out] resource
422  *   Pointer to the modify-header resource.
423  * @param[in] action
424  *   Pointer to action specification.
425  * @param[out] error
426  *   Pointer to the error structure.
427  *
428  * @return
429  *   0 on success, a negative errno value otherwise and rte_errno is set.
430  */
431 static int
432 flow_dv_convert_action_modify_ipv4
433                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
434                          const struct rte_flow_action *action,
435                          struct rte_flow_error *error)
436 {
437         const struct rte_flow_action_set_ipv4 *conf =
438                 (const struct rte_flow_action_set_ipv4 *)(action->conf);
439         struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_IPV4 };
440         struct rte_flow_item_ipv4 ipv4;
441         struct rte_flow_item_ipv4 ipv4_mask;
442
443         memset(&ipv4, 0, sizeof(ipv4));
444         memset(&ipv4_mask, 0, sizeof(ipv4_mask));
445         if (action->type == RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC) {
446                 ipv4.hdr.src_addr = conf->ipv4_addr;
447                 ipv4_mask.hdr.src_addr = rte_flow_item_ipv4_mask.hdr.src_addr;
448         } else {
449                 ipv4.hdr.dst_addr = conf->ipv4_addr;
450                 ipv4_mask.hdr.dst_addr = rte_flow_item_ipv4_mask.hdr.dst_addr;
451         }
452         item.spec = &ipv4;
453         item.mask = &ipv4_mask;
454         return flow_dv_convert_modify_action(&item, modify_ipv4, NULL, resource,
455                                              MLX5_MODIFICATION_TYPE_SET, error);
456 }
457
458 /**
459  * Convert modify-header set IPv6 address action to DV specification.
460  *
461  * @param[in,out] resource
462  *   Pointer to the modify-header resource.
463  * @param[in] action
464  *   Pointer to action specification.
465  * @param[out] error
466  *   Pointer to the error structure.
467  *
468  * @return
469  *   0 on success, a negative errno value otherwise and rte_errno is set.
470  */
471 static int
472 flow_dv_convert_action_modify_ipv6
473                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
474                          const struct rte_flow_action *action,
475                          struct rte_flow_error *error)
476 {
477         const struct rte_flow_action_set_ipv6 *conf =
478                 (const struct rte_flow_action_set_ipv6 *)(action->conf);
479         struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_IPV6 };
480         struct rte_flow_item_ipv6 ipv6;
481         struct rte_flow_item_ipv6 ipv6_mask;
482
483         memset(&ipv6, 0, sizeof(ipv6));
484         memset(&ipv6_mask, 0, sizeof(ipv6_mask));
485         if (action->type == RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC) {
486                 memcpy(&ipv6.hdr.src_addr, &conf->ipv6_addr,
487                        sizeof(ipv6.hdr.src_addr));
488                 memcpy(&ipv6_mask.hdr.src_addr,
489                        &rte_flow_item_ipv6_mask.hdr.src_addr,
490                        sizeof(ipv6.hdr.src_addr));
491         } else {
492                 memcpy(&ipv6.hdr.dst_addr, &conf->ipv6_addr,
493                        sizeof(ipv6.hdr.dst_addr));
494                 memcpy(&ipv6_mask.hdr.dst_addr,
495                        &rte_flow_item_ipv6_mask.hdr.dst_addr,
496                        sizeof(ipv6.hdr.dst_addr));
497         }
498         item.spec = &ipv6;
499         item.mask = &ipv6_mask;
500         return flow_dv_convert_modify_action(&item, modify_ipv6, NULL, resource,
501                                              MLX5_MODIFICATION_TYPE_SET, error);
502 }
503
504 /**
505  * Convert modify-header set MAC address action to DV specification.
506  *
507  * @param[in,out] resource
508  *   Pointer to the modify-header resource.
509  * @param[in] action
510  *   Pointer to action specification.
511  * @param[out] error
512  *   Pointer to the error structure.
513  *
514  * @return
515  *   0 on success, a negative errno value otherwise and rte_errno is set.
516  */
517 static int
518 flow_dv_convert_action_modify_mac
519                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
520                          const struct rte_flow_action *action,
521                          struct rte_flow_error *error)
522 {
523         const struct rte_flow_action_set_mac *conf =
524                 (const struct rte_flow_action_set_mac *)(action->conf);
525         struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_ETH };
526         struct rte_flow_item_eth eth;
527         struct rte_flow_item_eth eth_mask;
528
529         memset(&eth, 0, sizeof(eth));
530         memset(&eth_mask, 0, sizeof(eth_mask));
531         if (action->type == RTE_FLOW_ACTION_TYPE_SET_MAC_SRC) {
532                 memcpy(&eth.src.addr_bytes, &conf->mac_addr,
533                        sizeof(eth.src.addr_bytes));
534                 memcpy(&eth_mask.src.addr_bytes,
535                        &rte_flow_item_eth_mask.src.addr_bytes,
536                        sizeof(eth_mask.src.addr_bytes));
537         } else {
538                 memcpy(&eth.dst.addr_bytes, &conf->mac_addr,
539                        sizeof(eth.dst.addr_bytes));
540                 memcpy(&eth_mask.dst.addr_bytes,
541                        &rte_flow_item_eth_mask.dst.addr_bytes,
542                        sizeof(eth_mask.dst.addr_bytes));
543         }
544         item.spec = &eth;
545         item.mask = &eth_mask;
546         return flow_dv_convert_modify_action(&item, modify_eth, NULL, resource,
547                                              MLX5_MODIFICATION_TYPE_SET, error);
548 }
549
550 /**
551  * Convert modify-header set VLAN VID action to DV specification.
552  *
553  * @param[in,out] resource
554  *   Pointer to the modify-header resource.
555  * @param[in] action
556  *   Pointer to action specification.
557  * @param[out] error
558  *   Pointer to the error structure.
559  *
560  * @return
561  *   0 on success, a negative errno value otherwise and rte_errno is set.
562  */
563 static int
564 flow_dv_convert_action_modify_vlan_vid
565                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
566                          const struct rte_flow_action *action,
567                          struct rte_flow_error *error)
568 {
569         const struct rte_flow_action_of_set_vlan_vid *conf =
570                 (const struct rte_flow_action_of_set_vlan_vid *)(action->conf);
571         int i = resource->actions_num;
572         struct mlx5_modification_cmd *actions = &resource->actions[i];
573         struct field_modify_info *field = modify_vlan_out_first_vid;
574
575         if (i >= MLX5_MAX_MODIFY_NUM)
576                 return rte_flow_error_set(error, EINVAL,
577                          RTE_FLOW_ERROR_TYPE_ACTION, NULL,
578                          "too many items to modify");
579         actions[i].action_type = MLX5_MODIFICATION_TYPE_SET;
580         actions[i].field = field->id;
581         actions[i].length = field->size;
582         actions[i].offset = field->offset;
583         actions[i].data0 = rte_cpu_to_be_32(actions[i].data0);
584         actions[i].data1 = conf->vlan_vid;
585         actions[i].data1 = actions[i].data1 << 16;
586         resource->actions_num = ++i;
587         return 0;
588 }
589
590 /**
591  * Convert modify-header set TP action to DV specification.
592  *
593  * @param[in,out] resource
594  *   Pointer to the modify-header resource.
595  * @param[in] action
596  *   Pointer to action specification.
597  * @param[in] items
598  *   Pointer to rte_flow_item objects list.
599  * @param[in] attr
600  *   Pointer to flow attributes structure.
601  * @param[out] error
602  *   Pointer to the error structure.
603  *
604  * @return
605  *   0 on success, a negative errno value otherwise and rte_errno is set.
606  */
607 static int
608 flow_dv_convert_action_modify_tp
609                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
610                          const struct rte_flow_action *action,
611                          const struct rte_flow_item *items,
612                          union flow_dv_attr *attr,
613                          struct rte_flow_error *error)
614 {
615         const struct rte_flow_action_set_tp *conf =
616                 (const struct rte_flow_action_set_tp *)(action->conf);
617         struct rte_flow_item item;
618         struct rte_flow_item_udp udp;
619         struct rte_flow_item_udp udp_mask;
620         struct rte_flow_item_tcp tcp;
621         struct rte_flow_item_tcp tcp_mask;
622         struct field_modify_info *field;
623
624         if (!attr->valid)
625                 flow_dv_attr_init(items, attr);
626         if (attr->udp) {
627                 memset(&udp, 0, sizeof(udp));
628                 memset(&udp_mask, 0, sizeof(udp_mask));
629                 if (action->type == RTE_FLOW_ACTION_TYPE_SET_TP_SRC) {
630                         udp.hdr.src_port = conf->port;
631                         udp_mask.hdr.src_port =
632                                         rte_flow_item_udp_mask.hdr.src_port;
633                 } else {
634                         udp.hdr.dst_port = conf->port;
635                         udp_mask.hdr.dst_port =
636                                         rte_flow_item_udp_mask.hdr.dst_port;
637                 }
638                 item.type = RTE_FLOW_ITEM_TYPE_UDP;
639                 item.spec = &udp;
640                 item.mask = &udp_mask;
641                 field = modify_udp;
642         }
643         if (attr->tcp) {
644                 memset(&tcp, 0, sizeof(tcp));
645                 memset(&tcp_mask, 0, sizeof(tcp_mask));
646                 if (action->type == RTE_FLOW_ACTION_TYPE_SET_TP_SRC) {
647                         tcp.hdr.src_port = conf->port;
648                         tcp_mask.hdr.src_port =
649                                         rte_flow_item_tcp_mask.hdr.src_port;
650                 } else {
651                         tcp.hdr.dst_port = conf->port;
652                         tcp_mask.hdr.dst_port =
653                                         rte_flow_item_tcp_mask.hdr.dst_port;
654                 }
655                 item.type = RTE_FLOW_ITEM_TYPE_TCP;
656                 item.spec = &tcp;
657                 item.mask = &tcp_mask;
658                 field = modify_tcp;
659         }
660         return flow_dv_convert_modify_action(&item, field, NULL, resource,
661                                              MLX5_MODIFICATION_TYPE_SET, error);
662 }
663
664 /**
665  * Convert modify-header set TTL action to DV specification.
666  *
667  * @param[in,out] resource
668  *   Pointer to the modify-header resource.
669  * @param[in] action
670  *   Pointer to action specification.
671  * @param[in] items
672  *   Pointer to rte_flow_item objects list.
673  * @param[in] attr
674  *   Pointer to flow attributes structure.
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_ttl
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,
687                          struct rte_flow_error *error)
688 {
689         const struct rte_flow_action_set_ttl *conf =
690                 (const struct rte_flow_action_set_ttl *)(action->conf);
691         struct rte_flow_item item;
692         struct rte_flow_item_ipv4 ipv4;
693         struct rte_flow_item_ipv4 ipv4_mask;
694         struct rte_flow_item_ipv6 ipv6;
695         struct rte_flow_item_ipv6 ipv6_mask;
696         struct field_modify_info *field;
697
698         if (!attr->valid)
699                 flow_dv_attr_init(items, attr);
700         if (attr->ipv4) {
701                 memset(&ipv4, 0, sizeof(ipv4));
702                 memset(&ipv4_mask, 0, sizeof(ipv4_mask));
703                 ipv4.hdr.time_to_live = conf->ttl_value;
704                 ipv4_mask.hdr.time_to_live = 0xFF;
705                 item.type = RTE_FLOW_ITEM_TYPE_IPV4;
706                 item.spec = &ipv4;
707                 item.mask = &ipv4_mask;
708                 field = modify_ipv4;
709         }
710         if (attr->ipv6) {
711                 memset(&ipv6, 0, sizeof(ipv6));
712                 memset(&ipv6_mask, 0, sizeof(ipv6_mask));
713                 ipv6.hdr.hop_limits = conf->ttl_value;
714                 ipv6_mask.hdr.hop_limits = 0xFF;
715                 item.type = RTE_FLOW_ITEM_TYPE_IPV6;
716                 item.spec = &ipv6;
717                 item.mask = &ipv6_mask;
718                 field = modify_ipv6;
719         }
720         return flow_dv_convert_modify_action(&item, field, NULL, resource,
721                                              MLX5_MODIFICATION_TYPE_SET, error);
722 }
723
724 /**
725  * Convert modify-header decrement TTL action to DV specification.
726  *
727  * @param[in,out] resource
728  *   Pointer to the modify-header resource.
729  * @param[in] action
730  *   Pointer to action specification.
731  * @param[in] items
732  *   Pointer to rte_flow_item objects list.
733  * @param[in] attr
734  *   Pointer to flow attributes structure.
735  * @param[out] error
736  *   Pointer to the error structure.
737  *
738  * @return
739  *   0 on success, a negative errno value otherwise and rte_errno is set.
740  */
741 static int
742 flow_dv_convert_action_modify_dec_ttl
743                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
744                          const struct rte_flow_item *items,
745                          union flow_dv_attr *attr,
746                          struct rte_flow_error *error)
747 {
748         struct rte_flow_item item;
749         struct rte_flow_item_ipv4 ipv4;
750         struct rte_flow_item_ipv4 ipv4_mask;
751         struct rte_flow_item_ipv6 ipv6;
752         struct rte_flow_item_ipv6 ipv6_mask;
753         struct field_modify_info *field;
754
755         if (!attr->valid)
756                 flow_dv_attr_init(items, attr);
757         if (attr->ipv4) {
758                 memset(&ipv4, 0, sizeof(ipv4));
759                 memset(&ipv4_mask, 0, sizeof(ipv4_mask));
760                 ipv4.hdr.time_to_live = 0xFF;
761                 ipv4_mask.hdr.time_to_live = 0xFF;
762                 item.type = RTE_FLOW_ITEM_TYPE_IPV4;
763                 item.spec = &ipv4;
764                 item.mask = &ipv4_mask;
765                 field = modify_ipv4;
766         }
767         if (attr->ipv6) {
768                 memset(&ipv6, 0, sizeof(ipv6));
769                 memset(&ipv6_mask, 0, sizeof(ipv6_mask));
770                 ipv6.hdr.hop_limits = 0xFF;
771                 ipv6_mask.hdr.hop_limits = 0xFF;
772                 item.type = RTE_FLOW_ITEM_TYPE_IPV6;
773                 item.spec = &ipv6;
774                 item.mask = &ipv6_mask;
775                 field = modify_ipv6;
776         }
777         return flow_dv_convert_modify_action(&item, field, NULL, resource,
778                                              MLX5_MODIFICATION_TYPE_ADD, error);
779 }
780
781 /**
782  * Convert modify-header increment/decrement TCP Sequence number
783  * to DV specification.
784  *
785  * @param[in,out] resource
786  *   Pointer to the modify-header resource.
787  * @param[in] action
788  *   Pointer to action specification.
789  * @param[out] error
790  *   Pointer to the error structure.
791  *
792  * @return
793  *   0 on success, a negative errno value otherwise and rte_errno is set.
794  */
795 static int
796 flow_dv_convert_action_modify_tcp_seq
797                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
798                          const struct rte_flow_action *action,
799                          struct rte_flow_error *error)
800 {
801         const rte_be32_t *conf = (const rte_be32_t *)(action->conf);
802         uint64_t value = rte_be_to_cpu_32(*conf);
803         struct rte_flow_item item;
804         struct rte_flow_item_tcp tcp;
805         struct rte_flow_item_tcp tcp_mask;
806
807         memset(&tcp, 0, sizeof(tcp));
808         memset(&tcp_mask, 0, sizeof(tcp_mask));
809         if (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ)
810                 /*
811                  * The HW has no decrement operation, only increment operation.
812                  * To simulate decrement X from Y using increment operation
813                  * we need to add UINT32_MAX X times to Y.
814                  * Each adding of UINT32_MAX decrements Y by 1.
815                  */
816                 value *= UINT32_MAX;
817         tcp.hdr.sent_seq = rte_cpu_to_be_32((uint32_t)value);
818         tcp_mask.hdr.sent_seq = RTE_BE32(UINT32_MAX);
819         item.type = RTE_FLOW_ITEM_TYPE_TCP;
820         item.spec = &tcp;
821         item.mask = &tcp_mask;
822         return flow_dv_convert_modify_action(&item, modify_tcp, NULL, resource,
823                                              MLX5_MODIFICATION_TYPE_ADD, error);
824 }
825
826 /**
827  * Convert modify-header increment/decrement TCP Acknowledgment number
828  * to DV specification.
829  *
830  * @param[in,out] resource
831  *   Pointer to the modify-header resource.
832  * @param[in] action
833  *   Pointer to action specification.
834  * @param[out] error
835  *   Pointer to the error structure.
836  *
837  * @return
838  *   0 on success, a negative errno value otherwise and rte_errno is set.
839  */
840 static int
841 flow_dv_convert_action_modify_tcp_ack
842                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
843                          const struct rte_flow_action *action,
844                          struct rte_flow_error *error)
845 {
846         const rte_be32_t *conf = (const rte_be32_t *)(action->conf);
847         uint64_t value = rte_be_to_cpu_32(*conf);
848         struct rte_flow_item item;
849         struct rte_flow_item_tcp tcp;
850         struct rte_flow_item_tcp tcp_mask;
851
852         memset(&tcp, 0, sizeof(tcp));
853         memset(&tcp_mask, 0, sizeof(tcp_mask));
854         if (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK)
855                 /*
856                  * The HW has no decrement operation, only increment operation.
857                  * To simulate decrement X from Y using increment operation
858                  * we need to add UINT32_MAX X times to Y.
859                  * Each adding of UINT32_MAX decrements Y by 1.
860                  */
861                 value *= UINT32_MAX;
862         tcp.hdr.recv_ack = rte_cpu_to_be_32((uint32_t)value);
863         tcp_mask.hdr.recv_ack = RTE_BE32(UINT32_MAX);
864         item.type = RTE_FLOW_ITEM_TYPE_TCP;
865         item.spec = &tcp;
866         item.mask = &tcp_mask;
867         return flow_dv_convert_modify_action(&item, modify_tcp, NULL, resource,
868                                              MLX5_MODIFICATION_TYPE_ADD, error);
869 }
870
871 static enum mlx5_modification_field reg_to_field[] = {
872         [REG_NONE] = MLX5_MODI_OUT_NONE,
873         [REG_A] = MLX5_MODI_META_DATA_REG_A,
874         [REG_B] = MLX5_MODI_META_DATA_REG_B,
875         [REG_C_0] = MLX5_MODI_META_REG_C_0,
876         [REG_C_1] = MLX5_MODI_META_REG_C_1,
877         [REG_C_2] = MLX5_MODI_META_REG_C_2,
878         [REG_C_3] = MLX5_MODI_META_REG_C_3,
879         [REG_C_4] = MLX5_MODI_META_REG_C_4,
880         [REG_C_5] = MLX5_MODI_META_REG_C_5,
881         [REG_C_6] = MLX5_MODI_META_REG_C_6,
882         [REG_C_7] = MLX5_MODI_META_REG_C_7,
883 };
884
885 /**
886  * Convert register set to DV specification.
887  *
888  * @param[in,out] resource
889  *   Pointer to the modify-header resource.
890  * @param[in] action
891  *   Pointer to action specification.
892  * @param[out] error
893  *   Pointer to the error structure.
894  *
895  * @return
896  *   0 on success, a negative errno value otherwise and rte_errno is set.
897  */
898 static int
899 flow_dv_convert_action_set_reg
900                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
901                          const struct rte_flow_action *action,
902                          struct rte_flow_error *error)
903 {
904         const struct mlx5_rte_flow_action_set_tag *conf = action->conf;
905         struct mlx5_modification_cmd *actions = resource->actions;
906         uint32_t i = resource->actions_num;
907
908         if (i >= MLX5_MAX_MODIFY_NUM)
909                 return rte_flow_error_set(error, EINVAL,
910                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
911                                           "too many items to modify");
912         assert(conf->id != REG_NONE);
913         assert(conf->id < RTE_DIM(reg_to_field));
914         actions[i].action_type = MLX5_MODIFICATION_TYPE_SET;
915         actions[i].field = reg_to_field[conf->id];
916         actions[i].data0 = rte_cpu_to_be_32(actions[i].data0);
917         actions[i].data1 = rte_cpu_to_be_32(conf->data);
918         ++i;
919         resource->actions_num = i;
920         return 0;
921 }
922
923 /**
924  * Convert SET_TAG action to DV specification.
925  *
926  * @param[in] dev
927  *   Pointer to the rte_eth_dev structure.
928  * @param[in,out] resource
929  *   Pointer to the modify-header resource.
930  * @param[in] conf
931  *   Pointer to action specification.
932  * @param[out] error
933  *   Pointer to the error structure.
934  *
935  * @return
936  *   0 on success, a negative errno value otherwise and rte_errno is set.
937  */
938 static int
939 flow_dv_convert_action_set_tag
940                         (struct rte_eth_dev *dev,
941                          struct mlx5_flow_dv_modify_hdr_resource *resource,
942                          const struct rte_flow_action_set_tag *conf,
943                          struct rte_flow_error *error)
944 {
945         rte_be32_t data = rte_cpu_to_be_32(conf->data);
946         rte_be32_t mask = rte_cpu_to_be_32(conf->mask);
947         struct rte_flow_item item = {
948                 .spec = &data,
949                 .mask = &mask,
950         };
951         struct field_modify_info reg_c_x[] = {
952                 [1] = {0, 0, 0},
953         };
954         enum mlx5_modification_field reg_type;
955         int ret;
956
957         ret = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, conf->index, error);
958         if (ret < 0)
959                 return ret;
960         assert(ret != REG_NONE);
961         assert((unsigned int)ret < RTE_DIM(reg_to_field));
962         reg_type = reg_to_field[ret];
963         assert(reg_type > 0);
964         reg_c_x[0] = (struct field_modify_info){4, 0, reg_type};
965         return flow_dv_convert_modify_action(&item, reg_c_x, NULL, resource,
966                                              MLX5_MODIFICATION_TYPE_SET, error);
967 }
968
969 /**
970  * Convert internal COPY_REG action to DV specification.
971  *
972  * @param[in] dev
973  *   Pointer to the rte_eth_dev structure.
974  * @param[in,out] res
975  *   Pointer to the modify-header resource.
976  * @param[in] action
977  *   Pointer to action specification.
978  * @param[out] error
979  *   Pointer to the error structure.
980  *
981  * @return
982  *   0 on success, a negative errno value otherwise and rte_errno is set.
983  */
984 static int
985 flow_dv_convert_action_copy_mreg(struct rte_eth_dev *dev,
986                                  struct mlx5_flow_dv_modify_hdr_resource *res,
987                                  const struct rte_flow_action *action,
988                                  struct rte_flow_error *error)
989 {
990         const struct mlx5_flow_action_copy_mreg *conf = action->conf;
991         rte_be32_t mask = RTE_BE32(UINT32_MAX);
992         struct rte_flow_item item = {
993                 .spec = NULL,
994                 .mask = &mask,
995         };
996         struct field_modify_info reg_src[] = {
997                 {4, 0, reg_to_field[conf->src]},
998                 {0, 0, 0},
999         };
1000         struct field_modify_info reg_dst = {
1001                 .offset = 0,
1002                 .id = reg_to_field[conf->dst],
1003         };
1004         /* Adjust reg_c[0] usage according to reported mask. */
1005         if (conf->dst == REG_C_0 || conf->src == REG_C_0) {
1006                 struct mlx5_priv *priv = dev->data->dev_private;
1007                 uint32_t reg_c0 = priv->sh->dv_regc0_mask;
1008
1009                 assert(reg_c0);
1010                 assert(priv->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY);
1011                 if (conf->dst == REG_C_0) {
1012                         /* Copy to reg_c[0], within mask only. */
1013                         reg_dst.offset = rte_bsf32(reg_c0);
1014                         /*
1015                          * Mask is ignoring the enianness, because
1016                          * there is no conversion in datapath.
1017                          */
1018 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
1019                         /* Copy from destination lower bits to reg_c[0]. */
1020                         mask = reg_c0 >> reg_dst.offset;
1021 #else
1022                         /* Copy from destination upper bits to reg_c[0]. */
1023                         mask = reg_c0 << (sizeof(reg_c0) * CHAR_BIT -
1024                                           rte_fls_u32(reg_c0));
1025 #endif
1026                 } else {
1027                         mask = rte_cpu_to_be_32(reg_c0);
1028 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
1029                         /* Copy from reg_c[0] to destination lower bits. */
1030                         reg_dst.offset = 0;
1031 #else
1032                         /* Copy from reg_c[0] to destination upper bits. */
1033                         reg_dst.offset = sizeof(reg_c0) * CHAR_BIT -
1034                                          (rte_fls_u32(reg_c0) -
1035                                           rte_bsf32(reg_c0));
1036 #endif
1037                 }
1038         }
1039         return flow_dv_convert_modify_action(&item,
1040                                              reg_src, &reg_dst, res,
1041                                              MLX5_MODIFICATION_TYPE_COPY,
1042                                              error);
1043 }
1044
1045 /**
1046  * Convert MARK action to DV specification. This routine is used
1047  * in extensive metadata only and requires metadata register to be
1048  * handled. In legacy mode hardware tag resource is engaged.
1049  *
1050  * @param[in] dev
1051  *   Pointer to the rte_eth_dev structure.
1052  * @param[in] conf
1053  *   Pointer to MARK action specification.
1054  * @param[in,out] resource
1055  *   Pointer to the modify-header resource.
1056  * @param[out] error
1057  *   Pointer to the error structure.
1058  *
1059  * @return
1060  *   0 on success, a negative errno value otherwise and rte_errno is set.
1061  */
1062 static int
1063 flow_dv_convert_action_mark(struct rte_eth_dev *dev,
1064                             const struct rte_flow_action_mark *conf,
1065                             struct mlx5_flow_dv_modify_hdr_resource *resource,
1066                             struct rte_flow_error *error)
1067 {
1068         struct mlx5_priv *priv = dev->data->dev_private;
1069         rte_be32_t mask = rte_cpu_to_be_32(MLX5_FLOW_MARK_MASK &
1070                                            priv->sh->dv_mark_mask);
1071         rte_be32_t data = rte_cpu_to_be_32(conf->id) & mask;
1072         struct rte_flow_item item = {
1073                 .spec = &data,
1074                 .mask = &mask,
1075         };
1076         struct field_modify_info reg_c_x[] = {
1077                 {4, 0, 0}, /* dynamic instead of MLX5_MODI_META_REG_C_1. */
1078                 {0, 0, 0},
1079         };
1080         enum modify_reg reg;
1081
1082         if (!mask)
1083                 return rte_flow_error_set(error, EINVAL,
1084                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1085                                           NULL, "zero mark action mask");
1086         reg = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
1087         if (reg < 0)
1088                 return reg;
1089         assert(reg > 0);
1090         if (reg == REG_C_0) {
1091                 uint32_t msk_c0 = priv->sh->dv_regc0_mask;
1092                 uint32_t shl_c0 = rte_bsf32(msk_c0);
1093
1094                 data = rte_cpu_to_be_32(rte_cpu_to_be_32(data) << shl_c0);
1095                 mask = rte_cpu_to_be_32(mask) & msk_c0;
1096                 mask = rte_cpu_to_be_32(mask << shl_c0);
1097         }
1098         reg_c_x[0].id = reg_to_field[reg];
1099         return flow_dv_convert_modify_action(&item, reg_c_x, NULL, resource,
1100                                              MLX5_MODIFICATION_TYPE_SET, error);
1101 }
1102
1103 /**
1104  * Get metadata register index for specified steering domain.
1105  *
1106  * @param[in] dev
1107  *   Pointer to the rte_eth_dev structure.
1108  * @param[in] attr
1109  *   Attributes of flow to determine steering domain.
1110  * @param[out] error
1111  *   Pointer to the error structure.
1112  *
1113  * @return
1114  *   positive index on success, a negative errno value otherwise
1115  *   and rte_errno is set.
1116  */
1117 static enum modify_reg
1118 flow_dv_get_metadata_reg(struct rte_eth_dev *dev,
1119                          const struct rte_flow_attr *attr,
1120                          struct rte_flow_error *error)
1121 {
1122         enum modify_reg reg =
1123                 mlx5_flow_get_reg_id(dev, attr->transfer ?
1124                                           MLX5_METADATA_FDB :
1125                                             attr->egress ?
1126                                             MLX5_METADATA_TX :
1127                                             MLX5_METADATA_RX, 0, error);
1128         if (reg < 0)
1129                 return rte_flow_error_set(error,
1130                                           ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
1131                                           NULL, "unavailable "
1132                                           "metadata register");
1133         return reg;
1134 }
1135
1136 /**
1137  * Convert SET_META action to DV specification.
1138  *
1139  * @param[in] dev
1140  *   Pointer to the rte_eth_dev structure.
1141  * @param[in,out] resource
1142  *   Pointer to the modify-header resource.
1143  * @param[in] attr
1144  *   Attributes of flow that includes this item.
1145  * @param[in] conf
1146  *   Pointer to action specification.
1147  * @param[out] error
1148  *   Pointer to the error structure.
1149  *
1150  * @return
1151  *   0 on success, a negative errno value otherwise and rte_errno is set.
1152  */
1153 static int
1154 flow_dv_convert_action_set_meta
1155                         (struct rte_eth_dev *dev,
1156                          struct mlx5_flow_dv_modify_hdr_resource *resource,
1157                          const struct rte_flow_attr *attr,
1158                          const struct rte_flow_action_set_meta *conf,
1159                          struct rte_flow_error *error)
1160 {
1161         uint32_t data = conf->data;
1162         uint32_t mask = conf->mask;
1163         struct rte_flow_item item = {
1164                 .spec = &data,
1165                 .mask = &mask,
1166         };
1167         struct field_modify_info reg_c_x[] = {
1168                 [1] = {0, 0, 0},
1169         };
1170         enum modify_reg reg = flow_dv_get_metadata_reg(dev, attr, error);
1171
1172         if (reg < 0)
1173                 return reg;
1174         /*
1175          * In datapath code there is no endianness
1176          * coversions for perfromance reasons, all
1177          * pattern conversions are done in rte_flow.
1178          */
1179         if (reg == REG_C_0) {
1180                 struct mlx5_priv *priv = dev->data->dev_private;
1181                 uint32_t msk_c0 = priv->sh->dv_regc0_mask;
1182                 uint32_t shl_c0;
1183
1184                 assert(msk_c0);
1185 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
1186                 shl_c0 = rte_bsf32(msk_c0);
1187 #else
1188                 shl_c0 = sizeof(msk_c0) * CHAR_BIT - rte_fls_u32(msk_c0);
1189 #endif
1190                 mask <<= shl_c0;
1191                 data <<= shl_c0;
1192                 assert(!(~msk_c0 & rte_cpu_to_be_32(mask)));
1193         }
1194         reg_c_x[0] = (struct field_modify_info){4, 0, reg_to_field[reg]};
1195         /* The routine expects parameters in memory as big-endian ones. */
1196         return flow_dv_convert_modify_action(&item, reg_c_x, NULL, resource,
1197                                              MLX5_MODIFICATION_TYPE_SET, error);
1198 }
1199
1200 /**
1201  * Convert modify-header set IPv4 DSCP action to DV specification.
1202  *
1203  * @param[in,out] resource
1204  *   Pointer to the modify-header resource.
1205  * @param[in] action
1206  *   Pointer to action specification.
1207  * @param[out] error
1208  *   Pointer to the error structure.
1209  *
1210  * @return
1211  *   0 on success, a negative errno value otherwise and rte_errno is set.
1212  */
1213 static int
1214 flow_dv_convert_action_modify_ipv4_dscp
1215                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
1216                          const struct rte_flow_action *action,
1217                          struct rte_flow_error *error)
1218 {
1219         const struct rte_flow_action_set_dscp *conf =
1220                 (const struct rte_flow_action_set_dscp *)(action->conf);
1221         struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_IPV4 };
1222         struct rte_flow_item_ipv4 ipv4;
1223         struct rte_flow_item_ipv4 ipv4_mask;
1224
1225         memset(&ipv4, 0, sizeof(ipv4));
1226         memset(&ipv4_mask, 0, sizeof(ipv4_mask));
1227         ipv4.hdr.type_of_service = conf->dscp;
1228         ipv4_mask.hdr.type_of_service = RTE_IPV4_HDR_DSCP_MASK >> 2;
1229         item.spec = &ipv4;
1230         item.mask = &ipv4_mask;
1231         return flow_dv_convert_modify_action(&item, modify_ipv4, NULL, resource,
1232                                              MLX5_MODIFICATION_TYPE_SET, error);
1233 }
1234
1235 /**
1236  * Convert modify-header set IPv6 DSCP action to DV specification.
1237  *
1238  * @param[in,out] resource
1239  *   Pointer to the modify-header resource.
1240  * @param[in] action
1241  *   Pointer to action specification.
1242  * @param[out] error
1243  *   Pointer to the error structure.
1244  *
1245  * @return
1246  *   0 on success, a negative errno value otherwise and rte_errno is set.
1247  */
1248 static int
1249 flow_dv_convert_action_modify_ipv6_dscp
1250                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
1251                          const struct rte_flow_action *action,
1252                          struct rte_flow_error *error)
1253 {
1254         const struct rte_flow_action_set_dscp *conf =
1255                 (const struct rte_flow_action_set_dscp *)(action->conf);
1256         struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_IPV6 };
1257         struct rte_flow_item_ipv6 ipv6;
1258         struct rte_flow_item_ipv6 ipv6_mask;
1259
1260         memset(&ipv6, 0, sizeof(ipv6));
1261         memset(&ipv6_mask, 0, sizeof(ipv6_mask));
1262         /*
1263          * Even though the DSCP bits offset of IPv6 is not byte aligned,
1264          * rdma-core only accept the DSCP bits byte aligned start from
1265          * bit 0 to 5 as to be compatible with IPv4. No need to shift the
1266          * bits in IPv6 case as rdma-core requires byte aligned value.
1267          */
1268         ipv6.hdr.vtc_flow = conf->dscp;
1269         ipv6_mask.hdr.vtc_flow = RTE_IPV6_HDR_DSCP_MASK >> 22;
1270         item.spec = &ipv6;
1271         item.mask = &ipv6_mask;
1272         return flow_dv_convert_modify_action(&item, modify_ipv6, NULL, resource,
1273                                              MLX5_MODIFICATION_TYPE_SET, error);
1274 }
1275
1276 /**
1277  * Validate MARK item.
1278  *
1279  * @param[in] dev
1280  *   Pointer to the rte_eth_dev structure.
1281  * @param[in] item
1282  *   Item specification.
1283  * @param[in] attr
1284  *   Attributes of flow that includes this item.
1285  * @param[out] error
1286  *   Pointer to error structure.
1287  *
1288  * @return
1289  *   0 on success, a negative errno value otherwise and rte_errno is set.
1290  */
1291 static int
1292 flow_dv_validate_item_mark(struct rte_eth_dev *dev,
1293                            const struct rte_flow_item *item,
1294                            const struct rte_flow_attr *attr __rte_unused,
1295                            struct rte_flow_error *error)
1296 {
1297         struct mlx5_priv *priv = dev->data->dev_private;
1298         struct mlx5_dev_config *config = &priv->config;
1299         const struct rte_flow_item_mark *spec = item->spec;
1300         const struct rte_flow_item_mark *mask = item->mask;
1301         const struct rte_flow_item_mark nic_mask = {
1302                 .id = priv->sh->dv_mark_mask,
1303         };
1304         int ret;
1305
1306         if (config->dv_xmeta_en == MLX5_XMETA_MODE_LEGACY)
1307                 return rte_flow_error_set(error, ENOTSUP,
1308                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1309                                           "extended metadata feature"
1310                                           " isn't enabled");
1311         if (!mlx5_flow_ext_mreg_supported(dev))
1312                 return rte_flow_error_set(error, ENOTSUP,
1313                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1314                                           "extended metadata register"
1315                                           " isn't supported");
1316         if (!nic_mask.id)
1317                 return rte_flow_error_set(error, ENOTSUP,
1318                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1319                                           "extended metadata register"
1320                                           " isn't available");
1321         ret = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
1322         if (ret < 0)
1323                 return ret;
1324         if (!spec)
1325                 return rte_flow_error_set(error, EINVAL,
1326                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
1327                                           item->spec,
1328                                           "data cannot be empty");
1329         if (spec->id >= (MLX5_FLOW_MARK_MAX & nic_mask.id))
1330                 return rte_flow_error_set(error, EINVAL,
1331                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1332                                           &spec->id,
1333                                           "mark id exceeds the limit");
1334         if (!mask)
1335                 mask = &nic_mask;
1336         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1337                                         (const uint8_t *)&nic_mask,
1338                                         sizeof(struct rte_flow_item_mark),
1339                                         error);
1340         if (ret < 0)
1341                 return ret;
1342         return 0;
1343 }
1344
1345 /**
1346  * Validate META item.
1347  *
1348  * @param[in] dev
1349  *   Pointer to the rte_eth_dev structure.
1350  * @param[in] item
1351  *   Item specification.
1352  * @param[in] attr
1353  *   Attributes of flow that includes this item.
1354  * @param[out] error
1355  *   Pointer to error structure.
1356  *
1357  * @return
1358  *   0 on success, a negative errno value otherwise and rte_errno is set.
1359  */
1360 static int
1361 flow_dv_validate_item_meta(struct rte_eth_dev *dev __rte_unused,
1362                            const struct rte_flow_item *item,
1363                            const struct rte_flow_attr *attr,
1364                            struct rte_flow_error *error)
1365 {
1366         struct mlx5_priv *priv = dev->data->dev_private;
1367         struct mlx5_dev_config *config = &priv->config;
1368         const struct rte_flow_item_meta *spec = item->spec;
1369         const struct rte_flow_item_meta *mask = item->mask;
1370         struct rte_flow_item_meta nic_mask = {
1371                 .data = UINT32_MAX
1372         };
1373         enum modify_reg reg;
1374         int ret;
1375
1376         if (!spec)
1377                 return rte_flow_error_set(error, EINVAL,
1378                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
1379                                           item->spec,
1380                                           "data cannot be empty");
1381         if (!spec->data)
1382                 return rte_flow_error_set(error, EINVAL,
1383                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC, NULL,
1384                                           "data cannot be zero");
1385         if (config->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
1386                 if (!mlx5_flow_ext_mreg_supported(dev))
1387                         return rte_flow_error_set(error, ENOTSUP,
1388                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1389                                           "extended metadata register"
1390                                           " isn't supported");
1391                 reg = flow_dv_get_metadata_reg(dev, attr, error);
1392                 if (reg < 0)
1393                         return reg;
1394                 if (reg == REG_B)
1395                         return rte_flow_error_set(error, ENOTSUP,
1396                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1397                                           "match on reg_b "
1398                                           "isn't supported");
1399                 if (reg != REG_A)
1400                         nic_mask.data = priv->sh->dv_meta_mask;
1401         }
1402         if (!mask)
1403                 mask = &rte_flow_item_meta_mask;
1404         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1405                                         (const uint8_t *)&nic_mask,
1406                                         sizeof(struct rte_flow_item_meta),
1407                                         error);
1408         return ret;
1409 }
1410
1411 /**
1412  * Validate TAG item.
1413  *
1414  * @param[in] dev
1415  *   Pointer to the rte_eth_dev structure.
1416  * @param[in] item
1417  *   Item specification.
1418  * @param[in] attr
1419  *   Attributes of flow that includes this item.
1420  * @param[out] error
1421  *   Pointer to error structure.
1422  *
1423  * @return
1424  *   0 on success, a negative errno value otherwise and rte_errno is set.
1425  */
1426 static int
1427 flow_dv_validate_item_tag(struct rte_eth_dev *dev,
1428                           const struct rte_flow_item *item,
1429                           const struct rte_flow_attr *attr __rte_unused,
1430                           struct rte_flow_error *error)
1431 {
1432         const struct rte_flow_item_tag *spec = item->spec;
1433         const struct rte_flow_item_tag *mask = item->mask;
1434         const struct rte_flow_item_tag nic_mask = {
1435                 .data = RTE_BE32(UINT32_MAX),
1436                 .index = 0xff,
1437         };
1438         int ret;
1439
1440         if (!mlx5_flow_ext_mreg_supported(dev))
1441                 return rte_flow_error_set(error, ENOTSUP,
1442                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1443                                           "extensive metadata register"
1444                                           " isn't supported");
1445         if (!spec)
1446                 return rte_flow_error_set(error, EINVAL,
1447                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
1448                                           item->spec,
1449                                           "data cannot be empty");
1450         if (!mask)
1451                 mask = &rte_flow_item_tag_mask;
1452         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1453                                         (const uint8_t *)&nic_mask,
1454                                         sizeof(struct rte_flow_item_tag),
1455                                         error);
1456         if (ret < 0)
1457                 return ret;
1458         if (mask->index != 0xff)
1459                 return rte_flow_error_set(error, EINVAL,
1460                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC, NULL,
1461                                           "partial mask for tag index"
1462                                           " is not supported");
1463         ret = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, spec->index, error);
1464         if (ret < 0)
1465                 return ret;
1466         assert(ret != REG_NONE);
1467         return 0;
1468 }
1469
1470 /**
1471  * Validate vport item.
1472  *
1473  * @param[in] dev
1474  *   Pointer to the rte_eth_dev structure.
1475  * @param[in] item
1476  *   Item specification.
1477  * @param[in] attr
1478  *   Attributes of flow that includes this item.
1479  * @param[in] item_flags
1480  *   Bit-fields that holds the items detected until now.
1481  * @param[out] error
1482  *   Pointer to error structure.
1483  *
1484  * @return
1485  *   0 on success, a negative errno value otherwise and rte_errno is set.
1486  */
1487 static int
1488 flow_dv_validate_item_port_id(struct rte_eth_dev *dev,
1489                               const struct rte_flow_item *item,
1490                               const struct rte_flow_attr *attr,
1491                               uint64_t item_flags,
1492                               struct rte_flow_error *error)
1493 {
1494         const struct rte_flow_item_port_id *spec = item->spec;
1495         const struct rte_flow_item_port_id *mask = item->mask;
1496         const struct rte_flow_item_port_id switch_mask = {
1497                         .id = 0xffffffff,
1498         };
1499         struct mlx5_priv *esw_priv;
1500         struct mlx5_priv *dev_priv;
1501         int ret;
1502
1503         if (!attr->transfer)
1504                 return rte_flow_error_set(error, EINVAL,
1505                                           RTE_FLOW_ERROR_TYPE_ITEM,
1506                                           NULL,
1507                                           "match on port id is valid only"
1508                                           " when transfer flag is enabled");
1509         if (item_flags & MLX5_FLOW_ITEM_PORT_ID)
1510                 return rte_flow_error_set(error, ENOTSUP,
1511                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1512                                           "multiple source ports are not"
1513                                           " supported");
1514         if (!mask)
1515                 mask = &switch_mask;
1516         if (mask->id != 0xffffffff)
1517                 return rte_flow_error_set(error, ENOTSUP,
1518                                            RTE_FLOW_ERROR_TYPE_ITEM_MASK,
1519                                            mask,
1520                                            "no support for partial mask on"
1521                                            " \"id\" field");
1522         ret = mlx5_flow_item_acceptable
1523                                 (item, (const uint8_t *)mask,
1524                                  (const uint8_t *)&rte_flow_item_port_id_mask,
1525                                  sizeof(struct rte_flow_item_port_id),
1526                                  error);
1527         if (ret)
1528                 return ret;
1529         if (!spec)
1530                 return 0;
1531         esw_priv = mlx5_port_to_eswitch_info(spec->id, false);
1532         if (!esw_priv)
1533                 return rte_flow_error_set(error, rte_errno,
1534                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC, spec,
1535                                           "failed to obtain E-Switch info for"
1536                                           " port");
1537         dev_priv = mlx5_dev_to_eswitch_info(dev);
1538         if (!dev_priv)
1539                 return rte_flow_error_set(error, rte_errno,
1540                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1541                                           NULL,
1542                                           "failed to obtain E-Switch info");
1543         if (esw_priv->domain_id != dev_priv->domain_id)
1544                 return rte_flow_error_set(error, EINVAL,
1545                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC, spec,
1546                                           "cannot match on a port from a"
1547                                           " different E-Switch");
1548         return 0;
1549 }
1550
1551 /**
1552  * Validate GTP item.
1553  *
1554  * @param[in] dev
1555  *   Pointer to the rte_eth_dev structure.
1556  * @param[in] item
1557  *   Item specification.
1558  * @param[in] item_flags
1559  *   Bit-fields that holds the items detected until now.
1560  * @param[out] error
1561  *   Pointer to error structure.
1562  *
1563  * @return
1564  *   0 on success, a negative errno value otherwise and rte_errno is set.
1565  */
1566 static int
1567 flow_dv_validate_item_gtp(struct rte_eth_dev *dev,
1568                           const struct rte_flow_item *item,
1569                           uint64_t item_flags,
1570                           struct rte_flow_error *error)
1571 {
1572         struct mlx5_priv *priv = dev->data->dev_private;
1573         const struct rte_flow_item_gtp *mask = item->mask;
1574         const struct rte_flow_item_gtp nic_mask = {
1575                 .msg_type = 0xff,
1576                 .teid = RTE_BE32(0xffffffff),
1577         };
1578
1579         if (!priv->config.hca_attr.tunnel_stateless_gtp)
1580                 return rte_flow_error_set(error, ENOTSUP,
1581                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1582                                           "GTP support is not enabled");
1583         if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
1584                 return rte_flow_error_set(error, ENOTSUP,
1585                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1586                                           "multiple tunnel layers not"
1587                                           " supported");
1588         if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_UDP))
1589                 return rte_flow_error_set(error, EINVAL,
1590                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1591                                           "no outer UDP layer found");
1592         if (!mask)
1593                 mask = &rte_flow_item_gtp_mask;
1594         return mlx5_flow_item_acceptable
1595                 (item, (const uint8_t *)mask,
1596                  (const uint8_t *)&nic_mask,
1597                  sizeof(struct rte_flow_item_gtp),
1598                  error);
1599 }
1600
1601 /**
1602  * Validate the pop VLAN action.
1603  *
1604  * @param[in] dev
1605  *   Pointer to the rte_eth_dev structure.
1606  * @param[in] action_flags
1607  *   Holds the actions detected until now.
1608  * @param[in] action
1609  *   Pointer to the pop vlan action.
1610  * @param[in] item_flags
1611  *   The items found in this flow rule.
1612  * @param[in] attr
1613  *   Pointer to flow attributes.
1614  * @param[out] error
1615  *   Pointer to error structure.
1616  *
1617  * @return
1618  *   0 on success, a negative errno value otherwise and rte_errno is set.
1619  */
1620 static int
1621 flow_dv_validate_action_pop_vlan(struct rte_eth_dev *dev,
1622                                  uint64_t action_flags,
1623                                  const struct rte_flow_action *action,
1624                                  uint64_t item_flags,
1625                                  const struct rte_flow_attr *attr,
1626                                  struct rte_flow_error *error)
1627 {
1628         struct mlx5_priv *priv = dev->data->dev_private;
1629
1630         (void)action;
1631         (void)attr;
1632         if (!priv->sh->pop_vlan_action)
1633                 return rte_flow_error_set(error, ENOTSUP,
1634                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1635                                           NULL,
1636                                           "pop vlan action is not supported");
1637         /*
1638          * Check for inconsistencies:
1639          *  fail strip_vlan in a flow that matches packets without VLAN tags.
1640          *  fail strip_vlan in a flow that matches packets without explicitly a
1641          *  matching on VLAN tag ?
1642          */
1643         if (action_flags & MLX5_FLOW_ACTION_OF_POP_VLAN)
1644                 return rte_flow_error_set(error, ENOTSUP,
1645                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1646                                           NULL,
1647                                           "no support for multiple vlan pop "
1648                                           "actions");
1649         if (!(item_flags & MLX5_FLOW_LAYER_OUTER_VLAN))
1650                 return rte_flow_error_set(error, ENOTSUP,
1651                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1652                                           NULL,
1653                                           "cannot pop vlan without a "
1654                                           "match on (outer) vlan in the flow");
1655         if (action_flags & MLX5_FLOW_ACTION_PORT_ID)
1656                 return rte_flow_error_set(error, EINVAL,
1657                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
1658                                           "wrong action order, port_id should "
1659                                           "be after pop VLAN action");
1660         return 0;
1661 }
1662
1663 /**
1664  * Get VLAN default info from vlan match info.
1665  *
1666  * @param[in] dev
1667  *   Pointer to the rte_eth_dev structure.
1668  * @param[in] item
1669  *   the list of item specifications.
1670  * @param[out] vlan
1671  *   pointer VLAN info to fill to.
1672  * @param[out] error
1673  *   Pointer to error structure.
1674  *
1675  * @return
1676  *   0 on success, a negative errno value otherwise and rte_errno is set.
1677  */
1678 static void
1679 flow_dev_get_vlan_info_from_items(const struct rte_flow_item *items,
1680                                   struct rte_vlan_hdr *vlan)
1681 {
1682         const struct rte_flow_item_vlan nic_mask = {
1683                 .tci = RTE_BE16(MLX5DV_FLOW_VLAN_PCP_MASK |
1684                                 MLX5DV_FLOW_VLAN_VID_MASK),
1685                 .inner_type = RTE_BE16(0xffff),
1686         };
1687
1688         if (items == NULL)
1689                 return;
1690         for (; items->type != RTE_FLOW_ITEM_TYPE_END &&
1691                items->type != RTE_FLOW_ITEM_TYPE_VLAN; items++)
1692                 ;
1693         if (items->type == RTE_FLOW_ITEM_TYPE_VLAN) {
1694                 const struct rte_flow_item_vlan *vlan_m = items->mask;
1695                 const struct rte_flow_item_vlan *vlan_v = items->spec;
1696
1697                 if (!vlan_m)
1698                         vlan_m = &nic_mask;
1699                 /* Only full match values are accepted */
1700                 if ((vlan_m->tci & MLX5DV_FLOW_VLAN_PCP_MASK_BE) ==
1701                      MLX5DV_FLOW_VLAN_PCP_MASK_BE) {
1702                         vlan->vlan_tci &= MLX5DV_FLOW_VLAN_PCP_MASK;
1703                         vlan->vlan_tci |=
1704                                 rte_be_to_cpu_16(vlan_v->tci &
1705                                                  MLX5DV_FLOW_VLAN_PCP_MASK_BE);
1706                 }
1707                 if ((vlan_m->tci & MLX5DV_FLOW_VLAN_VID_MASK_BE) ==
1708                      MLX5DV_FLOW_VLAN_VID_MASK_BE) {
1709                         vlan->vlan_tci &= ~MLX5DV_FLOW_VLAN_VID_MASK;
1710                         vlan->vlan_tci |=
1711                                 rte_be_to_cpu_16(vlan_v->tci &
1712                                                  MLX5DV_FLOW_VLAN_VID_MASK_BE);
1713                 }
1714                 if (vlan_m->inner_type == nic_mask.inner_type)
1715                         vlan->eth_proto = rte_be_to_cpu_16(vlan_v->inner_type &
1716                                                            vlan_m->inner_type);
1717         }
1718 }
1719
1720 /**
1721  * Validate the push VLAN action.
1722  *
1723  * @param[in] action_flags
1724  *   Holds the actions detected until now.
1725  * @param[in] action
1726  *   Pointer to the encap action.
1727  * @param[in] attr
1728  *   Pointer to flow attributes
1729  * @param[out] error
1730  *   Pointer to error structure.
1731  *
1732  * @return
1733  *   0 on success, a negative errno value otherwise and rte_errno is set.
1734  */
1735 static int
1736 flow_dv_validate_action_push_vlan(uint64_t action_flags,
1737                                   uint64_t item_flags,
1738                                   const struct rte_flow_action *action,
1739                                   const struct rte_flow_attr *attr,
1740                                   struct rte_flow_error *error)
1741 {
1742         const struct rte_flow_action_of_push_vlan *push_vlan = action->conf;
1743
1744         if (push_vlan->ethertype != RTE_BE16(RTE_ETHER_TYPE_VLAN) &&
1745             push_vlan->ethertype != RTE_BE16(RTE_ETHER_TYPE_QINQ))
1746                 return rte_flow_error_set(error, EINVAL,
1747                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
1748                                           "invalid vlan ethertype");
1749         if (action_flags &
1750                 (MLX5_FLOW_ACTION_OF_POP_VLAN | MLX5_FLOW_ACTION_OF_PUSH_VLAN))
1751                 return rte_flow_error_set(error, ENOTSUP,
1752                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
1753                                           "no support for multiple VLAN "
1754                                           "actions");
1755         if (!mlx5_flow_find_action
1756                         (action + 1, RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID) &&
1757             !(item_flags & MLX5_FLOW_LAYER_OUTER_VLAN))
1758                 return rte_flow_error_set(error, ENOTSUP,
1759                                 RTE_FLOW_ERROR_TYPE_ACTION, action,
1760                                 "push VLAN needs to match on VLAN in order to "
1761                                 "get VLAN VID information because there is "
1762                                 "no followed set VLAN VID action");
1763         if (action_flags & MLX5_FLOW_ACTION_PORT_ID)
1764                 return rte_flow_error_set(error, EINVAL,
1765                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
1766                                           "wrong action order, port_id should "
1767                                           "be after push VLAN");
1768         (void)attr;
1769         return 0;
1770 }
1771
1772 /**
1773  * Validate the set VLAN PCP.
1774  *
1775  * @param[in] action_flags
1776  *   Holds the actions detected until now.
1777  * @param[in] actions
1778  *   Pointer to the list of actions remaining in the flow rule.
1779  * @param[in] attr
1780  *   Pointer to flow attributes
1781  * @param[out] error
1782  *   Pointer to error structure.
1783  *
1784  * @return
1785  *   0 on success, a negative errno value otherwise and rte_errno is set.
1786  */
1787 static int
1788 flow_dv_validate_action_set_vlan_pcp(uint64_t action_flags,
1789                                      const struct rte_flow_action actions[],
1790                                      struct rte_flow_error *error)
1791 {
1792         const struct rte_flow_action *action = actions;
1793         const struct rte_flow_action_of_set_vlan_pcp *conf = action->conf;
1794
1795         if (conf->vlan_pcp > 7)
1796                 return rte_flow_error_set(error, EINVAL,
1797                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
1798                                           "VLAN PCP value is too big");
1799         if (!(action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN))
1800                 return rte_flow_error_set(error, ENOTSUP,
1801                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
1802                                           "set VLAN PCP action must follow "
1803                                           "the push VLAN action");
1804         if (action_flags & MLX5_FLOW_ACTION_OF_SET_VLAN_PCP)
1805                 return rte_flow_error_set(error, ENOTSUP,
1806                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
1807                                           "Multiple VLAN PCP modification are "
1808                                           "not supported");
1809         if (action_flags & MLX5_FLOW_ACTION_PORT_ID)
1810                 return rte_flow_error_set(error, EINVAL,
1811                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
1812                                           "wrong action order, port_id should "
1813                                           "be after set VLAN PCP");
1814         return 0;
1815 }
1816
1817 /**
1818  * Validate the set VLAN VID.
1819  *
1820  * @param[in] item_flags
1821  *   Holds the items detected in this rule.
1822  * @param[in] actions
1823  *   Pointer to the list of actions remaining in the flow rule.
1824  * @param[in] attr
1825  *   Pointer to flow attributes
1826  * @param[out] error
1827  *   Pointer to error structure.
1828  *
1829  * @return
1830  *   0 on success, a negative errno value otherwise and rte_errno is set.
1831  */
1832 static int
1833 flow_dv_validate_action_set_vlan_vid(uint64_t item_flags,
1834                                      uint64_t action_flags,
1835                                      const struct rte_flow_action actions[],
1836                                      struct rte_flow_error *error)
1837 {
1838         const struct rte_flow_action *action = actions;
1839         const struct rte_flow_action_of_set_vlan_vid *conf = action->conf;
1840
1841         if (conf->vlan_vid > RTE_BE16(0xFFE))
1842                 return rte_flow_error_set(error, EINVAL,
1843                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
1844                                           "VLAN VID value is too big");
1845         /* there is an of_push_vlan action before us */
1846         if (action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN) {
1847                 if (mlx5_flow_find_action(actions + 1,
1848                                           RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID))
1849                         return rte_flow_error_set(error, ENOTSUP,
1850                                         RTE_FLOW_ERROR_TYPE_ACTION, action,
1851                                         "Multiple VLAN VID modifications are "
1852                                         "not supported");
1853                 else
1854                         return 0;
1855         }
1856
1857         /*
1858          * Action is on an existing VLAN header:
1859          *    Need to verify this is a single modify CID action.
1860          *   Rule mast include a match on outer VLAN.
1861          */
1862         if (action_flags & MLX5_FLOW_ACTION_OF_SET_VLAN_VID)
1863                 return rte_flow_error_set(error, ENOTSUP,
1864                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
1865                                           "Multiple VLAN VID modifications are "
1866                                           "not supported");
1867         if (!(item_flags & MLX5_FLOW_LAYER_OUTER_VLAN))
1868                 return rte_flow_error_set(error, EINVAL,
1869                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
1870                                           "match on VLAN is required in order "
1871                                           "to set VLAN VID");
1872         if (action_flags & MLX5_FLOW_ACTION_PORT_ID)
1873                 return rte_flow_error_set(error, EINVAL,
1874                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
1875                                           "wrong action order, port_id should "
1876                                           "be after set VLAN VID");
1877         return 0;
1878 }
1879
1880 /*
1881  * Validate the FLAG action.
1882  *
1883  * @param[in] dev
1884  *   Pointer to the rte_eth_dev structure.
1885  * @param[in] action_flags
1886  *   Holds the actions detected until now.
1887  * @param[in] attr
1888  *   Pointer to flow attributes
1889  * @param[out] error
1890  *   Pointer to error structure.
1891  *
1892  * @return
1893  *   0 on success, a negative errno value otherwise and rte_errno is set.
1894  */
1895 static int
1896 flow_dv_validate_action_flag(struct rte_eth_dev *dev,
1897                              uint64_t action_flags,
1898                              const struct rte_flow_attr *attr,
1899                              struct rte_flow_error *error)
1900 {
1901         struct mlx5_priv *priv = dev->data->dev_private;
1902         struct mlx5_dev_config *config = &priv->config;
1903         int ret;
1904
1905         /* Fall back if no extended metadata register support. */
1906         if (config->dv_xmeta_en == MLX5_XMETA_MODE_LEGACY)
1907                 return mlx5_flow_validate_action_flag(action_flags, attr,
1908                                                       error);
1909         /* Extensive metadata mode requires registers. */
1910         if (!mlx5_flow_ext_mreg_supported(dev))
1911                 return rte_flow_error_set(error, ENOTSUP,
1912                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1913                                           "no metadata registers "
1914                                           "to support flag action");
1915         if (!(priv->sh->dv_mark_mask & MLX5_FLOW_MARK_DEFAULT))
1916                 return rte_flow_error_set(error, ENOTSUP,
1917                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1918                                           "extended metadata register"
1919                                           " isn't available");
1920         ret = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
1921         if (ret < 0)
1922                 return ret;
1923         assert(ret > 0);
1924         if (action_flags & MLX5_FLOW_ACTION_DROP)
1925                 return rte_flow_error_set(error, EINVAL,
1926                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1927                                           "can't drop and flag in same flow");
1928         if (action_flags & MLX5_FLOW_ACTION_MARK)
1929                 return rte_flow_error_set(error, EINVAL,
1930                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1931                                           "can't mark and flag in same flow");
1932         if (action_flags & MLX5_FLOW_ACTION_FLAG)
1933                 return rte_flow_error_set(error, EINVAL,
1934                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1935                                           "can't have 2 flag"
1936                                           " actions in same flow");
1937         return 0;
1938 }
1939
1940 /**
1941  * Validate MARK action.
1942  *
1943  * @param[in] dev
1944  *   Pointer to the rte_eth_dev structure.
1945  * @param[in] action
1946  *   Pointer to action.
1947  * @param[in] action_flags
1948  *   Holds the actions detected until now.
1949  * @param[in] attr
1950  *   Pointer to flow attributes
1951  * @param[out] error
1952  *   Pointer to error structure.
1953  *
1954  * @return
1955  *   0 on success, a negative errno value otherwise and rte_errno is set.
1956  */
1957 static int
1958 flow_dv_validate_action_mark(struct rte_eth_dev *dev,
1959                              const struct rte_flow_action *action,
1960                              uint64_t action_flags,
1961                              const struct rte_flow_attr *attr,
1962                              struct rte_flow_error *error)
1963 {
1964         struct mlx5_priv *priv = dev->data->dev_private;
1965         struct mlx5_dev_config *config = &priv->config;
1966         const struct rte_flow_action_mark *mark = action->conf;
1967         int ret;
1968
1969         /* Fall back if no extended metadata register support. */
1970         if (config->dv_xmeta_en == MLX5_XMETA_MODE_LEGACY)
1971                 return mlx5_flow_validate_action_mark(action, action_flags,
1972                                                       attr, error);
1973         /* Extensive metadata mode requires registers. */
1974         if (!mlx5_flow_ext_mreg_supported(dev))
1975                 return rte_flow_error_set(error, ENOTSUP,
1976                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1977                                           "no metadata registers "
1978                                           "to support mark action");
1979         if (!priv->sh->dv_mark_mask)
1980                 return rte_flow_error_set(error, ENOTSUP,
1981                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1982                                           "extended metadata register"
1983                                           " isn't available");
1984         ret = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
1985         if (ret < 0)
1986                 return ret;
1987         assert(ret > 0);
1988         if (!mark)
1989                 return rte_flow_error_set(error, EINVAL,
1990                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
1991                                           "configuration cannot be null");
1992         if (mark->id >= (MLX5_FLOW_MARK_MAX & priv->sh->dv_mark_mask))
1993                 return rte_flow_error_set(error, EINVAL,
1994                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1995                                           &mark->id,
1996                                           "mark id exceeds the limit");
1997         if (action_flags & MLX5_FLOW_ACTION_DROP)
1998                 return rte_flow_error_set(error, EINVAL,
1999                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2000                                           "can't drop and mark in same flow");
2001         if (action_flags & MLX5_FLOW_ACTION_FLAG)
2002                 return rte_flow_error_set(error, EINVAL,
2003                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2004                                           "can't flag and mark in same flow");
2005         if (action_flags & MLX5_FLOW_ACTION_MARK)
2006                 return rte_flow_error_set(error, EINVAL,
2007                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2008                                           "can't have 2 mark actions in same"
2009                                           " flow");
2010         return 0;
2011 }
2012
2013 /**
2014  * Validate SET_META action.
2015  *
2016  * @param[in] dev
2017  *   Pointer to the rte_eth_dev structure.
2018  * @param[in] action
2019  *   Pointer to the encap action.
2020  * @param[in] action_flags
2021  *   Holds the actions detected until now.
2022  * @param[in] attr
2023  *   Pointer to flow attributes
2024  * @param[out] error
2025  *   Pointer to error structure.
2026  *
2027  * @return
2028  *   0 on success, a negative errno value otherwise and rte_errno is set.
2029  */
2030 static int
2031 flow_dv_validate_action_set_meta(struct rte_eth_dev *dev,
2032                                  const struct rte_flow_action *action,
2033                                  uint64_t action_flags __rte_unused,
2034                                  const struct rte_flow_attr *attr,
2035                                  struct rte_flow_error *error)
2036 {
2037         const struct rte_flow_action_set_meta *conf;
2038         uint32_t nic_mask = UINT32_MAX;
2039         enum modify_reg reg;
2040
2041         if (!mlx5_flow_ext_mreg_supported(dev))
2042                 return rte_flow_error_set(error, ENOTSUP,
2043                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2044                                           "extended metadata register"
2045                                           " isn't supported");
2046         reg = flow_dv_get_metadata_reg(dev, attr, error);
2047         if (reg < 0)
2048                 return reg;
2049         if (reg != REG_A && reg != REG_B) {
2050                 struct mlx5_priv *priv = dev->data->dev_private;
2051
2052                 nic_mask = priv->sh->dv_meta_mask;
2053         }
2054         if (!(action->conf))
2055                 return rte_flow_error_set(error, EINVAL,
2056                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2057                                           "configuration cannot be null");
2058         conf = (const struct rte_flow_action_set_meta *)action->conf;
2059         if (!conf->mask)
2060                 return rte_flow_error_set(error, EINVAL,
2061                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2062                                           "zero mask doesn't have any effect");
2063         if (conf->mask & ~nic_mask)
2064                 return rte_flow_error_set(error, EINVAL,
2065                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2066                                           "meta data must be within reg C0");
2067         if (!(conf->data & conf->mask))
2068                 return rte_flow_error_set(error, EINVAL,
2069                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2070                                           "zero value has no effect");
2071         return 0;
2072 }
2073
2074 /**
2075  * Validate SET_TAG action.
2076  *
2077  * @param[in] dev
2078  *   Pointer to the rte_eth_dev structure.
2079  * @param[in] action
2080  *   Pointer to the encap action.
2081  * @param[in] action_flags
2082  *   Holds the actions detected until now.
2083  * @param[in] attr
2084  *   Pointer to flow attributes
2085  * @param[out] error
2086  *   Pointer to error structure.
2087  *
2088  * @return
2089  *   0 on success, a negative errno value otherwise and rte_errno is set.
2090  */
2091 static int
2092 flow_dv_validate_action_set_tag(struct rte_eth_dev *dev,
2093                                 const struct rte_flow_action *action,
2094                                 uint64_t action_flags,
2095                                 const struct rte_flow_attr *attr,
2096                                 struct rte_flow_error *error)
2097 {
2098         const struct rte_flow_action_set_tag *conf;
2099         const uint64_t terminal_action_flags =
2100                 MLX5_FLOW_ACTION_DROP | MLX5_FLOW_ACTION_QUEUE |
2101                 MLX5_FLOW_ACTION_RSS;
2102         int ret;
2103
2104         if (!mlx5_flow_ext_mreg_supported(dev))
2105                 return rte_flow_error_set(error, ENOTSUP,
2106                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2107                                           "extensive metadata register"
2108                                           " isn't supported");
2109         if (!(action->conf))
2110                 return rte_flow_error_set(error, EINVAL,
2111                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2112                                           "configuration cannot be null");
2113         conf = (const struct rte_flow_action_set_tag *)action->conf;
2114         if (!conf->mask)
2115                 return rte_flow_error_set(error, EINVAL,
2116                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2117                                           "zero mask doesn't have any effect");
2118         ret = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, conf->index, error);
2119         if (ret < 0)
2120                 return ret;
2121         if (!attr->transfer && attr->ingress &&
2122             (action_flags & terminal_action_flags))
2123                 return rte_flow_error_set(error, EINVAL,
2124                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2125                                           "set_tag has no effect"
2126                                           " with terminal actions");
2127         return 0;
2128 }
2129
2130 /**
2131  * Validate count action.
2132  *
2133  * @param[in] dev
2134  *   device otr.
2135  * @param[out] error
2136  *   Pointer to error structure.
2137  *
2138  * @return
2139  *   0 on success, a negative errno value otherwise and rte_errno is set.
2140  */
2141 static int
2142 flow_dv_validate_action_count(struct rte_eth_dev *dev,
2143                               struct rte_flow_error *error)
2144 {
2145         struct mlx5_priv *priv = dev->data->dev_private;
2146
2147         if (!priv->config.devx)
2148                 goto notsup_err;
2149 #ifdef HAVE_IBV_FLOW_DEVX_COUNTERS
2150         return 0;
2151 #endif
2152 notsup_err:
2153         return rte_flow_error_set
2154                       (error, ENOTSUP,
2155                        RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2156                        NULL,
2157                        "count action not supported");
2158 }
2159
2160 /**
2161  * Validate the L2 encap action.
2162  *
2163  * @param[in] action_flags
2164  *   Holds the actions detected until now.
2165  * @param[in] action
2166  *   Pointer to the encap action.
2167  * @param[in] attr
2168  *   Pointer to flow attributes
2169  * @param[out] error
2170  *   Pointer to error structure.
2171  *
2172  * @return
2173  *   0 on success, a negative errno value otherwise and rte_errno is set.
2174  */
2175 static int
2176 flow_dv_validate_action_l2_encap(uint64_t action_flags,
2177                                  const struct rte_flow_action *action,
2178                                  const struct rte_flow_attr *attr,
2179                                  struct rte_flow_error *error)
2180 {
2181         if (!(action->conf))
2182                 return rte_flow_error_set(error, EINVAL,
2183                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2184                                           "configuration cannot be null");
2185         if (action_flags & MLX5_FLOW_ACTION_DROP)
2186                 return rte_flow_error_set(error, EINVAL,
2187                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2188                                           "can't drop and encap in same flow");
2189         if (action_flags & (MLX5_FLOW_ENCAP_ACTIONS | MLX5_FLOW_DECAP_ACTIONS))
2190                 return rte_flow_error_set(error, EINVAL,
2191                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2192                                           "can only have a single encap or"
2193                                           " decap action in a flow");
2194         if (!attr->transfer && attr->ingress)
2195                 return rte_flow_error_set(error, ENOTSUP,
2196                                           RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
2197                                           NULL,
2198                                           "encap action not supported for "
2199                                           "ingress");
2200         return 0;
2201 }
2202
2203 /**
2204  * Validate the L2 decap action.
2205  *
2206  * @param[in] action_flags
2207  *   Holds the actions detected until now.
2208  * @param[in] attr
2209  *   Pointer to flow attributes
2210  * @param[out] error
2211  *   Pointer to error structure.
2212  *
2213  * @return
2214  *   0 on success, a negative errno value otherwise and rte_errno is set.
2215  */
2216 static int
2217 flow_dv_validate_action_l2_decap(uint64_t action_flags,
2218                                  const struct rte_flow_attr *attr,
2219                                  struct rte_flow_error *error)
2220 {
2221         if (action_flags & MLX5_FLOW_ACTION_DROP)
2222                 return rte_flow_error_set(error, EINVAL,
2223                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2224                                           "can't drop and decap in same flow");
2225         if (action_flags & (MLX5_FLOW_ENCAP_ACTIONS | MLX5_FLOW_DECAP_ACTIONS))
2226                 return rte_flow_error_set(error, EINVAL,
2227                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2228                                           "can only have a single encap or"
2229                                           " decap action in a flow");
2230         if (action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS)
2231                 return rte_flow_error_set(error, EINVAL,
2232                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2233                                           "can't have decap action after"
2234                                           " modify action");
2235         if (attr->egress)
2236                 return rte_flow_error_set(error, ENOTSUP,
2237                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
2238                                           NULL,
2239                                           "decap action not supported for "
2240                                           "egress");
2241         return 0;
2242 }
2243
2244 /**
2245  * Validate the raw encap action.
2246  *
2247  * @param[in] action_flags
2248  *   Holds the actions detected until now.
2249  * @param[in] action
2250  *   Pointer to the encap action.
2251  * @param[in] attr
2252  *   Pointer to flow attributes
2253  * @param[out] error
2254  *   Pointer to error structure.
2255  *
2256  * @return
2257  *   0 on success, a negative errno value otherwise and rte_errno is set.
2258  */
2259 static int
2260 flow_dv_validate_action_raw_encap(uint64_t action_flags,
2261                                   const struct rte_flow_action *action,
2262                                   const struct rte_flow_attr *attr,
2263                                   struct rte_flow_error *error)
2264 {
2265         const struct rte_flow_action_raw_encap *raw_encap =
2266                 (const struct rte_flow_action_raw_encap *)action->conf;
2267         if (!(action->conf))
2268                 return rte_flow_error_set(error, EINVAL,
2269                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2270                                           "configuration cannot be null");
2271         if (action_flags & MLX5_FLOW_ACTION_DROP)
2272                 return rte_flow_error_set(error, EINVAL,
2273                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2274                                           "can't drop and encap in same flow");
2275         if (action_flags & MLX5_FLOW_ENCAP_ACTIONS)
2276                 return rte_flow_error_set(error, EINVAL,
2277                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2278                                           "can only have a single encap"
2279                                           " action in a flow");
2280         /* encap without preceding decap is not supported for ingress */
2281         if (!attr->transfer &&  attr->ingress &&
2282             !(action_flags & MLX5_FLOW_ACTION_RAW_DECAP))
2283                 return rte_flow_error_set(error, ENOTSUP,
2284                                           RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
2285                                           NULL,
2286                                           "encap action not supported for "
2287                                           "ingress");
2288         if (!raw_encap->size || !raw_encap->data)
2289                 return rte_flow_error_set(error, EINVAL,
2290                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2291                                           "raw encap data cannot be empty");
2292         return 0;
2293 }
2294
2295 /**
2296  * Validate the raw decap action.
2297  *
2298  * @param[in] action_flags
2299  *   Holds the actions detected until now.
2300  * @param[in] action
2301  *   Pointer to the encap action.
2302  * @param[in] attr
2303  *   Pointer to flow attributes
2304  * @param[out] error
2305  *   Pointer to error structure.
2306  *
2307  * @return
2308  *   0 on success, a negative errno value otherwise and rte_errno is set.
2309  */
2310 static int
2311 flow_dv_validate_action_raw_decap(uint64_t action_flags,
2312                                   const struct rte_flow_action *action,
2313                                   const struct rte_flow_attr *attr,
2314                                   struct rte_flow_error *error)
2315 {
2316         const struct rte_flow_action_raw_decap *decap   = action->conf;
2317
2318         if (action_flags & MLX5_FLOW_ACTION_DROP)
2319                 return rte_flow_error_set(error, EINVAL,
2320                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2321                                           "can't drop and decap in same flow");
2322         if (action_flags & MLX5_FLOW_ENCAP_ACTIONS)
2323                 return rte_flow_error_set(error, EINVAL,
2324                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2325                                           "can't have encap action before"
2326                                           " decap action");
2327         if (action_flags & MLX5_FLOW_DECAP_ACTIONS)
2328                 return rte_flow_error_set(error, EINVAL,
2329                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2330                                           "can only have a single decap"
2331                                           " action in a flow");
2332         /* decap action is valid on egress only if it is followed by encap */
2333         if (attr->egress && decap &&
2334             decap->size > MLX5_ENCAPSULATION_DECISION_SIZE) {
2335                 return rte_flow_error_set(error, ENOTSUP,
2336                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
2337                                           NULL, "decap action not supported"
2338                                           " for egress");
2339         } else if (decap && decap->size > MLX5_ENCAPSULATION_DECISION_SIZE &&
2340                    (action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS)) {
2341                 return rte_flow_error_set(error, EINVAL,
2342                                           RTE_FLOW_ERROR_TYPE_ACTION,
2343                                           NULL,
2344                                           "can't have decap action "
2345                                           "after modify action");
2346         }
2347         return 0;
2348 }
2349
2350 /**
2351  * Find existing encap/decap resource or create and register a new one.
2352  *
2353  * @param[in, out] dev
2354  *   Pointer to rte_eth_dev structure.
2355  * @param[in, out] resource
2356  *   Pointer to encap/decap resource.
2357  * @parm[in, out] dev_flow
2358  *   Pointer to the dev_flow.
2359  * @param[out] error
2360  *   pointer to error structure.
2361  *
2362  * @return
2363  *   0 on success otherwise -errno and errno is set.
2364  */
2365 static int
2366 flow_dv_encap_decap_resource_register
2367                         (struct rte_eth_dev *dev,
2368                          struct mlx5_flow_dv_encap_decap_resource *resource,
2369                          struct mlx5_flow *dev_flow,
2370                          struct rte_flow_error *error)
2371 {
2372         struct mlx5_priv *priv = dev->data->dev_private;
2373         struct mlx5_ibv_shared *sh = priv->sh;
2374         struct mlx5_flow_dv_encap_decap_resource *cache_resource;
2375         struct mlx5dv_dr_domain *domain;
2376
2377         resource->flags = dev_flow->group ? 0 : 1;
2378         if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB)
2379                 domain = sh->fdb_domain;
2380         else if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_NIC_RX)
2381                 domain = sh->rx_domain;
2382         else
2383                 domain = sh->tx_domain;
2384         /* Lookup a matching resource from cache. */
2385         LIST_FOREACH(cache_resource, &sh->encaps_decaps, next) {
2386                 if (resource->reformat_type == cache_resource->reformat_type &&
2387                     resource->ft_type == cache_resource->ft_type &&
2388                     resource->flags == cache_resource->flags &&
2389                     resource->size == cache_resource->size &&
2390                     !memcmp((const void *)resource->buf,
2391                             (const void *)cache_resource->buf,
2392                             resource->size)) {
2393                         DRV_LOG(DEBUG, "encap/decap resource %p: refcnt %d++",
2394                                 (void *)cache_resource,
2395                                 rte_atomic32_read(&cache_resource->refcnt));
2396                         rte_atomic32_inc(&cache_resource->refcnt);
2397                         dev_flow->dv.encap_decap = cache_resource;
2398                         return 0;
2399                 }
2400         }
2401         /* Register new encap/decap resource. */
2402         cache_resource = rte_calloc(__func__, 1, sizeof(*cache_resource), 0);
2403         if (!cache_resource)
2404                 return rte_flow_error_set(error, ENOMEM,
2405                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2406                                           "cannot allocate resource memory");
2407         *cache_resource = *resource;
2408         cache_resource->verbs_action =
2409                 mlx5_glue->dv_create_flow_action_packet_reformat
2410                         (sh->ctx, cache_resource->reformat_type,
2411                          cache_resource->ft_type, domain, cache_resource->flags,
2412                          cache_resource->size,
2413                          (cache_resource->size ? cache_resource->buf : NULL));
2414         if (!cache_resource->verbs_action) {
2415                 rte_free(cache_resource);
2416                 return rte_flow_error_set(error, ENOMEM,
2417                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2418                                           NULL, "cannot create action");
2419         }
2420         rte_atomic32_init(&cache_resource->refcnt);
2421         rte_atomic32_inc(&cache_resource->refcnt);
2422         LIST_INSERT_HEAD(&sh->encaps_decaps, cache_resource, next);
2423         dev_flow->dv.encap_decap = cache_resource;
2424         DRV_LOG(DEBUG, "new encap/decap resource %p: refcnt %d++",
2425                 (void *)cache_resource,
2426                 rte_atomic32_read(&cache_resource->refcnt));
2427         return 0;
2428 }
2429
2430 /**
2431  * Find existing table jump resource or create and register a new one.
2432  *
2433  * @param[in, out] dev
2434  *   Pointer to rte_eth_dev structure.
2435  * @param[in, out] tbl
2436  *   Pointer to flow table resource.
2437  * @parm[in, out] dev_flow
2438  *   Pointer to the dev_flow.
2439  * @param[out] error
2440  *   pointer to error structure.
2441  *
2442  * @return
2443  *   0 on success otherwise -errno and errno is set.
2444  */
2445 static int
2446 flow_dv_jump_tbl_resource_register
2447                         (struct rte_eth_dev *dev __rte_unused,
2448                          struct mlx5_flow_tbl_resource *tbl,
2449                          struct mlx5_flow *dev_flow,
2450                          struct rte_flow_error *error)
2451 {
2452         struct mlx5_flow_tbl_data_entry *tbl_data =
2453                 container_of(tbl, struct mlx5_flow_tbl_data_entry, tbl);
2454         int cnt;
2455
2456         assert(tbl);
2457         cnt = rte_atomic32_read(&tbl_data->jump.refcnt);
2458         if (!cnt) {
2459                 tbl_data->jump.action =
2460                         mlx5_glue->dr_create_flow_action_dest_flow_tbl
2461                         (tbl->obj);
2462                 if (!tbl_data->jump.action)
2463                         return rte_flow_error_set(error, ENOMEM,
2464                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2465                                         NULL, "cannot create jump action");
2466                 DRV_LOG(DEBUG, "new jump table resource %p: refcnt %d++",
2467                         (void *)&tbl_data->jump, cnt);
2468         } else {
2469                 assert(tbl_data->jump.action);
2470                 DRV_LOG(DEBUG, "existed jump table resource %p: refcnt %d++",
2471                         (void *)&tbl_data->jump, cnt);
2472         }
2473         rte_atomic32_inc(&tbl_data->jump.refcnt);
2474         dev_flow->dv.jump = &tbl_data->jump;
2475         return 0;
2476 }
2477
2478 /**
2479  * Find existing table port ID resource or create and register a new one.
2480  *
2481  * @param[in, out] dev
2482  *   Pointer to rte_eth_dev structure.
2483  * @param[in, out] resource
2484  *   Pointer to port ID action resource.
2485  * @parm[in, out] dev_flow
2486  *   Pointer to the dev_flow.
2487  * @param[out] error
2488  *   pointer to error structure.
2489  *
2490  * @return
2491  *   0 on success otherwise -errno and errno is set.
2492  */
2493 static int
2494 flow_dv_port_id_action_resource_register
2495                         (struct rte_eth_dev *dev,
2496                          struct mlx5_flow_dv_port_id_action_resource *resource,
2497                          struct mlx5_flow *dev_flow,
2498                          struct rte_flow_error *error)
2499 {
2500         struct mlx5_priv *priv = dev->data->dev_private;
2501         struct mlx5_ibv_shared *sh = priv->sh;
2502         struct mlx5_flow_dv_port_id_action_resource *cache_resource;
2503
2504         /* Lookup a matching resource from cache. */
2505         LIST_FOREACH(cache_resource, &sh->port_id_action_list, next) {
2506                 if (resource->port_id == cache_resource->port_id) {
2507                         DRV_LOG(DEBUG, "port id action resource resource %p: "
2508                                 "refcnt %d++",
2509                                 (void *)cache_resource,
2510                                 rte_atomic32_read(&cache_resource->refcnt));
2511                         rte_atomic32_inc(&cache_resource->refcnt);
2512                         dev_flow->dv.port_id_action = cache_resource;
2513                         return 0;
2514                 }
2515         }
2516         /* Register new port id action resource. */
2517         cache_resource = rte_calloc(__func__, 1, sizeof(*cache_resource), 0);
2518         if (!cache_resource)
2519                 return rte_flow_error_set(error, ENOMEM,
2520                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2521                                           "cannot allocate resource memory");
2522         *cache_resource = *resource;
2523         /*
2524          * Depending on rdma_core version the glue routine calls
2525          * either mlx5dv_dr_action_create_dest_ib_port(domain, ibv_port)
2526          * or mlx5dv_dr_action_create_dest_vport(domain, vport_id).
2527          */
2528         cache_resource->action =
2529                 mlx5_glue->dr_create_flow_action_dest_port
2530                         (priv->sh->fdb_domain, resource->port_id);
2531         if (!cache_resource->action) {
2532                 rte_free(cache_resource);
2533                 return rte_flow_error_set(error, ENOMEM,
2534                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2535                                           NULL, "cannot create action");
2536         }
2537         rte_atomic32_init(&cache_resource->refcnt);
2538         rte_atomic32_inc(&cache_resource->refcnt);
2539         LIST_INSERT_HEAD(&sh->port_id_action_list, cache_resource, next);
2540         dev_flow->dv.port_id_action = cache_resource;
2541         DRV_LOG(DEBUG, "new port id action resource %p: refcnt %d++",
2542                 (void *)cache_resource,
2543                 rte_atomic32_read(&cache_resource->refcnt));
2544         return 0;
2545 }
2546
2547 /**
2548  * Find existing push vlan resource or create and register a new one.
2549  *
2550  * @param [in, out] dev
2551  *   Pointer to rte_eth_dev structure.
2552  * @param[in, out] resource
2553  *   Pointer to port ID action resource.
2554  * @parm[in, out] dev_flow
2555  *   Pointer to the dev_flow.
2556  * @param[out] error
2557  *   pointer to error structure.
2558  *
2559  * @return
2560  *   0 on success otherwise -errno and errno is set.
2561  */
2562 static int
2563 flow_dv_push_vlan_action_resource_register
2564                        (struct rte_eth_dev *dev,
2565                         struct mlx5_flow_dv_push_vlan_action_resource *resource,
2566                         struct mlx5_flow *dev_flow,
2567                         struct rte_flow_error *error)
2568 {
2569         struct mlx5_priv *priv = dev->data->dev_private;
2570         struct mlx5_ibv_shared *sh = priv->sh;
2571         struct mlx5_flow_dv_push_vlan_action_resource *cache_resource;
2572         struct mlx5dv_dr_domain *domain;
2573
2574         /* Lookup a matching resource from cache. */
2575         LIST_FOREACH(cache_resource, &sh->push_vlan_action_list, next) {
2576                 if (resource->vlan_tag == cache_resource->vlan_tag &&
2577                     resource->ft_type == cache_resource->ft_type) {
2578                         DRV_LOG(DEBUG, "push-VLAN action resource resource %p: "
2579                                 "refcnt %d++",
2580                                 (void *)cache_resource,
2581                                 rte_atomic32_read(&cache_resource->refcnt));
2582                         rte_atomic32_inc(&cache_resource->refcnt);
2583                         dev_flow->dv.push_vlan_res = cache_resource;
2584                         return 0;
2585                 }
2586         }
2587         /* Register new push_vlan action resource. */
2588         cache_resource = rte_calloc(__func__, 1, sizeof(*cache_resource), 0);
2589         if (!cache_resource)
2590                 return rte_flow_error_set(error, ENOMEM,
2591                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2592                                           "cannot allocate resource memory");
2593         *cache_resource = *resource;
2594         if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB)
2595                 domain = sh->fdb_domain;
2596         else if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_NIC_RX)
2597                 domain = sh->rx_domain;
2598         else
2599                 domain = sh->tx_domain;
2600         cache_resource->action =
2601                 mlx5_glue->dr_create_flow_action_push_vlan(domain,
2602                                                            resource->vlan_tag);
2603         if (!cache_resource->action) {
2604                 rte_free(cache_resource);
2605                 return rte_flow_error_set(error, ENOMEM,
2606                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2607                                           NULL, "cannot create action");
2608         }
2609         rte_atomic32_init(&cache_resource->refcnt);
2610         rte_atomic32_inc(&cache_resource->refcnt);
2611         LIST_INSERT_HEAD(&sh->push_vlan_action_list, cache_resource, next);
2612         dev_flow->dv.push_vlan_res = cache_resource;
2613         DRV_LOG(DEBUG, "new push vlan action resource %p: refcnt %d++",
2614                 (void *)cache_resource,
2615                 rte_atomic32_read(&cache_resource->refcnt));
2616         return 0;
2617 }
2618 /**
2619  * Get the size of specific rte_flow_item_type
2620  *
2621  * @param[in] item_type
2622  *   Tested rte_flow_item_type.
2623  *
2624  * @return
2625  *   sizeof struct item_type, 0 if void or irrelevant.
2626  */
2627 static size_t
2628 flow_dv_get_item_len(const enum rte_flow_item_type item_type)
2629 {
2630         size_t retval;
2631
2632         switch (item_type) {
2633         case RTE_FLOW_ITEM_TYPE_ETH:
2634                 retval = sizeof(struct rte_flow_item_eth);
2635                 break;
2636         case RTE_FLOW_ITEM_TYPE_VLAN:
2637                 retval = sizeof(struct rte_flow_item_vlan);
2638                 break;
2639         case RTE_FLOW_ITEM_TYPE_IPV4:
2640                 retval = sizeof(struct rte_flow_item_ipv4);
2641                 break;
2642         case RTE_FLOW_ITEM_TYPE_IPV6:
2643                 retval = sizeof(struct rte_flow_item_ipv6);
2644                 break;
2645         case RTE_FLOW_ITEM_TYPE_UDP:
2646                 retval = sizeof(struct rte_flow_item_udp);
2647                 break;
2648         case RTE_FLOW_ITEM_TYPE_TCP:
2649                 retval = sizeof(struct rte_flow_item_tcp);
2650                 break;
2651         case RTE_FLOW_ITEM_TYPE_VXLAN:
2652                 retval = sizeof(struct rte_flow_item_vxlan);
2653                 break;
2654         case RTE_FLOW_ITEM_TYPE_GRE:
2655                 retval = sizeof(struct rte_flow_item_gre);
2656                 break;
2657         case RTE_FLOW_ITEM_TYPE_NVGRE:
2658                 retval = sizeof(struct rte_flow_item_nvgre);
2659                 break;
2660         case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
2661                 retval = sizeof(struct rte_flow_item_vxlan_gpe);
2662                 break;
2663         case RTE_FLOW_ITEM_TYPE_MPLS:
2664                 retval = sizeof(struct rte_flow_item_mpls);
2665                 break;
2666         case RTE_FLOW_ITEM_TYPE_VOID: /* Fall through. */
2667         default:
2668                 retval = 0;
2669                 break;
2670         }
2671         return retval;
2672 }
2673
2674 #define MLX5_ENCAP_IPV4_VERSION         0x40
2675 #define MLX5_ENCAP_IPV4_IHL_MIN         0x05
2676 #define MLX5_ENCAP_IPV4_TTL_DEF         0x40
2677 #define MLX5_ENCAP_IPV6_VTC_FLOW        0x60000000
2678 #define MLX5_ENCAP_IPV6_HOP_LIMIT       0xff
2679 #define MLX5_ENCAP_VXLAN_FLAGS          0x08000000
2680 #define MLX5_ENCAP_VXLAN_GPE_FLAGS      0x04
2681
2682 /**
2683  * Convert the encap action data from list of rte_flow_item to raw buffer
2684  *
2685  * @param[in] items
2686  *   Pointer to rte_flow_item objects list.
2687  * @param[out] buf
2688  *   Pointer to the output buffer.
2689  * @param[out] size
2690  *   Pointer to the output buffer size.
2691  * @param[out] error
2692  *   Pointer to the error structure.
2693  *
2694  * @return
2695  *   0 on success, a negative errno value otherwise and rte_errno is set.
2696  */
2697 static int
2698 flow_dv_convert_encap_data(const struct rte_flow_item *items, uint8_t *buf,
2699                            size_t *size, struct rte_flow_error *error)
2700 {
2701         struct rte_ether_hdr *eth = NULL;
2702         struct rte_vlan_hdr *vlan = NULL;
2703         struct rte_ipv4_hdr *ipv4 = NULL;
2704         struct rte_ipv6_hdr *ipv6 = NULL;
2705         struct rte_udp_hdr *udp = NULL;
2706         struct rte_vxlan_hdr *vxlan = NULL;
2707         struct rte_vxlan_gpe_hdr *vxlan_gpe = NULL;
2708         struct rte_gre_hdr *gre = NULL;
2709         size_t len;
2710         size_t temp_size = 0;
2711
2712         if (!items)
2713                 return rte_flow_error_set(error, EINVAL,
2714                                           RTE_FLOW_ERROR_TYPE_ACTION,
2715                                           NULL, "invalid empty data");
2716         for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
2717                 len = flow_dv_get_item_len(items->type);
2718                 if (len + temp_size > MLX5_ENCAP_MAX_LEN)
2719                         return rte_flow_error_set(error, EINVAL,
2720                                                   RTE_FLOW_ERROR_TYPE_ACTION,
2721                                                   (void *)items->type,
2722                                                   "items total size is too big"
2723                                                   " for encap action");
2724                 rte_memcpy((void *)&buf[temp_size], items->spec, len);
2725                 switch (items->type) {
2726                 case RTE_FLOW_ITEM_TYPE_ETH:
2727                         eth = (struct rte_ether_hdr *)&buf[temp_size];
2728                         break;
2729                 case RTE_FLOW_ITEM_TYPE_VLAN:
2730                         vlan = (struct rte_vlan_hdr *)&buf[temp_size];
2731                         if (!eth)
2732                                 return rte_flow_error_set(error, EINVAL,
2733                                                 RTE_FLOW_ERROR_TYPE_ACTION,
2734                                                 (void *)items->type,
2735                                                 "eth header not found");
2736                         if (!eth->ether_type)
2737                                 eth->ether_type = RTE_BE16(RTE_ETHER_TYPE_VLAN);
2738                         break;
2739                 case RTE_FLOW_ITEM_TYPE_IPV4:
2740                         ipv4 = (struct rte_ipv4_hdr *)&buf[temp_size];
2741                         if (!vlan && !eth)
2742                                 return rte_flow_error_set(error, EINVAL,
2743                                                 RTE_FLOW_ERROR_TYPE_ACTION,
2744                                                 (void *)items->type,
2745                                                 "neither eth nor vlan"
2746                                                 " header found");
2747                         if (vlan && !vlan->eth_proto)
2748                                 vlan->eth_proto = RTE_BE16(RTE_ETHER_TYPE_IPV4);
2749                         else if (eth && !eth->ether_type)
2750                                 eth->ether_type = RTE_BE16(RTE_ETHER_TYPE_IPV4);
2751                         if (!ipv4->version_ihl)
2752                                 ipv4->version_ihl = MLX5_ENCAP_IPV4_VERSION |
2753                                                     MLX5_ENCAP_IPV4_IHL_MIN;
2754                         if (!ipv4->time_to_live)
2755                                 ipv4->time_to_live = MLX5_ENCAP_IPV4_TTL_DEF;
2756                         break;
2757                 case RTE_FLOW_ITEM_TYPE_IPV6:
2758                         ipv6 = (struct rte_ipv6_hdr *)&buf[temp_size];
2759                         if (!vlan && !eth)
2760                                 return rte_flow_error_set(error, EINVAL,
2761                                                 RTE_FLOW_ERROR_TYPE_ACTION,
2762                                                 (void *)items->type,
2763                                                 "neither eth nor vlan"
2764                                                 " header found");
2765                         if (vlan && !vlan->eth_proto)
2766                                 vlan->eth_proto = RTE_BE16(RTE_ETHER_TYPE_IPV6);
2767                         else if (eth && !eth->ether_type)
2768                                 eth->ether_type = RTE_BE16(RTE_ETHER_TYPE_IPV6);
2769                         if (!ipv6->vtc_flow)
2770                                 ipv6->vtc_flow =
2771                                         RTE_BE32(MLX5_ENCAP_IPV6_VTC_FLOW);
2772                         if (!ipv6->hop_limits)
2773                                 ipv6->hop_limits = MLX5_ENCAP_IPV6_HOP_LIMIT;
2774                         break;
2775                 case RTE_FLOW_ITEM_TYPE_UDP:
2776                         udp = (struct rte_udp_hdr *)&buf[temp_size];
2777                         if (!ipv4 && !ipv6)
2778                                 return rte_flow_error_set(error, EINVAL,
2779                                                 RTE_FLOW_ERROR_TYPE_ACTION,
2780                                                 (void *)items->type,
2781                                                 "ip header not found");
2782                         if (ipv4 && !ipv4->next_proto_id)
2783                                 ipv4->next_proto_id = IPPROTO_UDP;
2784                         else if (ipv6 && !ipv6->proto)
2785                                 ipv6->proto = IPPROTO_UDP;
2786                         break;
2787                 case RTE_FLOW_ITEM_TYPE_VXLAN:
2788                         vxlan = (struct rte_vxlan_hdr *)&buf[temp_size];
2789                         if (!udp)
2790                                 return rte_flow_error_set(error, EINVAL,
2791                                                 RTE_FLOW_ERROR_TYPE_ACTION,
2792                                                 (void *)items->type,
2793                                                 "udp header not found");
2794                         if (!udp->dst_port)
2795                                 udp->dst_port = RTE_BE16(MLX5_UDP_PORT_VXLAN);
2796                         if (!vxlan->vx_flags)
2797                                 vxlan->vx_flags =
2798                                         RTE_BE32(MLX5_ENCAP_VXLAN_FLAGS);
2799                         break;
2800                 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
2801                         vxlan_gpe = (struct rte_vxlan_gpe_hdr *)&buf[temp_size];
2802                         if (!udp)
2803                                 return rte_flow_error_set(error, EINVAL,
2804                                                 RTE_FLOW_ERROR_TYPE_ACTION,
2805                                                 (void *)items->type,
2806                                                 "udp header not found");
2807                         if (!vxlan_gpe->proto)
2808                                 return rte_flow_error_set(error, EINVAL,
2809                                                 RTE_FLOW_ERROR_TYPE_ACTION,
2810                                                 (void *)items->type,
2811                                                 "next protocol not found");
2812                         if (!udp->dst_port)
2813                                 udp->dst_port =
2814                                         RTE_BE16(MLX5_UDP_PORT_VXLAN_GPE);
2815                         if (!vxlan_gpe->vx_flags)
2816                                 vxlan_gpe->vx_flags =
2817                                                 MLX5_ENCAP_VXLAN_GPE_FLAGS;
2818                         break;
2819                 case RTE_FLOW_ITEM_TYPE_GRE:
2820                 case RTE_FLOW_ITEM_TYPE_NVGRE:
2821                         gre = (struct rte_gre_hdr *)&buf[temp_size];
2822                         if (!gre->proto)
2823                                 return rte_flow_error_set(error, EINVAL,
2824                                                 RTE_FLOW_ERROR_TYPE_ACTION,
2825                                                 (void *)items->type,
2826                                                 "next protocol not found");
2827                         if (!ipv4 && !ipv6)
2828                                 return rte_flow_error_set(error, EINVAL,
2829                                                 RTE_FLOW_ERROR_TYPE_ACTION,
2830                                                 (void *)items->type,
2831                                                 "ip header not found");
2832                         if (ipv4 && !ipv4->next_proto_id)
2833                                 ipv4->next_proto_id = IPPROTO_GRE;
2834                         else if (ipv6 && !ipv6->proto)
2835                                 ipv6->proto = IPPROTO_GRE;
2836                         break;
2837                 case RTE_FLOW_ITEM_TYPE_VOID:
2838                         break;
2839                 default:
2840                         return rte_flow_error_set(error, EINVAL,
2841                                                   RTE_FLOW_ERROR_TYPE_ACTION,
2842                                                   (void *)items->type,
2843                                                   "unsupported item type");
2844                         break;
2845                 }
2846                 temp_size += len;
2847         }
2848         *size = temp_size;
2849         return 0;
2850 }
2851
2852 static int
2853 flow_dv_zero_encap_udp_csum(void *data, struct rte_flow_error *error)
2854 {
2855         struct rte_ether_hdr *eth = NULL;
2856         struct rte_vlan_hdr *vlan = NULL;
2857         struct rte_ipv6_hdr *ipv6 = NULL;
2858         struct rte_udp_hdr *udp = NULL;
2859         char *next_hdr;
2860         uint16_t proto;
2861
2862         eth = (struct rte_ether_hdr *)data;
2863         next_hdr = (char *)(eth + 1);
2864         proto = RTE_BE16(eth->ether_type);
2865
2866         /* VLAN skipping */
2867         while (proto == RTE_ETHER_TYPE_VLAN || proto == RTE_ETHER_TYPE_QINQ) {
2868                 vlan = (struct rte_vlan_hdr *)next_hdr;
2869                 proto = RTE_BE16(vlan->eth_proto);
2870                 next_hdr += sizeof(struct rte_vlan_hdr);
2871         }
2872
2873         /* HW calculates IPv4 csum. no need to proceed */
2874         if (proto == RTE_ETHER_TYPE_IPV4)
2875                 return 0;
2876
2877         /* non IPv4/IPv6 header. not supported */
2878         if (proto != RTE_ETHER_TYPE_IPV6) {
2879                 return rte_flow_error_set(error, ENOTSUP,
2880                                           RTE_FLOW_ERROR_TYPE_ACTION,
2881                                           NULL, "Cannot offload non IPv4/IPv6");
2882         }
2883
2884         ipv6 = (struct rte_ipv6_hdr *)next_hdr;
2885
2886         /* ignore non UDP */
2887         if (ipv6->proto != IPPROTO_UDP)
2888                 return 0;
2889
2890         udp = (struct rte_udp_hdr *)(ipv6 + 1);
2891         udp->dgram_cksum = 0;
2892
2893         return 0;
2894 }
2895
2896 /**
2897  * Convert L2 encap action to DV specification.
2898  *
2899  * @param[in] dev
2900  *   Pointer to rte_eth_dev structure.
2901  * @param[in] action
2902  *   Pointer to action structure.
2903  * @param[in, out] dev_flow
2904  *   Pointer to the mlx5_flow.
2905  * @param[in] transfer
2906  *   Mark if the flow is E-Switch flow.
2907  * @param[out] error
2908  *   Pointer to the error structure.
2909  *
2910  * @return
2911  *   0 on success, a negative errno value otherwise and rte_errno is set.
2912  */
2913 static int
2914 flow_dv_create_action_l2_encap(struct rte_eth_dev *dev,
2915                                const struct rte_flow_action *action,
2916                                struct mlx5_flow *dev_flow,
2917                                uint8_t transfer,
2918                                struct rte_flow_error *error)
2919 {
2920         const struct rte_flow_item *encap_data;
2921         const struct rte_flow_action_raw_encap *raw_encap_data;
2922         struct mlx5_flow_dv_encap_decap_resource res = {
2923                 .reformat_type =
2924                         MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TO_L2_TUNNEL,
2925                 .ft_type = transfer ? MLX5DV_FLOW_TABLE_TYPE_FDB :
2926                                       MLX5DV_FLOW_TABLE_TYPE_NIC_TX,
2927         };
2928
2929         if (action->type == RTE_FLOW_ACTION_TYPE_RAW_ENCAP) {
2930                 raw_encap_data =
2931                         (const struct rte_flow_action_raw_encap *)action->conf;
2932                 res.size = raw_encap_data->size;
2933                 memcpy(res.buf, raw_encap_data->data, res.size);
2934                 if (flow_dv_zero_encap_udp_csum(res.buf, error))
2935                         return -rte_errno;
2936         } else {
2937                 if (action->type == RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP)
2938                         encap_data =
2939                                 ((const struct rte_flow_action_vxlan_encap *)
2940                                                 action->conf)->definition;
2941                 else
2942                         encap_data =
2943                                 ((const struct rte_flow_action_nvgre_encap *)
2944                                                 action->conf)->definition;
2945                 if (flow_dv_convert_encap_data(encap_data, res.buf,
2946                                                &res.size, error))
2947                         return -rte_errno;
2948         }
2949         if (flow_dv_encap_decap_resource_register(dev, &res, dev_flow, error))
2950                 return rte_flow_error_set(error, EINVAL,
2951                                           RTE_FLOW_ERROR_TYPE_ACTION,
2952                                           NULL, "can't create L2 encap action");
2953         return 0;
2954 }
2955
2956 /**
2957  * Convert L2 decap action to DV specification.
2958  *
2959  * @param[in] dev
2960  *   Pointer to rte_eth_dev structure.
2961  * @param[in, out] dev_flow
2962  *   Pointer to the mlx5_flow.
2963  * @param[in] transfer
2964  *   Mark if the flow is E-Switch flow.
2965  * @param[out] error
2966  *   Pointer to the error structure.
2967  *
2968  * @return
2969  *   0 on success, a negative errno value otherwise and rte_errno is set.
2970  */
2971 static int
2972 flow_dv_create_action_l2_decap(struct rte_eth_dev *dev,
2973                                struct mlx5_flow *dev_flow,
2974                                uint8_t transfer,
2975                                struct rte_flow_error *error)
2976 {
2977         struct mlx5_flow_dv_encap_decap_resource res = {
2978                 .size = 0,
2979                 .reformat_type =
2980                         MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TUNNEL_TO_L2,
2981                 .ft_type = transfer ? MLX5DV_FLOW_TABLE_TYPE_FDB :
2982                                       MLX5DV_FLOW_TABLE_TYPE_NIC_RX,
2983         };
2984
2985         if (flow_dv_encap_decap_resource_register(dev, &res, dev_flow, error))
2986                 return rte_flow_error_set(error, EINVAL,
2987                                           RTE_FLOW_ERROR_TYPE_ACTION,
2988                                           NULL, "can't create L2 decap action");
2989         return 0;
2990 }
2991
2992 /**
2993  * Convert raw decap/encap (L3 tunnel) action to DV specification.
2994  *
2995  * @param[in] dev
2996  *   Pointer to rte_eth_dev structure.
2997  * @param[in] action
2998  *   Pointer to action structure.
2999  * @param[in, out] dev_flow
3000  *   Pointer to the mlx5_flow.
3001  * @param[in] attr
3002  *   Pointer to the flow attributes.
3003  * @param[out] error
3004  *   Pointer to the error structure.
3005  *
3006  * @return
3007  *   0 on success, a negative errno value otherwise and rte_errno is set.
3008  */
3009 static int
3010 flow_dv_create_action_raw_encap(struct rte_eth_dev *dev,
3011                                 const struct rte_flow_action *action,
3012                                 struct mlx5_flow *dev_flow,
3013                                 const struct rte_flow_attr *attr,
3014                                 struct rte_flow_error *error)
3015 {
3016         const struct rte_flow_action_raw_encap *encap_data;
3017         struct mlx5_flow_dv_encap_decap_resource res;
3018
3019         encap_data = (const struct rte_flow_action_raw_encap *)action->conf;
3020         res.size = encap_data->size;
3021         memcpy(res.buf, encap_data->data, res.size);
3022         res.reformat_type = res.size < MLX5_ENCAPSULATION_DECISION_SIZE ?
3023                 MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L3_TUNNEL_TO_L2 :
3024                 MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TO_L3_TUNNEL;
3025         if (attr->transfer)
3026                 res.ft_type = MLX5DV_FLOW_TABLE_TYPE_FDB;
3027         else
3028                 res.ft_type = attr->egress ? MLX5DV_FLOW_TABLE_TYPE_NIC_TX :
3029                                              MLX5DV_FLOW_TABLE_TYPE_NIC_RX;
3030         if (flow_dv_encap_decap_resource_register(dev, &res, dev_flow, error))
3031                 return rte_flow_error_set(error, EINVAL,
3032                                           RTE_FLOW_ERROR_TYPE_ACTION,
3033                                           NULL, "can't create encap action");
3034         return 0;
3035 }
3036
3037 /**
3038  * Create action push VLAN.
3039  *
3040  * @param[in] dev
3041  *   Pointer to rte_eth_dev structure.
3042  * @param[in] vlan_tag
3043  *   the vlan tag to push to the Ethernet header.
3044  * @param[in, out] dev_flow
3045  *   Pointer to the mlx5_flow.
3046  * @param[in] attr
3047  *   Pointer to the flow attributes.
3048  * @param[out] error
3049  *   Pointer to the error structure.
3050  *
3051  * @return
3052  *   0 on success, a negative errno value otherwise and rte_errno is set.
3053  */
3054 static int
3055 flow_dv_create_action_push_vlan(struct rte_eth_dev *dev,
3056                                 const struct rte_flow_attr *attr,
3057                                 const struct rte_vlan_hdr *vlan,
3058                                 struct mlx5_flow *dev_flow,
3059                                 struct rte_flow_error *error)
3060 {
3061         struct mlx5_flow_dv_push_vlan_action_resource res;
3062
3063         res.vlan_tag =
3064                 rte_cpu_to_be_32(((uint32_t)vlan->eth_proto) << 16 |
3065                                  vlan->vlan_tci);
3066         if (attr->transfer)
3067                 res.ft_type = MLX5DV_FLOW_TABLE_TYPE_FDB;
3068         else
3069                 res.ft_type = attr->egress ? MLX5DV_FLOW_TABLE_TYPE_NIC_TX :
3070                                              MLX5DV_FLOW_TABLE_TYPE_NIC_RX;
3071         return flow_dv_push_vlan_action_resource_register
3072                                             (dev, &res, dev_flow, error);
3073 }
3074
3075 /**
3076  * Validate the modify-header actions.
3077  *
3078  * @param[in] action_flags
3079  *   Holds the actions detected until now.
3080  * @param[in] action
3081  *   Pointer to the modify action.
3082  * @param[out] error
3083  *   Pointer to error structure.
3084  *
3085  * @return
3086  *   0 on success, a negative errno value otherwise and rte_errno is set.
3087  */
3088 static int
3089 flow_dv_validate_action_modify_hdr(const uint64_t action_flags,
3090                                    const struct rte_flow_action *action,
3091                                    struct rte_flow_error *error)
3092 {
3093         if (action->type != RTE_FLOW_ACTION_TYPE_DEC_TTL && !action->conf)
3094                 return rte_flow_error_set(error, EINVAL,
3095                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
3096                                           NULL, "action configuration not set");
3097         if (action_flags & MLX5_FLOW_ENCAP_ACTIONS)
3098                 return rte_flow_error_set(error, EINVAL,
3099                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3100                                           "can't have encap action before"
3101                                           " modify action");
3102         return 0;
3103 }
3104
3105 /**
3106  * Validate the modify-header MAC address actions.
3107  *
3108  * @param[in] action_flags
3109  *   Holds the actions detected until now.
3110  * @param[in] action
3111  *   Pointer to the modify action.
3112  * @param[in] item_flags
3113  *   Holds the items detected.
3114  * @param[out] error
3115  *   Pointer to error structure.
3116  *
3117  * @return
3118  *   0 on success, a negative errno value otherwise and rte_errno is set.
3119  */
3120 static int
3121 flow_dv_validate_action_modify_mac(const uint64_t action_flags,
3122                                    const struct rte_flow_action *action,
3123                                    const uint64_t item_flags,
3124                                    struct rte_flow_error *error)
3125 {
3126         int ret = 0;
3127
3128         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3129         if (!ret) {
3130                 if (!(item_flags & MLX5_FLOW_LAYER_L2))
3131                         return rte_flow_error_set(error, EINVAL,
3132                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3133                                                   NULL,
3134                                                   "no L2 item in pattern");
3135         }
3136         return ret;
3137 }
3138
3139 /**
3140  * Validate the modify-header IPv4 address actions.
3141  *
3142  * @param[in] action_flags
3143  *   Holds the actions detected until now.
3144  * @param[in] action
3145  *   Pointer to the modify action.
3146  * @param[in] item_flags
3147  *   Holds the items detected.
3148  * @param[out] error
3149  *   Pointer to error structure.
3150  *
3151  * @return
3152  *   0 on success, a negative errno value otherwise and rte_errno is set.
3153  */
3154 static int
3155 flow_dv_validate_action_modify_ipv4(const uint64_t action_flags,
3156                                     const struct rte_flow_action *action,
3157                                     const uint64_t item_flags,
3158                                     struct rte_flow_error *error)
3159 {
3160         int ret = 0;
3161
3162         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3163         if (!ret) {
3164                 if (!(item_flags & MLX5_FLOW_LAYER_L3_IPV4))
3165                         return rte_flow_error_set(error, EINVAL,
3166                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3167                                                   NULL,
3168                                                   "no ipv4 item in pattern");
3169         }
3170         return ret;
3171 }
3172
3173 /**
3174  * Validate the modify-header IPv6 address actions.
3175  *
3176  * @param[in] action_flags
3177  *   Holds the actions detected until now.
3178  * @param[in] action
3179  *   Pointer to the modify action.
3180  * @param[in] item_flags
3181  *   Holds the items detected.
3182  * @param[out] error
3183  *   Pointer to error structure.
3184  *
3185  * @return
3186  *   0 on success, a negative errno value otherwise and rte_errno is set.
3187  */
3188 static int
3189 flow_dv_validate_action_modify_ipv6(const uint64_t action_flags,
3190                                     const struct rte_flow_action *action,
3191                                     const uint64_t item_flags,
3192                                     struct rte_flow_error *error)
3193 {
3194         int ret = 0;
3195
3196         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3197         if (!ret) {
3198                 if (!(item_flags & MLX5_FLOW_LAYER_L3_IPV6))
3199                         return rte_flow_error_set(error, EINVAL,
3200                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3201                                                   NULL,
3202                                                   "no ipv6 item in pattern");
3203         }
3204         return ret;
3205 }
3206
3207 /**
3208  * Validate the modify-header TP actions.
3209  *
3210  * @param[in] action_flags
3211  *   Holds the actions detected until now.
3212  * @param[in] action
3213  *   Pointer to the modify action.
3214  * @param[in] item_flags
3215  *   Holds the items detected.
3216  * @param[out] error
3217  *   Pointer to error structure.
3218  *
3219  * @return
3220  *   0 on success, a negative errno value otherwise and rte_errno is set.
3221  */
3222 static int
3223 flow_dv_validate_action_modify_tp(const uint64_t action_flags,
3224                                   const struct rte_flow_action *action,
3225                                   const uint64_t item_flags,
3226                                   struct rte_flow_error *error)
3227 {
3228         int ret = 0;
3229
3230         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3231         if (!ret) {
3232                 if (!(item_flags & MLX5_FLOW_LAYER_L4))
3233                         return rte_flow_error_set(error, EINVAL,
3234                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3235                                                   NULL, "no transport layer "
3236                                                   "in pattern");
3237         }
3238         return ret;
3239 }
3240
3241 /**
3242  * Validate the modify-header actions of increment/decrement
3243  * TCP Sequence-number.
3244  *
3245  * @param[in] action_flags
3246  *   Holds the actions detected until now.
3247  * @param[in] action
3248  *   Pointer to the modify action.
3249  * @param[in] item_flags
3250  *   Holds the items detected.
3251  * @param[out] error
3252  *   Pointer to error structure.
3253  *
3254  * @return
3255  *   0 on success, a negative errno value otherwise and rte_errno is set.
3256  */
3257 static int
3258 flow_dv_validate_action_modify_tcp_seq(const uint64_t action_flags,
3259                                        const struct rte_flow_action *action,
3260                                        const uint64_t item_flags,
3261                                        struct rte_flow_error *error)
3262 {
3263         int ret = 0;
3264
3265         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3266         if (!ret) {
3267                 if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_TCP))
3268                         return rte_flow_error_set(error, EINVAL,
3269                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3270                                                   NULL, "no TCP item in"
3271                                                   " pattern");
3272                 if ((action->type == RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ &&
3273                         (action_flags & MLX5_FLOW_ACTION_DEC_TCP_SEQ)) ||
3274                     (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ &&
3275                         (action_flags & MLX5_FLOW_ACTION_INC_TCP_SEQ)))
3276                         return rte_flow_error_set(error, EINVAL,
3277                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3278                                                   NULL,
3279                                                   "cannot decrease and increase"
3280                                                   " TCP sequence number"
3281                                                   " at the same time");
3282         }
3283         return ret;
3284 }
3285
3286 /**
3287  * Validate the modify-header actions of increment/decrement
3288  * TCP Acknowledgment number.
3289  *
3290  * @param[in] action_flags
3291  *   Holds the actions detected until now.
3292  * @param[in] action
3293  *   Pointer to the modify action.
3294  * @param[in] item_flags
3295  *   Holds the items detected.
3296  * @param[out] error
3297  *   Pointer to error structure.
3298  *
3299  * @return
3300  *   0 on success, a negative errno value otherwise and rte_errno is set.
3301  */
3302 static int
3303 flow_dv_validate_action_modify_tcp_ack(const uint64_t action_flags,
3304                                        const struct rte_flow_action *action,
3305                                        const uint64_t item_flags,
3306                                        struct rte_flow_error *error)
3307 {
3308         int ret = 0;
3309
3310         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3311         if (!ret) {
3312                 if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_TCP))
3313                         return rte_flow_error_set(error, EINVAL,
3314                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3315                                                   NULL, "no TCP item in"
3316                                                   " pattern");
3317                 if ((action->type == RTE_FLOW_ACTION_TYPE_INC_TCP_ACK &&
3318                         (action_flags & MLX5_FLOW_ACTION_DEC_TCP_ACK)) ||
3319                     (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK &&
3320                         (action_flags & MLX5_FLOW_ACTION_INC_TCP_ACK)))
3321                         return rte_flow_error_set(error, EINVAL,
3322                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3323                                                   NULL,
3324                                                   "cannot decrease and increase"
3325                                                   " TCP acknowledgment number"
3326                                                   " at the same time");
3327         }
3328         return ret;
3329 }
3330
3331 /**
3332  * Validate the modify-header TTL actions.
3333  *
3334  * @param[in] action_flags
3335  *   Holds the actions detected until now.
3336  * @param[in] action
3337  *   Pointer to the modify action.
3338  * @param[in] item_flags
3339  *   Holds the items detected.
3340  * @param[out] error
3341  *   Pointer to error structure.
3342  *
3343  * @return
3344  *   0 on success, a negative errno value otherwise and rte_errno is set.
3345  */
3346 static int
3347 flow_dv_validate_action_modify_ttl(const uint64_t action_flags,
3348                                    const struct rte_flow_action *action,
3349                                    const uint64_t item_flags,
3350                                    struct rte_flow_error *error)
3351 {
3352         int ret = 0;
3353
3354         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3355         if (!ret) {
3356                 if (!(item_flags & MLX5_FLOW_LAYER_L3))
3357                         return rte_flow_error_set(error, EINVAL,
3358                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3359                                                   NULL,
3360                                                   "no IP protocol in pattern");
3361         }
3362         return ret;
3363 }
3364
3365 /**
3366  * Validate jump action.
3367  *
3368  * @param[in] action
3369  *   Pointer to the jump action.
3370  * @param[in] action_flags
3371  *   Holds the actions detected until now.
3372  * @param[in] attributes
3373  *   Pointer to flow attributes
3374  * @param[in] external
3375  *   Action belongs to flow rule created by request external to PMD.
3376  * @param[out] error
3377  *   Pointer to error structure.
3378  *
3379  * @return
3380  *   0 on success, a negative errno value otherwise and rte_errno is set.
3381  */
3382 static int
3383 flow_dv_validate_action_jump(const struct rte_flow_action *action,
3384                              uint64_t action_flags,
3385                              const struct rte_flow_attr *attributes,
3386                              bool external, struct rte_flow_error *error)
3387 {
3388         uint32_t target_group, table;
3389         int ret = 0;
3390
3391         if (action_flags & (MLX5_FLOW_FATE_ACTIONS |
3392                             MLX5_FLOW_FATE_ESWITCH_ACTIONS))
3393                 return rte_flow_error_set(error, EINVAL,
3394                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3395                                           "can't have 2 fate actions in"
3396                                           " same flow");
3397         if (action_flags & MLX5_FLOW_ACTION_METER)
3398                 return rte_flow_error_set(error, ENOTSUP,
3399                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3400                                           "jump with meter not support");
3401         if (!action->conf)
3402                 return rte_flow_error_set(error, EINVAL,
3403                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
3404                                           NULL, "action configuration not set");
3405         target_group =
3406                 ((const struct rte_flow_action_jump *)action->conf)->group;
3407         ret = mlx5_flow_group_to_table(attributes, external, target_group,
3408                                        &table, error);
3409         if (ret)
3410                 return ret;
3411         if (attributes->group == target_group)
3412                 return rte_flow_error_set(error, EINVAL,
3413                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3414                                           "target group must be other than"
3415                                           " the current flow group");
3416         return 0;
3417 }
3418
3419 /*
3420  * Validate the port_id action.
3421  *
3422  * @param[in] dev
3423  *   Pointer to rte_eth_dev structure.
3424  * @param[in] action_flags
3425  *   Bit-fields that holds the actions detected until now.
3426  * @param[in] action
3427  *   Port_id RTE action structure.
3428  * @param[in] attr
3429  *   Attributes of flow that includes this action.
3430  * @param[out] error
3431  *   Pointer to error structure.
3432  *
3433  * @return
3434  *   0 on success, a negative errno value otherwise and rte_errno is set.
3435  */
3436 static int
3437 flow_dv_validate_action_port_id(struct rte_eth_dev *dev,
3438                                 uint64_t action_flags,
3439                                 const struct rte_flow_action *action,
3440                                 const struct rte_flow_attr *attr,
3441                                 struct rte_flow_error *error)
3442 {
3443         const struct rte_flow_action_port_id *port_id;
3444         struct mlx5_priv *act_priv;
3445         struct mlx5_priv *dev_priv;
3446         uint16_t port;
3447
3448         if (!attr->transfer)
3449                 return rte_flow_error_set(error, ENOTSUP,
3450                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3451                                           NULL,
3452                                           "port id action is valid in transfer"
3453                                           " mode only");
3454         if (!action || !action->conf)
3455                 return rte_flow_error_set(error, ENOTSUP,
3456                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
3457                                           NULL,
3458                                           "port id action parameters must be"
3459                                           " specified");
3460         if (action_flags & (MLX5_FLOW_FATE_ACTIONS |
3461                             MLX5_FLOW_FATE_ESWITCH_ACTIONS))
3462                 return rte_flow_error_set(error, EINVAL,
3463                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3464                                           "can have only one fate actions in"
3465                                           " a flow");
3466         dev_priv = mlx5_dev_to_eswitch_info(dev);
3467         if (!dev_priv)
3468                 return rte_flow_error_set(error, rte_errno,
3469                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3470                                           NULL,
3471                                           "failed to obtain E-Switch info");
3472         port_id = action->conf;
3473         port = port_id->original ? dev->data->port_id : port_id->id;
3474         act_priv = mlx5_port_to_eswitch_info(port, false);
3475         if (!act_priv)
3476                 return rte_flow_error_set
3477                                 (error, rte_errno,
3478                                  RTE_FLOW_ERROR_TYPE_ACTION_CONF, port_id,
3479                                  "failed to obtain E-Switch port id for port");
3480         if (act_priv->domain_id != dev_priv->domain_id)
3481                 return rte_flow_error_set
3482                                 (error, EINVAL,
3483                                  RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3484                                  "port does not belong to"
3485                                  " E-Switch being configured");
3486         return 0;
3487 }
3488
3489 /**
3490  * Get the maximum number of modify header actions.
3491  *
3492  * @param dev
3493  *   Pointer to rte_eth_dev structure.
3494  * @param flags
3495  *   Flags bits to check if root level.
3496  *
3497  * @return
3498  *   Max number of modify header actions device can support.
3499  */
3500 static unsigned int
3501 flow_dv_modify_hdr_action_max(struct rte_eth_dev *dev, uint64_t flags)
3502 {
3503         /*
3504          * There's no way to directly query the max cap. Although it has to be
3505          * acquried by iterative trial, it is a safe assumption that more
3506          * actions are supported by FW if extensive metadata register is
3507          * supported. (Only in the root table)
3508          */
3509         if (!(flags & MLX5DV_DR_ACTION_FLAGS_ROOT_LEVEL))
3510                 return MLX5_MAX_MODIFY_NUM;
3511         else
3512                 return mlx5_flow_ext_mreg_supported(dev) ?
3513                                         MLX5_ROOT_TBL_MODIFY_NUM :
3514                                         MLX5_ROOT_TBL_MODIFY_NUM_NO_MREG;
3515 }
3516
3517 /**
3518  * Validate the meter action.
3519  *
3520  * @param[in] dev
3521  *   Pointer to rte_eth_dev structure.
3522  * @param[in] action_flags
3523  *   Bit-fields that holds the actions detected until now.
3524  * @param[in] action
3525  *   Pointer to the meter action.
3526  * @param[in] attr
3527  *   Attributes of flow that includes this action.
3528  * @param[out] error
3529  *   Pointer to error structure.
3530  *
3531  * @return
3532  *   0 on success, a negative errno value otherwise and rte_ernno is set.
3533  */
3534 static int
3535 mlx5_flow_validate_action_meter(struct rte_eth_dev *dev,
3536                                 uint64_t action_flags,
3537                                 const struct rte_flow_action *action,
3538                                 const struct rte_flow_attr *attr,
3539                                 struct rte_flow_error *error)
3540 {
3541         struct mlx5_priv *priv = dev->data->dev_private;
3542         const struct rte_flow_action_meter *am = action->conf;
3543         struct mlx5_flow_meter *fm;
3544
3545         if (!am)
3546                 return rte_flow_error_set(error, EINVAL,
3547                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3548                                           "meter action conf is NULL");
3549
3550         if (action_flags & MLX5_FLOW_ACTION_METER)
3551                 return rte_flow_error_set(error, ENOTSUP,
3552                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3553                                           "meter chaining not support");
3554         if (action_flags & MLX5_FLOW_ACTION_JUMP)
3555                 return rte_flow_error_set(error, ENOTSUP,
3556                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3557                                           "meter with jump not support");
3558         if (!priv->mtr_en)
3559                 return rte_flow_error_set(error, ENOTSUP,
3560                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3561                                           NULL,
3562                                           "meter action not supported");
3563         fm = mlx5_flow_meter_find(priv, am->mtr_id);
3564         if (!fm)
3565                 return rte_flow_error_set(error, EINVAL,
3566                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3567                                           "Meter not found");
3568         if (fm->ref_cnt && (!(fm->attr.transfer == attr->transfer ||
3569               (!fm->attr.ingress && !attr->ingress && attr->egress) ||
3570               (!fm->attr.egress && !attr->egress && attr->ingress))))
3571                 return rte_flow_error_set(error, EINVAL,
3572                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3573                                           "Flow attributes are either invalid "
3574                                           "or have a conflict with current "
3575                                           "meter attributes");
3576         return 0;
3577 }
3578
3579 /**
3580  * Validate the modify-header IPv4 DSCP actions.
3581  *
3582  * @param[in] action_flags
3583  *   Holds the actions detected until now.
3584  * @param[in] action
3585  *   Pointer to the modify action.
3586  * @param[in] item_flags
3587  *   Holds the items detected.
3588  * @param[out] error
3589  *   Pointer to error structure.
3590  *
3591  * @return
3592  *   0 on success, a negative errno value otherwise and rte_errno is set.
3593  */
3594 static int
3595 flow_dv_validate_action_modify_ipv4_dscp(const uint64_t action_flags,
3596                                          const struct rte_flow_action *action,
3597                                          const uint64_t item_flags,
3598                                          struct rte_flow_error *error)
3599 {
3600         int ret = 0;
3601
3602         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3603         if (!ret) {
3604                 if (!(item_flags & MLX5_FLOW_LAYER_L3_IPV4))
3605                         return rte_flow_error_set(error, EINVAL,
3606                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3607                                                   NULL,
3608                                                   "no ipv4 item in pattern");
3609         }
3610         return ret;
3611 }
3612
3613 /**
3614  * Validate the modify-header IPv6 DSCP actions.
3615  *
3616  * @param[in] action_flags
3617  *   Holds the actions detected until now.
3618  * @param[in] action
3619  *   Pointer to the modify action.
3620  * @param[in] item_flags
3621  *   Holds the items detected.
3622  * @param[out] error
3623  *   Pointer to error structure.
3624  *
3625  * @return
3626  *   0 on success, a negative errno value otherwise and rte_errno is set.
3627  */
3628 static int
3629 flow_dv_validate_action_modify_ipv6_dscp(const uint64_t action_flags,
3630                                          const struct rte_flow_action *action,
3631                                          const uint64_t item_flags,
3632                                          struct rte_flow_error *error)
3633 {
3634         int ret = 0;
3635
3636         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3637         if (!ret) {
3638                 if (!(item_flags & MLX5_FLOW_LAYER_L3_IPV6))
3639                         return rte_flow_error_set(error, EINVAL,
3640                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3641                                                   NULL,
3642                                                   "no ipv6 item in pattern");
3643         }
3644         return ret;
3645 }
3646
3647 /**
3648  * Find existing modify-header resource or create and register a new one.
3649  *
3650  * @param dev[in, out]
3651  *   Pointer to rte_eth_dev structure.
3652  * @param[in, out] resource
3653  *   Pointer to modify-header resource.
3654  * @parm[in, out] dev_flow
3655  *   Pointer to the dev_flow.
3656  * @param[out] error
3657  *   pointer to error structure.
3658  *
3659  * @return
3660  *   0 on success otherwise -errno and errno is set.
3661  */
3662 static int
3663 flow_dv_modify_hdr_resource_register
3664                         (struct rte_eth_dev *dev,
3665                          struct mlx5_flow_dv_modify_hdr_resource *resource,
3666                          struct mlx5_flow *dev_flow,
3667                          struct rte_flow_error *error)
3668 {
3669         struct mlx5_priv *priv = dev->data->dev_private;
3670         struct mlx5_ibv_shared *sh = priv->sh;
3671         struct mlx5_flow_dv_modify_hdr_resource *cache_resource;
3672         struct mlx5dv_dr_domain *ns;
3673         uint32_t actions_len;
3674
3675         resource->flags =
3676                 dev_flow->group ? 0 : MLX5DV_DR_ACTION_FLAGS_ROOT_LEVEL;
3677         if (resource->actions_num > flow_dv_modify_hdr_action_max(dev,
3678                                     resource->flags))
3679                 return rte_flow_error_set(error, EOVERFLOW,
3680                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3681                                           "too many modify header items");
3682         if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB)
3683                 ns = sh->fdb_domain;
3684         else if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_NIC_TX)
3685                 ns = sh->tx_domain;
3686         else
3687                 ns = sh->rx_domain;
3688         /* Lookup a matching resource from cache. */
3689         actions_len = resource->actions_num * sizeof(resource->actions[0]);
3690         LIST_FOREACH(cache_resource, &sh->modify_cmds, next) {
3691                 if (resource->ft_type == cache_resource->ft_type &&
3692                     resource->actions_num == cache_resource->actions_num &&
3693                     resource->flags == cache_resource->flags &&
3694                     !memcmp((const void *)resource->actions,
3695                             (const void *)cache_resource->actions,
3696                             actions_len)) {
3697                         DRV_LOG(DEBUG, "modify-header resource %p: refcnt %d++",
3698                                 (void *)cache_resource,
3699                                 rte_atomic32_read(&cache_resource->refcnt));
3700                         rte_atomic32_inc(&cache_resource->refcnt);
3701                         dev_flow->dv.modify_hdr = cache_resource;
3702                         return 0;
3703                 }
3704         }
3705         /* Register new modify-header resource. */
3706         cache_resource = rte_calloc(__func__, 1,
3707                                     sizeof(*cache_resource) + actions_len, 0);
3708         if (!cache_resource)
3709                 return rte_flow_error_set(error, ENOMEM,
3710                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
3711                                           "cannot allocate resource memory");
3712         *cache_resource = *resource;
3713         rte_memcpy(cache_resource->actions, resource->actions, actions_len);
3714         cache_resource->verbs_action =
3715                 mlx5_glue->dv_create_flow_action_modify_header
3716                                         (sh->ctx, cache_resource->ft_type, ns,
3717                                          cache_resource->flags, actions_len,
3718                                          (uint64_t *)cache_resource->actions);
3719         if (!cache_resource->verbs_action) {
3720                 rte_free(cache_resource);
3721                 return rte_flow_error_set(error, ENOMEM,
3722                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3723                                           NULL, "cannot create action");
3724         }
3725         rte_atomic32_init(&cache_resource->refcnt);
3726         rte_atomic32_inc(&cache_resource->refcnt);
3727         LIST_INSERT_HEAD(&sh->modify_cmds, cache_resource, next);
3728         dev_flow->dv.modify_hdr = cache_resource;
3729         DRV_LOG(DEBUG, "new modify-header resource %p: refcnt %d++",
3730                 (void *)cache_resource,
3731                 rte_atomic32_read(&cache_resource->refcnt));
3732         return 0;
3733 }
3734
3735 #define MLX5_CNT_CONTAINER_RESIZE 64
3736
3737 /**
3738  * Get or create a flow counter.
3739  *
3740  * @param[in] dev
3741  *   Pointer to the Ethernet device structure.
3742  * @param[in] shared
3743  *   Indicate if this counter is shared with other flows.
3744  * @param[in] id
3745  *   Counter identifier.
3746  *
3747  * @return
3748  *   pointer to flow counter on success, NULL otherwise and rte_errno is set.
3749  */
3750 static struct mlx5_flow_counter *
3751 flow_dv_counter_alloc_fallback(struct rte_eth_dev *dev, uint32_t shared,
3752                                uint32_t id)
3753 {
3754         struct mlx5_priv *priv = dev->data->dev_private;
3755         struct mlx5_flow_counter *cnt = NULL;
3756         struct mlx5_devx_obj *dcs = NULL;
3757
3758         if (!priv->config.devx) {
3759                 rte_errno = ENOTSUP;
3760                 return NULL;
3761         }
3762         if (shared) {
3763                 TAILQ_FOREACH(cnt, &priv->sh->cmng.flow_counters, next) {
3764                         if (cnt->shared && cnt->id == id) {
3765                                 cnt->ref_cnt++;
3766                                 return cnt;
3767                         }
3768                 }
3769         }
3770         dcs = mlx5_devx_cmd_flow_counter_alloc(priv->sh->ctx, 0);
3771         if (!dcs)
3772                 return NULL;
3773         cnt = rte_calloc(__func__, 1, sizeof(*cnt), 0);
3774         if (!cnt) {
3775                 claim_zero(mlx5_devx_cmd_destroy(cnt->dcs));
3776                 rte_errno = ENOMEM;
3777                 return NULL;
3778         }
3779         struct mlx5_flow_counter tmpl = {
3780                 .shared = shared,
3781                 .ref_cnt = 1,
3782                 .id = id,
3783                 .dcs = dcs,
3784         };
3785         tmpl.action = mlx5_glue->dv_create_flow_action_counter(dcs->obj, 0);
3786         if (!tmpl.action) {
3787                 claim_zero(mlx5_devx_cmd_destroy(cnt->dcs));
3788                 rte_errno = errno;
3789                 rte_free(cnt);
3790                 return NULL;
3791         }
3792         *cnt = tmpl;
3793         TAILQ_INSERT_HEAD(&priv->sh->cmng.flow_counters, cnt, next);
3794         return cnt;
3795 }
3796
3797 /**
3798  * Release a flow counter.
3799  *
3800  * @param[in] dev
3801  *   Pointer to the Ethernet device structure.
3802  * @param[in] counter
3803  *   Pointer to the counter handler.
3804  */
3805 static void
3806 flow_dv_counter_release_fallback(struct rte_eth_dev *dev,
3807                                  struct mlx5_flow_counter *counter)
3808 {
3809         struct mlx5_priv *priv = dev->data->dev_private;
3810
3811         if (!counter)
3812                 return;
3813         if (--counter->ref_cnt == 0) {
3814                 TAILQ_REMOVE(&priv->sh->cmng.flow_counters, counter, next);
3815                 claim_zero(mlx5_devx_cmd_destroy(counter->dcs));
3816                 rte_free(counter);
3817         }
3818 }
3819
3820 /**
3821  * Query a devx flow counter.
3822  *
3823  * @param[in] dev
3824  *   Pointer to the Ethernet device structure.
3825  * @param[in] cnt
3826  *   Pointer to the flow counter.
3827  * @param[out] pkts
3828  *   The statistics value of packets.
3829  * @param[out] bytes
3830  *   The statistics value of bytes.
3831  *
3832  * @return
3833  *   0 on success, otherwise a negative errno value and rte_errno is set.
3834  */
3835 static inline int
3836 _flow_dv_query_count_fallback(struct rte_eth_dev *dev __rte_unused,
3837                      struct mlx5_flow_counter *cnt, uint64_t *pkts,
3838                      uint64_t *bytes)
3839 {
3840         return mlx5_devx_cmd_flow_counter_query(cnt->dcs, 0, 0, pkts, bytes,
3841                                                 0, NULL, NULL, 0);
3842 }
3843
3844 /**
3845  * Get a pool by a counter.
3846  *
3847  * @param[in] cnt
3848  *   Pointer to the counter.
3849  *
3850  * @return
3851  *   The counter pool.
3852  */
3853 static struct mlx5_flow_counter_pool *
3854 flow_dv_counter_pool_get(struct mlx5_flow_counter *cnt)
3855 {
3856         if (!cnt->batch) {
3857                 cnt -= cnt->dcs->id % MLX5_COUNTERS_PER_POOL;
3858                 return (struct mlx5_flow_counter_pool *)cnt - 1;
3859         }
3860         return cnt->pool;
3861 }
3862
3863 /**
3864  * Get a pool by devx counter ID.
3865  *
3866  * @param[in] cont
3867  *   Pointer to the counter container.
3868  * @param[in] id
3869  *   The counter devx ID.
3870  *
3871  * @return
3872  *   The counter pool pointer if exists, NULL otherwise,
3873  */
3874 static struct mlx5_flow_counter_pool *
3875 flow_dv_find_pool_by_id(struct mlx5_pools_container *cont, int id)
3876 {
3877         struct mlx5_flow_counter_pool *pool;
3878
3879         TAILQ_FOREACH(pool, &cont->pool_list, next) {
3880                 int base = (pool->min_dcs->id / MLX5_COUNTERS_PER_POOL) *
3881                                 MLX5_COUNTERS_PER_POOL;
3882
3883                 if (id >= base && id < base + MLX5_COUNTERS_PER_POOL)
3884                         return pool;
3885         };
3886         return NULL;
3887 }
3888
3889 /**
3890  * Allocate a new memory for the counter values wrapped by all the needed
3891  * management.
3892  *
3893  * @param[in] dev
3894  *   Pointer to the Ethernet device structure.
3895  * @param[in] raws_n
3896  *   The raw memory areas - each one for MLX5_COUNTERS_PER_POOL counters.
3897  *
3898  * @return
3899  *   The new memory management pointer on success, otherwise NULL and rte_errno
3900  *   is set.
3901  */
3902 static struct mlx5_counter_stats_mem_mng *
3903 flow_dv_create_counter_stat_mem_mng(struct rte_eth_dev *dev, int raws_n)
3904 {
3905         struct mlx5_ibv_shared *sh = ((struct mlx5_priv *)
3906                                         (dev->data->dev_private))->sh;
3907         struct mlx5_devx_mkey_attr mkey_attr;
3908         struct mlx5_counter_stats_mem_mng *mem_mng;
3909         volatile struct flow_counter_stats *raw_data;
3910         int size = (sizeof(struct flow_counter_stats) *
3911                         MLX5_COUNTERS_PER_POOL +
3912                         sizeof(struct mlx5_counter_stats_raw)) * raws_n +
3913                         sizeof(struct mlx5_counter_stats_mem_mng);
3914         uint8_t *mem = rte_calloc(__func__, 1, size, sysconf(_SC_PAGESIZE));
3915         int i;
3916
3917         if (!mem) {
3918                 rte_errno = ENOMEM;
3919                 return NULL;
3920         }
3921         mem_mng = (struct mlx5_counter_stats_mem_mng *)(mem + size) - 1;
3922         size = sizeof(*raw_data) * MLX5_COUNTERS_PER_POOL * raws_n;
3923         mem_mng->umem = mlx5_glue->devx_umem_reg(sh->ctx, mem, size,
3924                                                  IBV_ACCESS_LOCAL_WRITE);
3925         if (!mem_mng->umem) {
3926                 rte_errno = errno;
3927                 rte_free(mem);
3928                 return NULL;
3929         }
3930         mkey_attr.addr = (uintptr_t)mem;
3931         mkey_attr.size = size;
3932         mkey_attr.umem_id = mem_mng->umem->umem_id;
3933         mkey_attr.pd = sh->pdn;
3934         mem_mng->dm = mlx5_devx_cmd_mkey_create(sh->ctx, &mkey_attr);
3935         if (!mem_mng->dm) {
3936                 mlx5_glue->devx_umem_dereg(mem_mng->umem);
3937                 rte_errno = errno;
3938                 rte_free(mem);
3939                 return NULL;
3940         }
3941         mem_mng->raws = (struct mlx5_counter_stats_raw *)(mem + size);
3942         raw_data = (volatile struct flow_counter_stats *)mem;
3943         for (i = 0; i < raws_n; ++i) {
3944                 mem_mng->raws[i].mem_mng = mem_mng;
3945                 mem_mng->raws[i].data = raw_data + i * MLX5_COUNTERS_PER_POOL;
3946         }
3947         LIST_INSERT_HEAD(&sh->cmng.mem_mngs, mem_mng, next);
3948         return mem_mng;
3949 }
3950
3951 /**
3952  * Resize a counter container.
3953  *
3954  * @param[in] dev
3955  *   Pointer to the Ethernet device structure.
3956  * @param[in] batch
3957  *   Whether the pool is for counter that was allocated by batch command.
3958  *
3959  * @return
3960  *   The new container pointer on success, otherwise NULL and rte_errno is set.
3961  */
3962 static struct mlx5_pools_container *
3963 flow_dv_container_resize(struct rte_eth_dev *dev, uint32_t batch)
3964 {
3965         struct mlx5_priv *priv = dev->data->dev_private;
3966         struct mlx5_pools_container *cont =
3967                         MLX5_CNT_CONTAINER(priv->sh, batch, 0);
3968         struct mlx5_pools_container *new_cont =
3969                         MLX5_CNT_CONTAINER_UNUSED(priv->sh, batch, 0);
3970         struct mlx5_counter_stats_mem_mng *mem_mng;
3971         uint32_t resize = cont->n + MLX5_CNT_CONTAINER_RESIZE;
3972         uint32_t mem_size = sizeof(struct mlx5_flow_counter_pool *) * resize;
3973         int i;
3974
3975         if (cont != MLX5_CNT_CONTAINER(priv->sh, batch, 1)) {
3976                 /* The last resize still hasn't detected by the host thread. */
3977                 rte_errno = EAGAIN;
3978                 return NULL;
3979         }
3980         new_cont->pools = rte_calloc(__func__, 1, mem_size, 0);
3981         if (!new_cont->pools) {
3982                 rte_errno = ENOMEM;
3983                 return NULL;
3984         }
3985         if (cont->n)
3986                 memcpy(new_cont->pools, cont->pools, cont->n *
3987                        sizeof(struct mlx5_flow_counter_pool *));
3988         mem_mng = flow_dv_create_counter_stat_mem_mng(dev,
3989                 MLX5_CNT_CONTAINER_RESIZE + MLX5_MAX_PENDING_QUERIES);
3990         if (!mem_mng) {
3991                 rte_free(new_cont->pools);
3992                 return NULL;
3993         }
3994         for (i = 0; i < MLX5_MAX_PENDING_QUERIES; ++i)
3995                 LIST_INSERT_HEAD(&priv->sh->cmng.free_stat_raws,
3996                                  mem_mng->raws + MLX5_CNT_CONTAINER_RESIZE +
3997                                  i, next);
3998         new_cont->n = resize;
3999         rte_atomic16_set(&new_cont->n_valid, rte_atomic16_read(&cont->n_valid));
4000         TAILQ_INIT(&new_cont->pool_list);
4001         TAILQ_CONCAT(&new_cont->pool_list, &cont->pool_list, next);
4002         new_cont->init_mem_mng = mem_mng;
4003         rte_cio_wmb();
4004          /* Flip the master container. */
4005         priv->sh->cmng.mhi[batch] ^= (uint8_t)1;
4006         return new_cont;
4007 }
4008
4009 /**
4010  * Query a devx flow counter.
4011  *
4012  * @param[in] dev
4013  *   Pointer to the Ethernet device structure.
4014  * @param[in] cnt
4015  *   Pointer to the flow counter.
4016  * @param[out] pkts
4017  *   The statistics value of packets.
4018  * @param[out] bytes
4019  *   The statistics value of bytes.
4020  *
4021  * @return
4022  *   0 on success, otherwise a negative errno value and rte_errno is set.
4023  */
4024 static inline int
4025 _flow_dv_query_count(struct rte_eth_dev *dev,
4026                      struct mlx5_flow_counter *cnt, uint64_t *pkts,
4027                      uint64_t *bytes)
4028 {
4029         struct mlx5_priv *priv = dev->data->dev_private;
4030         struct mlx5_flow_counter_pool *pool =
4031                         flow_dv_counter_pool_get(cnt);
4032         int offset = cnt - &pool->counters_raw[0];
4033
4034         if (priv->counter_fallback)
4035                 return _flow_dv_query_count_fallback(dev, cnt, pkts, bytes);
4036
4037         rte_spinlock_lock(&pool->sl);
4038         /*
4039          * The single counters allocation may allocate smaller ID than the
4040          * current allocated in parallel to the host reading.
4041          * In this case the new counter values must be reported as 0.
4042          */
4043         if (unlikely(!cnt->batch && cnt->dcs->id < pool->raw->min_dcs_id)) {
4044                 *pkts = 0;
4045                 *bytes = 0;
4046         } else {
4047                 *pkts = rte_be_to_cpu_64(pool->raw->data[offset].hits);
4048                 *bytes = rte_be_to_cpu_64(pool->raw->data[offset].bytes);
4049         }
4050         rte_spinlock_unlock(&pool->sl);
4051         return 0;
4052 }
4053
4054 /**
4055  * Create and initialize a new counter pool.
4056  *
4057  * @param[in] dev
4058  *   Pointer to the Ethernet device structure.
4059  * @param[out] dcs
4060  *   The devX counter handle.
4061  * @param[in] batch
4062  *   Whether the pool is for counter that was allocated by batch command.
4063  *
4064  * @return
4065  *   A new pool pointer on success, NULL otherwise and rte_errno is set.
4066  */
4067 static struct mlx5_flow_counter_pool *
4068 flow_dv_pool_create(struct rte_eth_dev *dev, struct mlx5_devx_obj *dcs,
4069                     uint32_t batch)
4070 {
4071         struct mlx5_priv *priv = dev->data->dev_private;
4072         struct mlx5_flow_counter_pool *pool;
4073         struct mlx5_pools_container *cont = MLX5_CNT_CONTAINER(priv->sh, batch,
4074                                                                0);
4075         int16_t n_valid = rte_atomic16_read(&cont->n_valid);
4076         uint32_t size;
4077
4078         if (cont->n == n_valid) {
4079                 cont = flow_dv_container_resize(dev, batch);
4080                 if (!cont)
4081                         return NULL;
4082         }
4083         size = sizeof(*pool) + MLX5_COUNTERS_PER_POOL *
4084                         sizeof(struct mlx5_flow_counter);
4085         pool = rte_calloc(__func__, 1, size, 0);
4086         if (!pool) {
4087                 rte_errno = ENOMEM;
4088                 return NULL;
4089         }
4090         pool->min_dcs = dcs;
4091         pool->raw = cont->init_mem_mng->raws + n_valid %
4092                                                      MLX5_CNT_CONTAINER_RESIZE;
4093         pool->raw_hw = NULL;
4094         rte_spinlock_init(&pool->sl);
4095         /*
4096          * The generation of the new allocated counters in this pool is 0, 2 in
4097          * the pool generation makes all the counters valid for allocation.
4098          */
4099         rte_atomic64_set(&pool->query_gen, 0x2);
4100         TAILQ_INIT(&pool->counters);
4101         TAILQ_INSERT_TAIL(&cont->pool_list, pool, next);
4102         cont->pools[n_valid] = pool;
4103         /* Pool initialization must be updated before host thread access. */
4104         rte_cio_wmb();
4105         rte_atomic16_add(&cont->n_valid, 1);
4106         return pool;
4107 }
4108
4109 /**
4110  * Prepare a new counter and/or a new counter pool.
4111  *
4112  * @param[in] dev
4113  *   Pointer to the Ethernet device structure.
4114  * @param[out] cnt_free
4115  *   Where to put the pointer of a new counter.
4116  * @param[in] batch
4117  *   Whether the pool is for counter that was allocated by batch command.
4118  *
4119  * @return
4120  *   The free counter pool pointer and @p cnt_free is set on success,
4121  *   NULL otherwise and rte_errno is set.
4122  */
4123 static struct mlx5_flow_counter_pool *
4124 flow_dv_counter_pool_prepare(struct rte_eth_dev *dev,
4125                              struct mlx5_flow_counter **cnt_free,
4126                              uint32_t batch)
4127 {
4128         struct mlx5_priv *priv = dev->data->dev_private;
4129         struct mlx5_flow_counter_pool *pool;
4130         struct mlx5_devx_obj *dcs = NULL;
4131         struct mlx5_flow_counter *cnt;
4132         uint32_t i;
4133
4134         if (!batch) {
4135                 /* bulk_bitmap must be 0 for single counter allocation. */
4136                 dcs = mlx5_devx_cmd_flow_counter_alloc(priv->sh->ctx, 0);
4137                 if (!dcs)
4138                         return NULL;
4139                 pool = flow_dv_find_pool_by_id
4140                         (MLX5_CNT_CONTAINER(priv->sh, batch, 0), dcs->id);
4141                 if (!pool) {
4142                         pool = flow_dv_pool_create(dev, dcs, batch);
4143                         if (!pool) {
4144                                 mlx5_devx_cmd_destroy(dcs);
4145                                 return NULL;
4146                         }
4147                 } else if (dcs->id < pool->min_dcs->id) {
4148                         rte_atomic64_set(&pool->a64_dcs,
4149                                          (int64_t)(uintptr_t)dcs);
4150                 }
4151                 cnt = &pool->counters_raw[dcs->id % MLX5_COUNTERS_PER_POOL];
4152                 TAILQ_INSERT_HEAD(&pool->counters, cnt, next);
4153                 cnt->dcs = dcs;
4154                 *cnt_free = cnt;
4155                 return pool;
4156         }
4157         /* bulk_bitmap is in 128 counters units. */
4158         if (priv->config.hca_attr.flow_counter_bulk_alloc_bitmap & 0x4)
4159                 dcs = mlx5_devx_cmd_flow_counter_alloc(priv->sh->ctx, 0x4);
4160         if (!dcs) {
4161                 rte_errno = ENODATA;
4162                 return NULL;
4163         }
4164         pool = flow_dv_pool_create(dev, dcs, batch);
4165         if (!pool) {
4166                 mlx5_devx_cmd_destroy(dcs);
4167                 return NULL;
4168         }
4169         for (i = 0; i < MLX5_COUNTERS_PER_POOL; ++i) {
4170                 cnt = &pool->counters_raw[i];
4171                 cnt->pool = pool;
4172                 TAILQ_INSERT_HEAD(&pool->counters, cnt, next);
4173         }
4174         *cnt_free = &pool->counters_raw[0];
4175         return pool;
4176 }
4177
4178 /**
4179  * Search for existed shared counter.
4180  *
4181  * @param[in] cont
4182  *   Pointer to the relevant counter pool container.
4183  * @param[in] id
4184  *   The shared counter ID to search.
4185  *
4186  * @return
4187  *   NULL if not existed, otherwise pointer to the shared counter.
4188  */
4189 static struct mlx5_flow_counter *
4190 flow_dv_counter_shared_search(struct mlx5_pools_container *cont,
4191                               uint32_t id)
4192 {
4193         static struct mlx5_flow_counter *cnt;
4194         struct mlx5_flow_counter_pool *pool;
4195         int i;
4196
4197         TAILQ_FOREACH(pool, &cont->pool_list, next) {
4198                 for (i = 0; i < MLX5_COUNTERS_PER_POOL; ++i) {
4199                         cnt = &pool->counters_raw[i];
4200                         if (cnt->ref_cnt && cnt->shared && cnt->id == id)
4201                                 return cnt;
4202                 }
4203         }
4204         return NULL;
4205 }
4206
4207 /**
4208  * Allocate a flow counter.
4209  *
4210  * @param[in] dev
4211  *   Pointer to the Ethernet device structure.
4212  * @param[in] shared
4213  *   Indicate if this counter is shared with other flows.
4214  * @param[in] id
4215  *   Counter identifier.
4216  * @param[in] group
4217  *   Counter flow group.
4218  *
4219  * @return
4220  *   pointer to flow counter on success, NULL otherwise and rte_errno is set.
4221  */
4222 static struct mlx5_flow_counter *
4223 flow_dv_counter_alloc(struct rte_eth_dev *dev, uint32_t shared, uint32_t id,
4224                       uint16_t group)
4225 {
4226         struct mlx5_priv *priv = dev->data->dev_private;
4227         struct mlx5_flow_counter_pool *pool = NULL;
4228         struct mlx5_flow_counter *cnt_free = NULL;
4229         /*
4230          * Currently group 0 flow counter cannot be assigned to a flow if it is
4231          * not the first one in the batch counter allocation, so it is better
4232          * to allocate counters one by one for these flows in a separate
4233          * container.
4234          * A counter can be shared between different groups so need to take
4235          * shared counters from the single container.
4236          */
4237         uint32_t batch = (group && !shared) ? 1 : 0;
4238         struct mlx5_pools_container *cont = MLX5_CNT_CONTAINER(priv->sh, batch,
4239                                                                0);
4240
4241         if (priv->counter_fallback)
4242                 return flow_dv_counter_alloc_fallback(dev, shared, id);
4243         if (!priv->config.devx) {
4244                 rte_errno = ENOTSUP;
4245                 return NULL;
4246         }
4247         if (shared) {
4248                 cnt_free = flow_dv_counter_shared_search(cont, id);
4249                 if (cnt_free) {
4250                         if (cnt_free->ref_cnt + 1 == 0) {
4251                                 rte_errno = E2BIG;
4252                                 return NULL;
4253                         }
4254                         cnt_free->ref_cnt++;
4255                         return cnt_free;
4256                 }
4257         }
4258         /* Pools which has a free counters are in the start. */
4259         TAILQ_FOREACH(pool, &cont->pool_list, next) {
4260                 /*
4261                  * The free counter reset values must be updated between the
4262                  * counter release to the counter allocation, so, at least one
4263                  * query must be done in this time. ensure it by saving the
4264                  * query generation in the release time.
4265                  * The free list is sorted according to the generation - so if
4266                  * the first one is not updated, all the others are not
4267                  * updated too.
4268                  */
4269                 cnt_free = TAILQ_FIRST(&pool->counters);
4270                 if (cnt_free && cnt_free->query_gen + 1 <
4271                     rte_atomic64_read(&pool->query_gen))
4272                         break;
4273                 cnt_free = NULL;
4274         }
4275         if (!cnt_free) {
4276                 pool = flow_dv_counter_pool_prepare(dev, &cnt_free, batch);
4277                 if (!pool)
4278                         return NULL;
4279         }
4280         cnt_free->batch = batch;
4281         /* Create a DV counter action only in the first time usage. */
4282         if (!cnt_free->action) {
4283                 uint16_t offset;
4284                 struct mlx5_devx_obj *dcs;
4285
4286                 if (batch) {
4287                         offset = cnt_free - &pool->counters_raw[0];
4288                         dcs = pool->min_dcs;
4289                 } else {
4290                         offset = 0;
4291                         dcs = cnt_free->dcs;
4292                 }
4293                 cnt_free->action = mlx5_glue->dv_create_flow_action_counter
4294                                         (dcs->obj, offset);
4295                 if (!cnt_free->action) {
4296                         rte_errno = errno;
4297                         return NULL;
4298                 }
4299         }
4300         /* Update the counter reset values. */
4301         if (_flow_dv_query_count(dev, cnt_free, &cnt_free->hits,
4302                                  &cnt_free->bytes))
4303                 return NULL;
4304         cnt_free->shared = shared;
4305         cnt_free->ref_cnt = 1;
4306         cnt_free->id = id;
4307         if (!priv->sh->cmng.query_thread_on)
4308                 /* Start the asynchronous batch query by the host thread. */
4309                 mlx5_set_query_alarm(priv->sh);
4310         TAILQ_REMOVE(&pool->counters, cnt_free, next);
4311         if (TAILQ_EMPTY(&pool->counters)) {
4312                 /* Move the pool to the end of the container pool list. */
4313                 TAILQ_REMOVE(&cont->pool_list, pool, next);
4314                 TAILQ_INSERT_TAIL(&cont->pool_list, pool, next);
4315         }
4316         return cnt_free;
4317 }
4318
4319 /**
4320  * Release a flow counter.
4321  *
4322  * @param[in] dev
4323  *   Pointer to the Ethernet device structure.
4324  * @param[in] counter
4325  *   Pointer to the counter handler.
4326  */
4327 static void
4328 flow_dv_counter_release(struct rte_eth_dev *dev,
4329                         struct mlx5_flow_counter *counter)
4330 {
4331         struct mlx5_priv *priv = dev->data->dev_private;
4332
4333         if (!counter)
4334                 return;
4335         if (priv->counter_fallback) {
4336                 flow_dv_counter_release_fallback(dev, counter);
4337                 return;
4338         }
4339         if (--counter->ref_cnt == 0) {
4340                 struct mlx5_flow_counter_pool *pool =
4341                                 flow_dv_counter_pool_get(counter);
4342
4343                 /* Put the counter in the end - the last updated one. */
4344                 TAILQ_INSERT_TAIL(&pool->counters, counter, next);
4345                 counter->query_gen = rte_atomic64_read(&pool->query_gen);
4346         }
4347 }
4348
4349 /**
4350  * Verify the @p attributes will be correctly understood by the NIC and store
4351  * them in the @p flow if everything is correct.
4352  *
4353  * @param[in] dev
4354  *   Pointer to dev struct.
4355  * @param[in] attributes
4356  *   Pointer to flow attributes
4357  * @param[in] external
4358  *   This flow rule is created by request external to PMD.
4359  * @param[out] error
4360  *   Pointer to error structure.
4361  *
4362  * @return
4363  *   0 on success, a negative errno value otherwise and rte_errno is set.
4364  */
4365 static int
4366 flow_dv_validate_attributes(struct rte_eth_dev *dev,
4367                             const struct rte_flow_attr *attributes,
4368                             bool external __rte_unused,
4369                             struct rte_flow_error *error)
4370 {
4371         struct mlx5_priv *priv = dev->data->dev_private;
4372         uint32_t priority_max = priv->config.flow_prio - 1;
4373
4374 #ifndef HAVE_MLX5DV_DR
4375         if (attributes->group)
4376                 return rte_flow_error_set(error, ENOTSUP,
4377                                           RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
4378                                           NULL,
4379                                           "groups are not supported");
4380 #else
4381         uint32_t table;
4382         int ret;
4383
4384         ret = mlx5_flow_group_to_table(attributes, external,
4385                                        attributes->group,
4386                                        &table, error);
4387         if (ret)
4388                 return ret;
4389 #endif
4390         if (attributes->priority != MLX5_FLOW_PRIO_RSVD &&
4391             attributes->priority >= priority_max)
4392                 return rte_flow_error_set(error, ENOTSUP,
4393                                           RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
4394                                           NULL,
4395                                           "priority out of range");
4396         if (attributes->transfer) {
4397                 if (!priv->config.dv_esw_en)
4398                         return rte_flow_error_set
4399                                 (error, ENOTSUP,
4400                                  RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
4401                                  "E-Switch dr is not supported");
4402                 if (!(priv->representor || priv->master))
4403                         return rte_flow_error_set
4404                                 (error, EINVAL, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
4405                                  NULL, "E-Switch configuration can only be"
4406                                  " done by a master or a representor device");
4407                 if (attributes->egress)
4408                         return rte_flow_error_set
4409                                 (error, ENOTSUP,
4410                                  RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, attributes,
4411                                  "egress is not supported");
4412         }
4413         if (!(attributes->egress ^ attributes->ingress))
4414                 return rte_flow_error_set(error, ENOTSUP,
4415                                           RTE_FLOW_ERROR_TYPE_ATTR, NULL,
4416                                           "must specify exactly one of "
4417                                           "ingress or egress");
4418         return 0;
4419 }
4420
4421 /**
4422  * Internal validation function. For validating both actions and items.
4423  *
4424  * @param[in] dev
4425  *   Pointer to the rte_eth_dev structure.
4426  * @param[in] attr
4427  *   Pointer to the flow attributes.
4428  * @param[in] items
4429  *   Pointer to the list of items.
4430  * @param[in] actions
4431  *   Pointer to the list of actions.
4432  * @param[in] external
4433  *   This flow rule is created by request external to PMD.
4434  * @param[out] error
4435  *   Pointer to the error structure.
4436  *
4437  * @return
4438  *   0 on success, a negative errno value otherwise and rte_errno is set.
4439  */
4440 static int
4441 flow_dv_validate(struct rte_eth_dev *dev, const struct rte_flow_attr *attr,
4442                  const struct rte_flow_item items[],
4443                  const struct rte_flow_action actions[],
4444                  bool external, struct rte_flow_error *error)
4445 {
4446         int ret;
4447         uint64_t action_flags = 0;
4448         uint64_t item_flags = 0;
4449         uint64_t last_item = 0;
4450         uint8_t next_protocol = 0xff;
4451         uint16_t ether_type = 0;
4452         int actions_n = 0;
4453         const struct rte_flow_item *gre_item = NULL;
4454         struct rte_flow_item_tcp nic_tcp_mask = {
4455                 .hdr = {
4456                         .tcp_flags = 0xFF,
4457                         .src_port = RTE_BE16(UINT16_MAX),
4458                         .dst_port = RTE_BE16(UINT16_MAX),
4459                 }
4460         };
4461         struct mlx5_priv *priv = dev->data->dev_private;
4462         struct mlx5_dev_config *dev_conf = &priv->config;
4463
4464         if (items == NULL)
4465                 return -1;
4466         ret = flow_dv_validate_attributes(dev, attr, external, error);
4467         if (ret < 0)
4468                 return ret;
4469         for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
4470                 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
4471                 int type = items->type;
4472
4473                 switch (type) {
4474                 case RTE_FLOW_ITEM_TYPE_VOID:
4475                         break;
4476                 case RTE_FLOW_ITEM_TYPE_PORT_ID:
4477                         ret = flow_dv_validate_item_port_id
4478                                         (dev, items, attr, item_flags, error);
4479                         if (ret < 0)
4480                                 return ret;
4481                         last_item = MLX5_FLOW_ITEM_PORT_ID;
4482                         break;
4483                 case RTE_FLOW_ITEM_TYPE_ETH:
4484                         ret = mlx5_flow_validate_item_eth(items, item_flags,
4485                                                           error);
4486                         if (ret < 0)
4487                                 return ret;
4488                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
4489                                              MLX5_FLOW_LAYER_OUTER_L2;
4490                         if (items->mask != NULL && items->spec != NULL) {
4491                                 ether_type =
4492                                         ((const struct rte_flow_item_eth *)
4493                                          items->spec)->type;
4494                                 ether_type &=
4495                                         ((const struct rte_flow_item_eth *)
4496                                          items->mask)->type;
4497                                 ether_type = rte_be_to_cpu_16(ether_type);
4498                         } else {
4499                                 ether_type = 0;
4500                         }
4501                         break;
4502                 case RTE_FLOW_ITEM_TYPE_VLAN:
4503                         ret = mlx5_flow_validate_item_vlan(items, item_flags,
4504                                                            dev, error);
4505                         if (ret < 0)
4506                                 return ret;
4507                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_VLAN :
4508                                              MLX5_FLOW_LAYER_OUTER_VLAN;
4509                         if (items->mask != NULL && items->spec != NULL) {
4510                                 ether_type =
4511                                         ((const struct rte_flow_item_vlan *)
4512                                          items->spec)->inner_type;
4513                                 ether_type &=
4514                                         ((const struct rte_flow_item_vlan *)
4515                                          items->mask)->inner_type;
4516                                 ether_type = rte_be_to_cpu_16(ether_type);
4517                         } else {
4518                                 ether_type = 0;
4519                         }
4520                         break;
4521                 case RTE_FLOW_ITEM_TYPE_IPV4:
4522                         mlx5_flow_tunnel_ip_check(items, next_protocol,
4523                                                   &item_flags, &tunnel);
4524                         ret = mlx5_flow_validate_item_ipv4(items, item_flags,
4525                                                            last_item,
4526                                                            ether_type, NULL,
4527                                                            error);
4528                         if (ret < 0)
4529                                 return ret;
4530                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
4531                                              MLX5_FLOW_LAYER_OUTER_L3_IPV4;
4532                         if (items->mask != NULL &&
4533                             ((const struct rte_flow_item_ipv4 *)
4534                              items->mask)->hdr.next_proto_id) {
4535                                 next_protocol =
4536                                         ((const struct rte_flow_item_ipv4 *)
4537                                          (items->spec))->hdr.next_proto_id;
4538                                 next_protocol &=
4539                                         ((const struct rte_flow_item_ipv4 *)
4540                                          (items->mask))->hdr.next_proto_id;
4541                         } else {
4542                                 /* Reset for inner layer. */
4543                                 next_protocol = 0xff;
4544                         }
4545                         break;
4546                 case RTE_FLOW_ITEM_TYPE_IPV6:
4547                         mlx5_flow_tunnel_ip_check(items, next_protocol,
4548                                                   &item_flags, &tunnel);
4549                         ret = mlx5_flow_validate_item_ipv6(items, item_flags,
4550                                                            last_item,
4551                                                            ether_type, NULL,
4552                                                            error);
4553                         if (ret < 0)
4554                                 return ret;
4555                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
4556                                              MLX5_FLOW_LAYER_OUTER_L3_IPV6;
4557                         if (items->mask != NULL &&
4558                             ((const struct rte_flow_item_ipv6 *)
4559                              items->mask)->hdr.proto) {
4560                                 next_protocol =
4561                                         ((const struct rte_flow_item_ipv6 *)
4562                                          items->spec)->hdr.proto;
4563                                 next_protocol &=
4564                                         ((const struct rte_flow_item_ipv6 *)
4565                                          items->mask)->hdr.proto;
4566                         } else {
4567                                 /* Reset for inner layer. */
4568                                 next_protocol = 0xff;
4569                         }
4570                         break;
4571                 case RTE_FLOW_ITEM_TYPE_TCP:
4572                         ret = mlx5_flow_validate_item_tcp
4573                                                 (items, item_flags,
4574                                                  next_protocol,
4575                                                  &nic_tcp_mask,
4576                                                  error);
4577                         if (ret < 0)
4578                                 return ret;
4579                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
4580                                              MLX5_FLOW_LAYER_OUTER_L4_TCP;
4581                         break;
4582                 case RTE_FLOW_ITEM_TYPE_UDP:
4583                         ret = mlx5_flow_validate_item_udp(items, item_flags,
4584                                                           next_protocol,
4585                                                           error);
4586                         if (ret < 0)
4587                                 return ret;
4588                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
4589                                              MLX5_FLOW_LAYER_OUTER_L4_UDP;
4590                         break;
4591                 case RTE_FLOW_ITEM_TYPE_GRE:
4592                         ret = mlx5_flow_validate_item_gre(items, item_flags,
4593                                                           next_protocol, error);
4594                         if (ret < 0)
4595                                 return ret;
4596                         gre_item = items;
4597                         last_item = MLX5_FLOW_LAYER_GRE;
4598                         break;
4599                 case RTE_FLOW_ITEM_TYPE_NVGRE:
4600                         ret = mlx5_flow_validate_item_nvgre(items, item_flags,
4601                                                             next_protocol,
4602                                                             error);
4603                         if (ret < 0)
4604                                 return ret;
4605                         last_item = MLX5_FLOW_LAYER_NVGRE;
4606                         break;
4607                 case RTE_FLOW_ITEM_TYPE_GRE_KEY:
4608                         ret = mlx5_flow_validate_item_gre_key
4609                                 (items, item_flags, gre_item, error);
4610                         if (ret < 0)
4611                                 return ret;
4612                         last_item = MLX5_FLOW_LAYER_GRE_KEY;
4613                         break;
4614                 case RTE_FLOW_ITEM_TYPE_VXLAN:
4615                         ret = mlx5_flow_validate_item_vxlan(items, item_flags,
4616                                                             error);
4617                         if (ret < 0)
4618                                 return ret;
4619                         last_item = MLX5_FLOW_LAYER_VXLAN;
4620                         break;
4621                 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
4622                         ret = mlx5_flow_validate_item_vxlan_gpe(items,
4623                                                                 item_flags, dev,
4624                                                                 error);
4625                         if (ret < 0)
4626                                 return ret;
4627                         last_item = MLX5_FLOW_LAYER_VXLAN_GPE;
4628                         break;
4629                 case RTE_FLOW_ITEM_TYPE_GENEVE:
4630                         ret = mlx5_flow_validate_item_geneve(items,
4631                                                              item_flags, dev,
4632                                                              error);
4633                         if (ret < 0)
4634                                 return ret;
4635                         last_item = MLX5_FLOW_LAYER_GENEVE;
4636                         break;
4637                 case RTE_FLOW_ITEM_TYPE_MPLS:
4638                         ret = mlx5_flow_validate_item_mpls(dev, items,
4639                                                            item_flags,
4640                                                            last_item, error);
4641                         if (ret < 0)
4642                                 return ret;
4643                         last_item = MLX5_FLOW_LAYER_MPLS;
4644                         break;
4645
4646                 case RTE_FLOW_ITEM_TYPE_MARK:
4647                         ret = flow_dv_validate_item_mark(dev, items, attr,
4648                                                          error);
4649                         if (ret < 0)
4650                                 return ret;
4651                         last_item = MLX5_FLOW_ITEM_MARK;
4652                         break;
4653                 case RTE_FLOW_ITEM_TYPE_META:
4654                         ret = flow_dv_validate_item_meta(dev, items, attr,
4655                                                          error);
4656                         if (ret < 0)
4657                                 return ret;
4658                         last_item = MLX5_FLOW_ITEM_METADATA;
4659                         break;
4660                 case RTE_FLOW_ITEM_TYPE_ICMP:
4661                         ret = mlx5_flow_validate_item_icmp(items, item_flags,
4662                                                            next_protocol,
4663                                                            error);
4664                         if (ret < 0)
4665                                 return ret;
4666                         last_item = MLX5_FLOW_LAYER_ICMP;
4667                         break;
4668                 case RTE_FLOW_ITEM_TYPE_ICMP6:
4669                         ret = mlx5_flow_validate_item_icmp6(items, item_flags,
4670                                                             next_protocol,
4671                                                             error);
4672                         if (ret < 0)
4673                                 return ret;
4674                         last_item = MLX5_FLOW_LAYER_ICMP6;
4675                         break;
4676                 case RTE_FLOW_ITEM_TYPE_TAG:
4677                         ret = flow_dv_validate_item_tag(dev, items,
4678                                                         attr, error);
4679                         if (ret < 0)
4680                                 return ret;
4681                         last_item = MLX5_FLOW_ITEM_TAG;
4682                         break;
4683                 case MLX5_RTE_FLOW_ITEM_TYPE_TAG:
4684                 case MLX5_RTE_FLOW_ITEM_TYPE_TX_QUEUE:
4685                         break;
4686                 case RTE_FLOW_ITEM_TYPE_GTP:
4687                         ret = flow_dv_validate_item_gtp(dev, items, item_flags,
4688                                                         error);
4689                         if (ret < 0)
4690                                 return ret;
4691                         last_item = MLX5_FLOW_LAYER_GTP;
4692                         break;
4693                 default:
4694                         return rte_flow_error_set(error, ENOTSUP,
4695                                                   RTE_FLOW_ERROR_TYPE_ITEM,
4696                                                   NULL, "item not supported");
4697                 }
4698                 item_flags |= last_item;
4699         }
4700         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
4701                 int type = actions->type;
4702                 if (actions_n == MLX5_DV_MAX_NUMBER_OF_ACTIONS)
4703                         return rte_flow_error_set(error, ENOTSUP,
4704                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4705                                                   actions, "too many actions");
4706                 switch (type) {
4707                 case RTE_FLOW_ACTION_TYPE_VOID:
4708                         break;
4709                 case RTE_FLOW_ACTION_TYPE_PORT_ID:
4710                         ret = flow_dv_validate_action_port_id(dev,
4711                                                               action_flags,
4712                                                               actions,
4713                                                               attr,
4714                                                               error);
4715                         if (ret)
4716                                 return ret;
4717                         action_flags |= MLX5_FLOW_ACTION_PORT_ID;
4718                         ++actions_n;
4719                         break;
4720                 case RTE_FLOW_ACTION_TYPE_FLAG:
4721                         ret = flow_dv_validate_action_flag(dev, action_flags,
4722                                                            attr, error);
4723                         if (ret < 0)
4724                                 return ret;
4725                         if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
4726                                 /* Count all modify-header actions as one. */
4727                                 if (!(action_flags &
4728                                       MLX5_FLOW_MODIFY_HDR_ACTIONS))
4729                                         ++actions_n;
4730                                 action_flags |= MLX5_FLOW_ACTION_FLAG |
4731                                                 MLX5_FLOW_ACTION_MARK_EXT;
4732                         } else {
4733                                 action_flags |= MLX5_FLOW_ACTION_FLAG;
4734                                 ++actions_n;
4735                         }
4736                         break;
4737                 case RTE_FLOW_ACTION_TYPE_MARK:
4738                         ret = flow_dv_validate_action_mark(dev, actions,
4739                                                            action_flags,
4740                                                            attr, error);
4741                         if (ret < 0)
4742                                 return ret;
4743                         if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
4744                                 /* Count all modify-header actions as one. */
4745                                 if (!(action_flags &
4746                                       MLX5_FLOW_MODIFY_HDR_ACTIONS))
4747                                         ++actions_n;
4748                                 action_flags |= MLX5_FLOW_ACTION_MARK |
4749                                                 MLX5_FLOW_ACTION_MARK_EXT;
4750                         } else {
4751                                 action_flags |= MLX5_FLOW_ACTION_MARK;
4752                                 ++actions_n;
4753                         }
4754                         break;
4755                 case RTE_FLOW_ACTION_TYPE_SET_META:
4756                         ret = flow_dv_validate_action_set_meta(dev, actions,
4757                                                                action_flags,
4758                                                                attr, error);
4759                         if (ret < 0)
4760                                 return ret;
4761                         /* Count all modify-header actions as one action. */
4762                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
4763                                 ++actions_n;
4764                         action_flags |= MLX5_FLOW_ACTION_SET_META;
4765                         break;
4766                 case RTE_FLOW_ACTION_TYPE_SET_TAG:
4767                         ret = flow_dv_validate_action_set_tag(dev, actions,
4768                                                               action_flags,
4769                                                               attr, error);
4770                         if (ret < 0)
4771                                 return ret;
4772                         /* Count all modify-header actions as one action. */
4773                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
4774                                 ++actions_n;
4775                         action_flags |= MLX5_FLOW_ACTION_SET_TAG;
4776                         break;
4777                 case RTE_FLOW_ACTION_TYPE_DROP:
4778                         ret = mlx5_flow_validate_action_drop(action_flags,
4779                                                              attr, error);
4780                         if (ret < 0)
4781                                 return ret;
4782                         action_flags |= MLX5_FLOW_ACTION_DROP;
4783                         ++actions_n;
4784                         break;
4785                 case RTE_FLOW_ACTION_TYPE_QUEUE:
4786                         ret = mlx5_flow_validate_action_queue(actions,
4787                                                               action_flags, dev,
4788                                                               attr, error);
4789                         if (ret < 0)
4790                                 return ret;
4791                         action_flags |= MLX5_FLOW_ACTION_QUEUE;
4792                         ++actions_n;
4793                         break;
4794                 case RTE_FLOW_ACTION_TYPE_RSS:
4795                         ret = mlx5_flow_validate_action_rss(actions,
4796                                                             action_flags, dev,
4797                                                             attr, item_flags,
4798                                                             error);
4799                         if (ret < 0)
4800                                 return ret;
4801                         action_flags |= MLX5_FLOW_ACTION_RSS;
4802                         ++actions_n;
4803                         break;
4804                 case RTE_FLOW_ACTION_TYPE_COUNT:
4805                         ret = flow_dv_validate_action_count(dev, error);
4806                         if (ret < 0)
4807                                 return ret;
4808                         action_flags |= MLX5_FLOW_ACTION_COUNT;
4809                         ++actions_n;
4810                         break;
4811                 case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
4812                         if (flow_dv_validate_action_pop_vlan(dev,
4813                                                              action_flags,
4814                                                              actions,
4815                                                              item_flags, attr,
4816                                                              error))
4817                                 return -rte_errno;
4818                         action_flags |= MLX5_FLOW_ACTION_OF_POP_VLAN;
4819                         ++actions_n;
4820                         break;
4821                 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
4822                         ret = flow_dv_validate_action_push_vlan(action_flags,
4823                                                                 item_flags,
4824                                                                 actions, attr,
4825                                                                 error);
4826                         if (ret < 0)
4827                                 return ret;
4828                         action_flags |= MLX5_FLOW_ACTION_OF_PUSH_VLAN;
4829                         ++actions_n;
4830                         break;
4831                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
4832                         ret = flow_dv_validate_action_set_vlan_pcp
4833                                                 (action_flags, actions, error);
4834                         if (ret < 0)
4835                                 return ret;
4836                         /* Count PCP with push_vlan command. */
4837                         action_flags |= MLX5_FLOW_ACTION_OF_SET_VLAN_PCP;
4838                         break;
4839                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
4840                         ret = flow_dv_validate_action_set_vlan_vid
4841                                                 (item_flags, action_flags,
4842                                                  actions, error);
4843                         if (ret < 0)
4844                                 return ret;
4845                         /* Count VID with push_vlan command. */
4846                         action_flags |= MLX5_FLOW_ACTION_OF_SET_VLAN_VID;
4847                         break;
4848                 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
4849                 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
4850                         ret = flow_dv_validate_action_l2_encap(action_flags,
4851                                                                actions, attr,
4852                                                                error);
4853                         if (ret < 0)
4854                                 return ret;
4855                         action_flags |= actions->type ==
4856                                         RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP ?
4857                                         MLX5_FLOW_ACTION_VXLAN_ENCAP :
4858                                         MLX5_FLOW_ACTION_NVGRE_ENCAP;
4859                         ++actions_n;
4860                         break;
4861                 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
4862                 case RTE_FLOW_ACTION_TYPE_NVGRE_DECAP:
4863                         ret = flow_dv_validate_action_l2_decap(action_flags,
4864                                                                attr, error);
4865                         if (ret < 0)
4866                                 return ret;
4867                         action_flags |= actions->type ==
4868                                         RTE_FLOW_ACTION_TYPE_VXLAN_DECAP ?
4869                                         MLX5_FLOW_ACTION_VXLAN_DECAP :
4870                                         MLX5_FLOW_ACTION_NVGRE_DECAP;
4871                         ++actions_n;
4872                         break;
4873                 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
4874                         ret = flow_dv_validate_action_raw_encap(action_flags,
4875                                                                 actions, attr,
4876                                                                 error);
4877                         if (ret < 0)
4878                                 return ret;
4879                         action_flags |= MLX5_FLOW_ACTION_RAW_ENCAP;
4880                         ++actions_n;
4881                         break;
4882                 case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
4883                         ret = flow_dv_validate_action_raw_decap(action_flags,
4884                                                                 actions, attr,
4885                                                                 error);
4886                         if (ret < 0)
4887                                 return ret;
4888                         action_flags |= MLX5_FLOW_ACTION_RAW_DECAP;
4889                         ++actions_n;
4890                         break;
4891                 case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
4892                 case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
4893                         ret = flow_dv_validate_action_modify_mac(action_flags,
4894                                                                  actions,
4895                                                                  item_flags,
4896                                                                  error);
4897                         if (ret < 0)
4898                                 return ret;
4899                         /* Count all modify-header actions as one action. */
4900                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
4901                                 ++actions_n;
4902                         action_flags |= actions->type ==
4903                                         RTE_FLOW_ACTION_TYPE_SET_MAC_SRC ?
4904                                                 MLX5_FLOW_ACTION_SET_MAC_SRC :
4905                                                 MLX5_FLOW_ACTION_SET_MAC_DST;
4906                         break;
4907
4908                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
4909                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
4910                         ret = flow_dv_validate_action_modify_ipv4(action_flags,
4911                                                                   actions,
4912                                                                   item_flags,
4913                                                                   error);
4914                         if (ret < 0)
4915                                 return ret;
4916                         /* Count all modify-header actions as one action. */
4917                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
4918                                 ++actions_n;
4919                         action_flags |= actions->type ==
4920                                         RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC ?
4921                                                 MLX5_FLOW_ACTION_SET_IPV4_SRC :
4922                                                 MLX5_FLOW_ACTION_SET_IPV4_DST;
4923                         break;
4924                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
4925                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
4926                         ret = flow_dv_validate_action_modify_ipv6(action_flags,
4927                                                                   actions,
4928                                                                   item_flags,
4929                                                                   error);
4930                         if (ret < 0)
4931                                 return ret;
4932                         /* Count all modify-header actions as one action. */
4933                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
4934                                 ++actions_n;
4935                         action_flags |= actions->type ==
4936                                         RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC ?
4937                                                 MLX5_FLOW_ACTION_SET_IPV6_SRC :
4938                                                 MLX5_FLOW_ACTION_SET_IPV6_DST;
4939                         break;
4940                 case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
4941                 case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
4942                         ret = flow_dv_validate_action_modify_tp(action_flags,
4943                                                                 actions,
4944                                                                 item_flags,
4945                                                                 error);
4946                         if (ret < 0)
4947                                 return ret;
4948                         /* Count all modify-header actions as one action. */
4949                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
4950                                 ++actions_n;
4951                         action_flags |= actions->type ==
4952                                         RTE_FLOW_ACTION_TYPE_SET_TP_SRC ?
4953                                                 MLX5_FLOW_ACTION_SET_TP_SRC :
4954                                                 MLX5_FLOW_ACTION_SET_TP_DST;
4955                         break;
4956                 case RTE_FLOW_ACTION_TYPE_DEC_TTL:
4957                 case RTE_FLOW_ACTION_TYPE_SET_TTL:
4958                         ret = flow_dv_validate_action_modify_ttl(action_flags,
4959                                                                  actions,
4960                                                                  item_flags,
4961                                                                  error);
4962                         if (ret < 0)
4963                                 return ret;
4964                         /* Count all modify-header actions as one action. */
4965                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
4966                                 ++actions_n;
4967                         action_flags |= actions->type ==
4968                                         RTE_FLOW_ACTION_TYPE_SET_TTL ?
4969                                                 MLX5_FLOW_ACTION_SET_TTL :
4970                                                 MLX5_FLOW_ACTION_DEC_TTL;
4971                         break;
4972                 case RTE_FLOW_ACTION_TYPE_JUMP:
4973                         ret = flow_dv_validate_action_jump(actions,
4974                                                            action_flags,
4975                                                            attr, external,
4976                                                            error);
4977                         if (ret)
4978                                 return ret;
4979                         ++actions_n;
4980                         action_flags |= MLX5_FLOW_ACTION_JUMP;
4981                         break;
4982                 case RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ:
4983                 case RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ:
4984                         ret = flow_dv_validate_action_modify_tcp_seq
4985                                                                 (action_flags,
4986                                                                  actions,
4987                                                                  item_flags,
4988                                                                  error);
4989                         if (ret < 0)
4990                                 return ret;
4991                         /* Count all modify-header actions as one action. */
4992                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
4993                                 ++actions_n;
4994                         action_flags |= actions->type ==
4995                                         RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ ?
4996                                                 MLX5_FLOW_ACTION_INC_TCP_SEQ :
4997                                                 MLX5_FLOW_ACTION_DEC_TCP_SEQ;
4998                         break;
4999                 case RTE_FLOW_ACTION_TYPE_INC_TCP_ACK:
5000                 case RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK:
5001                         ret = flow_dv_validate_action_modify_tcp_ack
5002                                                                 (action_flags,
5003                                                                  actions,
5004                                                                  item_flags,
5005                                                                  error);
5006                         if (ret < 0)
5007                                 return ret;
5008                         /* Count all modify-header actions as one action. */
5009                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5010                                 ++actions_n;
5011                         action_flags |= actions->type ==
5012                                         RTE_FLOW_ACTION_TYPE_INC_TCP_ACK ?
5013                                                 MLX5_FLOW_ACTION_INC_TCP_ACK :
5014                                                 MLX5_FLOW_ACTION_DEC_TCP_ACK;
5015                         break;
5016                 case MLX5_RTE_FLOW_ACTION_TYPE_TAG:
5017                 case MLX5_RTE_FLOW_ACTION_TYPE_MARK:
5018                 case MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG:
5019                         break;
5020                 case RTE_FLOW_ACTION_TYPE_METER:
5021                         ret = mlx5_flow_validate_action_meter(dev,
5022                                                               action_flags,
5023                                                               actions, attr,
5024                                                               error);
5025                         if (ret < 0)
5026                                 return ret;
5027                         action_flags |= MLX5_FLOW_ACTION_METER;
5028                         ++actions_n;
5029                         break;
5030                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DSCP:
5031                         ret = flow_dv_validate_action_modify_ipv4_dscp
5032                                                          (action_flags,
5033                                                           actions,
5034                                                           item_flags,
5035                                                           error);
5036                         if (ret < 0)
5037                                 return ret;
5038                         /* Count all modify-header actions as one action. */
5039                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5040                                 ++actions_n;
5041                         action_flags |= MLX5_FLOW_ACTION_SET_IPV4_DSCP;
5042                         break;
5043                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DSCP:
5044                         ret = flow_dv_validate_action_modify_ipv6_dscp
5045                                                                 (action_flags,
5046                                                                  actions,
5047                                                                  item_flags,
5048                                                                  error);
5049                         if (ret < 0)
5050                                 return ret;
5051                         /* Count all modify-header actions as one action. */
5052                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5053                                 ++actions_n;
5054                         action_flags |= MLX5_FLOW_ACTION_SET_IPV6_DSCP;
5055                         break;
5056                 default:
5057                         return rte_flow_error_set(error, ENOTSUP,
5058                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5059                                                   actions,
5060                                                   "action not supported");
5061                 }
5062         }
5063         /* Eswitch has few restrictions on using items and actions */
5064         if (attr->transfer) {
5065                 if (!mlx5_flow_ext_mreg_supported(dev) &&
5066                     action_flags & MLX5_FLOW_ACTION_FLAG)
5067                         return rte_flow_error_set(error, ENOTSUP,
5068                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5069                                                   NULL,
5070                                                   "unsupported action FLAG");
5071                 if (!mlx5_flow_ext_mreg_supported(dev) &&
5072                     action_flags & MLX5_FLOW_ACTION_MARK)
5073                         return rte_flow_error_set(error, ENOTSUP,
5074                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5075                                                   NULL,
5076                                                   "unsupported action MARK");
5077                 if (action_flags & MLX5_FLOW_ACTION_QUEUE)
5078                         return rte_flow_error_set(error, ENOTSUP,
5079                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5080                                                   NULL,
5081                                                   "unsupported action QUEUE");
5082                 if (action_flags & MLX5_FLOW_ACTION_RSS)
5083                         return rte_flow_error_set(error, ENOTSUP,
5084                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5085                                                   NULL,
5086                                                   "unsupported action RSS");
5087                 if (!(action_flags & MLX5_FLOW_FATE_ESWITCH_ACTIONS))
5088                         return rte_flow_error_set(error, EINVAL,
5089                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5090                                                   actions,
5091                                                   "no fate action is found");
5092         } else {
5093                 if (!(action_flags & MLX5_FLOW_FATE_ACTIONS) && attr->ingress)
5094                         return rte_flow_error_set(error, EINVAL,
5095                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5096                                                   actions,
5097                                                   "no fate action is found");
5098         }
5099         return 0;
5100 }
5101
5102 /**
5103  * Internal preparation function. Allocates the DV flow size,
5104  * this size is constant.
5105  *
5106  * @param[in] attr
5107  *   Pointer to the flow attributes.
5108  * @param[in] items
5109  *   Pointer to the list of items.
5110  * @param[in] actions
5111  *   Pointer to the list of actions.
5112  * @param[out] error
5113  *   Pointer to the error structure.
5114  *
5115  * @return
5116  *   Pointer to mlx5_flow object on success,
5117  *   otherwise NULL and rte_errno is set.
5118  */
5119 static struct mlx5_flow *
5120 flow_dv_prepare(const struct rte_flow_attr *attr __rte_unused,
5121                 const struct rte_flow_item items[] __rte_unused,
5122                 const struct rte_flow_action actions[] __rte_unused,
5123                 struct rte_flow_error *error)
5124 {
5125         size_t size = sizeof(struct mlx5_flow);
5126         struct mlx5_flow *dev_flow;
5127
5128         dev_flow = rte_calloc(__func__, 1, size, 0);
5129         if (!dev_flow) {
5130                 rte_flow_error_set(error, ENOMEM,
5131                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
5132                                    "not enough memory to create flow");
5133                 return NULL;
5134         }
5135         dev_flow->dv.value.size = MLX5_ST_SZ_BYTES(fte_match_param);
5136         dev_flow->ingress = attr->ingress;
5137         dev_flow->transfer = attr->transfer;
5138         return dev_flow;
5139 }
5140
5141 #ifndef NDEBUG
5142 /**
5143  * Sanity check for match mask and value. Similar to check_valid_spec() in
5144  * kernel driver. If unmasked bit is present in value, it returns failure.
5145  *
5146  * @param match_mask
5147  *   pointer to match mask buffer.
5148  * @param match_value
5149  *   pointer to match value buffer.
5150  *
5151  * @return
5152  *   0 if valid, -EINVAL otherwise.
5153  */
5154 static int
5155 flow_dv_check_valid_spec(void *match_mask, void *match_value)
5156 {
5157         uint8_t *m = match_mask;
5158         uint8_t *v = match_value;
5159         unsigned int i;
5160
5161         for (i = 0; i < MLX5_ST_SZ_BYTES(fte_match_param); ++i) {
5162                 if (v[i] & ~m[i]) {
5163                         DRV_LOG(ERR,
5164                                 "match_value differs from match_criteria"
5165                                 " %p[%u] != %p[%u]",
5166                                 match_value, i, match_mask, i);
5167                         return -EINVAL;
5168                 }
5169         }
5170         return 0;
5171 }
5172 #endif
5173
5174 /**
5175  * Add Ethernet item to matcher and to the value.
5176  *
5177  * @param[in, out] matcher
5178  *   Flow matcher.
5179  * @param[in, out] key
5180  *   Flow matcher value.
5181  * @param[in] item
5182  *   Flow pattern to translate.
5183  * @param[in] inner
5184  *   Item is inner pattern.
5185  */
5186 static void
5187 flow_dv_translate_item_eth(void *matcher, void *key,
5188                            const struct rte_flow_item *item, int inner)
5189 {
5190         const struct rte_flow_item_eth *eth_m = item->mask;
5191         const struct rte_flow_item_eth *eth_v = item->spec;
5192         const struct rte_flow_item_eth nic_mask = {
5193                 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
5194                 .src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
5195                 .type = RTE_BE16(0xffff),
5196         };
5197         void *headers_m;
5198         void *headers_v;
5199         char *l24_v;
5200         unsigned int i;
5201
5202         if (!eth_v)
5203                 return;
5204         if (!eth_m)
5205                 eth_m = &nic_mask;
5206         if (inner) {
5207                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5208                                          inner_headers);
5209                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
5210         } else {
5211                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5212                                          outer_headers);
5213                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
5214         }
5215         memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m, dmac_47_16),
5216                &eth_m->dst, sizeof(eth_m->dst));
5217         /* The value must be in the range of the mask. */
5218         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, dmac_47_16);
5219         for (i = 0; i < sizeof(eth_m->dst); ++i)
5220                 l24_v[i] = eth_m->dst.addr_bytes[i] & eth_v->dst.addr_bytes[i];
5221         memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m, smac_47_16),
5222                &eth_m->src, sizeof(eth_m->src));
5223         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, smac_47_16);
5224         /* The value must be in the range of the mask. */
5225         for (i = 0; i < sizeof(eth_m->dst); ++i)
5226                 l24_v[i] = eth_m->src.addr_bytes[i] & eth_v->src.addr_bytes[i];
5227         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ethertype,
5228                  rte_be_to_cpu_16(eth_m->type));
5229         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, ethertype);
5230         *(uint16_t *)(l24_v) = eth_m->type & eth_v->type;
5231 }
5232
5233 /**
5234  * Add VLAN item to matcher and to the value.
5235  *
5236  * @param[in, out] dev_flow
5237  *   Flow descriptor.
5238  * @param[in, out] matcher
5239  *   Flow matcher.
5240  * @param[in, out] key
5241  *   Flow matcher value.
5242  * @param[in] item
5243  *   Flow pattern to translate.
5244  * @param[in] inner
5245  *   Item is inner pattern.
5246  */
5247 static void
5248 flow_dv_translate_item_vlan(struct mlx5_flow *dev_flow,
5249                             void *matcher, void *key,
5250                             const struct rte_flow_item *item,
5251                             int inner)
5252 {
5253         const struct rte_flow_item_vlan *vlan_m = item->mask;
5254         const struct rte_flow_item_vlan *vlan_v = item->spec;
5255         void *headers_m;
5256         void *headers_v;
5257         uint16_t tci_m;
5258         uint16_t tci_v;
5259
5260         if (!vlan_v)
5261                 return;
5262         if (!vlan_m)
5263                 vlan_m = &rte_flow_item_vlan_mask;
5264         if (inner) {
5265                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5266                                          inner_headers);
5267                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
5268         } else {
5269                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5270                                          outer_headers);
5271                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
5272                 /*
5273                  * This is workaround, masks are not supported,
5274                  * and pre-validated.
5275                  */
5276                 dev_flow->dv.vf_vlan.tag =
5277                         rte_be_to_cpu_16(vlan_v->tci) & 0x0fff;
5278         }
5279         tci_m = rte_be_to_cpu_16(vlan_m->tci);
5280         tci_v = rte_be_to_cpu_16(vlan_m->tci & vlan_v->tci);
5281         MLX5_SET(fte_match_set_lyr_2_4, headers_m, cvlan_tag, 1);
5282         MLX5_SET(fte_match_set_lyr_2_4, headers_v, cvlan_tag, 1);
5283         MLX5_SET(fte_match_set_lyr_2_4, headers_m, first_vid, tci_m);
5284         MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_vid, tci_v);
5285         MLX5_SET(fte_match_set_lyr_2_4, headers_m, first_cfi, tci_m >> 12);
5286         MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_cfi, tci_v >> 12);
5287         MLX5_SET(fte_match_set_lyr_2_4, headers_m, first_prio, tci_m >> 13);
5288         MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_prio, tci_v >> 13);
5289         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ethertype,
5290                  rte_be_to_cpu_16(vlan_m->inner_type));
5291         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype,
5292                  rte_be_to_cpu_16(vlan_m->inner_type & vlan_v->inner_type));
5293 }
5294
5295 /**
5296  * Add IPV4 item to matcher and to the value.
5297  *
5298  * @param[in, out] matcher
5299  *   Flow matcher.
5300  * @param[in, out] key
5301  *   Flow matcher value.
5302  * @param[in] item
5303  *   Flow pattern to translate.
5304  * @param[in] inner
5305  *   Item is inner pattern.
5306  * @param[in] group
5307  *   The group to insert the rule.
5308  */
5309 static void
5310 flow_dv_translate_item_ipv4(void *matcher, void *key,
5311                             const struct rte_flow_item *item,
5312                             int inner, uint32_t group)
5313 {
5314         const struct rte_flow_item_ipv4 *ipv4_m = item->mask;
5315         const struct rte_flow_item_ipv4 *ipv4_v = item->spec;
5316         const struct rte_flow_item_ipv4 nic_mask = {
5317                 .hdr = {
5318                         .src_addr = RTE_BE32(0xffffffff),
5319                         .dst_addr = RTE_BE32(0xffffffff),
5320                         .type_of_service = 0xff,
5321                         .next_proto_id = 0xff,
5322                 },
5323         };
5324         void *headers_m;
5325         void *headers_v;
5326         char *l24_m;
5327         char *l24_v;
5328         uint8_t tos;
5329
5330         if (inner) {
5331                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5332                                          inner_headers);
5333                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
5334         } else {
5335                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5336                                          outer_headers);
5337                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
5338         }
5339         if (group == 0)
5340                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version, 0xf);
5341         else
5342                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version, 0x4);
5343         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_version, 4);
5344         if (!ipv4_v)
5345                 return;
5346         if (!ipv4_m)
5347                 ipv4_m = &nic_mask;
5348         l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
5349                              dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
5350         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
5351                              dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
5352         *(uint32_t *)l24_m = ipv4_m->hdr.dst_addr;
5353         *(uint32_t *)l24_v = ipv4_m->hdr.dst_addr & ipv4_v->hdr.dst_addr;
5354         l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
5355                           src_ipv4_src_ipv6.ipv4_layout.ipv4);
5356         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
5357                           src_ipv4_src_ipv6.ipv4_layout.ipv4);
5358         *(uint32_t *)l24_m = ipv4_m->hdr.src_addr;
5359         *(uint32_t *)l24_v = ipv4_m->hdr.src_addr & ipv4_v->hdr.src_addr;
5360         tos = ipv4_m->hdr.type_of_service & ipv4_v->hdr.type_of_service;
5361         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ecn,
5362                  ipv4_m->hdr.type_of_service);
5363         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn, tos);
5364         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_dscp,
5365                  ipv4_m->hdr.type_of_service >> 2);
5366         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp, tos >> 2);
5367         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol,
5368                  ipv4_m->hdr.next_proto_id);
5369         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
5370                  ipv4_v->hdr.next_proto_id & ipv4_m->hdr.next_proto_id);
5371 }
5372
5373 /**
5374  * Add IPV6 item to matcher and to the value.
5375  *
5376  * @param[in, out] matcher
5377  *   Flow matcher.
5378  * @param[in, out] key
5379  *   Flow matcher value.
5380  * @param[in] item
5381  *   Flow pattern to translate.
5382  * @param[in] inner
5383  *   Item is inner pattern.
5384  * @param[in] group
5385  *   The group to insert the rule.
5386  */
5387 static void
5388 flow_dv_translate_item_ipv6(void *matcher, void *key,
5389                             const struct rte_flow_item *item,
5390                             int inner, uint32_t group)
5391 {
5392         const struct rte_flow_item_ipv6 *ipv6_m = item->mask;
5393         const struct rte_flow_item_ipv6 *ipv6_v = item->spec;
5394         const struct rte_flow_item_ipv6 nic_mask = {
5395                 .hdr = {
5396                         .src_addr =
5397                                 "\xff\xff\xff\xff\xff\xff\xff\xff"
5398                                 "\xff\xff\xff\xff\xff\xff\xff\xff",
5399                         .dst_addr =
5400                                 "\xff\xff\xff\xff\xff\xff\xff\xff"
5401                                 "\xff\xff\xff\xff\xff\xff\xff\xff",
5402                         .vtc_flow = RTE_BE32(0xffffffff),
5403                         .proto = 0xff,
5404                         .hop_limits = 0xff,
5405                 },
5406         };
5407         void *headers_m;
5408         void *headers_v;
5409         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
5410         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
5411         char *l24_m;
5412         char *l24_v;
5413         uint32_t vtc_m;
5414         uint32_t vtc_v;
5415         int i;
5416         int size;
5417
5418         if (inner) {
5419                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5420                                          inner_headers);
5421                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
5422         } else {
5423                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5424                                          outer_headers);
5425                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
5426         }
5427         if (group == 0)
5428                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version, 0xf);
5429         else
5430                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version, 0x6);
5431         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_version, 6);
5432         if (!ipv6_v)
5433                 return;
5434         if (!ipv6_m)
5435                 ipv6_m = &nic_mask;
5436         size = sizeof(ipv6_m->hdr.dst_addr);
5437         l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
5438                              dst_ipv4_dst_ipv6.ipv6_layout.ipv6);
5439         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
5440                              dst_ipv4_dst_ipv6.ipv6_layout.ipv6);
5441         memcpy(l24_m, ipv6_m->hdr.dst_addr, size);
5442         for (i = 0; i < size; ++i)
5443                 l24_v[i] = l24_m[i] & ipv6_v->hdr.dst_addr[i];
5444         l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
5445                              src_ipv4_src_ipv6.ipv6_layout.ipv6);
5446         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
5447                              src_ipv4_src_ipv6.ipv6_layout.ipv6);
5448         memcpy(l24_m, ipv6_m->hdr.src_addr, size);
5449         for (i = 0; i < size; ++i)
5450                 l24_v[i] = l24_m[i] & ipv6_v->hdr.src_addr[i];
5451         /* TOS. */
5452         vtc_m = rte_be_to_cpu_32(ipv6_m->hdr.vtc_flow);
5453         vtc_v = rte_be_to_cpu_32(ipv6_m->hdr.vtc_flow & ipv6_v->hdr.vtc_flow);
5454         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ecn, vtc_m >> 20);
5455         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn, vtc_v >> 20);
5456         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_dscp, vtc_m >> 22);
5457         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp, vtc_v >> 22);
5458         /* Label. */
5459         if (inner) {
5460                 MLX5_SET(fte_match_set_misc, misc_m, inner_ipv6_flow_label,
5461                          vtc_m);
5462                 MLX5_SET(fte_match_set_misc, misc_v, inner_ipv6_flow_label,
5463                          vtc_v);
5464         } else {
5465                 MLX5_SET(fte_match_set_misc, misc_m, outer_ipv6_flow_label,
5466                          vtc_m);
5467                 MLX5_SET(fte_match_set_misc, misc_v, outer_ipv6_flow_label,
5468                          vtc_v);
5469         }
5470         /* Protocol. */
5471         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol,
5472                  ipv6_m->hdr.proto);
5473         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
5474                  ipv6_v->hdr.proto & ipv6_m->hdr.proto);
5475 }
5476
5477 /**
5478  * Add TCP item to matcher and to the value.
5479  *
5480  * @param[in, out] matcher
5481  *   Flow matcher.
5482  * @param[in, out] key
5483  *   Flow matcher value.
5484  * @param[in] item
5485  *   Flow pattern to translate.
5486  * @param[in] inner
5487  *   Item is inner pattern.
5488  */
5489 static void
5490 flow_dv_translate_item_tcp(void *matcher, void *key,
5491                            const struct rte_flow_item *item,
5492                            int inner)
5493 {
5494         const struct rte_flow_item_tcp *tcp_m = item->mask;
5495         const struct rte_flow_item_tcp *tcp_v = item->spec;
5496         void *headers_m;
5497         void *headers_v;
5498
5499         if (inner) {
5500                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5501                                          inner_headers);
5502                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
5503         } else {
5504                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5505                                          outer_headers);
5506                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
5507         }
5508         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
5509         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_TCP);
5510         if (!tcp_v)
5511                 return;
5512         if (!tcp_m)
5513                 tcp_m = &rte_flow_item_tcp_mask;
5514         MLX5_SET(fte_match_set_lyr_2_4, headers_m, tcp_sport,
5515                  rte_be_to_cpu_16(tcp_m->hdr.src_port));
5516         MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
5517                  rte_be_to_cpu_16(tcp_v->hdr.src_port & tcp_m->hdr.src_port));
5518         MLX5_SET(fte_match_set_lyr_2_4, headers_m, tcp_dport,
5519                  rte_be_to_cpu_16(tcp_m->hdr.dst_port));
5520         MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
5521                  rte_be_to_cpu_16(tcp_v->hdr.dst_port & tcp_m->hdr.dst_port));
5522         MLX5_SET(fte_match_set_lyr_2_4, headers_m, tcp_flags,
5523                  tcp_m->hdr.tcp_flags);
5524         MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_flags,
5525                  (tcp_v->hdr.tcp_flags & tcp_m->hdr.tcp_flags));
5526 }
5527
5528 /**
5529  * Add UDP item to matcher and to the value.
5530  *
5531  * @param[in, out] matcher
5532  *   Flow matcher.
5533  * @param[in, out] key
5534  *   Flow matcher value.
5535  * @param[in] item
5536  *   Flow pattern to translate.
5537  * @param[in] inner
5538  *   Item is inner pattern.
5539  */
5540 static void
5541 flow_dv_translate_item_udp(void *matcher, void *key,
5542                            const struct rte_flow_item *item,
5543                            int inner)
5544 {
5545         const struct rte_flow_item_udp *udp_m = item->mask;
5546         const struct rte_flow_item_udp *udp_v = item->spec;
5547         void *headers_m;
5548         void *headers_v;
5549
5550         if (inner) {
5551                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5552                                          inner_headers);
5553                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
5554         } else {
5555                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5556                                          outer_headers);
5557                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
5558         }
5559         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
5560         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_UDP);
5561         if (!udp_v)
5562                 return;
5563         if (!udp_m)
5564                 udp_m = &rte_flow_item_udp_mask;
5565         MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_sport,
5566                  rte_be_to_cpu_16(udp_m->hdr.src_port));
5567         MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
5568                  rte_be_to_cpu_16(udp_v->hdr.src_port & udp_m->hdr.src_port));
5569         MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport,
5570                  rte_be_to_cpu_16(udp_m->hdr.dst_port));
5571         MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
5572                  rte_be_to_cpu_16(udp_v->hdr.dst_port & udp_m->hdr.dst_port));
5573 }
5574
5575 /**
5576  * Add GRE optional Key item to matcher and to the value.
5577  *
5578  * @param[in, out] matcher
5579  *   Flow matcher.
5580  * @param[in, out] key
5581  *   Flow matcher value.
5582  * @param[in] item
5583  *   Flow pattern to translate.
5584  * @param[in] inner
5585  *   Item is inner pattern.
5586  */
5587 static void
5588 flow_dv_translate_item_gre_key(void *matcher, void *key,
5589                                    const struct rte_flow_item *item)
5590 {
5591         const rte_be32_t *key_m = item->mask;
5592         const rte_be32_t *key_v = item->spec;
5593         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
5594         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
5595         rte_be32_t gre_key_default_mask = RTE_BE32(UINT32_MAX);
5596
5597         if (!key_v)
5598                 return;
5599         if (!key_m)
5600                 key_m = &gre_key_default_mask;
5601         /* GRE K bit must be on and should already be validated */
5602         MLX5_SET(fte_match_set_misc, misc_m, gre_k_present, 1);
5603         MLX5_SET(fte_match_set_misc, misc_v, gre_k_present, 1);
5604         MLX5_SET(fte_match_set_misc, misc_m, gre_key_h,
5605                  rte_be_to_cpu_32(*key_m) >> 8);
5606         MLX5_SET(fte_match_set_misc, misc_v, gre_key_h,
5607                  rte_be_to_cpu_32((*key_v) & (*key_m)) >> 8);
5608         MLX5_SET(fte_match_set_misc, misc_m, gre_key_l,
5609                  rte_be_to_cpu_32(*key_m) & 0xFF);
5610         MLX5_SET(fte_match_set_misc, misc_v, gre_key_l,
5611                  rte_be_to_cpu_32((*key_v) & (*key_m)) & 0xFF);
5612 }
5613
5614 /**
5615  * Add GRE item to matcher and to the value.
5616  *
5617  * @param[in, out] matcher
5618  *   Flow matcher.
5619  * @param[in, out] key
5620  *   Flow matcher value.
5621  * @param[in] item
5622  *   Flow pattern to translate.
5623  * @param[in] inner
5624  *   Item is inner pattern.
5625  */
5626 static void
5627 flow_dv_translate_item_gre(void *matcher, void *key,
5628                            const struct rte_flow_item *item,
5629                            int inner)
5630 {
5631         const struct rte_flow_item_gre *gre_m = item->mask;
5632         const struct rte_flow_item_gre *gre_v = item->spec;
5633         void *headers_m;
5634         void *headers_v;
5635         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
5636         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
5637         struct {
5638                 union {
5639                         __extension__
5640                         struct {
5641                                 uint16_t version:3;
5642                                 uint16_t rsvd0:9;
5643                                 uint16_t s_present:1;
5644                                 uint16_t k_present:1;
5645                                 uint16_t rsvd_bit1:1;
5646                                 uint16_t c_present:1;
5647                         };
5648                         uint16_t value;
5649                 };
5650         } gre_crks_rsvd0_ver_m, gre_crks_rsvd0_ver_v;
5651
5652         if (inner) {
5653                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5654                                          inner_headers);
5655                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
5656         } else {
5657                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5658                                          outer_headers);
5659                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
5660         }
5661         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
5662         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_GRE);
5663         if (!gre_v)
5664                 return;
5665         if (!gre_m)
5666                 gre_m = &rte_flow_item_gre_mask;
5667         MLX5_SET(fte_match_set_misc, misc_m, gre_protocol,
5668                  rte_be_to_cpu_16(gre_m->protocol));
5669         MLX5_SET(fte_match_set_misc, misc_v, gre_protocol,
5670                  rte_be_to_cpu_16(gre_v->protocol & gre_m->protocol));
5671         gre_crks_rsvd0_ver_m.value = rte_be_to_cpu_16(gre_m->c_rsvd0_ver);
5672         gre_crks_rsvd0_ver_v.value = rte_be_to_cpu_16(gre_v->c_rsvd0_ver);
5673         MLX5_SET(fte_match_set_misc, misc_m, gre_c_present,
5674                  gre_crks_rsvd0_ver_m.c_present);
5675         MLX5_SET(fte_match_set_misc, misc_v, gre_c_present,
5676                  gre_crks_rsvd0_ver_v.c_present &
5677                  gre_crks_rsvd0_ver_m.c_present);
5678         MLX5_SET(fte_match_set_misc, misc_m, gre_k_present,
5679                  gre_crks_rsvd0_ver_m.k_present);
5680         MLX5_SET(fte_match_set_misc, misc_v, gre_k_present,
5681                  gre_crks_rsvd0_ver_v.k_present &
5682                  gre_crks_rsvd0_ver_m.k_present);
5683         MLX5_SET(fte_match_set_misc, misc_m, gre_s_present,
5684                  gre_crks_rsvd0_ver_m.s_present);
5685         MLX5_SET(fte_match_set_misc, misc_v, gre_s_present,
5686                  gre_crks_rsvd0_ver_v.s_present &
5687                  gre_crks_rsvd0_ver_m.s_present);
5688 }
5689
5690 /**
5691  * Add NVGRE item to matcher and to the value.
5692  *
5693  * @param[in, out] matcher
5694  *   Flow matcher.
5695  * @param[in, out] key
5696  *   Flow matcher value.
5697  * @param[in] item
5698  *   Flow pattern to translate.
5699  * @param[in] inner
5700  *   Item is inner pattern.
5701  */
5702 static void
5703 flow_dv_translate_item_nvgre(void *matcher, void *key,
5704                              const struct rte_flow_item *item,
5705                              int inner)
5706 {
5707         const struct rte_flow_item_nvgre *nvgre_m = item->mask;
5708         const struct rte_flow_item_nvgre *nvgre_v = item->spec;
5709         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
5710         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
5711         const char *tni_flow_id_m = (const char *)nvgre_m->tni;
5712         const char *tni_flow_id_v = (const char *)nvgre_v->tni;
5713         char *gre_key_m;
5714         char *gre_key_v;
5715         int size;
5716         int i;
5717
5718         /* For NVGRE, GRE header fields must be set with defined values. */
5719         const struct rte_flow_item_gre gre_spec = {
5720                 .c_rsvd0_ver = RTE_BE16(0x2000),
5721                 .protocol = RTE_BE16(RTE_ETHER_TYPE_TEB)
5722         };
5723         const struct rte_flow_item_gre gre_mask = {
5724                 .c_rsvd0_ver = RTE_BE16(0xB000),
5725                 .protocol = RTE_BE16(UINT16_MAX),
5726         };
5727         const struct rte_flow_item gre_item = {
5728                 .spec = &gre_spec,
5729                 .mask = &gre_mask,
5730                 .last = NULL,
5731         };
5732         flow_dv_translate_item_gre(matcher, key, &gre_item, inner);
5733         if (!nvgre_v)
5734                 return;
5735         if (!nvgre_m)
5736                 nvgre_m = &rte_flow_item_nvgre_mask;
5737         size = sizeof(nvgre_m->tni) + sizeof(nvgre_m->flow_id);
5738         gre_key_m = MLX5_ADDR_OF(fte_match_set_misc, misc_m, gre_key_h);
5739         gre_key_v = MLX5_ADDR_OF(fte_match_set_misc, misc_v, gre_key_h);
5740         memcpy(gre_key_m, tni_flow_id_m, size);
5741         for (i = 0; i < size; ++i)
5742                 gre_key_v[i] = gre_key_m[i] & tni_flow_id_v[i];
5743 }
5744
5745 /**
5746  * Add VXLAN item to matcher and to the value.
5747  *
5748  * @param[in, out] matcher
5749  *   Flow matcher.
5750  * @param[in, out] key
5751  *   Flow matcher value.
5752  * @param[in] item
5753  *   Flow pattern to translate.
5754  * @param[in] inner
5755  *   Item is inner pattern.
5756  */
5757 static void
5758 flow_dv_translate_item_vxlan(void *matcher, void *key,
5759                              const struct rte_flow_item *item,
5760                              int inner)
5761 {
5762         const struct rte_flow_item_vxlan *vxlan_m = item->mask;
5763         const struct rte_flow_item_vxlan *vxlan_v = item->spec;
5764         void *headers_m;
5765         void *headers_v;
5766         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
5767         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
5768         char *vni_m;
5769         char *vni_v;
5770         uint16_t dport;
5771         int size;
5772         int i;
5773
5774         if (inner) {
5775                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5776                                          inner_headers);
5777                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
5778         } else {
5779                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5780                                          outer_headers);
5781                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
5782         }
5783         dport = item->type == RTE_FLOW_ITEM_TYPE_VXLAN ?
5784                 MLX5_UDP_PORT_VXLAN : MLX5_UDP_PORT_VXLAN_GPE;
5785         if (!MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport)) {
5786                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xFFFF);
5787                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, dport);
5788         }
5789         if (!vxlan_v)
5790                 return;
5791         if (!vxlan_m)
5792                 vxlan_m = &rte_flow_item_vxlan_mask;
5793         size = sizeof(vxlan_m->vni);
5794         vni_m = MLX5_ADDR_OF(fte_match_set_misc, misc_m, vxlan_vni);
5795         vni_v = MLX5_ADDR_OF(fte_match_set_misc, misc_v, vxlan_vni);
5796         memcpy(vni_m, vxlan_m->vni, size);
5797         for (i = 0; i < size; ++i)
5798                 vni_v[i] = vni_m[i] & vxlan_v->vni[i];
5799 }
5800
5801 /**
5802  * Add Geneve item to matcher and to the value.
5803  *
5804  * @param[in, out] matcher
5805  *   Flow matcher.
5806  * @param[in, out] key
5807  *   Flow matcher value.
5808  * @param[in] item
5809  *   Flow pattern to translate.
5810  * @param[in] inner
5811  *   Item is inner pattern.
5812  */
5813
5814 static void
5815 flow_dv_translate_item_geneve(void *matcher, void *key,
5816                               const struct rte_flow_item *item, int inner)
5817 {
5818         const struct rte_flow_item_geneve *geneve_m = item->mask;
5819         const struct rte_flow_item_geneve *geneve_v = item->spec;
5820         void *headers_m;
5821         void *headers_v;
5822         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
5823         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
5824         uint16_t dport;
5825         uint16_t gbhdr_m;
5826         uint16_t gbhdr_v;
5827         char *vni_m;
5828         char *vni_v;
5829         size_t size, i;
5830
5831         if (inner) {
5832                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5833                                          inner_headers);
5834                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
5835         } else {
5836                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5837                                          outer_headers);
5838                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
5839         }
5840         dport = MLX5_UDP_PORT_GENEVE;
5841         if (!MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport)) {
5842                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xFFFF);
5843                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, dport);
5844         }
5845         if (!geneve_v)
5846                 return;
5847         if (!geneve_m)
5848                 geneve_m = &rte_flow_item_geneve_mask;
5849         size = sizeof(geneve_m->vni);
5850         vni_m = MLX5_ADDR_OF(fte_match_set_misc, misc_m, geneve_vni);
5851         vni_v = MLX5_ADDR_OF(fte_match_set_misc, misc_v, geneve_vni);
5852         memcpy(vni_m, geneve_m->vni, size);
5853         for (i = 0; i < size; ++i)
5854                 vni_v[i] = vni_m[i] & geneve_v->vni[i];
5855         MLX5_SET(fte_match_set_misc, misc_m, geneve_protocol_type,
5856                  rte_be_to_cpu_16(geneve_m->protocol));
5857         MLX5_SET(fte_match_set_misc, misc_v, geneve_protocol_type,
5858                  rte_be_to_cpu_16(geneve_v->protocol & geneve_m->protocol));
5859         gbhdr_m = rte_be_to_cpu_16(geneve_m->ver_opt_len_o_c_rsvd0);
5860         gbhdr_v = rte_be_to_cpu_16(geneve_v->ver_opt_len_o_c_rsvd0);
5861         MLX5_SET(fte_match_set_misc, misc_m, geneve_oam,
5862                  MLX5_GENEVE_OAMF_VAL(gbhdr_m));
5863         MLX5_SET(fte_match_set_misc, misc_v, geneve_oam,
5864                  MLX5_GENEVE_OAMF_VAL(gbhdr_v) & MLX5_GENEVE_OAMF_VAL(gbhdr_m));
5865         MLX5_SET(fte_match_set_misc, misc_m, geneve_opt_len,
5866                  MLX5_GENEVE_OPTLEN_VAL(gbhdr_m));
5867         MLX5_SET(fte_match_set_misc, misc_v, geneve_opt_len,
5868                  MLX5_GENEVE_OPTLEN_VAL(gbhdr_v) &
5869                  MLX5_GENEVE_OPTLEN_VAL(gbhdr_m));
5870 }
5871
5872 /**
5873  * Add MPLS item to matcher and to the value.
5874  *
5875  * @param[in, out] matcher
5876  *   Flow matcher.
5877  * @param[in, out] key
5878  *   Flow matcher value.
5879  * @param[in] item
5880  *   Flow pattern to translate.
5881  * @param[in] prev_layer
5882  *   The protocol layer indicated in previous item.
5883  * @param[in] inner
5884  *   Item is inner pattern.
5885  */
5886 static void
5887 flow_dv_translate_item_mpls(void *matcher, void *key,
5888                             const struct rte_flow_item *item,
5889                             uint64_t prev_layer,
5890                             int inner)
5891 {
5892         const uint32_t *in_mpls_m = item->mask;
5893         const uint32_t *in_mpls_v = item->spec;
5894         uint32_t *out_mpls_m = 0;
5895         uint32_t *out_mpls_v = 0;
5896         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
5897         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
5898         void *misc2_m = MLX5_ADDR_OF(fte_match_param, matcher,
5899                                      misc_parameters_2);
5900         void *misc2_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_2);
5901         void *headers_m = MLX5_ADDR_OF(fte_match_param, matcher, outer_headers);
5902         void *headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
5903
5904         switch (prev_layer) {
5905         case MLX5_FLOW_LAYER_OUTER_L4_UDP:
5906                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xffff);
5907                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
5908                          MLX5_UDP_PORT_MPLS);
5909                 break;
5910         case MLX5_FLOW_LAYER_GRE:
5911                 MLX5_SET(fte_match_set_misc, misc_m, gre_protocol, 0xffff);
5912                 MLX5_SET(fte_match_set_misc, misc_v, gre_protocol,
5913                          RTE_ETHER_TYPE_MPLS);
5914                 break;
5915         default:
5916                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
5917                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
5918                          IPPROTO_MPLS);
5919                 break;
5920         }
5921         if (!in_mpls_v)
5922                 return;
5923         if (!in_mpls_m)
5924                 in_mpls_m = (const uint32_t *)&rte_flow_item_mpls_mask;
5925         switch (prev_layer) {
5926         case MLX5_FLOW_LAYER_OUTER_L4_UDP:
5927                 out_mpls_m =
5928                         (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_m,
5929                                                  outer_first_mpls_over_udp);
5930                 out_mpls_v =
5931                         (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_v,
5932                                                  outer_first_mpls_over_udp);
5933                 break;
5934         case MLX5_FLOW_LAYER_GRE:
5935                 out_mpls_m =
5936                         (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_m,
5937                                                  outer_first_mpls_over_gre);
5938                 out_mpls_v =
5939                         (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_v,
5940                                                  outer_first_mpls_over_gre);
5941                 break;
5942         default:
5943                 /* Inner MPLS not over GRE is not supported. */
5944                 if (!inner) {
5945                         out_mpls_m =
5946                                 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2,
5947                                                          misc2_m,
5948                                                          outer_first_mpls);
5949                         out_mpls_v =
5950                                 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2,
5951                                                          misc2_v,
5952                                                          outer_first_mpls);
5953                 }
5954                 break;
5955         }
5956         if (out_mpls_m && out_mpls_v) {
5957                 *out_mpls_m = *in_mpls_m;
5958                 *out_mpls_v = *in_mpls_v & *in_mpls_m;
5959         }
5960 }
5961
5962 /**
5963  * Add metadata register item to matcher
5964  *
5965  * @param[in, out] matcher
5966  *   Flow matcher.
5967  * @param[in, out] key
5968  *   Flow matcher value.
5969  * @param[in] reg_type
5970  *   Type of device metadata register
5971  * @param[in] value
5972  *   Register value
5973  * @param[in] mask
5974  *   Register mask
5975  */
5976 static void
5977 flow_dv_match_meta_reg(void *matcher, void *key,
5978                        enum modify_reg reg_type,
5979                        uint32_t data, uint32_t mask)
5980 {
5981         void *misc2_m =
5982                 MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters_2);
5983         void *misc2_v =
5984                 MLX5_ADDR_OF(fte_match_param, key, misc_parameters_2);
5985         uint32_t temp;
5986
5987         data &= mask;
5988         switch (reg_type) {
5989         case REG_A:
5990                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_a, mask);
5991                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_a, data);
5992                 break;
5993         case REG_B:
5994                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_b, mask);
5995                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_b, data);
5996                 break;
5997         case REG_C_0:
5998                 /*
5999                  * The metadata register C0 field might be divided into
6000                  * source vport index and META item value, we should set
6001                  * this field according to specified mask, not as whole one.
6002                  */
6003                 temp = MLX5_GET(fte_match_set_misc2, misc2_m, metadata_reg_c_0);
6004                 temp |= mask;
6005                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_0, temp);
6006                 temp = MLX5_GET(fte_match_set_misc2, misc2_v, metadata_reg_c_0);
6007                 temp &= ~mask;
6008                 temp |= data;
6009                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_0, temp);
6010                 break;
6011         case REG_C_1:
6012                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_1, mask);
6013                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_1, data);
6014                 break;
6015         case REG_C_2:
6016                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_2, mask);
6017                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_2, data);
6018                 break;
6019         case REG_C_3:
6020                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_3, mask);
6021                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_3, data);
6022                 break;
6023         case REG_C_4:
6024                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_4, mask);
6025                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_4, data);
6026                 break;
6027         case REG_C_5:
6028                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_5, mask);
6029                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_5, data);
6030                 break;
6031         case REG_C_6:
6032                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_6, mask);
6033                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_6, data);
6034                 break;
6035         case REG_C_7:
6036                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_7, mask);
6037                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_7, data);
6038                 break;
6039         default:
6040                 assert(false);
6041                 break;
6042         }
6043 }
6044
6045 /**
6046  * Add MARK item to matcher
6047  *
6048  * @param[in] dev
6049  *   The device to configure through.
6050  * @param[in, out] matcher
6051  *   Flow matcher.
6052  * @param[in, out] key
6053  *   Flow matcher value.
6054  * @param[in] item
6055  *   Flow pattern to translate.
6056  */
6057 static void
6058 flow_dv_translate_item_mark(struct rte_eth_dev *dev,
6059                             void *matcher, void *key,
6060                             const struct rte_flow_item *item)
6061 {
6062         struct mlx5_priv *priv = dev->data->dev_private;
6063         const struct rte_flow_item_mark *mark;
6064         uint32_t value;
6065         uint32_t mask;
6066
6067         mark = item->mask ? (const void *)item->mask :
6068                             &rte_flow_item_mark_mask;
6069         mask = mark->id & priv->sh->dv_mark_mask;
6070         mark = (const void *)item->spec;
6071         assert(mark);
6072         value = mark->id & priv->sh->dv_mark_mask & mask;
6073         if (mask) {
6074                 enum modify_reg reg;
6075
6076                 /* Get the metadata register index for the mark. */
6077                 reg = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, NULL);
6078                 assert(reg > 0);
6079                 if (reg == REG_C_0) {
6080                         struct mlx5_priv *priv = dev->data->dev_private;
6081                         uint32_t msk_c0 = priv->sh->dv_regc0_mask;
6082                         uint32_t shl_c0 = rte_bsf32(msk_c0);
6083
6084                         mask &= msk_c0;
6085                         mask <<= shl_c0;
6086                         value <<= shl_c0;
6087                 }
6088                 flow_dv_match_meta_reg(matcher, key, reg, value, mask);
6089         }
6090 }
6091
6092 /**
6093  * Add META item to matcher
6094  *
6095  * @param[in] dev
6096  *   The devich to configure through.
6097  * @param[in, out] matcher
6098  *   Flow matcher.
6099  * @param[in, out] key
6100  *   Flow matcher value.
6101  * @param[in] attr
6102  *   Attributes of flow that includes this item.
6103  * @param[in] item
6104  *   Flow pattern to translate.
6105  */
6106 static void
6107 flow_dv_translate_item_meta(struct rte_eth_dev *dev,
6108                             void *matcher, void *key,
6109                             const struct rte_flow_attr *attr,
6110                             const struct rte_flow_item *item)
6111 {
6112         const struct rte_flow_item_meta *meta_m;
6113         const struct rte_flow_item_meta *meta_v;
6114
6115         meta_m = (const void *)item->mask;
6116         if (!meta_m)
6117                 meta_m = &rte_flow_item_meta_mask;
6118         meta_v = (const void *)item->spec;
6119         if (meta_v) {
6120                 enum modify_reg reg;
6121                 uint32_t value = meta_v->data;
6122                 uint32_t mask = meta_m->data;
6123
6124                 reg = flow_dv_get_metadata_reg(dev, attr, NULL);
6125                 if (reg < 0)
6126                         return;
6127                 /*
6128                  * In datapath code there is no endianness
6129                  * coversions for perfromance reasons, all
6130                  * pattern conversions are done in rte_flow.
6131                  */
6132                 value = rte_cpu_to_be_32(value);
6133                 mask = rte_cpu_to_be_32(mask);
6134                 if (reg == REG_C_0) {
6135                         struct mlx5_priv *priv = dev->data->dev_private;
6136                         uint32_t msk_c0 = priv->sh->dv_regc0_mask;
6137                         uint32_t shl_c0 = rte_bsf32(msk_c0);
6138 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
6139                         uint32_t shr_c0 = __builtin_clz(priv->sh->dv_meta_mask);
6140
6141                         value >>= shr_c0;
6142                         mask >>= shr_c0;
6143 #endif
6144                         value <<= shl_c0;
6145                         mask <<= shl_c0;
6146                         assert(msk_c0);
6147                         assert(!(~msk_c0 & mask));
6148                 }
6149                 flow_dv_match_meta_reg(matcher, key, reg, value, mask);
6150         }
6151 }
6152
6153 /**
6154  * Add vport metadata Reg C0 item to matcher
6155  *
6156  * @param[in, out] matcher
6157  *   Flow matcher.
6158  * @param[in, out] key
6159  *   Flow matcher value.
6160  * @param[in] reg
6161  *   Flow pattern to translate.
6162  */
6163 static void
6164 flow_dv_translate_item_meta_vport(void *matcher, void *key,
6165                                   uint32_t value, uint32_t mask)
6166 {
6167         flow_dv_match_meta_reg(matcher, key, REG_C_0, value, mask);
6168 }
6169
6170 /**
6171  * Add tag item to matcher
6172  *
6173  * @param[in] dev
6174  *   The devich to configure through.
6175  * @param[in, out] matcher
6176  *   Flow matcher.
6177  * @param[in, out] key
6178  *   Flow matcher value.
6179  * @param[in] item
6180  *   Flow pattern to translate.
6181  */
6182 static void
6183 flow_dv_translate_mlx5_item_tag(struct rte_eth_dev *dev,
6184                                 void *matcher, void *key,
6185                                 const struct rte_flow_item *item)
6186 {
6187         const struct mlx5_rte_flow_item_tag *tag_v = item->spec;
6188         const struct mlx5_rte_flow_item_tag *tag_m = item->mask;
6189         uint32_t mask, value;
6190
6191         assert(tag_v);
6192         value = tag_v->data;
6193         mask = tag_m ? tag_m->data : UINT32_MAX;
6194         if (tag_v->id == REG_C_0) {
6195                 struct mlx5_priv *priv = dev->data->dev_private;
6196                 uint32_t msk_c0 = priv->sh->dv_regc0_mask;
6197                 uint32_t shl_c0 = rte_bsf32(msk_c0);
6198
6199                 mask &= msk_c0;
6200                 mask <<= shl_c0;
6201                 value <<= shl_c0;
6202         }
6203         flow_dv_match_meta_reg(matcher, key, tag_v->id, value, mask);
6204 }
6205
6206 /**
6207  * Add TAG item to matcher
6208  *
6209  * @param[in] dev
6210  *   The devich to configure through.
6211  * @param[in, out] matcher
6212  *   Flow matcher.
6213  * @param[in, out] key
6214  *   Flow matcher value.
6215  * @param[in] item
6216  *   Flow pattern to translate.
6217  */
6218 static void
6219 flow_dv_translate_item_tag(struct rte_eth_dev *dev,
6220                            void *matcher, void *key,
6221                            const struct rte_flow_item *item)
6222 {
6223         const struct rte_flow_item_tag *tag_v = item->spec;
6224         const struct rte_flow_item_tag *tag_m = item->mask;
6225         enum modify_reg reg;
6226
6227         assert(tag_v);
6228         tag_m = tag_m ? tag_m : &rte_flow_item_tag_mask;
6229         /* Get the metadata register index for the tag. */
6230         reg = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, tag_v->index, NULL);
6231         assert(reg > 0);
6232         flow_dv_match_meta_reg(matcher, key, reg, tag_v->data, tag_m->data);
6233 }
6234
6235 /**
6236  * Add source vport match to the specified matcher.
6237  *
6238  * @param[in, out] matcher
6239  *   Flow matcher.
6240  * @param[in, out] key
6241  *   Flow matcher value.
6242  * @param[in] port
6243  *   Source vport value to match
6244  * @param[in] mask
6245  *   Mask
6246  */
6247 static void
6248 flow_dv_translate_item_source_vport(void *matcher, void *key,
6249                                     int16_t port, uint16_t mask)
6250 {
6251         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
6252         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
6253
6254         MLX5_SET(fte_match_set_misc, misc_m, source_port, mask);
6255         MLX5_SET(fte_match_set_misc, misc_v, source_port, port);
6256 }
6257
6258 /**
6259  * Translate port-id item to eswitch match on  port-id.
6260  *
6261  * @param[in] dev
6262  *   The devich to configure through.
6263  * @param[in, out] matcher
6264  *   Flow matcher.
6265  * @param[in, out] key
6266  *   Flow matcher value.
6267  * @param[in] item
6268  *   Flow pattern to translate.
6269  *
6270  * @return
6271  *   0 on success, a negative errno value otherwise.
6272  */
6273 static int
6274 flow_dv_translate_item_port_id(struct rte_eth_dev *dev, void *matcher,
6275                                void *key, const struct rte_flow_item *item)
6276 {
6277         const struct rte_flow_item_port_id *pid_m = item ? item->mask : NULL;
6278         const struct rte_flow_item_port_id *pid_v = item ? item->spec : NULL;
6279         struct mlx5_priv *priv;
6280         uint16_t mask, id;
6281
6282         mask = pid_m ? pid_m->id : 0xffff;
6283         id = pid_v ? pid_v->id : dev->data->port_id;
6284         priv = mlx5_port_to_eswitch_info(id, item == NULL);
6285         if (!priv)
6286                 return -rte_errno;
6287         /* Translate to vport field or to metadata, depending on mode. */
6288         if (priv->vport_meta_mask)
6289                 flow_dv_translate_item_meta_vport(matcher, key,
6290                                                   priv->vport_meta_tag,
6291                                                   priv->vport_meta_mask);
6292         else
6293                 flow_dv_translate_item_source_vport(matcher, key,
6294                                                     priv->vport_id, mask);
6295         return 0;
6296 }
6297
6298 /**
6299  * Add ICMP6 item to matcher and to the value.
6300  *
6301  * @param[in, out] matcher
6302  *   Flow matcher.
6303  * @param[in, out] key
6304  *   Flow matcher value.
6305  * @param[in] item
6306  *   Flow pattern to translate.
6307  * @param[in] inner
6308  *   Item is inner pattern.
6309  */
6310 static void
6311 flow_dv_translate_item_icmp6(void *matcher, void *key,
6312                               const struct rte_flow_item *item,
6313                               int inner)
6314 {
6315         const struct rte_flow_item_icmp6 *icmp6_m = item->mask;
6316         const struct rte_flow_item_icmp6 *icmp6_v = item->spec;
6317         void *headers_m;
6318         void *headers_v;
6319         void *misc3_m = MLX5_ADDR_OF(fte_match_param, matcher,
6320                                      misc_parameters_3);
6321         void *misc3_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
6322         if (inner) {
6323                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6324                                          inner_headers);
6325                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
6326         } else {
6327                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6328                                          outer_headers);
6329                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6330         }
6331         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xFF);
6332         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_ICMPV6);
6333         if (!icmp6_v)
6334                 return;
6335         if (!icmp6_m)
6336                 icmp6_m = &rte_flow_item_icmp6_mask;
6337         MLX5_SET(fte_match_set_misc3, misc3_m, icmpv6_type, icmp6_m->type);
6338         MLX5_SET(fte_match_set_misc3, misc3_v, icmpv6_type,
6339                  icmp6_v->type & icmp6_m->type);
6340         MLX5_SET(fte_match_set_misc3, misc3_m, icmpv6_code, icmp6_m->code);
6341         MLX5_SET(fte_match_set_misc3, misc3_v, icmpv6_code,
6342                  icmp6_v->code & icmp6_m->code);
6343 }
6344
6345 /**
6346  * Add ICMP item to matcher and to the value.
6347  *
6348  * @param[in, out] matcher
6349  *   Flow matcher.
6350  * @param[in, out] key
6351  *   Flow matcher value.
6352  * @param[in] item
6353  *   Flow pattern to translate.
6354  * @param[in] inner
6355  *   Item is inner pattern.
6356  */
6357 static void
6358 flow_dv_translate_item_icmp(void *matcher, void *key,
6359                             const struct rte_flow_item *item,
6360                             int inner)
6361 {
6362         const struct rte_flow_item_icmp *icmp_m = item->mask;
6363         const struct rte_flow_item_icmp *icmp_v = item->spec;
6364         void *headers_m;
6365         void *headers_v;
6366         void *misc3_m = MLX5_ADDR_OF(fte_match_param, matcher,
6367                                      misc_parameters_3);
6368         void *misc3_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
6369         if (inner) {
6370                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6371                                          inner_headers);
6372                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
6373         } else {
6374                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6375                                          outer_headers);
6376                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6377         }
6378         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xFF);
6379         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_ICMP);
6380         if (!icmp_v)
6381                 return;
6382         if (!icmp_m)
6383                 icmp_m = &rte_flow_item_icmp_mask;
6384         MLX5_SET(fte_match_set_misc3, misc3_m, icmp_type,
6385                  icmp_m->hdr.icmp_type);
6386         MLX5_SET(fte_match_set_misc3, misc3_v, icmp_type,
6387                  icmp_v->hdr.icmp_type & icmp_m->hdr.icmp_type);
6388         MLX5_SET(fte_match_set_misc3, misc3_m, icmp_code,
6389                  icmp_m->hdr.icmp_code);
6390         MLX5_SET(fte_match_set_misc3, misc3_v, icmp_code,
6391                  icmp_v->hdr.icmp_code & icmp_m->hdr.icmp_code);
6392 }
6393
6394 /**
6395  * Add GTP item to matcher and to the value.
6396  *
6397  * @param[in, out] matcher
6398  *   Flow matcher.
6399  * @param[in, out] key
6400  *   Flow matcher value.
6401  * @param[in] item
6402  *   Flow pattern to translate.
6403  * @param[in] inner
6404  *   Item is inner pattern.
6405  */
6406 static void
6407 flow_dv_translate_item_gtp(void *matcher, void *key,
6408                            const struct rte_flow_item *item, int inner)
6409 {
6410         const struct rte_flow_item_gtp *gtp_m = item->mask;
6411         const struct rte_flow_item_gtp *gtp_v = item->spec;
6412         void *headers_m;
6413         void *headers_v;
6414         void *misc3_m = MLX5_ADDR_OF(fte_match_param, matcher,
6415                                      misc_parameters_3);
6416         void *misc3_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
6417         uint16_t dport = RTE_GTPU_UDP_PORT;
6418
6419         if (inner) {
6420                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6421                                          inner_headers);
6422                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
6423         } else {
6424                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6425                                          outer_headers);
6426                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6427         }
6428         if (!MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport)) {
6429                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xFFFF);
6430                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, dport);
6431         }
6432         if (!gtp_v)
6433                 return;
6434         if (!gtp_m)
6435                 gtp_m = &rte_flow_item_gtp_mask;
6436         MLX5_SET(fte_match_set_misc3, misc3_m, gtpu_msg_type, gtp_m->msg_type);
6437         MLX5_SET(fte_match_set_misc3, misc3_v, gtpu_msg_type,
6438                  gtp_v->msg_type & gtp_m->msg_type);
6439         MLX5_SET(fte_match_set_misc3, misc3_m, gtpu_teid,
6440                  rte_be_to_cpu_32(gtp_m->teid));
6441         MLX5_SET(fte_match_set_misc3, misc3_v, gtpu_teid,
6442                  rte_be_to_cpu_32(gtp_v->teid & gtp_m->teid));
6443 }
6444
6445 static uint32_t matcher_zero[MLX5_ST_SZ_DW(fte_match_param)] = { 0 };
6446
6447 #define HEADER_IS_ZERO(match_criteria, headers)                              \
6448         !(memcmp(MLX5_ADDR_OF(fte_match_param, match_criteria, headers),     \
6449                  matcher_zero, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
6450
6451 /**
6452  * Calculate flow matcher enable bitmap.
6453  *
6454  * @param match_criteria
6455  *   Pointer to flow matcher criteria.
6456  *
6457  * @return
6458  *   Bitmap of enabled fields.
6459  */
6460 static uint8_t
6461 flow_dv_matcher_enable(uint32_t *match_criteria)
6462 {
6463         uint8_t match_criteria_enable;
6464
6465         match_criteria_enable =
6466                 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
6467                 MLX5_MATCH_CRITERIA_ENABLE_OUTER_BIT;
6468         match_criteria_enable |=
6469                 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
6470                 MLX5_MATCH_CRITERIA_ENABLE_MISC_BIT;
6471         match_criteria_enable |=
6472                 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
6473                 MLX5_MATCH_CRITERIA_ENABLE_INNER_BIT;
6474         match_criteria_enable |=
6475                 (!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
6476                 MLX5_MATCH_CRITERIA_ENABLE_MISC2_BIT;
6477         match_criteria_enable |=
6478                 (!HEADER_IS_ZERO(match_criteria, misc_parameters_3)) <<
6479                 MLX5_MATCH_CRITERIA_ENABLE_MISC3_BIT;
6480         return match_criteria_enable;
6481 }
6482
6483
6484 /**
6485  * Get a flow table.
6486  *
6487  * @param[in, out] dev
6488  *   Pointer to rte_eth_dev structure.
6489  * @param[in] table_id
6490  *   Table id to use.
6491  * @param[in] egress
6492  *   Direction of the table.
6493  * @param[in] transfer
6494  *   E-Switch or NIC flow.
6495  * @param[out] error
6496  *   pointer to error structure.
6497  *
6498  * @return
6499  *   Returns tables resource based on the index, NULL in case of failed.
6500  */
6501 static struct mlx5_flow_tbl_resource *
6502 flow_dv_tbl_resource_get(struct rte_eth_dev *dev,
6503                          uint32_t table_id, uint8_t egress,
6504                          uint8_t transfer,
6505                          struct rte_flow_error *error)
6506 {
6507         struct mlx5_priv *priv = dev->data->dev_private;
6508         struct mlx5_ibv_shared *sh = priv->sh;
6509         struct mlx5_flow_tbl_resource *tbl;
6510         union mlx5_flow_tbl_key table_key = {
6511                 {
6512                         .table_id = table_id,
6513                         .reserved = 0,
6514                         .domain = !!transfer,
6515                         .direction = !!egress,
6516                 }
6517         };
6518         struct mlx5_hlist_entry *pos = mlx5_hlist_lookup(sh->flow_tbls,
6519                                                          table_key.v64);
6520         struct mlx5_flow_tbl_data_entry *tbl_data;
6521         int ret;
6522         void *domain;
6523
6524         if (pos) {
6525                 tbl_data = container_of(pos, struct mlx5_flow_tbl_data_entry,
6526                                         entry);
6527                 tbl = &tbl_data->tbl;
6528                 rte_atomic32_inc(&tbl->refcnt);
6529                 return tbl;
6530         }
6531         tbl_data = rte_zmalloc(NULL, sizeof(*tbl_data), 0);
6532         if (!tbl_data) {
6533                 rte_flow_error_set(error, ENOMEM,
6534                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
6535                                    NULL,
6536                                    "cannot allocate flow table data entry");
6537                 return NULL;
6538         }
6539         tbl = &tbl_data->tbl;
6540         pos = &tbl_data->entry;
6541         if (transfer)
6542                 domain = sh->fdb_domain;
6543         else if (egress)
6544                 domain = sh->tx_domain;
6545         else
6546                 domain = sh->rx_domain;
6547         tbl->obj = mlx5_glue->dr_create_flow_tbl(domain, table_id);
6548         if (!tbl->obj) {
6549                 rte_flow_error_set(error, ENOMEM,
6550                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
6551                                    NULL, "cannot create flow table object");
6552                 rte_free(tbl_data);
6553                 return NULL;
6554         }
6555         /*
6556          * No multi-threads now, but still better to initialize the reference
6557          * count before insert it into the hash list.
6558          */
6559         rte_atomic32_init(&tbl->refcnt);
6560         /* Jump action reference count is initialized here. */
6561         rte_atomic32_init(&tbl_data->jump.refcnt);
6562         pos->key = table_key.v64;
6563         ret = mlx5_hlist_insert(sh->flow_tbls, pos);
6564         if (ret < 0) {
6565                 rte_flow_error_set(error, -ret,
6566                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
6567                                    "cannot insert flow table data entry");
6568                 mlx5_glue->dr_destroy_flow_tbl(tbl->obj);
6569                 rte_free(tbl_data);
6570         }
6571         rte_atomic32_inc(&tbl->refcnt);
6572         return tbl;
6573 }
6574
6575 /**
6576  * Release a flow table.
6577  *
6578  * @param[in] dev
6579  *   Pointer to rte_eth_dev structure.
6580  * @param[in] tbl
6581  *   Table resource to be released.
6582  *
6583  * @return
6584  *   Returns 0 if table was released, else return 1;
6585  */
6586 static int
6587 flow_dv_tbl_resource_release(struct rte_eth_dev *dev,
6588                              struct mlx5_flow_tbl_resource *tbl)
6589 {
6590         struct mlx5_priv *priv = dev->data->dev_private;
6591         struct mlx5_ibv_shared *sh = priv->sh;
6592         struct mlx5_flow_tbl_data_entry *tbl_data =
6593                 container_of(tbl, struct mlx5_flow_tbl_data_entry, tbl);
6594
6595         if (!tbl)
6596                 return 0;
6597         if (rte_atomic32_dec_and_test(&tbl->refcnt)) {
6598                 struct mlx5_hlist_entry *pos = &tbl_data->entry;
6599
6600                 mlx5_glue->dr_destroy_flow_tbl(tbl->obj);
6601                 tbl->obj = NULL;
6602                 /* remove the entry from the hash list and free memory. */
6603                 mlx5_hlist_remove(sh->flow_tbls, pos);
6604                 rte_free(tbl_data);
6605                 return 0;
6606         }
6607         return 1;
6608 }
6609
6610 /**
6611  * Register the flow matcher.
6612  *
6613  * @param[in, out] dev
6614  *   Pointer to rte_eth_dev structure.
6615  * @param[in, out] matcher
6616  *   Pointer to flow matcher.
6617  * @param[in, out] key
6618  *   Pointer to flow table key.
6619  * @parm[in, out] dev_flow
6620  *   Pointer to the dev_flow.
6621  * @param[out] error
6622  *   pointer to error structure.
6623  *
6624  * @return
6625  *   0 on success otherwise -errno and errno is set.
6626  */
6627 static int
6628 flow_dv_matcher_register(struct rte_eth_dev *dev,
6629                          struct mlx5_flow_dv_matcher *matcher,
6630                          union mlx5_flow_tbl_key *key,
6631                          struct mlx5_flow *dev_flow,
6632                          struct rte_flow_error *error)
6633 {
6634         struct mlx5_priv *priv = dev->data->dev_private;
6635         struct mlx5_ibv_shared *sh = priv->sh;
6636         struct mlx5_flow_dv_matcher *cache_matcher;
6637         struct mlx5dv_flow_matcher_attr dv_attr = {
6638                 .type = IBV_FLOW_ATTR_NORMAL,
6639                 .match_mask = (void *)&matcher->mask,
6640         };
6641         struct mlx5_flow_tbl_resource *tbl;
6642         struct mlx5_flow_tbl_data_entry *tbl_data;
6643
6644         tbl = flow_dv_tbl_resource_get(dev, key->table_id, key->direction,
6645                                        key->domain, error);
6646         if (!tbl)
6647                 return -rte_errno;      /* No need to refill the error info */
6648         tbl_data = container_of(tbl, struct mlx5_flow_tbl_data_entry, tbl);
6649         /* Lookup from cache. */
6650         LIST_FOREACH(cache_matcher, &tbl_data->matchers, next) {
6651                 if (matcher->crc == cache_matcher->crc &&
6652                     matcher->priority == cache_matcher->priority &&
6653                     !memcmp((const void *)matcher->mask.buf,
6654                             (const void *)cache_matcher->mask.buf,
6655                             cache_matcher->mask.size)) {
6656                         DRV_LOG(DEBUG,
6657                                 "%s group %u priority %hd use %s "
6658                                 "matcher %p: refcnt %d++",
6659                                 key->domain ? "FDB" : "NIC", key->table_id,
6660                                 cache_matcher->priority,
6661                                 key->direction ? "tx" : "rx",
6662                                 (void *)cache_matcher,
6663                                 rte_atomic32_read(&cache_matcher->refcnt));
6664                         rte_atomic32_inc(&cache_matcher->refcnt);
6665                         dev_flow->dv.matcher = cache_matcher;
6666                         /* old matcher should not make the table ref++. */
6667                         flow_dv_tbl_resource_release(dev, tbl);
6668                         return 0;
6669                 }
6670         }
6671         /* Register new matcher. */
6672         cache_matcher = rte_calloc(__func__, 1, sizeof(*cache_matcher), 0);
6673         if (!cache_matcher) {
6674                 flow_dv_tbl_resource_release(dev, tbl);
6675                 return rte_flow_error_set(error, ENOMEM,
6676                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
6677                                           "cannot allocate matcher memory");
6678         }
6679         *cache_matcher = *matcher;
6680         dv_attr.match_criteria_enable =
6681                 flow_dv_matcher_enable(cache_matcher->mask.buf);
6682         dv_attr.priority = matcher->priority;
6683         if (key->direction)
6684                 dv_attr.flags |= IBV_FLOW_ATTR_FLAGS_EGRESS;
6685         cache_matcher->matcher_object =
6686                 mlx5_glue->dv_create_flow_matcher(sh->ctx, &dv_attr, tbl->obj);
6687         if (!cache_matcher->matcher_object) {
6688                 rte_free(cache_matcher);
6689 #ifdef HAVE_MLX5DV_DR
6690                 flow_dv_tbl_resource_release(dev, tbl);
6691 #endif
6692                 return rte_flow_error_set(error, ENOMEM,
6693                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
6694                                           NULL, "cannot create matcher");
6695         }
6696         /* Save the table information */
6697         cache_matcher->tbl = tbl;
6698         rte_atomic32_init(&cache_matcher->refcnt);
6699         /* only matcher ref++, table ref++ already done above in get API. */
6700         rte_atomic32_inc(&cache_matcher->refcnt);
6701         LIST_INSERT_HEAD(&tbl_data->matchers, cache_matcher, next);
6702         dev_flow->dv.matcher = cache_matcher;
6703         DRV_LOG(DEBUG, "%s group %u priority %hd new %s matcher %p: refcnt %d",
6704                 key->domain ? "FDB" : "NIC", key->table_id,
6705                 cache_matcher->priority,
6706                 key->direction ? "tx" : "rx", (void *)cache_matcher,
6707                 rte_atomic32_read(&cache_matcher->refcnt));
6708         return 0;
6709 }
6710
6711 /**
6712  * Find existing tag resource or create and register a new one.
6713  *
6714  * @param dev[in, out]
6715  *   Pointer to rte_eth_dev structure.
6716  * @param[in, out] tag_be24
6717  *   Tag value in big endian then R-shift 8.
6718  * @parm[in, out] dev_flow
6719  *   Pointer to the dev_flow.
6720  * @param[out] error
6721  *   pointer to error structure.
6722  *
6723  * @return
6724  *   0 on success otherwise -errno and errno is set.
6725  */
6726 static int
6727 flow_dv_tag_resource_register
6728                         (struct rte_eth_dev *dev,
6729                          uint32_t tag_be24,
6730                          struct mlx5_flow *dev_flow,
6731                          struct rte_flow_error *error)
6732 {
6733         struct mlx5_priv *priv = dev->data->dev_private;
6734         struct mlx5_ibv_shared *sh = priv->sh;
6735         struct mlx5_flow_dv_tag_resource *cache_resource;
6736         struct mlx5_hlist_entry *entry;
6737
6738         /* Lookup a matching resource from cache. */
6739         entry = mlx5_hlist_lookup(sh->tag_table, (uint64_t)tag_be24);
6740         if (entry) {
6741                 cache_resource = container_of
6742                         (entry, struct mlx5_flow_dv_tag_resource, entry);
6743                 rte_atomic32_inc(&cache_resource->refcnt);
6744                 dev_flow->dv.tag_resource = cache_resource;
6745                 DRV_LOG(DEBUG, "cached tag resource %p: refcnt now %d++",
6746                         (void *)cache_resource,
6747                         rte_atomic32_read(&cache_resource->refcnt));
6748                 return 0;
6749         }
6750         /* Register new resource. */
6751         cache_resource = rte_calloc(__func__, 1, sizeof(*cache_resource), 0);
6752         if (!cache_resource)
6753                 return rte_flow_error_set(error, ENOMEM,
6754                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
6755                                           "cannot allocate resource memory");
6756         cache_resource->entry.key = (uint64_t)tag_be24;
6757         cache_resource->action = mlx5_glue->dv_create_flow_action_tag(tag_be24);
6758         if (!cache_resource->action) {
6759                 rte_free(cache_resource);
6760                 return rte_flow_error_set(error, ENOMEM,
6761                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
6762                                           NULL, "cannot create action");
6763         }
6764         rte_atomic32_init(&cache_resource->refcnt);
6765         rte_atomic32_inc(&cache_resource->refcnt);
6766         if (mlx5_hlist_insert(sh->tag_table, &cache_resource->entry)) {
6767                 mlx5_glue->destroy_flow_action(cache_resource->action);
6768                 rte_free(cache_resource);
6769                 return rte_flow_error_set(error, EEXIST,
6770                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
6771                                           NULL, "cannot insert tag");
6772         }
6773         dev_flow->dv.tag_resource = cache_resource;
6774         DRV_LOG(DEBUG, "new tag resource %p: refcnt now %d++",
6775                 (void *)cache_resource,
6776                 rte_atomic32_read(&cache_resource->refcnt));
6777         return 0;
6778 }
6779
6780 /**
6781  * Release the tag.
6782  *
6783  * @param dev
6784  *   Pointer to Ethernet device.
6785  * @param flow
6786  *   Pointer to mlx5_flow.
6787  *
6788  * @return
6789  *   1 while a reference on it exists, 0 when freed.
6790  */
6791 static int
6792 flow_dv_tag_release(struct rte_eth_dev *dev,
6793                     struct mlx5_flow_dv_tag_resource *tag)
6794 {
6795         struct mlx5_priv *priv = dev->data->dev_private;
6796         struct mlx5_ibv_shared *sh = priv->sh;
6797
6798         assert(tag);
6799         DRV_LOG(DEBUG, "port %u tag %p: refcnt %d--",
6800                 dev->data->port_id, (void *)tag,
6801                 rte_atomic32_read(&tag->refcnt));
6802         if (rte_atomic32_dec_and_test(&tag->refcnt)) {
6803                 claim_zero(mlx5_glue->destroy_flow_action(tag->action));
6804                 mlx5_hlist_remove(sh->tag_table, &tag->entry);
6805                 DRV_LOG(DEBUG, "port %u tag %p: removed",
6806                         dev->data->port_id, (void *)tag);
6807                 rte_free(tag);
6808                 return 0;
6809         }
6810         return 1;
6811 }
6812
6813 /**
6814  * Translate port ID action to vport.
6815  *
6816  * @param[in] dev
6817  *   Pointer to rte_eth_dev structure.
6818  * @param[in] action
6819  *   Pointer to the port ID action.
6820  * @param[out] dst_port_id
6821  *   The target port ID.
6822  * @param[out] error
6823  *   Pointer to the error structure.
6824  *
6825  * @return
6826  *   0 on success, a negative errno value otherwise and rte_errno is set.
6827  */
6828 static int
6829 flow_dv_translate_action_port_id(struct rte_eth_dev *dev,
6830                                  const struct rte_flow_action *action,
6831                                  uint32_t *dst_port_id,
6832                                  struct rte_flow_error *error)
6833 {
6834         uint32_t port;
6835         struct mlx5_priv *priv;
6836         const struct rte_flow_action_port_id *conf =
6837                         (const struct rte_flow_action_port_id *)action->conf;
6838
6839         port = conf->original ? dev->data->port_id : conf->id;
6840         priv = mlx5_port_to_eswitch_info(port, false);
6841         if (!priv)
6842                 return rte_flow_error_set(error, -rte_errno,
6843                                           RTE_FLOW_ERROR_TYPE_ACTION,
6844                                           NULL,
6845                                           "No eswitch info was found for port");
6846 #ifdef HAVE_MLX5DV_DR_DEVX_PORT
6847         /*
6848          * This parameter is transferred to
6849          * mlx5dv_dr_action_create_dest_ib_port().
6850          */
6851         *dst_port_id = priv->ibv_port;
6852 #else
6853         /*
6854          * Legacy mode, no LAG configurations is supported.
6855          * This parameter is transferred to
6856          * mlx5dv_dr_action_create_dest_vport().
6857          */
6858         *dst_port_id = priv->vport_id;
6859 #endif
6860         return 0;
6861 }
6862
6863 /**
6864  * Add Tx queue matcher
6865  *
6866  * @param[in] dev
6867  *   Pointer to the dev struct.
6868  * @param[in, out] matcher
6869  *   Flow matcher.
6870  * @param[in, out] key
6871  *   Flow matcher value.
6872  * @param[in] item
6873  *   Flow pattern to translate.
6874  * @param[in] inner
6875  *   Item is inner pattern.
6876  */
6877 static void
6878 flow_dv_translate_item_tx_queue(struct rte_eth_dev *dev,
6879                                 void *matcher, void *key,
6880                                 const struct rte_flow_item *item)
6881 {
6882         const struct mlx5_rte_flow_item_tx_queue *queue_m;
6883         const struct mlx5_rte_flow_item_tx_queue *queue_v;
6884         void *misc_m =
6885                 MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
6886         void *misc_v =
6887                 MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
6888         struct mlx5_txq_ctrl *txq;
6889         uint32_t queue;
6890
6891
6892         queue_m = (const void *)item->mask;
6893         if (!queue_m)
6894                 return;
6895         queue_v = (const void *)item->spec;
6896         if (!queue_v)
6897                 return;
6898         txq = mlx5_txq_get(dev, queue_v->queue);
6899         if (!txq)
6900                 return;
6901         queue = txq->obj->sq->id;
6902         MLX5_SET(fte_match_set_misc, misc_m, source_sqn, queue_m->queue);
6903         MLX5_SET(fte_match_set_misc, misc_v, source_sqn,
6904                  queue & queue_m->queue);
6905         mlx5_txq_release(dev, queue_v->queue);
6906 }
6907
6908 /**
6909  * Set the hash fields according to the @p flow information.
6910  *
6911  * @param[in] dev_flow
6912  *   Pointer to the mlx5_flow.
6913  */
6914 static void
6915 flow_dv_hashfields_set(struct mlx5_flow *dev_flow)
6916 {
6917         struct rte_flow *flow = dev_flow->flow;
6918         uint64_t items = dev_flow->layers;
6919         int rss_inner = 0;
6920         uint64_t rss_types = rte_eth_rss_hf_refine(flow->rss.types);
6921
6922         dev_flow->hash_fields = 0;
6923 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
6924         if (flow->rss.level >= 2) {
6925                 dev_flow->hash_fields |= IBV_RX_HASH_INNER;
6926                 rss_inner = 1;
6927         }
6928 #endif
6929         if ((rss_inner && (items & MLX5_FLOW_LAYER_INNER_L3_IPV4)) ||
6930             (!rss_inner && (items & MLX5_FLOW_LAYER_OUTER_L3_IPV4))) {
6931                 if (rss_types & MLX5_IPV4_LAYER_TYPES) {
6932                         if (rss_types & ETH_RSS_L3_SRC_ONLY)
6933                                 dev_flow->hash_fields |= IBV_RX_HASH_SRC_IPV4;
6934                         else if (rss_types & ETH_RSS_L3_DST_ONLY)
6935                                 dev_flow->hash_fields |= IBV_RX_HASH_DST_IPV4;
6936                         else
6937                                 dev_flow->hash_fields |= MLX5_IPV4_IBV_RX_HASH;
6938                 }
6939         } else if ((rss_inner && (items & MLX5_FLOW_LAYER_INNER_L3_IPV6)) ||
6940                    (!rss_inner && (items & MLX5_FLOW_LAYER_OUTER_L3_IPV6))) {
6941                 if (rss_types & MLX5_IPV6_LAYER_TYPES) {
6942                         if (rss_types & ETH_RSS_L3_SRC_ONLY)
6943                                 dev_flow->hash_fields |= IBV_RX_HASH_SRC_IPV6;
6944                         else if (rss_types & ETH_RSS_L3_DST_ONLY)
6945                                 dev_flow->hash_fields |= IBV_RX_HASH_DST_IPV6;
6946                         else
6947                                 dev_flow->hash_fields |= MLX5_IPV6_IBV_RX_HASH;
6948                 }
6949         }
6950         if ((rss_inner && (items & MLX5_FLOW_LAYER_INNER_L4_UDP)) ||
6951             (!rss_inner && (items & MLX5_FLOW_LAYER_OUTER_L4_UDP))) {
6952                 if (rss_types & ETH_RSS_UDP) {
6953                         if (rss_types & ETH_RSS_L4_SRC_ONLY)
6954                                 dev_flow->hash_fields |=
6955                                                 IBV_RX_HASH_SRC_PORT_UDP;
6956                         else if (rss_types & ETH_RSS_L4_DST_ONLY)
6957                                 dev_flow->hash_fields |=
6958                                                 IBV_RX_HASH_DST_PORT_UDP;
6959                         else
6960                                 dev_flow->hash_fields |= MLX5_UDP_IBV_RX_HASH;
6961                 }
6962         } else if ((rss_inner && (items & MLX5_FLOW_LAYER_INNER_L4_TCP)) ||
6963                    (!rss_inner && (items & MLX5_FLOW_LAYER_OUTER_L4_TCP))) {
6964                 if (rss_types & ETH_RSS_TCP) {
6965                         if (rss_types & ETH_RSS_L4_SRC_ONLY)
6966                                 dev_flow->hash_fields |=
6967                                                 IBV_RX_HASH_SRC_PORT_TCP;
6968                         else if (rss_types & ETH_RSS_L4_DST_ONLY)
6969                                 dev_flow->hash_fields |=
6970                                                 IBV_RX_HASH_DST_PORT_TCP;
6971                         else
6972                                 dev_flow->hash_fields |= MLX5_TCP_IBV_RX_HASH;
6973                 }
6974         }
6975 }
6976
6977 /**
6978  * Fill the flow with DV spec, lock free
6979  * (mutex should be acquired by caller).
6980  *
6981  * @param[in] dev
6982  *   Pointer to rte_eth_dev structure.
6983  * @param[in, out] dev_flow
6984  *   Pointer to the sub flow.
6985  * @param[in] attr
6986  *   Pointer to the flow attributes.
6987  * @param[in] items
6988  *   Pointer to the list of items.
6989  * @param[in] actions
6990  *   Pointer to the list of actions.
6991  * @param[out] error
6992  *   Pointer to the error structure.
6993  *
6994  * @return
6995  *   0 on success, a negative errno value otherwise and rte_errno is set.
6996  */
6997 static int
6998 __flow_dv_translate(struct rte_eth_dev *dev,
6999                     struct mlx5_flow *dev_flow,
7000                     const struct rte_flow_attr *attr,
7001                     const struct rte_flow_item items[],
7002                     const struct rte_flow_action actions[],
7003                     struct rte_flow_error *error)
7004 {
7005         struct mlx5_priv *priv = dev->data->dev_private;
7006         struct mlx5_dev_config *dev_conf = &priv->config;
7007         struct rte_flow *flow = dev_flow->flow;
7008         uint64_t item_flags = 0;
7009         uint64_t last_item = 0;
7010         uint64_t action_flags = 0;
7011         uint64_t priority = attr->priority;
7012         struct mlx5_flow_dv_matcher matcher = {
7013                 .mask = {
7014                         .size = sizeof(matcher.mask.buf),
7015                 },
7016         };
7017         int actions_n = 0;
7018         bool actions_end = false;
7019         union {
7020                 struct mlx5_flow_dv_modify_hdr_resource res;
7021                 uint8_t len[sizeof(struct mlx5_flow_dv_modify_hdr_resource) +
7022                             sizeof(struct mlx5_modification_cmd) *
7023                             (MLX5_MAX_MODIFY_NUM + 1)];
7024         } mhdr_dummy;
7025         struct mlx5_flow_dv_modify_hdr_resource *mhdr_res = &mhdr_dummy.res;
7026         union flow_dv_attr flow_attr = { .attr = 0 };
7027         uint32_t tag_be;
7028         union mlx5_flow_tbl_key tbl_key;
7029         uint32_t modify_action_position = UINT32_MAX;
7030         void *match_mask = matcher.mask.buf;
7031         void *match_value = dev_flow->dv.value.buf;
7032         uint8_t next_protocol = 0xff;
7033         struct rte_vlan_hdr vlan = { 0 };
7034         uint32_t table;
7035         int ret = 0;
7036
7037         mhdr_res->ft_type = attr->egress ? MLX5DV_FLOW_TABLE_TYPE_NIC_TX :
7038                                            MLX5DV_FLOW_TABLE_TYPE_NIC_RX;
7039         ret = mlx5_flow_group_to_table(attr, dev_flow->external, attr->group,
7040                                        &table, error);
7041         if (ret)
7042                 return ret;
7043         dev_flow->group = table;
7044         if (attr->transfer)
7045                 mhdr_res->ft_type = MLX5DV_FLOW_TABLE_TYPE_FDB;
7046         if (priority == MLX5_FLOW_PRIO_RSVD)
7047                 priority = dev_conf->flow_prio - 1;
7048         /* number of actions must be set to 0 in case of dirty stack. */
7049         mhdr_res->actions_num = 0;
7050         for (; !actions_end ; actions++) {
7051                 const struct rte_flow_action_queue *queue;
7052                 const struct rte_flow_action_rss *rss;
7053                 const struct rte_flow_action *action = actions;
7054                 const struct rte_flow_action_count *count = action->conf;
7055                 const uint8_t *rss_key;
7056                 const struct rte_flow_action_jump *jump_data;
7057                 const struct rte_flow_action_meter *mtr;
7058                 struct mlx5_flow_tbl_resource *tbl;
7059                 uint32_t port_id = 0;
7060                 struct mlx5_flow_dv_port_id_action_resource port_id_resource;
7061                 int action_type = actions->type;
7062                 const struct rte_flow_action *found_action = NULL;
7063
7064                 switch (action_type) {
7065                 case RTE_FLOW_ACTION_TYPE_VOID:
7066                         break;
7067                 case RTE_FLOW_ACTION_TYPE_PORT_ID:
7068                         if (flow_dv_translate_action_port_id(dev, action,
7069                                                              &port_id, error))
7070                                 return -rte_errno;
7071                         port_id_resource.port_id = port_id;
7072                         if (flow_dv_port_id_action_resource_register
7073                             (dev, &port_id_resource, dev_flow, error))
7074                                 return -rte_errno;
7075                         dev_flow->dv.actions[actions_n++] =
7076                                 dev_flow->dv.port_id_action->action;
7077                         action_flags |= MLX5_FLOW_ACTION_PORT_ID;
7078                         break;
7079                 case RTE_FLOW_ACTION_TYPE_FLAG:
7080                         action_flags |= MLX5_FLOW_ACTION_FLAG;
7081                         if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
7082                                 struct rte_flow_action_mark mark = {
7083                                         .id = MLX5_FLOW_MARK_DEFAULT,
7084                                 };
7085
7086                                 if (flow_dv_convert_action_mark(dev, &mark,
7087                                                                 mhdr_res,
7088                                                                 error))
7089                                         return -rte_errno;
7090                                 action_flags |= MLX5_FLOW_ACTION_MARK_EXT;
7091                                 break;
7092                         }
7093                         tag_be = mlx5_flow_mark_set(MLX5_FLOW_MARK_DEFAULT);
7094                         if (!dev_flow->dv.tag_resource)
7095                                 if (flow_dv_tag_resource_register
7096                                     (dev, tag_be, dev_flow, error))
7097                                         return -rte_errno;
7098                         dev_flow->dv.actions[actions_n++] =
7099                                 dev_flow->dv.tag_resource->action;
7100                         break;
7101                 case RTE_FLOW_ACTION_TYPE_MARK:
7102                         action_flags |= MLX5_FLOW_ACTION_MARK;
7103                         if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
7104                                 const struct rte_flow_action_mark *mark =
7105                                         (const struct rte_flow_action_mark *)
7106                                                 actions->conf;
7107
7108                                 if (flow_dv_convert_action_mark(dev, mark,
7109                                                                 mhdr_res,
7110                                                                 error))
7111                                         return -rte_errno;
7112                                 action_flags |= MLX5_FLOW_ACTION_MARK_EXT;
7113                                 break;
7114                         }
7115                         /* Fall-through */
7116                 case MLX5_RTE_FLOW_ACTION_TYPE_MARK:
7117                         /* Legacy (non-extensive) MARK action. */
7118                         tag_be = mlx5_flow_mark_set
7119                               (((const struct rte_flow_action_mark *)
7120                                (actions->conf))->id);
7121                         if (!dev_flow->dv.tag_resource)
7122                                 if (flow_dv_tag_resource_register
7123                                     (dev, tag_be, dev_flow, error))
7124                                         return -rte_errno;
7125                         dev_flow->dv.actions[actions_n++] =
7126                                 dev_flow->dv.tag_resource->action;
7127                         break;
7128                 case RTE_FLOW_ACTION_TYPE_SET_META:
7129                         if (flow_dv_convert_action_set_meta
7130                                 (dev, mhdr_res, attr,
7131                                  (const struct rte_flow_action_set_meta *)
7132                                   actions->conf, error))
7133                                 return -rte_errno;
7134                         action_flags |= MLX5_FLOW_ACTION_SET_META;
7135                         break;
7136                 case RTE_FLOW_ACTION_TYPE_SET_TAG:
7137                         if (flow_dv_convert_action_set_tag
7138                                 (dev, mhdr_res,
7139                                  (const struct rte_flow_action_set_tag *)
7140                                   actions->conf, error))
7141                                 return -rte_errno;
7142                         action_flags |= MLX5_FLOW_ACTION_SET_TAG;
7143                         break;
7144                 case RTE_FLOW_ACTION_TYPE_DROP:
7145                         action_flags |= MLX5_FLOW_ACTION_DROP;
7146                         break;
7147                 case RTE_FLOW_ACTION_TYPE_QUEUE:
7148                         assert(flow->rss.queue);
7149                         queue = actions->conf;
7150                         flow->rss.queue_num = 1;
7151                         (*flow->rss.queue)[0] = queue->index;
7152                         action_flags |= MLX5_FLOW_ACTION_QUEUE;
7153                         break;
7154                 case RTE_FLOW_ACTION_TYPE_RSS:
7155                         assert(flow->rss.queue);
7156                         rss = actions->conf;
7157                         if (flow->rss.queue)
7158                                 memcpy((*flow->rss.queue), rss->queue,
7159                                        rss->queue_num * sizeof(uint16_t));
7160                         flow->rss.queue_num = rss->queue_num;
7161                         /* NULL RSS key indicates default RSS key. */
7162                         rss_key = !rss->key ? rss_hash_default_key : rss->key;
7163                         memcpy(flow->rss.key, rss_key, MLX5_RSS_HASH_KEY_LEN);
7164                         /*
7165                          * rss->level and rss.types should be set in advance
7166                          * when expanding items for RSS.
7167                          */
7168                         action_flags |= MLX5_FLOW_ACTION_RSS;
7169                         break;
7170                 case RTE_FLOW_ACTION_TYPE_COUNT:
7171                         if (!dev_conf->devx) {
7172                                 rte_errno = ENOTSUP;
7173                                 goto cnt_err;
7174                         }
7175                         flow->counter = flow_dv_counter_alloc(dev,
7176                                                               count->shared,
7177                                                               count->id,
7178                                                               dev_flow->group);
7179                         if (flow->counter == NULL)
7180                                 goto cnt_err;
7181                         dev_flow->dv.actions[actions_n++] =
7182                                 flow->counter->action;
7183                         action_flags |= MLX5_FLOW_ACTION_COUNT;
7184                         break;
7185 cnt_err:
7186                         if (rte_errno == ENOTSUP)
7187                                 return rte_flow_error_set
7188                                               (error, ENOTSUP,
7189                                                RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
7190                                                NULL,
7191                                                "count action not supported");
7192                         else
7193                                 return rte_flow_error_set
7194                                                 (error, rte_errno,
7195                                                  RTE_FLOW_ERROR_TYPE_ACTION,
7196                                                  action,
7197                                                  "cannot create counter"
7198                                                   " object.");
7199                         break;
7200                 case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
7201                         dev_flow->dv.actions[actions_n++] =
7202                                                 priv->sh->pop_vlan_action;
7203                         action_flags |= MLX5_FLOW_ACTION_OF_POP_VLAN;
7204                         break;
7205                 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
7206                         flow_dev_get_vlan_info_from_items(items, &vlan);
7207                         vlan.eth_proto = rte_be_to_cpu_16
7208                              ((((const struct rte_flow_action_of_push_vlan *)
7209                                                    actions->conf)->ethertype));
7210                         found_action = mlx5_flow_find_action
7211                                         (actions + 1,
7212                                          RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID);
7213                         if (found_action)
7214                                 mlx5_update_vlan_vid_pcp(found_action, &vlan);
7215                         found_action = mlx5_flow_find_action
7216                                         (actions + 1,
7217                                          RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP);
7218                         if (found_action)
7219                                 mlx5_update_vlan_vid_pcp(found_action, &vlan);
7220                         if (flow_dv_create_action_push_vlan
7221                                             (dev, attr, &vlan, dev_flow, error))
7222                                 return -rte_errno;
7223                         dev_flow->dv.actions[actions_n++] =
7224                                            dev_flow->dv.push_vlan_res->action;
7225                         action_flags |= MLX5_FLOW_ACTION_OF_PUSH_VLAN;
7226                         break;
7227                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
7228                         /* of_vlan_push action handled this action */
7229                         assert(action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN);
7230                         break;
7231                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
7232                         if (action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN)
7233                                 break;
7234                         flow_dev_get_vlan_info_from_items(items, &vlan);
7235                         mlx5_update_vlan_vid_pcp(actions, &vlan);
7236                         /* If no VLAN push - this is a modify header action */
7237                         if (flow_dv_convert_action_modify_vlan_vid
7238                                                 (mhdr_res, actions, error))
7239                                 return -rte_errno;
7240                         action_flags |= MLX5_FLOW_ACTION_OF_SET_VLAN_VID;
7241                         break;
7242                 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
7243                 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
7244                         if (flow_dv_create_action_l2_encap(dev, actions,
7245                                                            dev_flow,
7246                                                            attr->transfer,
7247                                                            error))
7248                                 return -rte_errno;
7249                         dev_flow->dv.actions[actions_n++] =
7250                                 dev_flow->dv.encap_decap->verbs_action;
7251                         action_flags |= actions->type ==
7252                                         RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP ?
7253                                         MLX5_FLOW_ACTION_VXLAN_ENCAP :
7254                                         MLX5_FLOW_ACTION_NVGRE_ENCAP;
7255                         break;
7256                 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
7257                 case RTE_FLOW_ACTION_TYPE_NVGRE_DECAP:
7258                         if (flow_dv_create_action_l2_decap(dev, dev_flow,
7259                                                            attr->transfer,
7260                                                            error))
7261                                 return -rte_errno;
7262                         dev_flow->dv.actions[actions_n++] =
7263                                 dev_flow->dv.encap_decap->verbs_action;
7264                         action_flags |= actions->type ==
7265                                         RTE_FLOW_ACTION_TYPE_VXLAN_DECAP ?
7266                                         MLX5_FLOW_ACTION_VXLAN_DECAP :
7267                                         MLX5_FLOW_ACTION_NVGRE_DECAP;
7268                         break;
7269                 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
7270                         /* Handle encap with preceding decap. */
7271                         if (action_flags & MLX5_FLOW_ACTION_RAW_DECAP) {
7272                                 if (flow_dv_create_action_raw_encap
7273                                         (dev, actions, dev_flow, attr, error))
7274                                         return -rte_errno;
7275                                 dev_flow->dv.actions[actions_n++] =
7276                                         dev_flow->dv.encap_decap->verbs_action;
7277                         } else {
7278                                 /* Handle encap without preceding decap. */
7279                                 if (flow_dv_create_action_l2_encap
7280                                     (dev, actions, dev_flow, attr->transfer,
7281                                      error))
7282                                         return -rte_errno;
7283                                 dev_flow->dv.actions[actions_n++] =
7284                                         dev_flow->dv.encap_decap->verbs_action;
7285                         }
7286                         action_flags |= MLX5_FLOW_ACTION_RAW_ENCAP;
7287                         break;
7288                 case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
7289                         /* Check if this decap is followed by encap. */
7290                         for (; action->type != RTE_FLOW_ACTION_TYPE_END &&
7291                                action->type != RTE_FLOW_ACTION_TYPE_RAW_ENCAP;
7292                                action++) {
7293                         }
7294                         /* Handle decap only if it isn't followed by encap. */
7295                         if (action->type != RTE_FLOW_ACTION_TYPE_RAW_ENCAP) {
7296                                 if (flow_dv_create_action_l2_decap
7297                                     (dev, dev_flow, attr->transfer, error))
7298                                         return -rte_errno;
7299                                 dev_flow->dv.actions[actions_n++] =
7300                                         dev_flow->dv.encap_decap->verbs_action;
7301                         }
7302                         /* If decap is followed by encap, handle it at encap. */
7303                         action_flags |= MLX5_FLOW_ACTION_RAW_DECAP;
7304                         break;
7305                 case RTE_FLOW_ACTION_TYPE_JUMP:
7306                         jump_data = action->conf;
7307                         ret = mlx5_flow_group_to_table(attr, dev_flow->external,
7308                                                        jump_data->group, &table,
7309                                                        error);
7310                         if (ret)
7311                                 return ret;
7312                         tbl = flow_dv_tbl_resource_get(dev, table,
7313                                                        attr->egress,
7314                                                        attr->transfer, error);
7315                         if (!tbl)
7316                                 return rte_flow_error_set
7317                                                 (error, errno,
7318                                                  RTE_FLOW_ERROR_TYPE_ACTION,
7319                                                  NULL,
7320                                                  "cannot create jump action.");
7321                         if (flow_dv_jump_tbl_resource_register
7322                             (dev, tbl, dev_flow, error)) {
7323                                 flow_dv_tbl_resource_release(dev, tbl);
7324                                 return rte_flow_error_set
7325                                                 (error, errno,
7326                                                  RTE_FLOW_ERROR_TYPE_ACTION,
7327                                                  NULL,
7328                                                  "cannot create jump action.");
7329                         }
7330                         dev_flow->dv.actions[actions_n++] =
7331                                 dev_flow->dv.jump->action;
7332                         action_flags |= MLX5_FLOW_ACTION_JUMP;
7333                         break;
7334                 case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
7335                 case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
7336                         if (flow_dv_convert_action_modify_mac
7337                                         (mhdr_res, actions, error))
7338                                 return -rte_errno;
7339                         action_flags |= actions->type ==
7340                                         RTE_FLOW_ACTION_TYPE_SET_MAC_SRC ?
7341                                         MLX5_FLOW_ACTION_SET_MAC_SRC :
7342                                         MLX5_FLOW_ACTION_SET_MAC_DST;
7343                         break;
7344                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
7345                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
7346                         if (flow_dv_convert_action_modify_ipv4
7347                                         (mhdr_res, actions, error))
7348                                 return -rte_errno;
7349                         action_flags |= actions->type ==
7350                                         RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC ?
7351                                         MLX5_FLOW_ACTION_SET_IPV4_SRC :
7352                                         MLX5_FLOW_ACTION_SET_IPV4_DST;
7353                         break;
7354                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
7355                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
7356                         if (flow_dv_convert_action_modify_ipv6
7357                                         (mhdr_res, actions, error))
7358                                 return -rte_errno;
7359                         action_flags |= actions->type ==
7360                                         RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC ?
7361                                         MLX5_FLOW_ACTION_SET_IPV6_SRC :
7362                                         MLX5_FLOW_ACTION_SET_IPV6_DST;
7363                         break;
7364                 case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
7365                 case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
7366                         if (flow_dv_convert_action_modify_tp
7367                                         (mhdr_res, actions, items,
7368                                          &flow_attr, error))
7369                                 return -rte_errno;
7370                         action_flags |= actions->type ==
7371                                         RTE_FLOW_ACTION_TYPE_SET_TP_SRC ?
7372                                         MLX5_FLOW_ACTION_SET_TP_SRC :
7373                                         MLX5_FLOW_ACTION_SET_TP_DST;
7374                         break;
7375                 case RTE_FLOW_ACTION_TYPE_DEC_TTL:
7376                         if (flow_dv_convert_action_modify_dec_ttl
7377                                         (mhdr_res, items, &flow_attr, error))
7378                                 return -rte_errno;
7379                         action_flags |= MLX5_FLOW_ACTION_DEC_TTL;
7380                         break;
7381                 case RTE_FLOW_ACTION_TYPE_SET_TTL:
7382                         if (flow_dv_convert_action_modify_ttl
7383                                         (mhdr_res, actions, items,
7384                                          &flow_attr, error))
7385                                 return -rte_errno;
7386                         action_flags |= MLX5_FLOW_ACTION_SET_TTL;
7387                         break;
7388                 case RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ:
7389                 case RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ:
7390                         if (flow_dv_convert_action_modify_tcp_seq
7391                                         (mhdr_res, actions, error))
7392                                 return -rte_errno;
7393                         action_flags |= actions->type ==
7394                                         RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ ?
7395                                         MLX5_FLOW_ACTION_INC_TCP_SEQ :
7396                                         MLX5_FLOW_ACTION_DEC_TCP_SEQ;
7397                         break;
7398
7399                 case RTE_FLOW_ACTION_TYPE_INC_TCP_ACK:
7400                 case RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK:
7401                         if (flow_dv_convert_action_modify_tcp_ack
7402                                         (mhdr_res, actions, error))
7403                                 return -rte_errno;
7404                         action_flags |= actions->type ==
7405                                         RTE_FLOW_ACTION_TYPE_INC_TCP_ACK ?
7406                                         MLX5_FLOW_ACTION_INC_TCP_ACK :
7407                                         MLX5_FLOW_ACTION_DEC_TCP_ACK;
7408                         break;
7409                 case MLX5_RTE_FLOW_ACTION_TYPE_TAG:
7410                         if (flow_dv_convert_action_set_reg
7411                                         (mhdr_res, actions, error))
7412                                 return -rte_errno;
7413                         action_flags |= MLX5_FLOW_ACTION_SET_TAG;
7414                         break;
7415                 case MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG:
7416                         if (flow_dv_convert_action_copy_mreg
7417                                         (dev, mhdr_res, actions, error))
7418                                 return -rte_errno;
7419                         action_flags |= MLX5_FLOW_ACTION_SET_TAG;
7420                         break;
7421                 case RTE_FLOW_ACTION_TYPE_METER:
7422                         mtr = actions->conf;
7423                         if (!flow->meter) {
7424                                 flow->meter = mlx5_flow_meter_attach(priv,
7425                                                         mtr->mtr_id, attr,
7426                                                         error);
7427                                 if (!flow->meter)
7428                                         return rte_flow_error_set(error,
7429                                                 rte_errno,
7430                                                 RTE_FLOW_ERROR_TYPE_ACTION,
7431                                                 NULL,
7432                                                 "meter not found "
7433                                                 "or invalid parameters");
7434                         }
7435                         /* Set the meter action. */
7436                         dev_flow->dv.actions[actions_n++] =
7437                                 flow->meter->mfts->meter_action;
7438                         action_flags |= MLX5_FLOW_ACTION_METER;
7439                         break;
7440                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DSCP:
7441                         if (flow_dv_convert_action_modify_ipv4_dscp(mhdr_res,
7442                                                               actions, error))
7443                                 return -rte_errno;
7444                         action_flags |= MLX5_FLOW_ACTION_SET_IPV4_DSCP;
7445                         break;
7446                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DSCP:
7447                         if (flow_dv_convert_action_modify_ipv6_dscp(mhdr_res,
7448                                                               actions, error))
7449                                 return -rte_errno;
7450                         action_flags |= MLX5_FLOW_ACTION_SET_IPV6_DSCP;
7451                         break;
7452                 case RTE_FLOW_ACTION_TYPE_END:
7453                         actions_end = true;
7454                         if (mhdr_res->actions_num) {
7455                                 /* create modify action if needed. */
7456                                 if (flow_dv_modify_hdr_resource_register
7457                                         (dev, mhdr_res, dev_flow, error))
7458                                         return -rte_errno;
7459                                 dev_flow->dv.actions[modify_action_position] =
7460                                         dev_flow->dv.modify_hdr->verbs_action;
7461                         }
7462                         break;
7463                 default:
7464                         break;
7465                 }
7466                 if (mhdr_res->actions_num &&
7467                     modify_action_position == UINT32_MAX)
7468                         modify_action_position = actions_n++;
7469         }
7470         dev_flow->dv.actions_n = actions_n;
7471         dev_flow->actions = action_flags;
7472         for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
7473                 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
7474                 int item_type = items->type;
7475
7476                 switch (item_type) {
7477                 case RTE_FLOW_ITEM_TYPE_PORT_ID:
7478                         flow_dv_translate_item_port_id(dev, match_mask,
7479                                                        match_value, items);
7480                         last_item = MLX5_FLOW_ITEM_PORT_ID;
7481                         break;
7482                 case RTE_FLOW_ITEM_TYPE_ETH:
7483                         flow_dv_translate_item_eth(match_mask, match_value,
7484                                                    items, tunnel);
7485                         matcher.priority = MLX5_PRIORITY_MAP_L2;
7486                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
7487                                              MLX5_FLOW_LAYER_OUTER_L2;
7488                         break;
7489                 case RTE_FLOW_ITEM_TYPE_VLAN:
7490                         flow_dv_translate_item_vlan(dev_flow,
7491                                                     match_mask, match_value,
7492                                                     items, tunnel);
7493                         matcher.priority = MLX5_PRIORITY_MAP_L2;
7494                         last_item = tunnel ? (MLX5_FLOW_LAYER_INNER_L2 |
7495                                               MLX5_FLOW_LAYER_INNER_VLAN) :
7496                                              (MLX5_FLOW_LAYER_OUTER_L2 |
7497                                               MLX5_FLOW_LAYER_OUTER_VLAN);
7498                         break;
7499                 case RTE_FLOW_ITEM_TYPE_IPV4:
7500                         mlx5_flow_tunnel_ip_check(items, next_protocol,
7501                                                   &item_flags, &tunnel);
7502                         flow_dv_translate_item_ipv4(match_mask, match_value,
7503                                                     items, tunnel,
7504                                                     dev_flow->group);
7505                         matcher.priority = MLX5_PRIORITY_MAP_L3;
7506                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
7507                                              MLX5_FLOW_LAYER_OUTER_L3_IPV4;
7508                         if (items->mask != NULL &&
7509                             ((const struct rte_flow_item_ipv4 *)
7510                              items->mask)->hdr.next_proto_id) {
7511                                 next_protocol =
7512                                         ((const struct rte_flow_item_ipv4 *)
7513                                          (items->spec))->hdr.next_proto_id;
7514                                 next_protocol &=
7515                                         ((const struct rte_flow_item_ipv4 *)
7516                                          (items->mask))->hdr.next_proto_id;
7517                         } else {
7518                                 /* Reset for inner layer. */
7519                                 next_protocol = 0xff;
7520                         }
7521                         break;
7522                 case RTE_FLOW_ITEM_TYPE_IPV6:
7523                         mlx5_flow_tunnel_ip_check(items, next_protocol,
7524                                                   &item_flags, &tunnel);
7525                         flow_dv_translate_item_ipv6(match_mask, match_value,
7526                                                     items, tunnel,
7527                                                     dev_flow->group);
7528                         matcher.priority = MLX5_PRIORITY_MAP_L3;
7529                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
7530                                              MLX5_FLOW_LAYER_OUTER_L3_IPV6;
7531                         if (items->mask != NULL &&
7532                             ((const struct rte_flow_item_ipv6 *)
7533                              items->mask)->hdr.proto) {
7534                                 next_protocol =
7535                                         ((const struct rte_flow_item_ipv6 *)
7536                                          items->spec)->hdr.proto;
7537                                 next_protocol &=
7538                                         ((const struct rte_flow_item_ipv6 *)
7539                                          items->mask)->hdr.proto;
7540                         } else {
7541                                 /* Reset for inner layer. */
7542                                 next_protocol = 0xff;
7543                         }
7544                         break;
7545                 case RTE_FLOW_ITEM_TYPE_TCP:
7546                         flow_dv_translate_item_tcp(match_mask, match_value,
7547                                                    items, tunnel);
7548                         matcher.priority = MLX5_PRIORITY_MAP_L4;
7549                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
7550                                              MLX5_FLOW_LAYER_OUTER_L4_TCP;
7551                         break;
7552                 case RTE_FLOW_ITEM_TYPE_UDP:
7553                         flow_dv_translate_item_udp(match_mask, match_value,
7554                                                    items, tunnel);
7555                         matcher.priority = MLX5_PRIORITY_MAP_L4;
7556                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
7557                                              MLX5_FLOW_LAYER_OUTER_L4_UDP;
7558                         break;
7559                 case RTE_FLOW_ITEM_TYPE_GRE:
7560                         flow_dv_translate_item_gre(match_mask, match_value,
7561                                                    items, tunnel);
7562                         last_item = MLX5_FLOW_LAYER_GRE;
7563                         break;
7564                 case RTE_FLOW_ITEM_TYPE_GRE_KEY:
7565                         flow_dv_translate_item_gre_key(match_mask,
7566                                                        match_value, items);
7567                         last_item = MLX5_FLOW_LAYER_GRE_KEY;
7568                         break;
7569                 case RTE_FLOW_ITEM_TYPE_NVGRE:
7570                         flow_dv_translate_item_nvgre(match_mask, match_value,
7571                                                      items, tunnel);
7572                         last_item = MLX5_FLOW_LAYER_GRE;
7573                         break;
7574                 case RTE_FLOW_ITEM_TYPE_VXLAN:
7575                         flow_dv_translate_item_vxlan(match_mask, match_value,
7576                                                      items, tunnel);
7577                         last_item = MLX5_FLOW_LAYER_VXLAN;
7578                         break;
7579                 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
7580                         flow_dv_translate_item_vxlan(match_mask, match_value,
7581                                                      items, tunnel);
7582                         last_item = MLX5_FLOW_LAYER_VXLAN_GPE;
7583                         break;
7584                 case RTE_FLOW_ITEM_TYPE_GENEVE:
7585                         flow_dv_translate_item_geneve(match_mask, match_value,
7586                                                       items, tunnel);
7587                         last_item = MLX5_FLOW_LAYER_GENEVE;
7588                         break;
7589                 case RTE_FLOW_ITEM_TYPE_MPLS:
7590                         flow_dv_translate_item_mpls(match_mask, match_value,
7591                                                     items, last_item, tunnel);
7592                         last_item = MLX5_FLOW_LAYER_MPLS;
7593                         break;
7594                 case RTE_FLOW_ITEM_TYPE_MARK:
7595                         flow_dv_translate_item_mark(dev, match_mask,
7596                                                     match_value, items);
7597                         last_item = MLX5_FLOW_ITEM_MARK;
7598                         break;
7599                 case RTE_FLOW_ITEM_TYPE_META:
7600                         flow_dv_translate_item_meta(dev, match_mask,
7601                                                     match_value, attr, items);
7602                         last_item = MLX5_FLOW_ITEM_METADATA;
7603                         break;
7604                 case RTE_FLOW_ITEM_TYPE_ICMP:
7605                         flow_dv_translate_item_icmp(match_mask, match_value,
7606                                                     items, tunnel);
7607                         last_item = MLX5_FLOW_LAYER_ICMP;
7608                         break;
7609                 case RTE_FLOW_ITEM_TYPE_ICMP6:
7610                         flow_dv_translate_item_icmp6(match_mask, match_value,
7611                                                       items, tunnel);
7612                         last_item = MLX5_FLOW_LAYER_ICMP6;
7613                         break;
7614                 case RTE_FLOW_ITEM_TYPE_TAG:
7615                         flow_dv_translate_item_tag(dev, match_mask,
7616                                                    match_value, items);
7617                         last_item = MLX5_FLOW_ITEM_TAG;
7618                         break;
7619                 case MLX5_RTE_FLOW_ITEM_TYPE_TAG:
7620                         flow_dv_translate_mlx5_item_tag(dev, match_mask,
7621                                                         match_value, items);
7622                         last_item = MLX5_FLOW_ITEM_TAG;
7623                         break;
7624                 case MLX5_RTE_FLOW_ITEM_TYPE_TX_QUEUE:
7625                         flow_dv_translate_item_tx_queue(dev, match_mask,
7626                                                         match_value,
7627                                                         items);
7628                         last_item = MLX5_FLOW_ITEM_TX_QUEUE;
7629                         break;
7630                 case RTE_FLOW_ITEM_TYPE_GTP:
7631                         flow_dv_translate_item_gtp(match_mask, match_value,
7632                                                    items, tunnel);
7633                         last_item = MLX5_FLOW_LAYER_GTP;
7634                         break;
7635                 default:
7636                         break;
7637                 }
7638                 item_flags |= last_item;
7639         }
7640         /*
7641          * In case of ingress traffic when E-Switch mode is enabled,
7642          * we have two cases where we need to set the source port manually.
7643          * The first one, is in case of Nic steering rule, and the second is
7644          * E-Switch rule where no port_id item was found. In both cases
7645          * the source port is set according the current port in use.
7646          */
7647         if ((attr->ingress && !(item_flags & MLX5_FLOW_ITEM_PORT_ID)) &&
7648             (priv->representor || priv->master)) {
7649                 if (flow_dv_translate_item_port_id(dev, match_mask,
7650                                                    match_value, NULL))
7651                         return -rte_errno;
7652         }
7653         assert(!flow_dv_check_valid_spec(matcher.mask.buf,
7654                                          dev_flow->dv.value.buf));
7655         dev_flow->layers = item_flags;
7656         if (action_flags & MLX5_FLOW_ACTION_RSS)
7657                 flow_dv_hashfields_set(dev_flow);
7658         /* Register matcher. */
7659         matcher.crc = rte_raw_cksum((const void *)matcher.mask.buf,
7660                                     matcher.mask.size);
7661         matcher.priority = mlx5_flow_adjust_priority(dev, priority,
7662                                                      matcher.priority);
7663         /* reserved field no needs to be set to 0 here. */
7664         tbl_key.domain = attr->transfer;
7665         tbl_key.direction = attr->egress;
7666         tbl_key.table_id = dev_flow->group;
7667         if (flow_dv_matcher_register(dev, &matcher, &tbl_key, dev_flow, error))
7668                 return -rte_errno;
7669         return 0;
7670 }
7671
7672 /**
7673  * Apply the flow to the NIC, lock free,
7674  * (mutex should be acquired by caller).
7675  *
7676  * @param[in] dev
7677  *   Pointer to the Ethernet device structure.
7678  * @param[in, out] flow
7679  *   Pointer to flow structure.
7680  * @param[out] error
7681  *   Pointer to error structure.
7682  *
7683  * @return
7684  *   0 on success, a negative errno value otherwise and rte_errno is set.
7685  */
7686 static int
7687 __flow_dv_apply(struct rte_eth_dev *dev, struct rte_flow *flow,
7688                 struct rte_flow_error *error)
7689 {
7690         struct mlx5_flow_dv *dv;
7691         struct mlx5_flow *dev_flow;
7692         struct mlx5_priv *priv = dev->data->dev_private;
7693         int n;
7694         int err;
7695
7696         LIST_FOREACH(dev_flow, &flow->dev_flows, next) {
7697                 dv = &dev_flow->dv;
7698                 n = dv->actions_n;
7699                 if (dev_flow->actions & MLX5_FLOW_ACTION_DROP) {
7700                         if (dev_flow->transfer) {
7701                                 dv->actions[n++] = priv->sh->esw_drop_action;
7702                         } else {
7703                                 dv->hrxq = mlx5_hrxq_drop_new(dev);
7704                                 if (!dv->hrxq) {
7705                                         rte_flow_error_set
7706                                                 (error, errno,
7707                                                  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
7708                                                  NULL,
7709                                                  "cannot get drop hash queue");
7710                                         goto error;
7711                                 }
7712                                 dv->actions[n++] = dv->hrxq->action;
7713                         }
7714                 } else if (dev_flow->actions &
7715                            (MLX5_FLOW_ACTION_QUEUE | MLX5_FLOW_ACTION_RSS)) {
7716                         struct mlx5_hrxq *hrxq;
7717
7718                         assert(flow->rss.queue);
7719                         hrxq = mlx5_hrxq_get(dev, flow->rss.key,
7720                                              MLX5_RSS_HASH_KEY_LEN,
7721                                              dev_flow->hash_fields,
7722                                              (*flow->rss.queue),
7723                                              flow->rss.queue_num);
7724                         if (!hrxq) {
7725                                 hrxq = mlx5_hrxq_new
7726                                         (dev, flow->rss.key,
7727                                          MLX5_RSS_HASH_KEY_LEN,
7728                                          dev_flow->hash_fields,
7729                                          (*flow->rss.queue),
7730                                          flow->rss.queue_num,
7731                                          !!(dev_flow->layers &
7732                                             MLX5_FLOW_LAYER_TUNNEL));
7733                         }
7734                         if (!hrxq) {
7735                                 rte_flow_error_set
7736                                         (error, rte_errno,
7737                                          RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
7738                                          "cannot get hash queue");
7739                                 goto error;
7740                         }
7741                         dv->hrxq = hrxq;
7742                         dv->actions[n++] = dv->hrxq->action;
7743                 }
7744                 dv->flow =
7745                         mlx5_glue->dv_create_flow(dv->matcher->matcher_object,
7746                                                   (void *)&dv->value, n,
7747                                                   dv->actions);
7748                 if (!dv->flow) {
7749                         rte_flow_error_set(error, errno,
7750                                            RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
7751                                            NULL,
7752                                            "hardware refuses to create flow");
7753                         goto error;
7754                 }
7755                 if (priv->vmwa_context &&
7756                     dev_flow->dv.vf_vlan.tag &&
7757                     !dev_flow->dv.vf_vlan.created) {
7758                         /*
7759                          * The rule contains the VLAN pattern.
7760                          * For VF we are going to create VLAN
7761                          * interface to make hypervisor set correct
7762                          * e-Switch vport context.
7763                          */
7764                         mlx5_vlan_vmwa_acquire(dev, &dev_flow->dv.vf_vlan);
7765                 }
7766         }
7767         return 0;
7768 error:
7769         err = rte_errno; /* Save rte_errno before cleanup. */
7770         LIST_FOREACH(dev_flow, &flow->dev_flows, next) {
7771                 struct mlx5_flow_dv *dv = &dev_flow->dv;
7772                 if (dv->hrxq) {
7773                         if (dev_flow->actions & MLX5_FLOW_ACTION_DROP)
7774                                 mlx5_hrxq_drop_release(dev);
7775                         else
7776                                 mlx5_hrxq_release(dev, dv->hrxq);
7777                         dv->hrxq = NULL;
7778                 }
7779                 if (dev_flow->dv.vf_vlan.tag &&
7780                     dev_flow->dv.vf_vlan.created)
7781                         mlx5_vlan_vmwa_release(dev, &dev_flow->dv.vf_vlan);
7782         }
7783         rte_errno = err; /* Restore rte_errno. */
7784         return -rte_errno;
7785 }
7786
7787 /**
7788  * Release the flow matcher.
7789  *
7790  * @param dev
7791  *   Pointer to Ethernet device.
7792  * @param flow
7793  *   Pointer to mlx5_flow.
7794  *
7795  * @return
7796  *   1 while a reference on it exists, 0 when freed.
7797  */
7798 static int
7799 flow_dv_matcher_release(struct rte_eth_dev *dev,
7800                         struct mlx5_flow *flow)
7801 {
7802         struct mlx5_flow_dv_matcher *matcher = flow->dv.matcher;
7803
7804         assert(matcher->matcher_object);
7805         DRV_LOG(DEBUG, "port %u matcher %p: refcnt %d--",
7806                 dev->data->port_id, (void *)matcher,
7807                 rte_atomic32_read(&matcher->refcnt));
7808         if (rte_atomic32_dec_and_test(&matcher->refcnt)) {
7809                 claim_zero(mlx5_glue->dv_destroy_flow_matcher
7810                            (matcher->matcher_object));
7811                 LIST_REMOVE(matcher, next);
7812                 /* table ref-- in release interface. */
7813                 flow_dv_tbl_resource_release(dev, matcher->tbl);
7814                 rte_free(matcher);
7815                 DRV_LOG(DEBUG, "port %u matcher %p: removed",
7816                         dev->data->port_id, (void *)matcher);
7817                 return 0;
7818         }
7819         return 1;
7820 }
7821
7822 /**
7823  * Release an encap/decap resource.
7824  *
7825  * @param flow
7826  *   Pointer to mlx5_flow.
7827  *
7828  * @return
7829  *   1 while a reference on it exists, 0 when freed.
7830  */
7831 static int
7832 flow_dv_encap_decap_resource_release(struct mlx5_flow *flow)
7833 {
7834         struct mlx5_flow_dv_encap_decap_resource *cache_resource =
7835                                                 flow->dv.encap_decap;
7836
7837         assert(cache_resource->verbs_action);
7838         DRV_LOG(DEBUG, "encap/decap resource %p: refcnt %d--",
7839                 (void *)cache_resource,
7840                 rte_atomic32_read(&cache_resource->refcnt));
7841         if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
7842                 claim_zero(mlx5_glue->destroy_flow_action
7843                                 (cache_resource->verbs_action));
7844                 LIST_REMOVE(cache_resource, next);
7845                 rte_free(cache_resource);
7846                 DRV_LOG(DEBUG, "encap/decap resource %p: removed",
7847                         (void *)cache_resource);
7848                 return 0;
7849         }
7850         return 1;
7851 }
7852
7853 /**
7854  * Release an jump to table action resource.
7855  *
7856  * @param dev
7857  *   Pointer to Ethernet device.
7858  * @param flow
7859  *   Pointer to mlx5_flow.
7860  *
7861  * @return
7862  *   1 while a reference on it exists, 0 when freed.
7863  */
7864 static int
7865 flow_dv_jump_tbl_resource_release(struct rte_eth_dev *dev,
7866                                   struct mlx5_flow *flow)
7867 {
7868         struct mlx5_flow_dv_jump_tbl_resource *cache_resource = flow->dv.jump;
7869         struct mlx5_flow_tbl_data_entry *tbl_data =
7870                         container_of(cache_resource,
7871                                      struct mlx5_flow_tbl_data_entry, jump);
7872
7873         assert(cache_resource->action);
7874         DRV_LOG(DEBUG, "jump table resource %p: refcnt %d--",
7875                 (void *)cache_resource,
7876                 rte_atomic32_read(&cache_resource->refcnt));
7877         if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
7878                 claim_zero(mlx5_glue->destroy_flow_action
7879                                 (cache_resource->action));
7880                 /* jump action memory free is inside the table release. */
7881                 flow_dv_tbl_resource_release(dev, &tbl_data->tbl);
7882                 DRV_LOG(DEBUG, "jump table resource %p: removed",
7883                         (void *)cache_resource);
7884                 return 0;
7885         }
7886         return 1;
7887 }
7888
7889 /**
7890  * Release a modify-header resource.
7891  *
7892  * @param flow
7893  *   Pointer to mlx5_flow.
7894  *
7895  * @return
7896  *   1 while a reference on it exists, 0 when freed.
7897  */
7898 static int
7899 flow_dv_modify_hdr_resource_release(struct mlx5_flow *flow)
7900 {
7901         struct mlx5_flow_dv_modify_hdr_resource *cache_resource =
7902                                                 flow->dv.modify_hdr;
7903
7904         assert(cache_resource->verbs_action);
7905         DRV_LOG(DEBUG, "modify-header resource %p: refcnt %d--",
7906                 (void *)cache_resource,
7907                 rte_atomic32_read(&cache_resource->refcnt));
7908         if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
7909                 claim_zero(mlx5_glue->destroy_flow_action
7910                                 (cache_resource->verbs_action));
7911                 LIST_REMOVE(cache_resource, next);
7912                 rte_free(cache_resource);
7913                 DRV_LOG(DEBUG, "modify-header resource %p: removed",
7914                         (void *)cache_resource);
7915                 return 0;
7916         }
7917         return 1;
7918 }
7919
7920 /**
7921  * Release port ID action resource.
7922  *
7923  * @param flow
7924  *   Pointer to mlx5_flow.
7925  *
7926  * @return
7927  *   1 while a reference on it exists, 0 when freed.
7928  */
7929 static int
7930 flow_dv_port_id_action_resource_release(struct mlx5_flow *flow)
7931 {
7932         struct mlx5_flow_dv_port_id_action_resource *cache_resource =
7933                 flow->dv.port_id_action;
7934
7935         assert(cache_resource->action);
7936         DRV_LOG(DEBUG, "port ID action resource %p: refcnt %d--",
7937                 (void *)cache_resource,
7938                 rte_atomic32_read(&cache_resource->refcnt));
7939         if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
7940                 claim_zero(mlx5_glue->destroy_flow_action
7941                                 (cache_resource->action));
7942                 LIST_REMOVE(cache_resource, next);
7943                 rte_free(cache_resource);
7944                 DRV_LOG(DEBUG, "port id action resource %p: removed",
7945                         (void *)cache_resource);
7946                 return 0;
7947         }
7948         return 1;
7949 }
7950
7951 /**
7952  * Release push vlan action resource.
7953  *
7954  * @param flow
7955  *   Pointer to mlx5_flow.
7956  *
7957  * @return
7958  *   1 while a reference on it exists, 0 when freed.
7959  */
7960 static int
7961 flow_dv_push_vlan_action_resource_release(struct mlx5_flow *flow)
7962 {
7963         struct mlx5_flow_dv_push_vlan_action_resource *cache_resource =
7964                 flow->dv.push_vlan_res;
7965
7966         assert(cache_resource->action);
7967         DRV_LOG(DEBUG, "push VLAN action resource %p: refcnt %d--",
7968                 (void *)cache_resource,
7969                 rte_atomic32_read(&cache_resource->refcnt));
7970         if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
7971                 claim_zero(mlx5_glue->destroy_flow_action
7972                                 (cache_resource->action));
7973                 LIST_REMOVE(cache_resource, next);
7974                 rte_free(cache_resource);
7975                 DRV_LOG(DEBUG, "push vlan action resource %p: removed",
7976                         (void *)cache_resource);
7977                 return 0;
7978         }
7979         return 1;
7980 }
7981
7982 /**
7983  * Remove the flow from the NIC but keeps it in memory.
7984  * Lock free, (mutex should be acquired by caller).
7985  *
7986  * @param[in] dev
7987  *   Pointer to Ethernet device.
7988  * @param[in, out] flow
7989  *   Pointer to flow structure.
7990  */
7991 static void
7992 __flow_dv_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
7993 {
7994         struct mlx5_flow_dv *dv;
7995         struct mlx5_flow *dev_flow;
7996
7997         if (!flow)
7998                 return;
7999         LIST_FOREACH(dev_flow, &flow->dev_flows, next) {
8000                 dv = &dev_flow->dv;
8001                 if (dv->flow) {
8002                         claim_zero(mlx5_glue->dv_destroy_flow(dv->flow));
8003                         dv->flow = NULL;
8004                 }
8005                 if (dv->hrxq) {
8006                         if (dev_flow->actions & MLX5_FLOW_ACTION_DROP)
8007                                 mlx5_hrxq_drop_release(dev);
8008                         else
8009                                 mlx5_hrxq_release(dev, dv->hrxq);
8010                         dv->hrxq = NULL;
8011                 }
8012                 if (dev_flow->dv.vf_vlan.tag &&
8013                     dev_flow->dv.vf_vlan.created)
8014                         mlx5_vlan_vmwa_release(dev, &dev_flow->dv.vf_vlan);
8015         }
8016 }
8017
8018 /**
8019  * Remove the flow from the NIC and the memory.
8020  * Lock free, (mutex should be acquired by caller).
8021  *
8022  * @param[in] dev
8023  *   Pointer to the Ethernet device structure.
8024  * @param[in, out] flow
8025  *   Pointer to flow structure.
8026  */
8027 static void
8028 __flow_dv_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
8029 {
8030         struct mlx5_flow *dev_flow;
8031
8032         if (!flow)
8033                 return;
8034         __flow_dv_remove(dev, flow);
8035         if (flow->counter) {
8036                 flow_dv_counter_release(dev, flow->counter);
8037                 flow->counter = NULL;
8038         }
8039         if (flow->meter) {
8040                 mlx5_flow_meter_detach(flow->meter);
8041                 flow->meter = NULL;
8042         }
8043         while (!LIST_EMPTY(&flow->dev_flows)) {
8044                 dev_flow = LIST_FIRST(&flow->dev_flows);
8045                 LIST_REMOVE(dev_flow, next);
8046                 if (dev_flow->dv.matcher)
8047                         flow_dv_matcher_release(dev, dev_flow);
8048                 if (dev_flow->dv.encap_decap)
8049                         flow_dv_encap_decap_resource_release(dev_flow);
8050                 if (dev_flow->dv.modify_hdr)
8051                         flow_dv_modify_hdr_resource_release(dev_flow);
8052                 if (dev_flow->dv.jump)
8053                         flow_dv_jump_tbl_resource_release(dev, dev_flow);
8054                 if (dev_flow->dv.port_id_action)
8055                         flow_dv_port_id_action_resource_release(dev_flow);
8056                 if (dev_flow->dv.push_vlan_res)
8057                         flow_dv_push_vlan_action_resource_release(dev_flow);
8058                 if (dev_flow->dv.tag_resource)
8059                         flow_dv_tag_release(dev, dev_flow->dv.tag_resource);
8060                 rte_free(dev_flow);
8061         }
8062 }
8063
8064 /**
8065  * Query a dv flow  rule for its statistics via devx.
8066  *
8067  * @param[in] dev
8068  *   Pointer to Ethernet device.
8069  * @param[in] flow
8070  *   Pointer to the sub flow.
8071  * @param[out] data
8072  *   data retrieved by the query.
8073  * @param[out] error
8074  *   Perform verbose error reporting if not NULL.
8075  *
8076  * @return
8077  *   0 on success, a negative errno value otherwise and rte_errno is set.
8078  */
8079 static int
8080 flow_dv_query_count(struct rte_eth_dev *dev, struct rte_flow *flow,
8081                     void *data, struct rte_flow_error *error)
8082 {
8083         struct mlx5_priv *priv = dev->data->dev_private;
8084         struct rte_flow_query_count *qc = data;
8085
8086         if (!priv->config.devx)
8087                 return rte_flow_error_set(error, ENOTSUP,
8088                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
8089                                           NULL,
8090                                           "counters are not supported");
8091         if (flow->counter) {
8092                 uint64_t pkts, bytes;
8093                 int err = _flow_dv_query_count(dev, flow->counter, &pkts,
8094                                                &bytes);
8095
8096                 if (err)
8097                         return rte_flow_error_set(error, -err,
8098                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
8099                                         NULL, "cannot read counters");
8100                 qc->hits_set = 1;
8101                 qc->bytes_set = 1;
8102                 qc->hits = pkts - flow->counter->hits;
8103                 qc->bytes = bytes - flow->counter->bytes;
8104                 if (qc->reset) {
8105                         flow->counter->hits = pkts;
8106                         flow->counter->bytes = bytes;
8107                 }
8108                 return 0;
8109         }
8110         return rte_flow_error_set(error, EINVAL,
8111                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
8112                                   NULL,
8113                                   "counters are not available");
8114 }
8115
8116 /**
8117  * Query a flow.
8118  *
8119  * @see rte_flow_query()
8120  * @see rte_flow_ops
8121  */
8122 static int
8123 flow_dv_query(struct rte_eth_dev *dev,
8124               struct rte_flow *flow __rte_unused,
8125               const struct rte_flow_action *actions __rte_unused,
8126               void *data __rte_unused,
8127               struct rte_flow_error *error __rte_unused)
8128 {
8129         int ret = -EINVAL;
8130
8131         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
8132                 switch (actions->type) {
8133                 case RTE_FLOW_ACTION_TYPE_VOID:
8134                         break;
8135                 case RTE_FLOW_ACTION_TYPE_COUNT:
8136                         ret = flow_dv_query_count(dev, flow, data, error);
8137                         break;
8138                 default:
8139                         return rte_flow_error_set(error, ENOTSUP,
8140                                                   RTE_FLOW_ERROR_TYPE_ACTION,
8141                                                   actions,
8142                                                   "action not supported");
8143                 }
8144         }
8145         return ret;
8146 }
8147
8148 /**
8149  * Destroy the meter table set.
8150  * Lock free, (mutex should be acquired by caller).
8151  *
8152  * @param[in] dev
8153  *   Pointer to Ethernet device.
8154  * @param[in] tbl
8155  *   Pointer to the meter table set.
8156  *
8157  * @return
8158  *   Always 0.
8159  */
8160 static int
8161 flow_dv_destroy_mtr_tbl(struct rte_eth_dev *dev,
8162                         struct mlx5_meter_domains_infos *tbl)
8163 {
8164         struct mlx5_priv *priv = dev->data->dev_private;
8165         struct mlx5_meter_domains_infos *mtd =
8166                                 (struct mlx5_meter_domains_infos *)tbl;
8167
8168         if (!mtd || !priv->config.dv_flow_en)
8169                 return 0;
8170         if (mtd->ingress.policer_rules[RTE_MTR_DROPPED])
8171                 claim_zero(mlx5_glue->dv_destroy_flow
8172                           (mtd->ingress.policer_rules[RTE_MTR_DROPPED]));
8173         if (mtd->egress.policer_rules[RTE_MTR_DROPPED])
8174                 claim_zero(mlx5_glue->dv_destroy_flow
8175                           (mtd->egress.policer_rules[RTE_MTR_DROPPED]));
8176         if (mtd->transfer.policer_rules[RTE_MTR_DROPPED])
8177                 claim_zero(mlx5_glue->dv_destroy_flow
8178                           (mtd->transfer.policer_rules[RTE_MTR_DROPPED]));
8179         if (mtd->egress.color_matcher)
8180                 claim_zero(mlx5_glue->dv_destroy_flow_matcher
8181                           (mtd->egress.color_matcher));
8182         if (mtd->egress.any_matcher)
8183                 claim_zero(mlx5_glue->dv_destroy_flow_matcher
8184                           (mtd->egress.any_matcher));
8185         if (mtd->egress.tbl)
8186                 claim_zero(flow_dv_tbl_resource_release(dev,
8187                                                         mtd->egress.tbl));
8188         if (mtd->ingress.color_matcher)
8189                 claim_zero(mlx5_glue->dv_destroy_flow_matcher
8190                           (mtd->ingress.color_matcher));
8191         if (mtd->ingress.any_matcher)
8192                 claim_zero(mlx5_glue->dv_destroy_flow_matcher
8193                           (mtd->ingress.any_matcher));
8194         if (mtd->ingress.tbl)
8195                 claim_zero(flow_dv_tbl_resource_release(dev,
8196                                                         mtd->ingress.tbl));
8197         if (mtd->transfer.color_matcher)
8198                 claim_zero(mlx5_glue->dv_destroy_flow_matcher
8199                           (mtd->transfer.color_matcher));
8200         if (mtd->transfer.any_matcher)
8201                 claim_zero(mlx5_glue->dv_destroy_flow_matcher
8202                           (mtd->transfer.any_matcher));
8203         if (mtd->transfer.tbl)
8204                 claim_zero(flow_dv_tbl_resource_release(dev,
8205                                                         mtd->transfer.tbl));
8206         if (mtd->drop_actn)
8207                 claim_zero(mlx5_glue->destroy_flow_action(mtd->drop_actn));
8208         rte_free(mtd);
8209         return 0;
8210 }
8211
8212 /* Number of meter flow actions, count and jump or count and drop. */
8213 #define METER_ACTIONS 2
8214
8215 /**
8216  * Create specify domain meter table and suffix table.
8217  *
8218  * @param[in] dev
8219  *   Pointer to Ethernet device.
8220  * @param[in,out] mtb
8221  *   Pointer to DV meter table set.
8222  * @param[in] egress
8223  *   Table attribute.
8224  * @param[in] transfer
8225  *   Table attribute.
8226  * @param[in] color_reg_c_idx
8227  *   Reg C index for color match.
8228  *
8229  * @return
8230  *   0 on success, -1 otherwise and rte_errno is set.
8231  */
8232 static int
8233 flow_dv_prepare_mtr_tables(struct rte_eth_dev *dev,
8234                            struct mlx5_meter_domains_infos *mtb,
8235                            uint8_t egress, uint8_t transfer,
8236                            uint32_t color_reg_c_idx)
8237 {
8238         struct mlx5_priv *priv = dev->data->dev_private;
8239         struct mlx5_ibv_shared *sh = priv->sh;
8240         struct mlx5_flow_dv_match_params mask = {
8241                 .size = sizeof(mask.buf),
8242         };
8243         struct mlx5_flow_dv_match_params value = {
8244                 .size = sizeof(value.buf),
8245         };
8246         struct mlx5dv_flow_matcher_attr dv_attr = {
8247                 .type = IBV_FLOW_ATTR_NORMAL,
8248                 .priority = 0,
8249                 .match_criteria_enable = 0,
8250                 .match_mask = (void *)&mask,
8251         };
8252         void *actions[METER_ACTIONS];
8253         struct mlx5_flow_tbl_resource **sfx_tbl;
8254         struct mlx5_meter_domain_info *dtb;
8255         struct rte_flow_error error;
8256         int i = 0;
8257
8258         if (transfer) {
8259                 sfx_tbl = &sh->fdb_mtr_sfx_tbl;
8260                 dtb = &mtb->transfer;
8261         } else if (egress) {
8262                 sfx_tbl = &sh->tx_mtr_sfx_tbl;
8263                 dtb = &mtb->egress;
8264         } else {
8265                 sfx_tbl = &sh->rx_mtr_sfx_tbl;
8266                 dtb = &mtb->ingress;
8267         }
8268         /* If the suffix table in missing, create it. */
8269         if (!(*sfx_tbl)) {
8270                 *sfx_tbl = flow_dv_tbl_resource_get(dev,
8271                                                 MLX5_FLOW_TABLE_LEVEL_SUFFIX,
8272                                                 egress, transfer, &error);
8273                 if (!(*sfx_tbl)) {
8274                         DRV_LOG(ERR, "Failed to create meter suffix table.");
8275                         return -1;
8276                 }
8277         }
8278         /* Create the meter table with METER level. */
8279         dtb->tbl = flow_dv_tbl_resource_get(dev, MLX5_FLOW_TABLE_LEVEL_METER,
8280                                             egress, transfer, &error);
8281         if (!dtb->tbl) {
8282                 DRV_LOG(ERR, "Failed to create meter policer table.");
8283                 return -1;
8284         }
8285         /* Create matchers, Any and Color. */
8286         dv_attr.priority = 3;
8287         dv_attr.match_criteria_enable = 0;
8288         dtb->any_matcher = mlx5_glue->dv_create_flow_matcher(sh->ctx,
8289                                                              &dv_attr,
8290                                                              dtb->tbl->obj);
8291         if (!dtb->any_matcher) {
8292                 DRV_LOG(ERR, "Failed to create meter"
8293                              " policer default matcher.");
8294                 goto error_exit;
8295         }
8296         dv_attr.priority = 0;
8297         dv_attr.match_criteria_enable =
8298                                 1 << MLX5_MATCH_CRITERIA_ENABLE_MISC2_BIT;
8299         flow_dv_match_meta_reg(mask.buf, value.buf, color_reg_c_idx,
8300                                rte_col_2_mlx5_col(RTE_COLORS), UINT32_MAX);
8301         dtb->color_matcher = mlx5_glue->dv_create_flow_matcher(sh->ctx,
8302                                                                &dv_attr,
8303                                                                dtb->tbl->obj);
8304         if (!dtb->color_matcher) {
8305                 DRV_LOG(ERR, "Failed to create meter policer color matcher.");
8306                 goto error_exit;
8307         }
8308         if (mtb->count_actns[RTE_MTR_DROPPED])
8309                 actions[i++] = mtb->count_actns[RTE_MTR_DROPPED];
8310         actions[i++] = mtb->drop_actn;
8311         /* Default rule: lowest priority, match any, actions: drop. */
8312         dtb->policer_rules[RTE_MTR_DROPPED] =
8313                         mlx5_glue->dv_create_flow(dtb->any_matcher,
8314                                                  (void *)&value, i, actions);
8315         if (!dtb->policer_rules[RTE_MTR_DROPPED]) {
8316                 DRV_LOG(ERR, "Failed to create meter policer drop rule.");
8317                 goto error_exit;
8318         }
8319         return 0;
8320 error_exit:
8321         return -1;
8322 }
8323
8324 /**
8325  * Create the needed meter and suffix tables.
8326  * Lock free, (mutex should be acquired by caller).
8327  *
8328  * @param[in] dev
8329  *   Pointer to Ethernet device.
8330  * @param[in] fm
8331  *   Pointer to the flow meter.
8332  *
8333  * @return
8334  *   Pointer to table set on success, NULL otherwise and rte_errno is set.
8335  */
8336 static struct mlx5_meter_domains_infos *
8337 flow_dv_create_mtr_tbl(struct rte_eth_dev *dev,
8338                        const struct mlx5_flow_meter *fm)
8339 {
8340         struct mlx5_priv *priv = dev->data->dev_private;
8341         struct mlx5_meter_domains_infos *mtb;
8342         int ret;
8343         int i;
8344
8345         if (!priv->mtr_en) {
8346                 rte_errno = ENOTSUP;
8347                 return NULL;
8348         }
8349         mtb = rte_calloc(__func__, 1, sizeof(*mtb), 0);
8350         if (!mtb) {
8351                 DRV_LOG(ERR, "Failed to allocate memory for meter.");
8352                 return NULL;
8353         }
8354         /* Create meter count actions */
8355         for (i = 0; i <= RTE_MTR_DROPPED; i++) {
8356                 if (!fm->policer_stats.cnt[i])
8357                         continue;
8358                 mtb->count_actns[i] = fm->policer_stats.cnt[i]->action;
8359         }
8360         /* Create drop action. */
8361         mtb->drop_actn = mlx5_glue->dr_create_flow_action_drop();
8362         if (!mtb->drop_actn) {
8363                 DRV_LOG(ERR, "Failed to create drop action.");
8364                 goto error_exit;
8365         }
8366         /* Egress meter table. */
8367         ret = flow_dv_prepare_mtr_tables(dev, mtb, 1, 0, priv->mtr_color_reg);
8368         if (ret) {
8369                 DRV_LOG(ERR, "Failed to prepare egress meter table.");
8370                 goto error_exit;
8371         }
8372         /* Ingress meter table. */
8373         ret = flow_dv_prepare_mtr_tables(dev, mtb, 0, 0, priv->mtr_color_reg);
8374         if (ret) {
8375                 DRV_LOG(ERR, "Failed to prepare ingress meter table.");
8376                 goto error_exit;
8377         }
8378         /* FDB meter table. */
8379         if (priv->config.dv_esw_en) {
8380                 ret = flow_dv_prepare_mtr_tables(dev, mtb, 0, 1,
8381                                                  priv->mtr_color_reg);
8382                 if (ret) {
8383                         DRV_LOG(ERR, "Failed to prepare fdb meter table.");
8384                         goto error_exit;
8385                 }
8386         }
8387         return mtb;
8388 error_exit:
8389         flow_dv_destroy_mtr_tbl(dev, mtb);
8390         return NULL;
8391 }
8392
8393 /**
8394  * Destroy domain policer rule.
8395  *
8396  * @param[in] dt
8397  *   Pointer to domain table.
8398  */
8399 static void
8400 flow_dv_destroy_domain_policer_rule(struct mlx5_meter_domain_info *dt)
8401 {
8402         int i;
8403
8404         for (i = 0; i < RTE_MTR_DROPPED; i++) {
8405                 if (dt->policer_rules[i]) {
8406                         claim_zero(mlx5_glue->dv_destroy_flow
8407                                   (dt->policer_rules[i]));
8408                         dt->policer_rules[i] = NULL;
8409                 }
8410         }
8411         if (dt->jump_actn) {
8412                 claim_zero(mlx5_glue->destroy_flow_action(dt->jump_actn));
8413                 dt->jump_actn = NULL;
8414         }
8415 }
8416
8417 /**
8418  * Destroy policer rules.
8419  *
8420  * @param[in] dev
8421  *   Pointer to Ethernet device.
8422  * @param[in] fm
8423  *   Pointer to flow meter structure.
8424  * @param[in] attr
8425  *   Pointer to flow attributes.
8426  *
8427  * @return
8428  *   Always 0.
8429  */
8430 static int
8431 flow_dv_destroy_policer_rules(struct rte_eth_dev *dev __rte_unused,
8432                               const struct mlx5_flow_meter *fm,
8433                               const struct rte_flow_attr *attr)
8434 {
8435         struct mlx5_meter_domains_infos *mtb = fm ? fm->mfts : NULL;
8436
8437         if (!mtb)
8438                 return 0;
8439         if (attr->egress)
8440                 flow_dv_destroy_domain_policer_rule(&mtb->egress);
8441         if (attr->ingress)
8442                 flow_dv_destroy_domain_policer_rule(&mtb->ingress);
8443         if (attr->transfer)
8444                 flow_dv_destroy_domain_policer_rule(&mtb->transfer);
8445         return 0;
8446 }
8447
8448 /**
8449  * Create specify domain meter policer rule.
8450  *
8451  * @param[in] fm
8452  *   Pointer to flow meter structure.
8453  * @param[in] mtb
8454  *   Pointer to DV meter table set.
8455  * @param[in] sfx_tb
8456  *   Pointer to suffix table.
8457  * @param[in] mtr_reg_c
8458  *   Color match REG_C.
8459  *
8460  * @return
8461  *   0 on success, -1 otherwise.
8462  */
8463 static int
8464 flow_dv_create_policer_forward_rule(struct mlx5_flow_meter *fm,
8465                                     struct mlx5_meter_domain_info *dtb,
8466                                     struct mlx5_flow_tbl_resource *sfx_tb,
8467                                     uint8_t mtr_reg_c)
8468 {
8469         struct mlx5_flow_dv_match_params matcher = {
8470                 .size = sizeof(matcher.buf),
8471         };
8472         struct mlx5_flow_dv_match_params value = {
8473                 .size = sizeof(value.buf),
8474         };
8475         struct mlx5_meter_domains_infos *mtb = fm->mfts;
8476         void *actions[METER_ACTIONS];
8477         int i;
8478
8479         /* Create jump action. */
8480         if (!sfx_tb)
8481                 return -1;
8482         if (!dtb->jump_actn)
8483                 dtb->jump_actn =
8484                         mlx5_glue->dr_create_flow_action_dest_flow_tbl
8485                                                         (sfx_tb->obj);
8486         if (!dtb->jump_actn) {
8487                 DRV_LOG(ERR, "Failed to create policer jump action.");
8488                 goto error;
8489         }
8490         for (i = 0; i < RTE_MTR_DROPPED; i++) {
8491                 int j = 0;
8492
8493                 flow_dv_match_meta_reg(matcher.buf, value.buf, mtr_reg_c,
8494                                        rte_col_2_mlx5_col(i), UINT32_MAX);
8495                 if (mtb->count_actns[i])
8496                         actions[j++] = mtb->count_actns[i];
8497                 if (fm->params.action[i] == MTR_POLICER_ACTION_DROP)
8498                         actions[j++] = mtb->drop_actn;
8499                 else
8500                         actions[j++] = dtb->jump_actn;
8501                 dtb->policer_rules[i] =
8502                         mlx5_glue->dv_create_flow(dtb->color_matcher,
8503                                                  (void *)&value,
8504                                                   j, actions);
8505                 if (!dtb->policer_rules[i]) {
8506                         DRV_LOG(ERR, "Failed to create policer rule.");
8507                         goto error;
8508                 }
8509         }
8510         return 0;
8511 error:
8512         rte_errno = errno;
8513         return -1;
8514 }
8515
8516 /**
8517  * Create policer rules.
8518  *
8519  * @param[in] dev
8520  *   Pointer to Ethernet device.
8521  * @param[in] fm
8522  *   Pointer to flow meter structure.
8523  * @param[in] attr
8524  *   Pointer to flow attributes.
8525  *
8526  * @return
8527  *   0 on success, -1 otherwise.
8528  */
8529 static int
8530 flow_dv_create_policer_rules(struct rte_eth_dev *dev,
8531                              struct mlx5_flow_meter *fm,
8532                              const struct rte_flow_attr *attr)
8533 {
8534         struct mlx5_priv *priv = dev->data->dev_private;
8535         struct mlx5_meter_domains_infos *mtb = fm->mfts;
8536         int ret;
8537
8538         if (attr->egress) {
8539                 ret = flow_dv_create_policer_forward_rule(fm, &mtb->egress,
8540                                                 priv->sh->tx_mtr_sfx_tbl,
8541                                                 priv->mtr_color_reg);
8542                 if (ret) {
8543                         DRV_LOG(ERR, "Failed to create egress policer.");
8544                         goto error;
8545                 }
8546         }
8547         if (attr->ingress) {
8548                 ret = flow_dv_create_policer_forward_rule(fm, &mtb->ingress,
8549                                                 priv->sh->rx_mtr_sfx_tbl,
8550                                                 priv->mtr_color_reg);
8551                 if (ret) {
8552                         DRV_LOG(ERR, "Failed to create ingress policer.");
8553                         goto error;
8554                 }
8555         }
8556         if (attr->transfer) {
8557                 ret = flow_dv_create_policer_forward_rule(fm, &mtb->transfer,
8558                                                 priv->sh->fdb_mtr_sfx_tbl,
8559                                                 priv->mtr_color_reg);
8560                 if (ret) {
8561                         DRV_LOG(ERR, "Failed to create transfer policer.");
8562                         goto error;
8563                 }
8564         }
8565         return 0;
8566 error:
8567         flow_dv_destroy_policer_rules(dev, fm, attr);
8568         return -1;
8569 }
8570
8571 /**
8572  * Query a devx counter.
8573  *
8574  * @param[in] dev
8575  *   Pointer to the Ethernet device structure.
8576  * @param[in] cnt
8577  *   Pointer to the flow counter.
8578  * @param[in] clear
8579  *   Set to clear the counter statistics.
8580  * @param[out] pkts
8581  *   The statistics value of packets.
8582  * @param[out] bytes
8583  *   The statistics value of bytes.
8584  *
8585  * @return
8586  *   0 on success, otherwise return -1.
8587  */
8588 static int
8589 flow_dv_counter_query(struct rte_eth_dev *dev,
8590                       struct mlx5_flow_counter *cnt, bool clear,
8591                       uint64_t *pkts, uint64_t *bytes)
8592 {
8593         struct mlx5_priv *priv = dev->data->dev_private;
8594         uint64_t inn_pkts, inn_bytes;
8595         int ret;
8596
8597         if (!priv->config.devx)
8598                 return -1;
8599         ret = _flow_dv_query_count(dev, cnt, &inn_pkts, &inn_bytes);
8600         if (ret)
8601                 return -1;
8602         *pkts = inn_pkts - cnt->hits;
8603         *bytes = inn_bytes - cnt->bytes;
8604         if (clear) {
8605                 cnt->hits = inn_pkts;
8606                 cnt->bytes = inn_bytes;
8607         }
8608         return 0;
8609 }
8610
8611 /*
8612  * Mutex-protected thunk to lock-free  __flow_dv_translate().
8613  */
8614 static int
8615 flow_dv_translate(struct rte_eth_dev *dev,
8616                   struct mlx5_flow *dev_flow,
8617                   const struct rte_flow_attr *attr,
8618                   const struct rte_flow_item items[],
8619                   const struct rte_flow_action actions[],
8620                   struct rte_flow_error *error)
8621 {
8622         int ret;
8623
8624         flow_dv_shared_lock(dev);
8625         ret = __flow_dv_translate(dev, dev_flow, attr, items, actions, error);
8626         flow_dv_shared_unlock(dev);
8627         return ret;
8628 }
8629
8630 /*
8631  * Mutex-protected thunk to lock-free  __flow_dv_apply().
8632  */
8633 static int
8634 flow_dv_apply(struct rte_eth_dev *dev,
8635               struct rte_flow *flow,
8636               struct rte_flow_error *error)
8637 {
8638         int ret;
8639
8640         flow_dv_shared_lock(dev);
8641         ret = __flow_dv_apply(dev, flow, error);
8642         flow_dv_shared_unlock(dev);
8643         return ret;
8644 }
8645
8646 /*
8647  * Mutex-protected thunk to lock-free __flow_dv_remove().
8648  */
8649 static void
8650 flow_dv_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
8651 {
8652         flow_dv_shared_lock(dev);
8653         __flow_dv_remove(dev, flow);
8654         flow_dv_shared_unlock(dev);
8655 }
8656
8657 /*
8658  * Mutex-protected thunk to lock-free __flow_dv_destroy().
8659  */
8660 static void
8661 flow_dv_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
8662 {
8663         flow_dv_shared_lock(dev);
8664         __flow_dv_destroy(dev, flow);
8665         flow_dv_shared_unlock(dev);
8666 }
8667
8668 /*
8669  * Mutex-protected thunk to lock-free flow_dv_counter_alloc().
8670  */
8671 static struct mlx5_flow_counter *
8672 flow_dv_counter_allocate(struct rte_eth_dev *dev)
8673 {
8674         struct mlx5_flow_counter *cnt;
8675
8676         flow_dv_shared_lock(dev);
8677         cnt = flow_dv_counter_alloc(dev, 0, 0, 1);
8678         flow_dv_shared_unlock(dev);
8679         return cnt;
8680 }
8681
8682 /*
8683  * Mutex-protected thunk to lock-free flow_dv_counter_release().
8684  */
8685 static void
8686 flow_dv_counter_free(struct rte_eth_dev *dev, struct mlx5_flow_counter *cnt)
8687 {
8688         flow_dv_shared_lock(dev);
8689         flow_dv_counter_release(dev, cnt);
8690         flow_dv_shared_unlock(dev);
8691 }
8692
8693 const struct mlx5_flow_driver_ops mlx5_flow_dv_drv_ops = {
8694         .validate = flow_dv_validate,
8695         .prepare = flow_dv_prepare,
8696         .translate = flow_dv_translate,
8697         .apply = flow_dv_apply,
8698         .remove = flow_dv_remove,
8699         .destroy = flow_dv_destroy,
8700         .query = flow_dv_query,
8701         .create_mtr_tbls = flow_dv_create_mtr_tbl,
8702         .destroy_mtr_tbls = flow_dv_destroy_mtr_tbl,
8703         .create_policer_rules = flow_dv_create_policer_rules,
8704         .destroy_policer_rules = flow_dv_destroy_policer_rules,
8705         .counter_alloc = flow_dv_counter_allocate,
8706         .counter_free = flow_dv_counter_free,
8707         .counter_query = flow_dv_counter_query,
8708 };
8709
8710 #endif /* HAVE_IBV_FLOW_DV_SUPPORT */