net/mlx5: zero out UDP checksum in encapsulation
[dpdk.git] / drivers / net / mlx5 / mlx5_flow_dv.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2018 Mellanox Technologies, Ltd
3  */
4
5 #include <sys/queue.h>
6 #include <stdalign.h>
7 #include <stdint.h>
8 #include <string.h>
9
10 /* Verbs header. */
11 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
12 #ifdef PEDANTIC
13 #pragma GCC diagnostic ignored "-Wpedantic"
14 #endif
15 #include <infiniband/verbs.h>
16 #ifdef PEDANTIC
17 #pragma GCC diagnostic error "-Wpedantic"
18 #endif
19
20 #include <rte_common.h>
21 #include <rte_ether.h>
22 #include <rte_ethdev_driver.h>
23 #include <rte_flow.h>
24 #include <rte_flow_driver.h>
25 #include <rte_malloc.h>
26 #include <rte_ip.h>
27 #include <rte_gre.h>
28
29 #include "mlx5.h"
30 #include "mlx5_defs.h"
31 #include "mlx5_glue.h"
32 #include "mlx5_flow.h"
33 #include "mlx5_prm.h"
34 #include "mlx5_rxtx.h"
35
36 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
37
38 #ifndef HAVE_IBV_FLOW_DEVX_COUNTERS
39 #define MLX5DV_FLOW_ACTION_COUNTERS_DEVX 0
40 #endif
41
42 #ifndef HAVE_MLX5DV_DR_ESWITCH
43 #ifndef MLX5DV_FLOW_TABLE_TYPE_FDB
44 #define MLX5DV_FLOW_TABLE_TYPE_FDB 0
45 #endif
46 #endif
47
48 #ifndef HAVE_MLX5DV_DR
49 #define MLX5DV_DR_ACTION_FLAGS_ROOT_LEVEL 1
50 #endif
51
52 union flow_dv_attr {
53         struct {
54                 uint32_t valid:1;
55                 uint32_t ipv4:1;
56                 uint32_t ipv6:1;
57                 uint32_t tcp:1;
58                 uint32_t udp:1;
59                 uint32_t reserved:27;
60         };
61         uint32_t attr;
62 };
63
64 /**
65  * Initialize flow attributes structure according to flow items' types.
66  *
67  * @param[in] item
68  *   Pointer to item specification.
69  * @param[out] attr
70  *   Pointer to flow attributes structure.
71  */
72 static void
73 flow_dv_attr_init(const struct rte_flow_item *item, union flow_dv_attr *attr)
74 {
75         for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
76                 switch (item->type) {
77                 case RTE_FLOW_ITEM_TYPE_IPV4:
78                         attr->ipv4 = 1;
79                         break;
80                 case RTE_FLOW_ITEM_TYPE_IPV6:
81                         attr->ipv6 = 1;
82                         break;
83                 case RTE_FLOW_ITEM_TYPE_UDP:
84                         attr->udp = 1;
85                         break;
86                 case RTE_FLOW_ITEM_TYPE_TCP:
87                         attr->tcp = 1;
88                         break;
89                 default:
90                         break;
91                 }
92         }
93         attr->valid = 1;
94 }
95
96 struct field_modify_info {
97         uint32_t size; /* Size of field in protocol header, in bytes. */
98         uint32_t offset; /* Offset of field in protocol header, in bytes. */
99         enum mlx5_modification_field id;
100 };
101
102 struct field_modify_info modify_eth[] = {
103         {4,  0, MLX5_MODI_OUT_DMAC_47_16},
104         {2,  4, MLX5_MODI_OUT_DMAC_15_0},
105         {4,  6, MLX5_MODI_OUT_SMAC_47_16},
106         {2, 10, MLX5_MODI_OUT_SMAC_15_0},
107         {0, 0, 0},
108 };
109
110 struct field_modify_info modify_ipv4[] = {
111         {1,  8, MLX5_MODI_OUT_IPV4_TTL},
112         {4, 12, MLX5_MODI_OUT_SIPV4},
113         {4, 16, MLX5_MODI_OUT_DIPV4},
114         {0, 0, 0},
115 };
116
117 struct field_modify_info modify_ipv6[] = {
118         {1,  7, MLX5_MODI_OUT_IPV6_HOPLIMIT},
119         {4,  8, MLX5_MODI_OUT_SIPV6_127_96},
120         {4, 12, MLX5_MODI_OUT_SIPV6_95_64},
121         {4, 16, MLX5_MODI_OUT_SIPV6_63_32},
122         {4, 20, MLX5_MODI_OUT_SIPV6_31_0},
123         {4, 24, MLX5_MODI_OUT_DIPV6_127_96},
124         {4, 28, MLX5_MODI_OUT_DIPV6_95_64},
125         {4, 32, MLX5_MODI_OUT_DIPV6_63_32},
126         {4, 36, MLX5_MODI_OUT_DIPV6_31_0},
127         {0, 0, 0},
128 };
129
130 struct field_modify_info modify_udp[] = {
131         {2, 0, MLX5_MODI_OUT_UDP_SPORT},
132         {2, 2, MLX5_MODI_OUT_UDP_DPORT},
133         {0, 0, 0},
134 };
135
136 struct field_modify_info modify_tcp[] = {
137         {2, 0, MLX5_MODI_OUT_TCP_SPORT},
138         {2, 2, MLX5_MODI_OUT_TCP_DPORT},
139         {4, 4, MLX5_MODI_OUT_TCP_SEQ_NUM},
140         {4, 8, MLX5_MODI_OUT_TCP_ACK_NUM},
141         {0, 0, 0},
142 };
143
144 /**
145  * Acquire the synchronizing object to protect multithreaded access
146  * to shared dv context. Lock occurs only if context is actually
147  * shared, i.e. we have multiport IB device and representors are
148  * created.
149  *
150  * @param[in] dev
151  *   Pointer to the rte_eth_dev structure.
152  */
153 static void
154 flow_d_shared_lock(struct rte_eth_dev *dev)
155 {
156         struct mlx5_priv *priv = dev->data->dev_private;
157         struct mlx5_ibv_shared *sh = priv->sh;
158
159         if (sh->dv_refcnt > 1) {
160                 int ret;
161
162                 ret = pthread_mutex_lock(&sh->dv_mutex);
163                 assert(!ret);
164                 (void)ret;
165         }
166 }
167
168 static void
169 flow_d_shared_unlock(struct rte_eth_dev *dev)
170 {
171         struct mlx5_priv *priv = dev->data->dev_private;
172         struct mlx5_ibv_shared *sh = priv->sh;
173
174         if (sh->dv_refcnt > 1) {
175                 int ret;
176
177                 ret = pthread_mutex_unlock(&sh->dv_mutex);
178                 assert(!ret);
179                 (void)ret;
180         }
181 }
182
183 /**
184  * Convert modify-header action to DV specification.
185  *
186  * @param[in] item
187  *   Pointer to item specification.
188  * @param[in] field
189  *   Pointer to field modification information.
190  * @param[in,out] resource
191  *   Pointer to the modify-header resource.
192  * @param[in] type
193  *   Type of modification.
194  * @param[out] error
195  *   Pointer to the error structure.
196  *
197  * @return
198  *   0 on success, a negative errno value otherwise and rte_errno is set.
199  */
200 static int
201 flow_dv_convert_modify_action(struct rte_flow_item *item,
202                               struct field_modify_info *field,
203                               struct mlx5_flow_dv_modify_hdr_resource *resource,
204                               uint32_t type,
205                               struct rte_flow_error *error)
206 {
207         uint32_t i = resource->actions_num;
208         struct mlx5_modification_cmd *actions = resource->actions;
209         const uint8_t *spec = item->spec;
210         const uint8_t *mask = item->mask;
211         uint32_t set;
212
213         while (field->size) {
214                 set = 0;
215                 /* Generate modify command for each mask segment. */
216                 memcpy(&set, &mask[field->offset], field->size);
217                 if (set) {
218                         if (i >= MLX5_MODIFY_NUM)
219                                 return rte_flow_error_set(error, EINVAL,
220                                          RTE_FLOW_ERROR_TYPE_ACTION, NULL,
221                                          "too many items to modify");
222                         actions[i].action_type = type;
223                         actions[i].field = field->id;
224                         actions[i].length = field->size ==
225                                         4 ? 0 : field->size * 8;
226                         rte_memcpy(&actions[i].data[4 - field->size],
227                                    &spec[field->offset], field->size);
228                         actions[i].data0 = rte_cpu_to_be_32(actions[i].data0);
229                         ++i;
230                 }
231                 if (resource->actions_num != i)
232                         resource->actions_num = i;
233                 field++;
234         }
235         if (!resource->actions_num)
236                 return rte_flow_error_set(error, EINVAL,
237                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
238                                           "invalid modification flow item");
239         return 0;
240 }
241
242 /**
243  * Convert modify-header set IPv4 address action to DV specification.
244  *
245  * @param[in,out] resource
246  *   Pointer to the modify-header resource.
247  * @param[in] action
248  *   Pointer to action specification.
249  * @param[out] error
250  *   Pointer to the error structure.
251  *
252  * @return
253  *   0 on success, a negative errno value otherwise and rte_errno is set.
254  */
255 static int
256 flow_dv_convert_action_modify_ipv4
257                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
258                          const struct rte_flow_action *action,
259                          struct rte_flow_error *error)
260 {
261         const struct rte_flow_action_set_ipv4 *conf =
262                 (const struct rte_flow_action_set_ipv4 *)(action->conf);
263         struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_IPV4 };
264         struct rte_flow_item_ipv4 ipv4;
265         struct rte_flow_item_ipv4 ipv4_mask;
266
267         memset(&ipv4, 0, sizeof(ipv4));
268         memset(&ipv4_mask, 0, sizeof(ipv4_mask));
269         if (action->type == RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC) {
270                 ipv4.hdr.src_addr = conf->ipv4_addr;
271                 ipv4_mask.hdr.src_addr = rte_flow_item_ipv4_mask.hdr.src_addr;
272         } else {
273                 ipv4.hdr.dst_addr = conf->ipv4_addr;
274                 ipv4_mask.hdr.dst_addr = rte_flow_item_ipv4_mask.hdr.dst_addr;
275         }
276         item.spec = &ipv4;
277         item.mask = &ipv4_mask;
278         return flow_dv_convert_modify_action(&item, modify_ipv4, resource,
279                                              MLX5_MODIFICATION_TYPE_SET, error);
280 }
281
282 /**
283  * Convert modify-header set IPv6 address action to DV specification.
284  *
285  * @param[in,out] resource
286  *   Pointer to the modify-header resource.
287  * @param[in] action
288  *   Pointer to action specification.
289  * @param[out] error
290  *   Pointer to the error structure.
291  *
292  * @return
293  *   0 on success, a negative errno value otherwise and rte_errno is set.
294  */
295 static int
296 flow_dv_convert_action_modify_ipv6
297                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
298                          const struct rte_flow_action *action,
299                          struct rte_flow_error *error)
300 {
301         const struct rte_flow_action_set_ipv6 *conf =
302                 (const struct rte_flow_action_set_ipv6 *)(action->conf);
303         struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_IPV6 };
304         struct rte_flow_item_ipv6 ipv6;
305         struct rte_flow_item_ipv6 ipv6_mask;
306
307         memset(&ipv6, 0, sizeof(ipv6));
308         memset(&ipv6_mask, 0, sizeof(ipv6_mask));
309         if (action->type == RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC) {
310                 memcpy(&ipv6.hdr.src_addr, &conf->ipv6_addr,
311                        sizeof(ipv6.hdr.src_addr));
312                 memcpy(&ipv6_mask.hdr.src_addr,
313                        &rte_flow_item_ipv6_mask.hdr.src_addr,
314                        sizeof(ipv6.hdr.src_addr));
315         } else {
316                 memcpy(&ipv6.hdr.dst_addr, &conf->ipv6_addr,
317                        sizeof(ipv6.hdr.dst_addr));
318                 memcpy(&ipv6_mask.hdr.dst_addr,
319                        &rte_flow_item_ipv6_mask.hdr.dst_addr,
320                        sizeof(ipv6.hdr.dst_addr));
321         }
322         item.spec = &ipv6;
323         item.mask = &ipv6_mask;
324         return flow_dv_convert_modify_action(&item, modify_ipv6, resource,
325                                              MLX5_MODIFICATION_TYPE_SET, error);
326 }
327
328 /**
329  * Convert modify-header set MAC address action to DV specification.
330  *
331  * @param[in,out] resource
332  *   Pointer to the modify-header resource.
333  * @param[in] action
334  *   Pointer to action specification.
335  * @param[out] error
336  *   Pointer to the error structure.
337  *
338  * @return
339  *   0 on success, a negative errno value otherwise and rte_errno is set.
340  */
341 static int
342 flow_dv_convert_action_modify_mac
343                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
344                          const struct rte_flow_action *action,
345                          struct rte_flow_error *error)
346 {
347         const struct rte_flow_action_set_mac *conf =
348                 (const struct rte_flow_action_set_mac *)(action->conf);
349         struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_ETH };
350         struct rte_flow_item_eth eth;
351         struct rte_flow_item_eth eth_mask;
352
353         memset(&eth, 0, sizeof(eth));
354         memset(&eth_mask, 0, sizeof(eth_mask));
355         if (action->type == RTE_FLOW_ACTION_TYPE_SET_MAC_SRC) {
356                 memcpy(&eth.src.addr_bytes, &conf->mac_addr,
357                        sizeof(eth.src.addr_bytes));
358                 memcpy(&eth_mask.src.addr_bytes,
359                        &rte_flow_item_eth_mask.src.addr_bytes,
360                        sizeof(eth_mask.src.addr_bytes));
361         } else {
362                 memcpy(&eth.dst.addr_bytes, &conf->mac_addr,
363                        sizeof(eth.dst.addr_bytes));
364                 memcpy(&eth_mask.dst.addr_bytes,
365                        &rte_flow_item_eth_mask.dst.addr_bytes,
366                        sizeof(eth_mask.dst.addr_bytes));
367         }
368         item.spec = &eth;
369         item.mask = &eth_mask;
370         return flow_dv_convert_modify_action(&item, modify_eth, resource,
371                                              MLX5_MODIFICATION_TYPE_SET, error);
372 }
373
374 /**
375  * Convert modify-header set TP action to DV specification.
376  *
377  * @param[in,out] resource
378  *   Pointer to the modify-header resource.
379  * @param[in] action
380  *   Pointer to action specification.
381  * @param[in] items
382  *   Pointer to rte_flow_item objects list.
383  * @param[in] attr
384  *   Pointer to flow attributes structure.
385  * @param[out] error
386  *   Pointer to the error structure.
387  *
388  * @return
389  *   0 on success, a negative errno value otherwise and rte_errno is set.
390  */
391 static int
392 flow_dv_convert_action_modify_tp
393                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
394                          const struct rte_flow_action *action,
395                          const struct rte_flow_item *items,
396                          union flow_dv_attr *attr,
397                          struct rte_flow_error *error)
398 {
399         const struct rte_flow_action_set_tp *conf =
400                 (const struct rte_flow_action_set_tp *)(action->conf);
401         struct rte_flow_item item;
402         struct rte_flow_item_udp udp;
403         struct rte_flow_item_udp udp_mask;
404         struct rte_flow_item_tcp tcp;
405         struct rte_flow_item_tcp tcp_mask;
406         struct field_modify_info *field;
407
408         if (!attr->valid)
409                 flow_dv_attr_init(items, attr);
410         if (attr->udp) {
411                 memset(&udp, 0, sizeof(udp));
412                 memset(&udp_mask, 0, sizeof(udp_mask));
413                 if (action->type == RTE_FLOW_ACTION_TYPE_SET_TP_SRC) {
414                         udp.hdr.src_port = conf->port;
415                         udp_mask.hdr.src_port =
416                                         rte_flow_item_udp_mask.hdr.src_port;
417                 } else {
418                         udp.hdr.dst_port = conf->port;
419                         udp_mask.hdr.dst_port =
420                                         rte_flow_item_udp_mask.hdr.dst_port;
421                 }
422                 item.type = RTE_FLOW_ITEM_TYPE_UDP;
423                 item.spec = &udp;
424                 item.mask = &udp_mask;
425                 field = modify_udp;
426         }
427         if (attr->tcp) {
428                 memset(&tcp, 0, sizeof(tcp));
429                 memset(&tcp_mask, 0, sizeof(tcp_mask));
430                 if (action->type == RTE_FLOW_ACTION_TYPE_SET_TP_SRC) {
431                         tcp.hdr.src_port = conf->port;
432                         tcp_mask.hdr.src_port =
433                                         rte_flow_item_tcp_mask.hdr.src_port;
434                 } else {
435                         tcp.hdr.dst_port = conf->port;
436                         tcp_mask.hdr.dst_port =
437                                         rte_flow_item_tcp_mask.hdr.dst_port;
438                 }
439                 item.type = RTE_FLOW_ITEM_TYPE_TCP;
440                 item.spec = &tcp;
441                 item.mask = &tcp_mask;
442                 field = modify_tcp;
443         }
444         return flow_dv_convert_modify_action(&item, field, resource,
445                                              MLX5_MODIFICATION_TYPE_SET, error);
446 }
447
448 /**
449  * Convert modify-header set TTL action to DV specification.
450  *
451  * @param[in,out] resource
452  *   Pointer to the modify-header resource.
453  * @param[in] action
454  *   Pointer to action specification.
455  * @param[in] items
456  *   Pointer to rte_flow_item objects list.
457  * @param[in] attr
458  *   Pointer to flow attributes structure.
459  * @param[out] error
460  *   Pointer to the error structure.
461  *
462  * @return
463  *   0 on success, a negative errno value otherwise and rte_errno is set.
464  */
465 static int
466 flow_dv_convert_action_modify_ttl
467                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
468                          const struct rte_flow_action *action,
469                          const struct rte_flow_item *items,
470                          union flow_dv_attr *attr,
471                          struct rte_flow_error *error)
472 {
473         const struct rte_flow_action_set_ttl *conf =
474                 (const struct rte_flow_action_set_ttl *)(action->conf);
475         struct rte_flow_item item;
476         struct rte_flow_item_ipv4 ipv4;
477         struct rte_flow_item_ipv4 ipv4_mask;
478         struct rte_flow_item_ipv6 ipv6;
479         struct rte_flow_item_ipv6 ipv6_mask;
480         struct field_modify_info *field;
481
482         if (!attr->valid)
483                 flow_dv_attr_init(items, attr);
484         if (attr->ipv4) {
485                 memset(&ipv4, 0, sizeof(ipv4));
486                 memset(&ipv4_mask, 0, sizeof(ipv4_mask));
487                 ipv4.hdr.time_to_live = conf->ttl_value;
488                 ipv4_mask.hdr.time_to_live = 0xFF;
489                 item.type = RTE_FLOW_ITEM_TYPE_IPV4;
490                 item.spec = &ipv4;
491                 item.mask = &ipv4_mask;
492                 field = modify_ipv4;
493         }
494         if (attr->ipv6) {
495                 memset(&ipv6, 0, sizeof(ipv6));
496                 memset(&ipv6_mask, 0, sizeof(ipv6_mask));
497                 ipv6.hdr.hop_limits = conf->ttl_value;
498                 ipv6_mask.hdr.hop_limits = 0xFF;
499                 item.type = RTE_FLOW_ITEM_TYPE_IPV6;
500                 item.spec = &ipv6;
501                 item.mask = &ipv6_mask;
502                 field = modify_ipv6;
503         }
504         return flow_dv_convert_modify_action(&item, field, resource,
505                                              MLX5_MODIFICATION_TYPE_SET, error);
506 }
507
508 /**
509  * Convert modify-header decrement TTL action to DV specification.
510  *
511  * @param[in,out] resource
512  *   Pointer to the modify-header resource.
513  * @param[in] action
514  *   Pointer to action specification.
515  * @param[in] items
516  *   Pointer to rte_flow_item objects list.
517  * @param[in] attr
518  *   Pointer to flow attributes structure.
519  * @param[out] error
520  *   Pointer to the error structure.
521  *
522  * @return
523  *   0 on success, a negative errno value otherwise and rte_errno is set.
524  */
525 static int
526 flow_dv_convert_action_modify_dec_ttl
527                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
528                          const struct rte_flow_item *items,
529                          union flow_dv_attr *attr,
530                          struct rte_flow_error *error)
531 {
532         struct rte_flow_item item;
533         struct rte_flow_item_ipv4 ipv4;
534         struct rte_flow_item_ipv4 ipv4_mask;
535         struct rte_flow_item_ipv6 ipv6;
536         struct rte_flow_item_ipv6 ipv6_mask;
537         struct field_modify_info *field;
538
539         if (!attr->valid)
540                 flow_dv_attr_init(items, attr);
541         if (attr->ipv4) {
542                 memset(&ipv4, 0, sizeof(ipv4));
543                 memset(&ipv4_mask, 0, sizeof(ipv4_mask));
544                 ipv4.hdr.time_to_live = 0xFF;
545                 ipv4_mask.hdr.time_to_live = 0xFF;
546                 item.type = RTE_FLOW_ITEM_TYPE_IPV4;
547                 item.spec = &ipv4;
548                 item.mask = &ipv4_mask;
549                 field = modify_ipv4;
550         }
551         if (attr->ipv6) {
552                 memset(&ipv6, 0, sizeof(ipv6));
553                 memset(&ipv6_mask, 0, sizeof(ipv6_mask));
554                 ipv6.hdr.hop_limits = 0xFF;
555                 ipv6_mask.hdr.hop_limits = 0xFF;
556                 item.type = RTE_FLOW_ITEM_TYPE_IPV6;
557                 item.spec = &ipv6;
558                 item.mask = &ipv6_mask;
559                 field = modify_ipv6;
560         }
561         return flow_dv_convert_modify_action(&item, field, resource,
562                                              MLX5_MODIFICATION_TYPE_ADD, error);
563 }
564
565 /**
566  * Convert modify-header increment/decrement TCP Sequence number
567  * to DV specification.
568  *
569  * @param[in,out] resource
570  *   Pointer to the modify-header resource.
571  * @param[in] action
572  *   Pointer to action specification.
573  * @param[out] error
574  *   Pointer to the error structure.
575  *
576  * @return
577  *   0 on success, a negative errno value otherwise and rte_errno is set.
578  */
579 static int
580 flow_dv_convert_action_modify_tcp_seq
581                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
582                          const struct rte_flow_action *action,
583                          struct rte_flow_error *error)
584 {
585         const rte_be32_t *conf = (const rte_be32_t *)(action->conf);
586         uint64_t value = rte_be_to_cpu_32(*conf);
587         struct rte_flow_item item;
588         struct rte_flow_item_tcp tcp;
589         struct rte_flow_item_tcp tcp_mask;
590
591         memset(&tcp, 0, sizeof(tcp));
592         memset(&tcp_mask, 0, sizeof(tcp_mask));
593         if (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ)
594                 /*
595                  * The HW has no decrement operation, only increment operation.
596                  * To simulate decrement X from Y using increment operation
597                  * we need to add UINT32_MAX X times to Y.
598                  * Each adding of UINT32_MAX decrements Y by 1.
599                  */
600                 value *= UINT32_MAX;
601         tcp.hdr.sent_seq = rte_cpu_to_be_32((uint32_t)value);
602         tcp_mask.hdr.sent_seq = RTE_BE32(UINT32_MAX);
603         item.type = RTE_FLOW_ITEM_TYPE_TCP;
604         item.spec = &tcp;
605         item.mask = &tcp_mask;
606         return flow_dv_convert_modify_action(&item, modify_tcp, resource,
607                                              MLX5_MODIFICATION_TYPE_ADD, error);
608 }
609
610 /**
611  * Convert modify-header increment/decrement TCP Acknowledgment number
612  * to DV specification.
613  *
614  * @param[in,out] resource
615  *   Pointer to the modify-header resource.
616  * @param[in] action
617  *   Pointer to action specification.
618  * @param[out] error
619  *   Pointer to the error structure.
620  *
621  * @return
622  *   0 on success, a negative errno value otherwise and rte_errno is set.
623  */
624 static int
625 flow_dv_convert_action_modify_tcp_ack
626                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
627                          const struct rte_flow_action *action,
628                          struct rte_flow_error *error)
629 {
630         const rte_be32_t *conf = (const rte_be32_t *)(action->conf);
631         uint64_t value = rte_be_to_cpu_32(*conf);
632         struct rte_flow_item item;
633         struct rte_flow_item_tcp tcp;
634         struct rte_flow_item_tcp tcp_mask;
635
636         memset(&tcp, 0, sizeof(tcp));
637         memset(&tcp_mask, 0, sizeof(tcp_mask));
638         if (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK)
639                 /*
640                  * The HW has no decrement operation, only increment operation.
641                  * To simulate decrement X from Y using increment operation
642                  * we need to add UINT32_MAX X times to Y.
643                  * Each adding of UINT32_MAX decrements Y by 1.
644                  */
645                 value *= UINT32_MAX;
646         tcp.hdr.recv_ack = rte_cpu_to_be_32((uint32_t)value);
647         tcp_mask.hdr.recv_ack = RTE_BE32(UINT32_MAX);
648         item.type = RTE_FLOW_ITEM_TYPE_TCP;
649         item.spec = &tcp;
650         item.mask = &tcp_mask;
651         return flow_dv_convert_modify_action(&item, modify_tcp, resource,
652                                              MLX5_MODIFICATION_TYPE_ADD, error);
653 }
654
655 /**
656  * Validate META item.
657  *
658  * @param[in] dev
659  *   Pointer to the rte_eth_dev structure.
660  * @param[in] item
661  *   Item specification.
662  * @param[in] attr
663  *   Attributes of flow that includes this item.
664  * @param[out] error
665  *   Pointer to error structure.
666  *
667  * @return
668  *   0 on success, a negative errno value otherwise and rte_errno is set.
669  */
670 static int
671 flow_dv_validate_item_meta(struct rte_eth_dev *dev,
672                            const struct rte_flow_item *item,
673                            const struct rte_flow_attr *attr,
674                            struct rte_flow_error *error)
675 {
676         const struct rte_flow_item_meta *spec = item->spec;
677         const struct rte_flow_item_meta *mask = item->mask;
678         const struct rte_flow_item_meta nic_mask = {
679                 .data = RTE_BE32(UINT32_MAX)
680         };
681         int ret;
682         uint64_t offloads = dev->data->dev_conf.txmode.offloads;
683
684         if (!(offloads & DEV_TX_OFFLOAD_MATCH_METADATA))
685                 return rte_flow_error_set(error, EPERM,
686                                           RTE_FLOW_ERROR_TYPE_ITEM,
687                                           NULL,
688                                           "match on metadata offload "
689                                           "configuration is off for this port");
690         if (!spec)
691                 return rte_flow_error_set(error, EINVAL,
692                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
693                                           item->spec,
694                                           "data cannot be empty");
695         if (!spec->data)
696                 return rte_flow_error_set(error, EINVAL,
697                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
698                                           NULL,
699                                           "data cannot be zero");
700         if (!mask)
701                 mask = &rte_flow_item_meta_mask;
702         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
703                                         (const uint8_t *)&nic_mask,
704                                         sizeof(struct rte_flow_item_meta),
705                                         error);
706         if (ret < 0)
707                 return ret;
708         if (attr->ingress)
709                 return rte_flow_error_set(error, ENOTSUP,
710                                           RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
711                                           NULL,
712                                           "pattern not supported for ingress");
713         return 0;
714 }
715
716 /**
717  * Validate vport item.
718  *
719  * @param[in] dev
720  *   Pointer to the rte_eth_dev structure.
721  * @param[in] item
722  *   Item specification.
723  * @param[in] attr
724  *   Attributes of flow that includes this item.
725  * @param[in] item_flags
726  *   Bit-fields that holds the items detected until now.
727  * @param[out] error
728  *   Pointer to error structure.
729  *
730  * @return
731  *   0 on success, a negative errno value otherwise and rte_errno is set.
732  */
733 static int
734 flow_dv_validate_item_port_id(struct rte_eth_dev *dev,
735                               const struct rte_flow_item *item,
736                               const struct rte_flow_attr *attr,
737                               uint64_t item_flags,
738                               struct rte_flow_error *error)
739 {
740         const struct rte_flow_item_port_id *spec = item->spec;
741         const struct rte_flow_item_port_id *mask = item->mask;
742         const struct rte_flow_item_port_id switch_mask = {
743                         .id = 0xffffffff,
744         };
745         uint16_t esw_domain_id;
746         uint16_t item_port_esw_domain_id;
747         int ret;
748
749         if (!attr->transfer)
750                 return rte_flow_error_set(error, EINVAL,
751                                           RTE_FLOW_ERROR_TYPE_ITEM,
752                                           NULL,
753                                           "match on port id is valid only"
754                                           " when transfer flag is enabled");
755         if (item_flags & MLX5_FLOW_ITEM_PORT_ID)
756                 return rte_flow_error_set(error, ENOTSUP,
757                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
758                                           "multiple source ports are not"
759                                           " supported");
760         if (!mask)
761                 mask = &switch_mask;
762         if (mask->id != 0xffffffff)
763                 return rte_flow_error_set(error, ENOTSUP,
764                                            RTE_FLOW_ERROR_TYPE_ITEM_MASK,
765                                            mask,
766                                            "no support for partial mask on"
767                                            " \"id\" field");
768         ret = mlx5_flow_item_acceptable
769                                 (item, (const uint8_t *)mask,
770                                  (const uint8_t *)&rte_flow_item_port_id_mask,
771                                  sizeof(struct rte_flow_item_port_id),
772                                  error);
773         if (ret)
774                 return ret;
775         if (!spec)
776                 return 0;
777         ret = mlx5_port_to_eswitch_info(spec->id, &item_port_esw_domain_id,
778                                         NULL);
779         if (ret)
780                 return rte_flow_error_set(error, -ret,
781                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC, spec,
782                                           "failed to obtain E-Switch info for"
783                                           " port");
784         ret = mlx5_port_to_eswitch_info(dev->data->port_id,
785                                         &esw_domain_id, NULL);
786         if (ret < 0)
787                 return rte_flow_error_set(error, -ret,
788                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
789                                           NULL,
790                                           "failed to obtain E-Switch info");
791         if (item_port_esw_domain_id != esw_domain_id)
792                 return rte_flow_error_set(error, -ret,
793                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC, spec,
794                                           "cannot match on a port from a"
795                                           " different E-Switch");
796         return 0;
797 }
798
799 /**
800  * Validate count action.
801  *
802  * @param[in] dev
803  *   device otr.
804  * @param[out] error
805  *   Pointer to error structure.
806  *
807  * @return
808  *   0 on success, a negative errno value otherwise and rte_errno is set.
809  */
810 static int
811 flow_dv_validate_action_count(struct rte_eth_dev *dev,
812                               struct rte_flow_error *error)
813 {
814         struct mlx5_priv *priv = dev->data->dev_private;
815
816         if (!priv->config.devx)
817                 goto notsup_err;
818 #ifdef HAVE_IBV_FLOW_DEVX_COUNTERS
819         return 0;
820 #endif
821 notsup_err:
822         return rte_flow_error_set
823                       (error, ENOTSUP,
824                        RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
825                        NULL,
826                        "count action not supported");
827 }
828
829 /**
830  * Validate the L2 encap action.
831  *
832  * @param[in] action_flags
833  *   Holds the actions detected until now.
834  * @param[in] action
835  *   Pointer to the encap action.
836  * @param[in] attr
837  *   Pointer to flow attributes
838  * @param[out] error
839  *   Pointer to error structure.
840  *
841  * @return
842  *   0 on success, a negative errno value otherwise and rte_errno is set.
843  */
844 static int
845 flow_dv_validate_action_l2_encap(uint64_t action_flags,
846                                  const struct rte_flow_action *action,
847                                  const struct rte_flow_attr *attr,
848                                  struct rte_flow_error *error)
849 {
850         if (!(action->conf))
851                 return rte_flow_error_set(error, EINVAL,
852                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
853                                           "configuration cannot be null");
854         if (action_flags & MLX5_FLOW_ACTION_DROP)
855                 return rte_flow_error_set(error, EINVAL,
856                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
857                                           "can't drop and encap in same flow");
858         if (action_flags & (MLX5_FLOW_ENCAP_ACTIONS | MLX5_FLOW_DECAP_ACTIONS))
859                 return rte_flow_error_set(error, EINVAL,
860                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
861                                           "can only have a single encap or"
862                                           " decap action in a flow");
863         if (!attr->transfer && attr->ingress)
864                 return rte_flow_error_set(error, ENOTSUP,
865                                           RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
866                                           NULL,
867                                           "encap action not supported for "
868                                           "ingress");
869         return 0;
870 }
871
872 /**
873  * Validate the L2 decap action.
874  *
875  * @param[in] action_flags
876  *   Holds the actions detected until now.
877  * @param[in] attr
878  *   Pointer to flow attributes
879  * @param[out] error
880  *   Pointer to error structure.
881  *
882  * @return
883  *   0 on success, a negative errno value otherwise and rte_errno is set.
884  */
885 static int
886 flow_dv_validate_action_l2_decap(uint64_t action_flags,
887                                  const struct rte_flow_attr *attr,
888                                  struct rte_flow_error *error)
889 {
890         if (action_flags & MLX5_FLOW_ACTION_DROP)
891                 return rte_flow_error_set(error, EINVAL,
892                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
893                                           "can't drop and decap in same flow");
894         if (action_flags & (MLX5_FLOW_ENCAP_ACTIONS | MLX5_FLOW_DECAP_ACTIONS))
895                 return rte_flow_error_set(error, EINVAL,
896                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
897                                           "can only have a single encap or"
898                                           " decap action in a flow");
899         if (action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS)
900                 return rte_flow_error_set(error, EINVAL,
901                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
902                                           "can't have decap action after"
903                                           " modify action");
904         if (attr->egress)
905                 return rte_flow_error_set(error, ENOTSUP,
906                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
907                                           NULL,
908                                           "decap action not supported for "
909                                           "egress");
910         return 0;
911 }
912
913 /**
914  * Validate the raw encap action.
915  *
916  * @param[in] action_flags
917  *   Holds the actions detected until now.
918  * @param[in] action
919  *   Pointer to the encap action.
920  * @param[in] attr
921  *   Pointer to flow attributes
922  * @param[out] error
923  *   Pointer to error structure.
924  *
925  * @return
926  *   0 on success, a negative errno value otherwise and rte_errno is set.
927  */
928 static int
929 flow_dv_validate_action_raw_encap(uint64_t action_flags,
930                                   const struct rte_flow_action *action,
931                                   const struct rte_flow_attr *attr,
932                                   struct rte_flow_error *error)
933 {
934         if (!(action->conf))
935                 return rte_flow_error_set(error, EINVAL,
936                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
937                                           "configuration cannot be null");
938         if (action_flags & MLX5_FLOW_ACTION_DROP)
939                 return rte_flow_error_set(error, EINVAL,
940                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
941                                           "can't drop and encap in same flow");
942         if (action_flags & MLX5_FLOW_ENCAP_ACTIONS)
943                 return rte_flow_error_set(error, EINVAL,
944                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
945                                           "can only have a single encap"
946                                           " action in a flow");
947         /* encap without preceding decap is not supported for ingress */
948         if (!attr->transfer &&  attr->ingress &&
949             !(action_flags & MLX5_FLOW_ACTION_RAW_DECAP))
950                 return rte_flow_error_set(error, ENOTSUP,
951                                           RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
952                                           NULL,
953                                           "encap action not supported for "
954                                           "ingress");
955         return 0;
956 }
957
958 /**
959  * Validate the raw decap action.
960  *
961  * @param[in] action_flags
962  *   Holds the actions detected until now.
963  * @param[in] action
964  *   Pointer to the encap action.
965  * @param[in] attr
966  *   Pointer to flow attributes
967  * @param[out] error
968  *   Pointer to error structure.
969  *
970  * @return
971  *   0 on success, a negative errno value otherwise and rte_errno is set.
972  */
973 static int
974 flow_dv_validate_action_raw_decap(uint64_t action_flags,
975                                   const struct rte_flow_action *action,
976                                   const struct rte_flow_attr *attr,
977                                   struct rte_flow_error *error)
978 {
979         if (action_flags & MLX5_FLOW_ACTION_DROP)
980                 return rte_flow_error_set(error, EINVAL,
981                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
982                                           "can't drop and decap in same flow");
983         if (action_flags & MLX5_FLOW_ENCAP_ACTIONS)
984                 return rte_flow_error_set(error, EINVAL,
985                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
986                                           "can't have encap action before"
987                                           " decap action");
988         if (action_flags & MLX5_FLOW_DECAP_ACTIONS)
989                 return rte_flow_error_set(error, EINVAL,
990                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
991                                           "can only have a single decap"
992                                           " action in a flow");
993         if (action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS)
994                 return rte_flow_error_set(error, EINVAL,
995                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
996                                           "can't have decap action after"
997                                           " modify action");
998         /* decap action is valid on egress only if it is followed by encap */
999         if (attr->egress) {
1000                 for (; action->type != RTE_FLOW_ACTION_TYPE_END &&
1001                        action->type != RTE_FLOW_ACTION_TYPE_RAW_ENCAP;
1002                        action++) {
1003                 }
1004                 if (action->type != RTE_FLOW_ACTION_TYPE_RAW_ENCAP)
1005                         return rte_flow_error_set
1006                                         (error, ENOTSUP,
1007                                          RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
1008                                          NULL, "decap action not supported"
1009                                          " for egress");
1010         }
1011         return 0;
1012 }
1013
1014 /**
1015  * Find existing encap/decap resource or create and register a new one.
1016  *
1017  * @param dev[in, out]
1018  *   Pointer to rte_eth_dev structure.
1019  * @param[in, out] resource
1020  *   Pointer to encap/decap resource.
1021  * @parm[in, out] dev_flow
1022  *   Pointer to the dev_flow.
1023  * @param[out] error
1024  *   pointer to error structure.
1025  *
1026  * @return
1027  *   0 on success otherwise -errno and errno is set.
1028  */
1029 static int
1030 flow_dv_encap_decap_resource_register
1031                         (struct rte_eth_dev *dev,
1032                          struct mlx5_flow_dv_encap_decap_resource *resource,
1033                          struct mlx5_flow *dev_flow,
1034                          struct rte_flow_error *error)
1035 {
1036         struct mlx5_priv *priv = dev->data->dev_private;
1037         struct mlx5_ibv_shared *sh = priv->sh;
1038         struct mlx5_flow_dv_encap_decap_resource *cache_resource;
1039         struct rte_flow *flow = dev_flow->flow;
1040         struct mlx5dv_dr_domain *domain;
1041
1042         resource->flags = flow->group ? 0 : 1;
1043         if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB)
1044                 domain = sh->fdb_domain;
1045         else if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_NIC_RX)
1046                 domain = sh->rx_domain;
1047         else
1048                 domain = sh->tx_domain;
1049
1050         /* Lookup a matching resource from cache. */
1051         LIST_FOREACH(cache_resource, &sh->encaps_decaps, next) {
1052                 if (resource->reformat_type == cache_resource->reformat_type &&
1053                     resource->ft_type == cache_resource->ft_type &&
1054                     resource->flags == cache_resource->flags &&
1055                     resource->size == cache_resource->size &&
1056                     !memcmp((const void *)resource->buf,
1057                             (const void *)cache_resource->buf,
1058                             resource->size)) {
1059                         DRV_LOG(DEBUG, "encap/decap resource %p: refcnt %d++",
1060                                 (void *)cache_resource,
1061                                 rte_atomic32_read(&cache_resource->refcnt));
1062                         rte_atomic32_inc(&cache_resource->refcnt);
1063                         dev_flow->dv.encap_decap = cache_resource;
1064                         return 0;
1065                 }
1066         }
1067         /* Register new encap/decap resource. */
1068         cache_resource = rte_calloc(__func__, 1, sizeof(*cache_resource), 0);
1069         if (!cache_resource)
1070                 return rte_flow_error_set(error, ENOMEM,
1071                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1072                                           "cannot allocate resource memory");
1073         *cache_resource = *resource;
1074         cache_resource->verbs_action =
1075                 mlx5_glue->dv_create_flow_action_packet_reformat
1076                         (sh->ctx, cache_resource->reformat_type,
1077                          cache_resource->ft_type, domain, cache_resource->flags,
1078                          cache_resource->size,
1079                          (cache_resource->size ? cache_resource->buf : NULL));
1080         if (!cache_resource->verbs_action) {
1081                 rte_free(cache_resource);
1082                 return rte_flow_error_set(error, ENOMEM,
1083                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1084                                           NULL, "cannot create action");
1085         }
1086         rte_atomic32_init(&cache_resource->refcnt);
1087         rte_atomic32_inc(&cache_resource->refcnt);
1088         LIST_INSERT_HEAD(&sh->encaps_decaps, cache_resource, next);
1089         dev_flow->dv.encap_decap = cache_resource;
1090         DRV_LOG(DEBUG, "new encap/decap resource %p: refcnt %d++",
1091                 (void *)cache_resource,
1092                 rte_atomic32_read(&cache_resource->refcnt));
1093         return 0;
1094 }
1095
1096 /**
1097  * Find existing table jump resource or create and register a new one.
1098  *
1099  * @param dev[in, out]
1100  *   Pointer to rte_eth_dev structure.
1101  * @param[in, out] resource
1102  *   Pointer to jump table resource.
1103  * @parm[in, out] dev_flow
1104  *   Pointer to the dev_flow.
1105  * @param[out] error
1106  *   pointer to error structure.
1107  *
1108  * @return
1109  *   0 on success otherwise -errno and errno is set.
1110  */
1111 static int
1112 flow_dv_jump_tbl_resource_register
1113                         (struct rte_eth_dev *dev,
1114                          struct mlx5_flow_dv_jump_tbl_resource *resource,
1115                          struct mlx5_flow *dev_flow,
1116                          struct rte_flow_error *error)
1117 {
1118         struct mlx5_priv *priv = dev->data->dev_private;
1119         struct mlx5_ibv_shared *sh = priv->sh;
1120         struct mlx5_flow_dv_jump_tbl_resource *cache_resource;
1121
1122         /* Lookup a matching resource from cache. */
1123         LIST_FOREACH(cache_resource, &sh->jump_tbl, next) {
1124                 if (resource->tbl == cache_resource->tbl) {
1125                         DRV_LOG(DEBUG, "jump table resource resource %p: refcnt %d++",
1126                                 (void *)cache_resource,
1127                                 rte_atomic32_read(&cache_resource->refcnt));
1128                         rte_atomic32_inc(&cache_resource->refcnt);
1129                         dev_flow->dv.jump = cache_resource;
1130                         return 0;
1131                 }
1132         }
1133         /* Register new jump table resource. */
1134         cache_resource = rte_calloc(__func__, 1, sizeof(*cache_resource), 0);
1135         if (!cache_resource)
1136                 return rte_flow_error_set(error, ENOMEM,
1137                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1138                                           "cannot allocate resource memory");
1139         *cache_resource = *resource;
1140         cache_resource->action =
1141                 mlx5_glue->dr_create_flow_action_dest_flow_tbl
1142                 (resource->tbl->obj);
1143         if (!cache_resource->action) {
1144                 rte_free(cache_resource);
1145                 return rte_flow_error_set(error, ENOMEM,
1146                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1147                                           NULL, "cannot create action");
1148         }
1149         rte_atomic32_init(&cache_resource->refcnt);
1150         rte_atomic32_inc(&cache_resource->refcnt);
1151         LIST_INSERT_HEAD(&sh->jump_tbl, cache_resource, next);
1152         dev_flow->dv.jump = cache_resource;
1153         DRV_LOG(DEBUG, "new jump table  resource %p: refcnt %d++",
1154                 (void *)cache_resource,
1155                 rte_atomic32_read(&cache_resource->refcnt));
1156         return 0;
1157 }
1158
1159 /**
1160  * Find existing table port ID resource or create and register a new one.
1161  *
1162  * @param dev[in, out]
1163  *   Pointer to rte_eth_dev structure.
1164  * @param[in, out] resource
1165  *   Pointer to port ID action resource.
1166  * @parm[in, out] dev_flow
1167  *   Pointer to the dev_flow.
1168  * @param[out] error
1169  *   pointer to error structure.
1170  *
1171  * @return
1172  *   0 on success otherwise -errno and errno is set.
1173  */
1174 static int
1175 flow_dv_port_id_action_resource_register
1176                         (struct rte_eth_dev *dev,
1177                          struct mlx5_flow_dv_port_id_action_resource *resource,
1178                          struct mlx5_flow *dev_flow,
1179                          struct rte_flow_error *error)
1180 {
1181         struct mlx5_priv *priv = dev->data->dev_private;
1182         struct mlx5_ibv_shared *sh = priv->sh;
1183         struct mlx5_flow_dv_port_id_action_resource *cache_resource;
1184
1185         /* Lookup a matching resource from cache. */
1186         LIST_FOREACH(cache_resource, &sh->port_id_action_list, next) {
1187                 if (resource->port_id == cache_resource->port_id) {
1188                         DRV_LOG(DEBUG, "port id action resource resource %p: "
1189                                 "refcnt %d++",
1190                                 (void *)cache_resource,
1191                                 rte_atomic32_read(&cache_resource->refcnt));
1192                         rte_atomic32_inc(&cache_resource->refcnt);
1193                         dev_flow->dv.port_id_action = cache_resource;
1194                         return 0;
1195                 }
1196         }
1197         /* Register new port id action resource. */
1198         cache_resource = rte_calloc(__func__, 1, sizeof(*cache_resource), 0);
1199         if (!cache_resource)
1200                 return rte_flow_error_set(error, ENOMEM,
1201                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1202                                           "cannot allocate resource memory");
1203         *cache_resource = *resource;
1204         cache_resource->action =
1205                 mlx5_glue->dr_create_flow_action_dest_vport
1206                         (priv->sh->fdb_domain, resource->port_id);
1207         if (!cache_resource->action) {
1208                 rte_free(cache_resource);
1209                 return rte_flow_error_set(error, ENOMEM,
1210                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1211                                           NULL, "cannot create action");
1212         }
1213         rte_atomic32_init(&cache_resource->refcnt);
1214         rte_atomic32_inc(&cache_resource->refcnt);
1215         LIST_INSERT_HEAD(&sh->port_id_action_list, cache_resource, next);
1216         dev_flow->dv.port_id_action = cache_resource;
1217         DRV_LOG(DEBUG, "new port id action resource %p: refcnt %d++",
1218                 (void *)cache_resource,
1219                 rte_atomic32_read(&cache_resource->refcnt));
1220         return 0;
1221 }
1222
1223 /**
1224  * Get the size of specific rte_flow_item_type
1225  *
1226  * @param[in] item_type
1227  *   Tested rte_flow_item_type.
1228  *
1229  * @return
1230  *   sizeof struct item_type, 0 if void or irrelevant.
1231  */
1232 static size_t
1233 flow_dv_get_item_len(const enum rte_flow_item_type item_type)
1234 {
1235         size_t retval;
1236
1237         switch (item_type) {
1238         case RTE_FLOW_ITEM_TYPE_ETH:
1239                 retval = sizeof(struct rte_flow_item_eth);
1240                 break;
1241         case RTE_FLOW_ITEM_TYPE_VLAN:
1242                 retval = sizeof(struct rte_flow_item_vlan);
1243                 break;
1244         case RTE_FLOW_ITEM_TYPE_IPV4:
1245                 retval = sizeof(struct rte_flow_item_ipv4);
1246                 break;
1247         case RTE_FLOW_ITEM_TYPE_IPV6:
1248                 retval = sizeof(struct rte_flow_item_ipv6);
1249                 break;
1250         case RTE_FLOW_ITEM_TYPE_UDP:
1251                 retval = sizeof(struct rte_flow_item_udp);
1252                 break;
1253         case RTE_FLOW_ITEM_TYPE_TCP:
1254                 retval = sizeof(struct rte_flow_item_tcp);
1255                 break;
1256         case RTE_FLOW_ITEM_TYPE_VXLAN:
1257                 retval = sizeof(struct rte_flow_item_vxlan);
1258                 break;
1259         case RTE_FLOW_ITEM_TYPE_GRE:
1260                 retval = sizeof(struct rte_flow_item_gre);
1261                 break;
1262         case RTE_FLOW_ITEM_TYPE_NVGRE:
1263                 retval = sizeof(struct rte_flow_item_nvgre);
1264                 break;
1265         case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
1266                 retval = sizeof(struct rte_flow_item_vxlan_gpe);
1267                 break;
1268         case RTE_FLOW_ITEM_TYPE_MPLS:
1269                 retval = sizeof(struct rte_flow_item_mpls);
1270                 break;
1271         case RTE_FLOW_ITEM_TYPE_VOID: /* Fall through. */
1272         default:
1273                 retval = 0;
1274                 break;
1275         }
1276         return retval;
1277 }
1278
1279 #define MLX5_ENCAP_IPV4_VERSION         0x40
1280 #define MLX5_ENCAP_IPV4_IHL_MIN         0x05
1281 #define MLX5_ENCAP_IPV4_TTL_DEF         0x40
1282 #define MLX5_ENCAP_IPV6_VTC_FLOW        0x60000000
1283 #define MLX5_ENCAP_IPV6_HOP_LIMIT       0xff
1284 #define MLX5_ENCAP_VXLAN_FLAGS          0x08000000
1285 #define MLX5_ENCAP_VXLAN_GPE_FLAGS      0x04
1286
1287 /**
1288  * Convert the encap action data from list of rte_flow_item to raw buffer
1289  *
1290  * @param[in] items
1291  *   Pointer to rte_flow_item objects list.
1292  * @param[out] buf
1293  *   Pointer to the output buffer.
1294  * @param[out] size
1295  *   Pointer to the output buffer size.
1296  * @param[out] error
1297  *   Pointer to the error structure.
1298  *
1299  * @return
1300  *   0 on success, a negative errno value otherwise and rte_errno is set.
1301  */
1302 static int
1303 flow_dv_convert_encap_data(const struct rte_flow_item *items, uint8_t *buf,
1304                            size_t *size, struct rte_flow_error *error)
1305 {
1306         struct rte_ether_hdr *eth = NULL;
1307         struct rte_vlan_hdr *vlan = NULL;
1308         struct rte_ipv4_hdr *ipv4 = NULL;
1309         struct rte_ipv6_hdr *ipv6 = NULL;
1310         struct rte_udp_hdr *udp = NULL;
1311         struct rte_vxlan_hdr *vxlan = NULL;
1312         struct rte_vxlan_gpe_hdr *vxlan_gpe = NULL;
1313         struct rte_gre_hdr *gre = NULL;
1314         size_t len;
1315         size_t temp_size = 0;
1316
1317         if (!items)
1318                 return rte_flow_error_set(error, EINVAL,
1319                                           RTE_FLOW_ERROR_TYPE_ACTION,
1320                                           NULL, "invalid empty data");
1321         for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
1322                 len = flow_dv_get_item_len(items->type);
1323                 if (len + temp_size > MLX5_ENCAP_MAX_LEN)
1324                         return rte_flow_error_set(error, EINVAL,
1325                                                   RTE_FLOW_ERROR_TYPE_ACTION,
1326                                                   (void *)items->type,
1327                                                   "items total size is too big"
1328                                                   " for encap action");
1329                 rte_memcpy((void *)&buf[temp_size], items->spec, len);
1330                 switch (items->type) {
1331                 case RTE_FLOW_ITEM_TYPE_ETH:
1332                         eth = (struct rte_ether_hdr *)&buf[temp_size];
1333                         break;
1334                 case RTE_FLOW_ITEM_TYPE_VLAN:
1335                         vlan = (struct rte_vlan_hdr *)&buf[temp_size];
1336                         if (!eth)
1337                                 return rte_flow_error_set(error, EINVAL,
1338                                                 RTE_FLOW_ERROR_TYPE_ACTION,
1339                                                 (void *)items->type,
1340                                                 "eth header not found");
1341                         if (!eth->ether_type)
1342                                 eth->ether_type = RTE_BE16(RTE_ETHER_TYPE_VLAN);
1343                         break;
1344                 case RTE_FLOW_ITEM_TYPE_IPV4:
1345                         ipv4 = (struct rte_ipv4_hdr *)&buf[temp_size];
1346                         if (!vlan && !eth)
1347                                 return rte_flow_error_set(error, EINVAL,
1348                                                 RTE_FLOW_ERROR_TYPE_ACTION,
1349                                                 (void *)items->type,
1350                                                 "neither eth nor vlan"
1351                                                 " header found");
1352                         if (vlan && !vlan->eth_proto)
1353                                 vlan->eth_proto = RTE_BE16(RTE_ETHER_TYPE_IPV4);
1354                         else if (eth && !eth->ether_type)
1355                                 eth->ether_type = RTE_BE16(RTE_ETHER_TYPE_IPV4);
1356                         if (!ipv4->version_ihl)
1357                                 ipv4->version_ihl = MLX5_ENCAP_IPV4_VERSION |
1358                                                     MLX5_ENCAP_IPV4_IHL_MIN;
1359                         if (!ipv4->time_to_live)
1360                                 ipv4->time_to_live = MLX5_ENCAP_IPV4_TTL_DEF;
1361                         break;
1362                 case RTE_FLOW_ITEM_TYPE_IPV6:
1363                         ipv6 = (struct rte_ipv6_hdr *)&buf[temp_size];
1364                         if (!vlan && !eth)
1365                                 return rte_flow_error_set(error, EINVAL,
1366                                                 RTE_FLOW_ERROR_TYPE_ACTION,
1367                                                 (void *)items->type,
1368                                                 "neither eth nor vlan"
1369                                                 " header found");
1370                         if (vlan && !vlan->eth_proto)
1371                                 vlan->eth_proto = RTE_BE16(RTE_ETHER_TYPE_IPV6);
1372                         else if (eth && !eth->ether_type)
1373                                 eth->ether_type = RTE_BE16(RTE_ETHER_TYPE_IPV6);
1374                         if (!ipv6->vtc_flow)
1375                                 ipv6->vtc_flow =
1376                                         RTE_BE32(MLX5_ENCAP_IPV6_VTC_FLOW);
1377                         if (!ipv6->hop_limits)
1378                                 ipv6->hop_limits = MLX5_ENCAP_IPV6_HOP_LIMIT;
1379                         break;
1380                 case RTE_FLOW_ITEM_TYPE_UDP:
1381                         udp = (struct rte_udp_hdr *)&buf[temp_size];
1382                         if (!ipv4 && !ipv6)
1383                                 return rte_flow_error_set(error, EINVAL,
1384                                                 RTE_FLOW_ERROR_TYPE_ACTION,
1385                                                 (void *)items->type,
1386                                                 "ip header not found");
1387                         if (ipv4 && !ipv4->next_proto_id)
1388                                 ipv4->next_proto_id = IPPROTO_UDP;
1389                         else if (ipv6 && !ipv6->proto)
1390                                 ipv6->proto = IPPROTO_UDP;
1391                         break;
1392                 case RTE_FLOW_ITEM_TYPE_VXLAN:
1393                         vxlan = (struct rte_vxlan_hdr *)&buf[temp_size];
1394                         if (!udp)
1395                                 return rte_flow_error_set(error, EINVAL,
1396                                                 RTE_FLOW_ERROR_TYPE_ACTION,
1397                                                 (void *)items->type,
1398                                                 "udp header not found");
1399                         if (!udp->dst_port)
1400                                 udp->dst_port = RTE_BE16(MLX5_UDP_PORT_VXLAN);
1401                         if (!vxlan->vx_flags)
1402                                 vxlan->vx_flags =
1403                                         RTE_BE32(MLX5_ENCAP_VXLAN_FLAGS);
1404                         break;
1405                 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
1406                         vxlan_gpe = (struct rte_vxlan_gpe_hdr *)&buf[temp_size];
1407                         if (!udp)
1408                                 return rte_flow_error_set(error, EINVAL,
1409                                                 RTE_FLOW_ERROR_TYPE_ACTION,
1410                                                 (void *)items->type,
1411                                                 "udp header not found");
1412                         if (!vxlan_gpe->proto)
1413                                 return rte_flow_error_set(error, EINVAL,
1414                                                 RTE_FLOW_ERROR_TYPE_ACTION,
1415                                                 (void *)items->type,
1416                                                 "next protocol not found");
1417                         if (!udp->dst_port)
1418                                 udp->dst_port =
1419                                         RTE_BE16(MLX5_UDP_PORT_VXLAN_GPE);
1420                         if (!vxlan_gpe->vx_flags)
1421                                 vxlan_gpe->vx_flags =
1422                                                 MLX5_ENCAP_VXLAN_GPE_FLAGS;
1423                         break;
1424                 case RTE_FLOW_ITEM_TYPE_GRE:
1425                 case RTE_FLOW_ITEM_TYPE_NVGRE:
1426                         gre = (struct rte_gre_hdr *)&buf[temp_size];
1427                         if (!gre->proto)
1428                                 return rte_flow_error_set(error, EINVAL,
1429                                                 RTE_FLOW_ERROR_TYPE_ACTION,
1430                                                 (void *)items->type,
1431                                                 "next protocol not found");
1432                         if (!ipv4 && !ipv6)
1433                                 return rte_flow_error_set(error, EINVAL,
1434                                                 RTE_FLOW_ERROR_TYPE_ACTION,
1435                                                 (void *)items->type,
1436                                                 "ip header not found");
1437                         if (ipv4 && !ipv4->next_proto_id)
1438                                 ipv4->next_proto_id = IPPROTO_GRE;
1439                         else if (ipv6 && !ipv6->proto)
1440                                 ipv6->proto = IPPROTO_GRE;
1441                         break;
1442                 case RTE_FLOW_ITEM_TYPE_VOID:
1443                         break;
1444                 default:
1445                         return rte_flow_error_set(error, EINVAL,
1446                                                   RTE_FLOW_ERROR_TYPE_ACTION,
1447                                                   (void *)items->type,
1448                                                   "unsupported item type");
1449                         break;
1450                 }
1451                 temp_size += len;
1452         }
1453         *size = temp_size;
1454         return 0;
1455 }
1456
1457 static int
1458 flow_dv_zero_encap_udp_csum(void *data, struct rte_flow_error *error)
1459 {
1460         struct rte_ether_hdr *eth = NULL;
1461         struct rte_vlan_hdr *vlan = NULL;
1462         struct rte_ipv6_hdr *ipv6 = NULL;
1463         struct rte_udp_hdr *udp = NULL;
1464         char *next_hdr;
1465         uint16_t proto;
1466
1467         eth = (struct rte_ether_hdr *)data;
1468         next_hdr = (char *)(eth + 1);
1469         proto = RTE_BE16(eth->ether_type);
1470
1471         /* VLAN skipping */
1472         while (proto == RTE_ETHER_TYPE_VLAN || proto == RTE_ETHER_TYPE_QINQ) {
1473                 next_hdr += sizeof(struct rte_vlan_hdr);
1474                 vlan = (struct rte_vlan_hdr *)next_hdr;
1475                 proto = RTE_BE16(vlan->eth_proto);
1476         }
1477
1478         /* HW calculates IPv4 csum. no need to proceed */
1479         if (proto == RTE_ETHER_TYPE_IPV4)
1480                 return 0;
1481
1482         /* non IPv4/IPv6 header. not supported */
1483         if (proto != RTE_ETHER_TYPE_IPV6) {
1484                 return rte_flow_error_set(error, ENOTSUP,
1485                                           RTE_FLOW_ERROR_TYPE_ACTION,
1486                                           NULL, "Cannot offload non IPv4/IPv6");
1487         }
1488
1489         ipv6 = (struct rte_ipv6_hdr *)next_hdr;
1490
1491         /* ignore non UDP */
1492         if (ipv6->proto != IPPROTO_UDP)
1493                 return 0;
1494
1495         udp = (struct rte_udp_hdr *)(ipv6 + 1);
1496         udp->dgram_cksum = 0;
1497
1498         return 0;
1499 }
1500
1501 /**
1502  * Convert L2 encap action to DV specification.
1503  *
1504  * @param[in] dev
1505  *   Pointer to rte_eth_dev structure.
1506  * @param[in] action
1507  *   Pointer to action structure.
1508  * @param[in, out] dev_flow
1509  *   Pointer to the mlx5_flow.
1510  * @param[in] transfer
1511  *   Mark if the flow is E-Switch flow.
1512  * @param[out] error
1513  *   Pointer to the error structure.
1514  *
1515  * @return
1516  *   0 on success, a negative errno value otherwise and rte_errno is set.
1517  */
1518 static int
1519 flow_dv_create_action_l2_encap(struct rte_eth_dev *dev,
1520                                const struct rte_flow_action *action,
1521                                struct mlx5_flow *dev_flow,
1522                                uint8_t transfer,
1523                                struct rte_flow_error *error)
1524 {
1525         const struct rte_flow_item *encap_data;
1526         const struct rte_flow_action_raw_encap *raw_encap_data;
1527         struct mlx5_flow_dv_encap_decap_resource res = {
1528                 .reformat_type =
1529                         MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TO_L2_TUNNEL,
1530                 .ft_type = transfer ? MLX5DV_FLOW_TABLE_TYPE_FDB :
1531                                       MLX5DV_FLOW_TABLE_TYPE_NIC_TX,
1532         };
1533
1534         if (action->type == RTE_FLOW_ACTION_TYPE_RAW_ENCAP) {
1535                 raw_encap_data =
1536                         (const struct rte_flow_action_raw_encap *)action->conf;
1537                 res.size = raw_encap_data->size;
1538                 memcpy(res.buf, raw_encap_data->data, res.size);
1539                 if (flow_dv_zero_encap_udp_csum(res.buf, error))
1540                         return -rte_errno;
1541         } else {
1542                 if (action->type == RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP)
1543                         encap_data =
1544                                 ((const struct rte_flow_action_vxlan_encap *)
1545                                                 action->conf)->definition;
1546                 else
1547                         encap_data =
1548                                 ((const struct rte_flow_action_nvgre_encap *)
1549                                                 action->conf)->definition;
1550                 if (flow_dv_convert_encap_data(encap_data, res.buf,
1551                                                &res.size, error))
1552                         return -rte_errno;
1553         }
1554         if (flow_dv_encap_decap_resource_register(dev, &res, dev_flow, error))
1555                 return rte_flow_error_set(error, EINVAL,
1556                                           RTE_FLOW_ERROR_TYPE_ACTION,
1557                                           NULL, "can't create L2 encap action");
1558         return 0;
1559 }
1560
1561 /**
1562  * Convert L2 decap action to DV specification.
1563  *
1564  * @param[in] dev
1565  *   Pointer to rte_eth_dev structure.
1566  * @param[in, out] dev_flow
1567  *   Pointer to the mlx5_flow.
1568  * @param[in] transfer
1569  *   Mark if the flow is E-Switch flow.
1570  * @param[out] error
1571  *   Pointer to the error structure.
1572  *
1573  * @return
1574  *   0 on success, a negative errno value otherwise and rte_errno is set.
1575  */
1576 static int
1577 flow_dv_create_action_l2_decap(struct rte_eth_dev *dev,
1578                                struct mlx5_flow *dev_flow,
1579                                uint8_t transfer,
1580                                struct rte_flow_error *error)
1581 {
1582         struct mlx5_flow_dv_encap_decap_resource res = {
1583                 .size = 0,
1584                 .reformat_type =
1585                         MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TUNNEL_TO_L2,
1586                 .ft_type = transfer ? MLX5DV_FLOW_TABLE_TYPE_FDB :
1587                                       MLX5DV_FLOW_TABLE_TYPE_NIC_RX,
1588         };
1589
1590         if (flow_dv_encap_decap_resource_register(dev, &res, dev_flow, error))
1591                 return rte_flow_error_set(error, EINVAL,
1592                                           RTE_FLOW_ERROR_TYPE_ACTION,
1593                                           NULL, "can't create L2 decap action");
1594         return 0;
1595 }
1596
1597 /**
1598  * Convert raw decap/encap (L3 tunnel) action to DV specification.
1599  *
1600  * @param[in] dev
1601  *   Pointer to rte_eth_dev structure.
1602  * @param[in] action
1603  *   Pointer to action structure.
1604  * @param[in, out] dev_flow
1605  *   Pointer to the mlx5_flow.
1606  * @param[in] attr
1607  *   Pointer to the flow attributes.
1608  * @param[out] error
1609  *   Pointer to the error structure.
1610  *
1611  * @return
1612  *   0 on success, a negative errno value otherwise and rte_errno is set.
1613  */
1614 static int
1615 flow_dv_create_action_raw_encap(struct rte_eth_dev *dev,
1616                                 const struct rte_flow_action *action,
1617                                 struct mlx5_flow *dev_flow,
1618                                 const struct rte_flow_attr *attr,
1619                                 struct rte_flow_error *error)
1620 {
1621         const struct rte_flow_action_raw_encap *encap_data;
1622         struct mlx5_flow_dv_encap_decap_resource res;
1623
1624         encap_data = (const struct rte_flow_action_raw_encap *)action->conf;
1625         res.size = encap_data->size;
1626         memcpy(res.buf, encap_data->data, res.size);
1627         res.reformat_type = attr->egress ?
1628                 MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TO_L3_TUNNEL :
1629                 MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L3_TUNNEL_TO_L2;
1630         if (attr->transfer)
1631                 res.ft_type = MLX5DV_FLOW_TABLE_TYPE_FDB;
1632         else
1633                 res.ft_type = attr->egress ? MLX5DV_FLOW_TABLE_TYPE_NIC_TX :
1634                                              MLX5DV_FLOW_TABLE_TYPE_NIC_RX;
1635         if (flow_dv_encap_decap_resource_register(dev, &res, dev_flow, error))
1636                 return rte_flow_error_set(error, EINVAL,
1637                                           RTE_FLOW_ERROR_TYPE_ACTION,
1638                                           NULL, "can't create encap action");
1639         return 0;
1640 }
1641
1642 /**
1643  * Validate the modify-header actions.
1644  *
1645  * @param[in] action_flags
1646  *   Holds the actions detected until now.
1647  * @param[in] action
1648  *   Pointer to the modify action.
1649  * @param[out] error
1650  *   Pointer to error structure.
1651  *
1652  * @return
1653  *   0 on success, a negative errno value otherwise and rte_errno is set.
1654  */
1655 static int
1656 flow_dv_validate_action_modify_hdr(const uint64_t action_flags,
1657                                    const struct rte_flow_action *action,
1658                                    struct rte_flow_error *error)
1659 {
1660         if (action->type != RTE_FLOW_ACTION_TYPE_DEC_TTL && !action->conf)
1661                 return rte_flow_error_set(error, EINVAL,
1662                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1663                                           NULL, "action configuration not set");
1664         if (action_flags & MLX5_FLOW_ENCAP_ACTIONS)
1665                 return rte_flow_error_set(error, EINVAL,
1666                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1667                                           "can't have encap action before"
1668                                           " modify action");
1669         return 0;
1670 }
1671
1672 /**
1673  * Validate the modify-header MAC address actions.
1674  *
1675  * @param[in] action_flags
1676  *   Holds the actions detected until now.
1677  * @param[in] action
1678  *   Pointer to the modify action.
1679  * @param[in] item_flags
1680  *   Holds the items detected.
1681  * @param[out] error
1682  *   Pointer to error structure.
1683  *
1684  * @return
1685  *   0 on success, a negative errno value otherwise and rte_errno is set.
1686  */
1687 static int
1688 flow_dv_validate_action_modify_mac(const uint64_t action_flags,
1689                                    const struct rte_flow_action *action,
1690                                    const uint64_t item_flags,
1691                                    struct rte_flow_error *error)
1692 {
1693         int ret = 0;
1694
1695         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
1696         if (!ret) {
1697                 if (!(item_flags & MLX5_FLOW_LAYER_L2))
1698                         return rte_flow_error_set(error, EINVAL,
1699                                                   RTE_FLOW_ERROR_TYPE_ACTION,
1700                                                   NULL,
1701                                                   "no L2 item in pattern");
1702         }
1703         return ret;
1704 }
1705
1706 /**
1707  * Validate the modify-header IPv4 address actions.
1708  *
1709  * @param[in] action_flags
1710  *   Holds the actions detected until now.
1711  * @param[in] action
1712  *   Pointer to the modify action.
1713  * @param[in] item_flags
1714  *   Holds the items detected.
1715  * @param[out] error
1716  *   Pointer to error structure.
1717  *
1718  * @return
1719  *   0 on success, a negative errno value otherwise and rte_errno is set.
1720  */
1721 static int
1722 flow_dv_validate_action_modify_ipv4(const uint64_t action_flags,
1723                                     const struct rte_flow_action *action,
1724                                     const uint64_t item_flags,
1725                                     struct rte_flow_error *error)
1726 {
1727         int ret = 0;
1728
1729         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
1730         if (!ret) {
1731                 if (!(item_flags & MLX5_FLOW_LAYER_L3_IPV4))
1732                         return rte_flow_error_set(error, EINVAL,
1733                                                   RTE_FLOW_ERROR_TYPE_ACTION,
1734                                                   NULL,
1735                                                   "no ipv4 item in pattern");
1736         }
1737         return ret;
1738 }
1739
1740 /**
1741  * Validate the modify-header IPv6 address actions.
1742  *
1743  * @param[in] action_flags
1744  *   Holds the actions detected until now.
1745  * @param[in] action
1746  *   Pointer to the modify action.
1747  * @param[in] item_flags
1748  *   Holds the items detected.
1749  * @param[out] error
1750  *   Pointer to error structure.
1751  *
1752  * @return
1753  *   0 on success, a negative errno value otherwise and rte_errno is set.
1754  */
1755 static int
1756 flow_dv_validate_action_modify_ipv6(const uint64_t action_flags,
1757                                     const struct rte_flow_action *action,
1758                                     const uint64_t item_flags,
1759                                     struct rte_flow_error *error)
1760 {
1761         int ret = 0;
1762
1763         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
1764         if (!ret) {
1765                 if (!(item_flags & MLX5_FLOW_LAYER_L3_IPV6))
1766                         return rte_flow_error_set(error, EINVAL,
1767                                                   RTE_FLOW_ERROR_TYPE_ACTION,
1768                                                   NULL,
1769                                                   "no ipv6 item in pattern");
1770         }
1771         return ret;
1772 }
1773
1774 /**
1775  * Validate the modify-header TP actions.
1776  *
1777  * @param[in] action_flags
1778  *   Holds the actions detected until now.
1779  * @param[in] action
1780  *   Pointer to the modify action.
1781  * @param[in] item_flags
1782  *   Holds the items detected.
1783  * @param[out] error
1784  *   Pointer to error structure.
1785  *
1786  * @return
1787  *   0 on success, a negative errno value otherwise and rte_errno is set.
1788  */
1789 static int
1790 flow_dv_validate_action_modify_tp(const uint64_t action_flags,
1791                                   const struct rte_flow_action *action,
1792                                   const uint64_t item_flags,
1793                                   struct rte_flow_error *error)
1794 {
1795         int ret = 0;
1796
1797         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
1798         if (!ret) {
1799                 if (!(item_flags & MLX5_FLOW_LAYER_L4))
1800                         return rte_flow_error_set(error, EINVAL,
1801                                                   RTE_FLOW_ERROR_TYPE_ACTION,
1802                                                   NULL, "no transport layer "
1803                                                   "in pattern");
1804         }
1805         return ret;
1806 }
1807
1808 /**
1809  * Validate the modify-header actions of increment/decrement
1810  * TCP Sequence-number.
1811  *
1812  * @param[in] action_flags
1813  *   Holds the actions detected until now.
1814  * @param[in] action
1815  *   Pointer to the modify action.
1816  * @param[in] item_flags
1817  *   Holds the items detected.
1818  * @param[out] error
1819  *   Pointer to error structure.
1820  *
1821  * @return
1822  *   0 on success, a negative errno value otherwise and rte_errno is set.
1823  */
1824 static int
1825 flow_dv_validate_action_modify_tcp_seq(const uint64_t action_flags,
1826                                        const struct rte_flow_action *action,
1827                                        const uint64_t item_flags,
1828                                        struct rte_flow_error *error)
1829 {
1830         int ret = 0;
1831
1832         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
1833         if (!ret) {
1834                 if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_TCP))
1835                         return rte_flow_error_set(error, EINVAL,
1836                                                   RTE_FLOW_ERROR_TYPE_ACTION,
1837                                                   NULL, "no TCP item in"
1838                                                   " pattern");
1839                 if ((action->type == RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ &&
1840                         (action_flags & MLX5_FLOW_ACTION_DEC_TCP_SEQ)) ||
1841                     (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ &&
1842                         (action_flags & MLX5_FLOW_ACTION_INC_TCP_SEQ)))
1843                         return rte_flow_error_set(error, EINVAL,
1844                                                   RTE_FLOW_ERROR_TYPE_ACTION,
1845                                                   NULL,
1846                                                   "cannot decrease and increase"
1847                                                   " TCP sequence number"
1848                                                   " at the same time");
1849         }
1850         return ret;
1851 }
1852
1853 /**
1854  * Validate the modify-header actions of increment/decrement
1855  * TCP Acknowledgment number.
1856  *
1857  * @param[in] action_flags
1858  *   Holds the actions detected until now.
1859  * @param[in] action
1860  *   Pointer to the modify action.
1861  * @param[in] item_flags
1862  *   Holds the items detected.
1863  * @param[out] error
1864  *   Pointer to error structure.
1865  *
1866  * @return
1867  *   0 on success, a negative errno value otherwise and rte_errno is set.
1868  */
1869 static int
1870 flow_dv_validate_action_modify_tcp_ack(const uint64_t action_flags,
1871                                        const struct rte_flow_action *action,
1872                                        const uint64_t item_flags,
1873                                        struct rte_flow_error *error)
1874 {
1875         int ret = 0;
1876
1877         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
1878         if (!ret) {
1879                 if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_TCP))
1880                         return rte_flow_error_set(error, EINVAL,
1881                                                   RTE_FLOW_ERROR_TYPE_ACTION,
1882                                                   NULL, "no TCP item in"
1883                                                   " pattern");
1884                 if ((action->type == RTE_FLOW_ACTION_TYPE_INC_TCP_ACK &&
1885                         (action_flags & MLX5_FLOW_ACTION_DEC_TCP_ACK)) ||
1886                     (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK &&
1887                         (action_flags & MLX5_FLOW_ACTION_INC_TCP_ACK)))
1888                         return rte_flow_error_set(error, EINVAL,
1889                                                   RTE_FLOW_ERROR_TYPE_ACTION,
1890                                                   NULL,
1891                                                   "cannot decrease and increase"
1892                                                   " TCP acknowledgment number"
1893                                                   " at the same time");
1894         }
1895         return ret;
1896 }
1897
1898 /**
1899  * Validate the modify-header TTL actions.
1900  *
1901  * @param[in] action_flags
1902  *   Holds the actions detected until now.
1903  * @param[in] action
1904  *   Pointer to the modify action.
1905  * @param[in] item_flags
1906  *   Holds the items detected.
1907  * @param[out] error
1908  *   Pointer to error structure.
1909  *
1910  * @return
1911  *   0 on success, a negative errno value otherwise and rte_errno is set.
1912  */
1913 static int
1914 flow_dv_validate_action_modify_ttl(const uint64_t action_flags,
1915                                    const struct rte_flow_action *action,
1916                                    const uint64_t item_flags,
1917                                    struct rte_flow_error *error)
1918 {
1919         int ret = 0;
1920
1921         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
1922         if (!ret) {
1923                 if (!(item_flags & MLX5_FLOW_LAYER_L3))
1924                         return rte_flow_error_set(error, EINVAL,
1925                                                   RTE_FLOW_ERROR_TYPE_ACTION,
1926                                                   NULL,
1927                                                   "no IP protocol in pattern");
1928         }
1929         return ret;
1930 }
1931
1932 /**
1933  * Validate jump action.
1934  *
1935  * @param[in] action
1936  *   Pointer to the modify action.
1937  * @param[in] group
1938  *   The group of the current flow.
1939  * @param[out] error
1940  *   Pointer to error structure.
1941  *
1942  * @return
1943  *   0 on success, a negative errno value otherwise and rte_errno is set.
1944  */
1945 static int
1946 flow_dv_validate_action_jump(const struct rte_flow_action *action,
1947                              uint32_t group,
1948                              struct rte_flow_error *error)
1949 {
1950         if (action->type != RTE_FLOW_ACTION_TYPE_JUMP && !action->conf)
1951                 return rte_flow_error_set(error, EINVAL,
1952                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1953                                           NULL, "action configuration not set");
1954         if (group >= ((const struct rte_flow_action_jump *)action->conf)->group)
1955                 return rte_flow_error_set(error, EINVAL,
1956                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1957                                           "target group must be higher then"
1958                                           " the current flow group");
1959         return 0;
1960 }
1961
1962 /*
1963  * Validate the port_id action.
1964  *
1965  * @param[in] dev
1966  *   Pointer to rte_eth_dev structure.
1967  * @param[in] action_flags
1968  *   Bit-fields that holds the actions detected until now.
1969  * @param[in] action
1970  *   Port_id RTE action structure.
1971  * @param[in] attr
1972  *   Attributes of flow that includes this action.
1973  * @param[out] error
1974  *   Pointer to error structure.
1975  *
1976  * @return
1977  *   0 on success, a negative errno value otherwise and rte_errno is set.
1978  */
1979 static int
1980 flow_dv_validate_action_port_id(struct rte_eth_dev *dev,
1981                                 uint64_t action_flags,
1982                                 const struct rte_flow_action *action,
1983                                 const struct rte_flow_attr *attr,
1984                                 struct rte_flow_error *error)
1985 {
1986         const struct rte_flow_action_port_id *port_id;
1987         uint16_t port;
1988         uint16_t esw_domain_id;
1989         uint16_t act_port_domain_id;
1990         int ret;
1991
1992         if (!attr->transfer)
1993                 return rte_flow_error_set(error, ENOTSUP,
1994                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1995                                           NULL,
1996                                           "port id action is valid in transfer"
1997                                           " mode only");
1998         if (!action || !action->conf)
1999                 return rte_flow_error_set(error, ENOTSUP,
2000                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
2001                                           NULL,
2002                                           "port id action parameters must be"
2003                                           " specified");
2004         if (action_flags & (MLX5_FLOW_FATE_ACTIONS |
2005                             MLX5_FLOW_FATE_ESWITCH_ACTIONS))
2006                 return rte_flow_error_set(error, EINVAL,
2007                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2008                                           "can have only one fate actions in"
2009                                           " a flow");
2010         ret = mlx5_port_to_eswitch_info(dev->data->port_id,
2011                                         &esw_domain_id, NULL);
2012         if (ret < 0)
2013                 return rte_flow_error_set(error, -ret,
2014                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2015                                           NULL,
2016                                           "failed to obtain E-Switch info");
2017         port_id = action->conf;
2018         port = port_id->original ? dev->data->port_id : port_id->id;
2019         ret = mlx5_port_to_eswitch_info(port, &act_port_domain_id, NULL);
2020         if (ret)
2021                 return rte_flow_error_set
2022                                 (error, -ret,
2023                                  RTE_FLOW_ERROR_TYPE_ACTION_CONF, port_id,
2024                                  "failed to obtain E-Switch port id for port");
2025         if (act_port_domain_id != esw_domain_id)
2026                 return rte_flow_error_set
2027                                 (error, -ret,
2028                                  RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2029                                  "port does not belong to"
2030                                  " E-Switch being configured");
2031         return 0;
2032 }
2033
2034 /**
2035  * Find existing modify-header resource or create and register a new one.
2036  *
2037  * @param dev[in, out]
2038  *   Pointer to rte_eth_dev structure.
2039  * @param[in, out] resource
2040  *   Pointer to modify-header resource.
2041  * @parm[in, out] dev_flow
2042  *   Pointer to the dev_flow.
2043  * @param[out] error
2044  *   pointer to error structure.
2045  *
2046  * @return
2047  *   0 on success otherwise -errno and errno is set.
2048  */
2049 static int
2050 flow_dv_modify_hdr_resource_register
2051                         (struct rte_eth_dev *dev,
2052                          struct mlx5_flow_dv_modify_hdr_resource *resource,
2053                          struct mlx5_flow *dev_flow,
2054                          struct rte_flow_error *error)
2055 {
2056         struct mlx5_priv *priv = dev->data->dev_private;
2057         struct mlx5_ibv_shared *sh = priv->sh;
2058         struct mlx5_flow_dv_modify_hdr_resource *cache_resource;
2059         struct mlx5dv_dr_domain *ns;
2060
2061         if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB)
2062                 ns = sh->fdb_domain;
2063         else if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_NIC_TX)
2064                 ns = sh->tx_domain;
2065         else
2066                 ns = sh->rx_domain;
2067         resource->flags =
2068                 dev_flow->flow->group ? 0 : MLX5DV_DR_ACTION_FLAGS_ROOT_LEVEL;
2069         /* Lookup a matching resource from cache. */
2070         LIST_FOREACH(cache_resource, &sh->modify_cmds, next) {
2071                 if (resource->ft_type == cache_resource->ft_type &&
2072                     resource->actions_num == cache_resource->actions_num &&
2073                     resource->flags == cache_resource->flags &&
2074                     !memcmp((const void *)resource->actions,
2075                             (const void *)cache_resource->actions,
2076                             (resource->actions_num *
2077                                             sizeof(resource->actions[0])))) {
2078                         DRV_LOG(DEBUG, "modify-header resource %p: refcnt %d++",
2079                                 (void *)cache_resource,
2080                                 rte_atomic32_read(&cache_resource->refcnt));
2081                         rte_atomic32_inc(&cache_resource->refcnt);
2082                         dev_flow->dv.modify_hdr = cache_resource;
2083                         return 0;
2084                 }
2085         }
2086         /* Register new modify-header resource. */
2087         cache_resource = rte_calloc(__func__, 1, sizeof(*cache_resource), 0);
2088         if (!cache_resource)
2089                 return rte_flow_error_set(error, ENOMEM,
2090                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2091                                           "cannot allocate resource memory");
2092         *cache_resource = *resource;
2093         cache_resource->verbs_action =
2094                 mlx5_glue->dv_create_flow_action_modify_header
2095                                         (sh->ctx, cache_resource->ft_type,
2096                                          ns, cache_resource->flags,
2097                                          cache_resource->actions_num *
2098                                          sizeof(cache_resource->actions[0]),
2099                                          (uint64_t *)cache_resource->actions);
2100         if (!cache_resource->verbs_action) {
2101                 rte_free(cache_resource);
2102                 return rte_flow_error_set(error, ENOMEM,
2103                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2104                                           NULL, "cannot create action");
2105         }
2106         rte_atomic32_init(&cache_resource->refcnt);
2107         rte_atomic32_inc(&cache_resource->refcnt);
2108         LIST_INSERT_HEAD(&sh->modify_cmds, cache_resource, next);
2109         dev_flow->dv.modify_hdr = cache_resource;
2110         DRV_LOG(DEBUG, "new modify-header resource %p: refcnt %d++",
2111                 (void *)cache_resource,
2112                 rte_atomic32_read(&cache_resource->refcnt));
2113         return 0;
2114 }
2115
2116 /**
2117  * Get or create a flow counter.
2118  *
2119  * @param[in] dev
2120  *   Pointer to the Ethernet device structure.
2121  * @param[in] shared
2122  *   Indicate if this counter is shared with other flows.
2123  * @param[in] id
2124  *   Counter identifier.
2125  *
2126  * @return
2127  *   pointer to flow counter on success, NULL otherwise and rte_errno is set.
2128  */
2129 static struct mlx5_flow_counter *
2130 flow_dv_counter_new(struct rte_eth_dev *dev, uint32_t shared, uint32_t id)
2131 {
2132         struct mlx5_priv *priv = dev->data->dev_private;
2133         struct mlx5_flow_counter *cnt = NULL;
2134         struct mlx5_devx_counter_set *dcs = NULL;
2135         int ret;
2136
2137         if (!priv->config.devx) {
2138                 ret = -ENOTSUP;
2139                 goto error_exit;
2140         }
2141         if (shared) {
2142                 LIST_FOREACH(cnt, &priv->flow_counters, next) {
2143                         if (cnt->shared && cnt->id == id) {
2144                                 cnt->ref_cnt++;
2145                                 return cnt;
2146                         }
2147                 }
2148         }
2149         cnt = rte_calloc(__func__, 1, sizeof(*cnt), 0);
2150         dcs = rte_calloc(__func__, 1, sizeof(*dcs), 0);
2151         if (!dcs || !cnt) {
2152                 ret = -ENOMEM;
2153                 goto error_exit;
2154         }
2155         ret = mlx5_devx_cmd_flow_counter_alloc(priv->sh->ctx, dcs);
2156         if (ret)
2157                 goto error_exit;
2158         struct mlx5_flow_counter tmpl = {
2159                 .shared = shared,
2160                 .ref_cnt = 1,
2161                 .id = id,
2162                 .dcs = dcs,
2163         };
2164         tmpl.action = mlx5_glue->dv_create_flow_action_counter(dcs->obj, 0);
2165         if (!tmpl.action) {
2166                 ret = errno;
2167                 goto error_exit;
2168         }
2169         *cnt = tmpl;
2170         LIST_INSERT_HEAD(&priv->flow_counters, cnt, next);
2171         return cnt;
2172 error_exit:
2173         rte_free(cnt);
2174         rte_free(dcs);
2175         rte_errno = -ret;
2176         return NULL;
2177 }
2178
2179 /**
2180  * Release a flow counter.
2181  *
2182  * @param[in] counter
2183  *   Pointer to the counter handler.
2184  */
2185 static void
2186 flow_dv_counter_release(struct mlx5_flow_counter *counter)
2187 {
2188         int ret;
2189
2190         if (!counter)
2191                 return;
2192         if (--counter->ref_cnt == 0) {
2193                 ret = mlx5_devx_cmd_flow_counter_free(counter->dcs->obj);
2194                 if (ret)
2195                         DRV_LOG(ERR, "Failed to free devx counters, %d", ret);
2196                 LIST_REMOVE(counter, next);
2197                 rte_free(counter->dcs);
2198                 rte_free(counter);
2199         }
2200 }
2201
2202 /**
2203  * Verify the @p attributes will be correctly understood by the NIC and store
2204  * them in the @p flow if everything is correct.
2205  *
2206  * @param[in] dev
2207  *   Pointer to dev struct.
2208  * @param[in] attributes
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_attributes(struct rte_eth_dev *dev,
2218                             const struct rte_flow_attr *attributes,
2219                             struct rte_flow_error *error)
2220 {
2221         struct mlx5_priv *priv = dev->data->dev_private;
2222         uint32_t priority_max = priv->config.flow_prio - 1;
2223
2224 #ifndef HAVE_MLX5DV_DR
2225         if (attributes->group)
2226                 return rte_flow_error_set(error, ENOTSUP,
2227                                           RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
2228                                           NULL,
2229                                           "groups is not supported");
2230 #endif
2231         if (attributes->priority != MLX5_FLOW_PRIO_RSVD &&
2232             attributes->priority >= priority_max)
2233                 return rte_flow_error_set(error, ENOTSUP,
2234                                           RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
2235                                           NULL,
2236                                           "priority out of range");
2237         if (attributes->transfer) {
2238                 if (!priv->config.dv_esw_en)
2239                         return rte_flow_error_set
2240                                 (error, ENOTSUP,
2241                                  RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2242                                  "E-Switch dr is not supported");
2243                 if (!(priv->representor || priv->master))
2244                         return rte_flow_error_set
2245                                 (error, EINVAL, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2246                                  NULL, "E-Switch configurationd can only be"
2247                                  " done by a master or a representor device");
2248                 if (attributes->egress)
2249                         return rte_flow_error_set
2250                                 (error, ENOTSUP,
2251                                  RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, attributes,
2252                                  "egress is not supported");
2253                 if (attributes->group >= MLX5_MAX_TABLES_FDB)
2254                         return rte_flow_error_set
2255                                 (error, EINVAL,
2256                                  RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
2257                                  NULL, "group must be smaller than "
2258                                  RTE_STR(MLX5_MAX_FDB_TABLES));
2259         }
2260         if (!(attributes->egress ^ attributes->ingress))
2261                 return rte_flow_error_set(error, ENOTSUP,
2262                                           RTE_FLOW_ERROR_TYPE_ATTR, NULL,
2263                                           "must specify exactly one of "
2264                                           "ingress or egress");
2265         return 0;
2266 }
2267
2268 /**
2269  * Internal validation function. For validating both actions and items.
2270  *
2271  * @param[in] dev
2272  *   Pointer to the rte_eth_dev structure.
2273  * @param[in] attr
2274  *   Pointer to the flow attributes.
2275  * @param[in] items
2276  *   Pointer to the list of items.
2277  * @param[in] actions
2278  *   Pointer to the list of actions.
2279  * @param[out] error
2280  *   Pointer to the error structure.
2281  *
2282  * @return
2283  *   0 on success, a negative errno value otherwise and rte_errno is set.
2284  */
2285 static int
2286 flow_dv_validate(struct rte_eth_dev *dev, const struct rte_flow_attr *attr,
2287                  const struct rte_flow_item items[],
2288                  const struct rte_flow_action actions[],
2289                  struct rte_flow_error *error)
2290 {
2291         int ret;
2292         uint64_t action_flags = 0;
2293         uint64_t item_flags = 0;
2294         uint64_t last_item = 0;
2295         uint8_t next_protocol = 0xff;
2296         int actions_n = 0;
2297         struct rte_flow_item_tcp nic_tcp_mask = {
2298                 .hdr = {
2299                         .tcp_flags = 0xFF,
2300                         .src_port = RTE_BE16(UINT16_MAX),
2301                         .dst_port = RTE_BE16(UINT16_MAX),
2302                 }
2303         };
2304
2305         if (items == NULL)
2306                 return -1;
2307         ret = flow_dv_validate_attributes(dev, attr, error);
2308         if (ret < 0)
2309                 return ret;
2310         for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
2311                 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
2312                 switch (items->type) {
2313                 case RTE_FLOW_ITEM_TYPE_VOID:
2314                         break;
2315                 case RTE_FLOW_ITEM_TYPE_PORT_ID:
2316                         ret = flow_dv_validate_item_port_id
2317                                         (dev, items, attr, item_flags, error);
2318                         if (ret < 0)
2319                                 return ret;
2320                         last_item |= MLX5_FLOW_ITEM_PORT_ID;
2321                         break;
2322                 case RTE_FLOW_ITEM_TYPE_ETH:
2323                         ret = mlx5_flow_validate_item_eth(items, item_flags,
2324                                                           error);
2325                         if (ret < 0)
2326                                 return ret;
2327                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
2328                                              MLX5_FLOW_LAYER_OUTER_L2;
2329                         break;
2330                 case RTE_FLOW_ITEM_TYPE_VLAN:
2331                         ret = mlx5_flow_validate_item_vlan(items, item_flags,
2332                                                            error);
2333                         if (ret < 0)
2334                                 return ret;
2335                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_VLAN :
2336                                              MLX5_FLOW_LAYER_OUTER_VLAN;
2337                         break;
2338                 case RTE_FLOW_ITEM_TYPE_IPV4:
2339                         ret = mlx5_flow_validate_item_ipv4(items, item_flags,
2340                                                            NULL, error);
2341                         if (ret < 0)
2342                                 return ret;
2343                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
2344                                              MLX5_FLOW_LAYER_OUTER_L3_IPV4;
2345                         if (items->mask != NULL &&
2346                             ((const struct rte_flow_item_ipv4 *)
2347                              items->mask)->hdr.next_proto_id) {
2348                                 next_protocol =
2349                                         ((const struct rte_flow_item_ipv4 *)
2350                                          (items->spec))->hdr.next_proto_id;
2351                                 next_protocol &=
2352                                         ((const struct rte_flow_item_ipv4 *)
2353                                          (items->mask))->hdr.next_proto_id;
2354                         } else {
2355                                 /* Reset for inner layer. */
2356                                 next_protocol = 0xff;
2357                         }
2358                         break;
2359                 case RTE_FLOW_ITEM_TYPE_IPV6:
2360                         ret = mlx5_flow_validate_item_ipv6(items, item_flags,
2361                                                            NULL, error);
2362                         if (ret < 0)
2363                                 return ret;
2364                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
2365                                              MLX5_FLOW_LAYER_OUTER_L3_IPV6;
2366                         if (items->mask != NULL &&
2367                             ((const struct rte_flow_item_ipv6 *)
2368                              items->mask)->hdr.proto) {
2369                                 next_protocol =
2370                                         ((const struct rte_flow_item_ipv6 *)
2371                                          items->spec)->hdr.proto;
2372                                 next_protocol &=
2373                                         ((const struct rte_flow_item_ipv6 *)
2374                                          items->mask)->hdr.proto;
2375                         } else {
2376                                 /* Reset for inner layer. */
2377                                 next_protocol = 0xff;
2378                         }
2379                         break;
2380                 case RTE_FLOW_ITEM_TYPE_TCP:
2381                         ret = mlx5_flow_validate_item_tcp
2382                                                 (items, item_flags,
2383                                                  next_protocol,
2384                                                  &nic_tcp_mask,
2385                                                  error);
2386                         if (ret < 0)
2387                                 return ret;
2388                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
2389                                              MLX5_FLOW_LAYER_OUTER_L4_TCP;
2390                         break;
2391                 case RTE_FLOW_ITEM_TYPE_UDP:
2392                         ret = mlx5_flow_validate_item_udp(items, item_flags,
2393                                                           next_protocol,
2394                                                           error);
2395                         if (ret < 0)
2396                                 return ret;
2397                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
2398                                              MLX5_FLOW_LAYER_OUTER_L4_UDP;
2399                         break;
2400                 case RTE_FLOW_ITEM_TYPE_GRE:
2401                 case RTE_FLOW_ITEM_TYPE_NVGRE:
2402                         ret = mlx5_flow_validate_item_gre(items, item_flags,
2403                                                           next_protocol, error);
2404                         if (ret < 0)
2405                                 return ret;
2406                         last_item = MLX5_FLOW_LAYER_GRE;
2407                         break;
2408                 case RTE_FLOW_ITEM_TYPE_VXLAN:
2409                         ret = mlx5_flow_validate_item_vxlan(items, item_flags,
2410                                                             error);
2411                         if (ret < 0)
2412                                 return ret;
2413                         last_item = MLX5_FLOW_LAYER_VXLAN;
2414                         break;
2415                 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
2416                         ret = mlx5_flow_validate_item_vxlan_gpe(items,
2417                                                                 item_flags, dev,
2418                                                                 error);
2419                         if (ret < 0)
2420                                 return ret;
2421                         last_item = MLX5_FLOW_LAYER_VXLAN_GPE;
2422                         break;
2423                 case RTE_FLOW_ITEM_TYPE_MPLS:
2424                         ret = mlx5_flow_validate_item_mpls(dev, items,
2425                                                            item_flags,
2426                                                            last_item, error);
2427                         if (ret < 0)
2428                                 return ret;
2429                         last_item = MLX5_FLOW_LAYER_MPLS;
2430                         break;
2431                 case RTE_FLOW_ITEM_TYPE_META:
2432                         ret = flow_dv_validate_item_meta(dev, items, attr,
2433                                                          error);
2434                         if (ret < 0)
2435                                 return ret;
2436                         last_item = MLX5_FLOW_ITEM_METADATA;
2437                         break;
2438                 default:
2439                         return rte_flow_error_set(error, ENOTSUP,
2440                                                   RTE_FLOW_ERROR_TYPE_ITEM,
2441                                                   NULL, "item not supported");
2442                 }
2443                 item_flags |= last_item;
2444         }
2445         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
2446                 if (actions_n == MLX5_DV_MAX_NUMBER_OF_ACTIONS)
2447                         return rte_flow_error_set(error, ENOTSUP,
2448                                                   RTE_FLOW_ERROR_TYPE_ACTION,
2449                                                   actions, "too many actions");
2450                 switch (actions->type) {
2451                 case RTE_FLOW_ACTION_TYPE_VOID:
2452                         break;
2453                 case RTE_FLOW_ACTION_TYPE_PORT_ID:
2454                         ret = flow_dv_validate_action_port_id(dev,
2455                                                               action_flags,
2456                                                               actions,
2457                                                               attr,
2458                                                               error);
2459                         if (ret)
2460                                 return ret;
2461                         action_flags |= MLX5_FLOW_ACTION_PORT_ID;
2462                         ++actions_n;
2463                         break;
2464                 case RTE_FLOW_ACTION_TYPE_FLAG:
2465                         ret = mlx5_flow_validate_action_flag(action_flags,
2466                                                              attr, error);
2467                         if (ret < 0)
2468                                 return ret;
2469                         action_flags |= MLX5_FLOW_ACTION_FLAG;
2470                         ++actions_n;
2471                         break;
2472                 case RTE_FLOW_ACTION_TYPE_MARK:
2473                         ret = mlx5_flow_validate_action_mark(actions,
2474                                                              action_flags,
2475                                                              attr, error);
2476                         if (ret < 0)
2477                                 return ret;
2478                         action_flags |= MLX5_FLOW_ACTION_MARK;
2479                         ++actions_n;
2480                         break;
2481                 case RTE_FLOW_ACTION_TYPE_DROP:
2482                         ret = mlx5_flow_validate_action_drop(action_flags,
2483                                                              attr, error);
2484                         if (ret < 0)
2485                                 return ret;
2486                         action_flags |= MLX5_FLOW_ACTION_DROP;
2487                         ++actions_n;
2488                         break;
2489                 case RTE_FLOW_ACTION_TYPE_QUEUE:
2490                         ret = mlx5_flow_validate_action_queue(actions,
2491                                                               action_flags, dev,
2492                                                               attr, error);
2493                         if (ret < 0)
2494                                 return ret;
2495                         action_flags |= MLX5_FLOW_ACTION_QUEUE;
2496                         ++actions_n;
2497                         break;
2498                 case RTE_FLOW_ACTION_TYPE_RSS:
2499                         ret = mlx5_flow_validate_action_rss(actions,
2500                                                             action_flags, dev,
2501                                                             attr, item_flags,
2502                                                             error);
2503                         if (ret < 0)
2504                                 return ret;
2505                         action_flags |= MLX5_FLOW_ACTION_RSS;
2506                         ++actions_n;
2507                         break;
2508                 case RTE_FLOW_ACTION_TYPE_COUNT:
2509                         ret = flow_dv_validate_action_count(dev, error);
2510                         if (ret < 0)
2511                                 return ret;
2512                         action_flags |= MLX5_FLOW_ACTION_COUNT;
2513                         ++actions_n;
2514                         break;
2515                 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
2516                 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
2517                         ret = flow_dv_validate_action_l2_encap(action_flags,
2518                                                                actions, attr,
2519                                                                error);
2520                         if (ret < 0)
2521                                 return ret;
2522                         action_flags |= actions->type ==
2523                                         RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP ?
2524                                         MLX5_FLOW_ACTION_VXLAN_ENCAP :
2525                                         MLX5_FLOW_ACTION_NVGRE_ENCAP;
2526                         ++actions_n;
2527                         break;
2528                 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
2529                 case RTE_FLOW_ACTION_TYPE_NVGRE_DECAP:
2530                         ret = flow_dv_validate_action_l2_decap(action_flags,
2531                                                                attr, error);
2532                         if (ret < 0)
2533                                 return ret;
2534                         action_flags |= actions->type ==
2535                                         RTE_FLOW_ACTION_TYPE_VXLAN_DECAP ?
2536                                         MLX5_FLOW_ACTION_VXLAN_DECAP :
2537                                         MLX5_FLOW_ACTION_NVGRE_DECAP;
2538                         ++actions_n;
2539                         break;
2540                 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
2541                         ret = flow_dv_validate_action_raw_encap(action_flags,
2542                                                                 actions, attr,
2543                                                                 error);
2544                         if (ret < 0)
2545                                 return ret;
2546                         action_flags |= MLX5_FLOW_ACTION_RAW_ENCAP;
2547                         ++actions_n;
2548                         break;
2549                 case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
2550                         ret = flow_dv_validate_action_raw_decap(action_flags,
2551                                                                 actions, attr,
2552                                                                 error);
2553                         if (ret < 0)
2554                                 return ret;
2555                         action_flags |= MLX5_FLOW_ACTION_RAW_DECAP;
2556                         ++actions_n;
2557                         break;
2558                 case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
2559                 case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
2560                         ret = flow_dv_validate_action_modify_mac(action_flags,
2561                                                                  actions,
2562                                                                  item_flags,
2563                                                                  error);
2564                         if (ret < 0)
2565                                 return ret;
2566                         /* Count all modify-header actions as one action. */
2567                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
2568                                 ++actions_n;
2569                         action_flags |= actions->type ==
2570                                         RTE_FLOW_ACTION_TYPE_SET_MAC_SRC ?
2571                                                 MLX5_FLOW_ACTION_SET_MAC_SRC :
2572                                                 MLX5_FLOW_ACTION_SET_MAC_DST;
2573                         break;
2574
2575                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
2576                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
2577                         ret = flow_dv_validate_action_modify_ipv4(action_flags,
2578                                                                   actions,
2579                                                                   item_flags,
2580                                                                   error);
2581                         if (ret < 0)
2582                                 return ret;
2583                         /* Count all modify-header actions as one action. */
2584                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
2585                                 ++actions_n;
2586                         action_flags |= actions->type ==
2587                                         RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC ?
2588                                                 MLX5_FLOW_ACTION_SET_IPV4_SRC :
2589                                                 MLX5_FLOW_ACTION_SET_IPV4_DST;
2590                         break;
2591                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
2592                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
2593                         ret = flow_dv_validate_action_modify_ipv6(action_flags,
2594                                                                   actions,
2595                                                                   item_flags,
2596                                                                   error);
2597                         if (ret < 0)
2598                                 return ret;
2599                         /* Count all modify-header actions as one action. */
2600                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
2601                                 ++actions_n;
2602                         action_flags |= actions->type ==
2603                                         RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC ?
2604                                                 MLX5_FLOW_ACTION_SET_IPV6_SRC :
2605                                                 MLX5_FLOW_ACTION_SET_IPV6_DST;
2606                         break;
2607                 case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
2608                 case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
2609                         ret = flow_dv_validate_action_modify_tp(action_flags,
2610                                                                 actions,
2611                                                                 item_flags,
2612                                                                 error);
2613                         if (ret < 0)
2614                                 return ret;
2615                         /* Count all modify-header actions as one action. */
2616                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
2617                                 ++actions_n;
2618                         action_flags |= actions->type ==
2619                                         RTE_FLOW_ACTION_TYPE_SET_TP_SRC ?
2620                                                 MLX5_FLOW_ACTION_SET_TP_SRC :
2621                                                 MLX5_FLOW_ACTION_SET_TP_DST;
2622                         break;
2623                 case RTE_FLOW_ACTION_TYPE_DEC_TTL:
2624                 case RTE_FLOW_ACTION_TYPE_SET_TTL:
2625                         ret = flow_dv_validate_action_modify_ttl(action_flags,
2626                                                                  actions,
2627                                                                  item_flags,
2628                                                                  error);
2629                         if (ret < 0)
2630                                 return ret;
2631                         /* Count all modify-header actions as one action. */
2632                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
2633                                 ++actions_n;
2634                         action_flags |= actions->type ==
2635                                         RTE_FLOW_ACTION_TYPE_SET_TTL ?
2636                                                 MLX5_FLOW_ACTION_SET_TTL :
2637                                                 MLX5_FLOW_ACTION_DEC_TTL;
2638                         break;
2639                 case RTE_FLOW_ACTION_TYPE_JUMP:
2640                         ret = flow_dv_validate_action_jump(actions,
2641                                                            attr->group, error);
2642                         if (ret)
2643                                 return ret;
2644                         ++actions_n;
2645                         action_flags |= MLX5_FLOW_ACTION_JUMP;
2646                         break;
2647                 case RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ:
2648                 case RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ:
2649                         ret = flow_dv_validate_action_modify_tcp_seq
2650                                                                 (action_flags,
2651                                                                  actions,
2652                                                                  item_flags,
2653                                                                  error);
2654                         if (ret < 0)
2655                                 return ret;
2656                         /* Count all modify-header actions as one action. */
2657                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
2658                                 ++actions_n;
2659                         action_flags |= actions->type ==
2660                                         RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ ?
2661                                                 MLX5_FLOW_ACTION_INC_TCP_SEQ :
2662                                                 MLX5_FLOW_ACTION_DEC_TCP_SEQ;
2663                         break;
2664                 case RTE_FLOW_ACTION_TYPE_INC_TCP_ACK:
2665                 case RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK:
2666                         ret = flow_dv_validate_action_modify_tcp_ack
2667                                                                 (action_flags,
2668                                                                  actions,
2669                                                                  item_flags,
2670                                                                  error);
2671                         if (ret < 0)
2672                                 return ret;
2673                         /* Count all modify-header actions as one action. */
2674                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
2675                                 ++actions_n;
2676                         action_flags |= actions->type ==
2677                                         RTE_FLOW_ACTION_TYPE_INC_TCP_ACK ?
2678                                                 MLX5_FLOW_ACTION_INC_TCP_ACK :
2679                                                 MLX5_FLOW_ACTION_DEC_TCP_ACK;
2680                         break;
2681                 default:
2682                         return rte_flow_error_set(error, ENOTSUP,
2683                                                   RTE_FLOW_ERROR_TYPE_ACTION,
2684                                                   actions,
2685                                                   "action not supported");
2686                 }
2687         }
2688         /* Eswitch has few restrictions on using items and actions */
2689         if (attr->transfer) {
2690                 if (action_flags & MLX5_FLOW_ACTION_FLAG)
2691                         return rte_flow_error_set(error, ENOTSUP,
2692                                                   RTE_FLOW_ERROR_TYPE_ACTION,
2693                                                   NULL,
2694                                                   "unsupported action FLAG");
2695                 if (action_flags & MLX5_FLOW_ACTION_MARK)
2696                         return rte_flow_error_set(error, ENOTSUP,
2697                                                   RTE_FLOW_ERROR_TYPE_ACTION,
2698                                                   NULL,
2699                                                   "unsupported action MARK");
2700                 if (action_flags & MLX5_FLOW_ACTION_QUEUE)
2701                         return rte_flow_error_set(error, ENOTSUP,
2702                                                   RTE_FLOW_ERROR_TYPE_ACTION,
2703                                                   NULL,
2704                                                   "unsupported action QUEUE");
2705                 if (action_flags & MLX5_FLOW_ACTION_RSS)
2706                         return rte_flow_error_set(error, ENOTSUP,
2707                                                   RTE_FLOW_ERROR_TYPE_ACTION,
2708                                                   NULL,
2709                                                   "unsupported action RSS");
2710                 if (!(action_flags & MLX5_FLOW_FATE_ESWITCH_ACTIONS))
2711                         return rte_flow_error_set(error, EINVAL,
2712                                                   RTE_FLOW_ERROR_TYPE_ACTION,
2713                                                   actions,
2714                                                   "no fate action is found");
2715         } else {
2716                 if (!(action_flags & MLX5_FLOW_FATE_ACTIONS) && attr->ingress)
2717                         return rte_flow_error_set(error, EINVAL,
2718                                                   RTE_FLOW_ERROR_TYPE_ACTION,
2719                                                   actions,
2720                                                   "no fate action is found");
2721         }
2722         return 0;
2723 }
2724
2725 /**
2726  * Internal preparation function. Allocates the DV flow size,
2727  * this size is constant.
2728  *
2729  * @param[in] attr
2730  *   Pointer to the flow attributes.
2731  * @param[in] items
2732  *   Pointer to the list of items.
2733  * @param[in] actions
2734  *   Pointer to the list of actions.
2735  * @param[out] error
2736  *   Pointer to the error structure.
2737  *
2738  * @return
2739  *   Pointer to mlx5_flow object on success,
2740  *   otherwise NULL and rte_errno is set.
2741  */
2742 static struct mlx5_flow *
2743 flow_dv_prepare(const struct rte_flow_attr *attr __rte_unused,
2744                 const struct rte_flow_item items[] __rte_unused,
2745                 const struct rte_flow_action actions[] __rte_unused,
2746                 struct rte_flow_error *error)
2747 {
2748         uint32_t size = sizeof(struct mlx5_flow);
2749         struct mlx5_flow *flow;
2750
2751         flow = rte_calloc(__func__, 1, size, 0);
2752         if (!flow) {
2753                 rte_flow_error_set(error, ENOMEM,
2754                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2755                                    "not enough memory to create flow");
2756                 return NULL;
2757         }
2758         flow->dv.value.size = MLX5_ST_SZ_BYTES(fte_match_param);
2759         return flow;
2760 }
2761
2762 #ifndef NDEBUG
2763 /**
2764  * Sanity check for match mask and value. Similar to check_valid_spec() in
2765  * kernel driver. If unmasked bit is present in value, it returns failure.
2766  *
2767  * @param match_mask
2768  *   pointer to match mask buffer.
2769  * @param match_value
2770  *   pointer to match value buffer.
2771  *
2772  * @return
2773  *   0 if valid, -EINVAL otherwise.
2774  */
2775 static int
2776 flow_dv_check_valid_spec(void *match_mask, void *match_value)
2777 {
2778         uint8_t *m = match_mask;
2779         uint8_t *v = match_value;
2780         unsigned int i;
2781
2782         for (i = 0; i < MLX5_ST_SZ_BYTES(fte_match_param); ++i) {
2783                 if (v[i] & ~m[i]) {
2784                         DRV_LOG(ERR,
2785                                 "match_value differs from match_criteria"
2786                                 " %p[%u] != %p[%u]",
2787                                 match_value, i, match_mask, i);
2788                         return -EINVAL;
2789                 }
2790         }
2791         return 0;
2792 }
2793 #endif
2794
2795 /**
2796  * Add Ethernet item to matcher and to the value.
2797  *
2798  * @param[in, out] matcher
2799  *   Flow matcher.
2800  * @param[in, out] key
2801  *   Flow matcher value.
2802  * @param[in] item
2803  *   Flow pattern to translate.
2804  * @param[in] inner
2805  *   Item is inner pattern.
2806  */
2807 static void
2808 flow_dv_translate_item_eth(void *matcher, void *key,
2809                            const struct rte_flow_item *item, int inner)
2810 {
2811         const struct rte_flow_item_eth *eth_m = item->mask;
2812         const struct rte_flow_item_eth *eth_v = item->spec;
2813         const struct rte_flow_item_eth nic_mask = {
2814                 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
2815                 .src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
2816                 .type = RTE_BE16(0xffff),
2817         };
2818         void *headers_m;
2819         void *headers_v;
2820         char *l24_v;
2821         unsigned int i;
2822
2823         if (!eth_v)
2824                 return;
2825         if (!eth_m)
2826                 eth_m = &nic_mask;
2827         if (inner) {
2828                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
2829                                          inner_headers);
2830                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
2831         } else {
2832                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
2833                                          outer_headers);
2834                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
2835         }
2836         memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m, dmac_47_16),
2837                &eth_m->dst, sizeof(eth_m->dst));
2838         /* The value must be in the range of the mask. */
2839         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, dmac_47_16);
2840         for (i = 0; i < sizeof(eth_m->dst); ++i)
2841                 l24_v[i] = eth_m->dst.addr_bytes[i] & eth_v->dst.addr_bytes[i];
2842         memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m, smac_47_16),
2843                &eth_m->src, sizeof(eth_m->src));
2844         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, smac_47_16);
2845         /* The value must be in the range of the mask. */
2846         for (i = 0; i < sizeof(eth_m->dst); ++i)
2847                 l24_v[i] = eth_m->src.addr_bytes[i] & eth_v->src.addr_bytes[i];
2848         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ethertype,
2849                  rte_be_to_cpu_16(eth_m->type));
2850         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, ethertype);
2851         *(uint16_t *)(l24_v) = eth_m->type & eth_v->type;
2852 }
2853
2854 /**
2855  * Add VLAN item to matcher and to the value.
2856  *
2857  * @param[in, out] matcher
2858  *   Flow matcher.
2859  * @param[in, out] key
2860  *   Flow matcher value.
2861  * @param[in] item
2862  *   Flow pattern to translate.
2863  * @param[in] inner
2864  *   Item is inner pattern.
2865  */
2866 static void
2867 flow_dv_translate_item_vlan(void *matcher, void *key,
2868                             const struct rte_flow_item *item,
2869                             int inner)
2870 {
2871         const struct rte_flow_item_vlan *vlan_m = item->mask;
2872         const struct rte_flow_item_vlan *vlan_v = item->spec;
2873         const struct rte_flow_item_vlan nic_mask = {
2874                 .tci = RTE_BE16(0x0fff),
2875                 .inner_type = RTE_BE16(0xffff),
2876         };
2877         void *headers_m;
2878         void *headers_v;
2879         uint16_t tci_m;
2880         uint16_t tci_v;
2881
2882         if (!vlan_v)
2883                 return;
2884         if (!vlan_m)
2885                 vlan_m = &nic_mask;
2886         if (inner) {
2887                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
2888                                          inner_headers);
2889                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
2890         } else {
2891                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
2892                                          outer_headers);
2893                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
2894         }
2895         tci_m = rte_be_to_cpu_16(vlan_m->tci);
2896         tci_v = rte_be_to_cpu_16(vlan_m->tci & vlan_v->tci);
2897         MLX5_SET(fte_match_set_lyr_2_4, headers_m, cvlan_tag, 1);
2898         MLX5_SET(fte_match_set_lyr_2_4, headers_v, cvlan_tag, 1);
2899         MLX5_SET(fte_match_set_lyr_2_4, headers_m, first_vid, tci_m);
2900         MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_vid, tci_v);
2901         MLX5_SET(fte_match_set_lyr_2_4, headers_m, first_cfi, tci_m >> 12);
2902         MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_cfi, tci_v >> 12);
2903         MLX5_SET(fte_match_set_lyr_2_4, headers_m, first_prio, tci_m >> 13);
2904         MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_prio, tci_v >> 13);
2905 }
2906
2907 /**
2908  * Add IPV4 item to matcher and to the value.
2909  *
2910  * @param[in, out] matcher
2911  *   Flow matcher.
2912  * @param[in, out] key
2913  *   Flow matcher value.
2914  * @param[in] item
2915  *   Flow pattern to translate.
2916  * @param[in] inner
2917  *   Item is inner pattern.
2918  * @param[in] group
2919  *   The group to insert the rule.
2920  */
2921 static void
2922 flow_dv_translate_item_ipv4(void *matcher, void *key,
2923                             const struct rte_flow_item *item,
2924                             int inner, uint32_t group)
2925 {
2926         const struct rte_flow_item_ipv4 *ipv4_m = item->mask;
2927         const struct rte_flow_item_ipv4 *ipv4_v = item->spec;
2928         const struct rte_flow_item_ipv4 nic_mask = {
2929                 .hdr = {
2930                         .src_addr = RTE_BE32(0xffffffff),
2931                         .dst_addr = RTE_BE32(0xffffffff),
2932                         .type_of_service = 0xff,
2933                         .next_proto_id = 0xff,
2934                 },
2935         };
2936         void *headers_m;
2937         void *headers_v;
2938         char *l24_m;
2939         char *l24_v;
2940         uint8_t tos;
2941
2942         if (inner) {
2943                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
2944                                          inner_headers);
2945                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
2946         } else {
2947                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
2948                                          outer_headers);
2949                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
2950         }
2951         if (group == 0)
2952                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version, 0xf);
2953         else
2954                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version, 0x4);
2955         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_version, 4);
2956         if (!ipv4_v)
2957                 return;
2958         if (!ipv4_m)
2959                 ipv4_m = &nic_mask;
2960         l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
2961                              dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
2962         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2963                              dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
2964         *(uint32_t *)l24_m = ipv4_m->hdr.dst_addr;
2965         *(uint32_t *)l24_v = ipv4_m->hdr.dst_addr & ipv4_v->hdr.dst_addr;
2966         l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
2967                           src_ipv4_src_ipv6.ipv4_layout.ipv4);
2968         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2969                           src_ipv4_src_ipv6.ipv4_layout.ipv4);
2970         *(uint32_t *)l24_m = ipv4_m->hdr.src_addr;
2971         *(uint32_t *)l24_v = ipv4_m->hdr.src_addr & ipv4_v->hdr.src_addr;
2972         tos = ipv4_m->hdr.type_of_service & ipv4_v->hdr.type_of_service;
2973         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ecn,
2974                  ipv4_m->hdr.type_of_service);
2975         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn, tos);
2976         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_dscp,
2977                  ipv4_m->hdr.type_of_service >> 2);
2978         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp, tos >> 2);
2979         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol,
2980                  ipv4_m->hdr.next_proto_id);
2981         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2982                  ipv4_v->hdr.next_proto_id & ipv4_m->hdr.next_proto_id);
2983 }
2984
2985 /**
2986  * Add IPV6 item to matcher and to the value.
2987  *
2988  * @param[in, out] matcher
2989  *   Flow matcher.
2990  * @param[in, out] key
2991  *   Flow matcher value.
2992  * @param[in] item
2993  *   Flow pattern to translate.
2994  * @param[in] inner
2995  *   Item is inner pattern.
2996  * @param[in] group
2997  *   The group to insert the rule.
2998  */
2999 static void
3000 flow_dv_translate_item_ipv6(void *matcher, void *key,
3001                             const struct rte_flow_item *item,
3002                             int inner, uint32_t group)
3003 {
3004         const struct rte_flow_item_ipv6 *ipv6_m = item->mask;
3005         const struct rte_flow_item_ipv6 *ipv6_v = item->spec;
3006         const struct rte_flow_item_ipv6 nic_mask = {
3007                 .hdr = {
3008                         .src_addr =
3009                                 "\xff\xff\xff\xff\xff\xff\xff\xff"
3010                                 "\xff\xff\xff\xff\xff\xff\xff\xff",
3011                         .dst_addr =
3012                                 "\xff\xff\xff\xff\xff\xff\xff\xff"
3013                                 "\xff\xff\xff\xff\xff\xff\xff\xff",
3014                         .vtc_flow = RTE_BE32(0xffffffff),
3015                         .proto = 0xff,
3016                         .hop_limits = 0xff,
3017                 },
3018         };
3019         void *headers_m;
3020         void *headers_v;
3021         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
3022         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
3023         char *l24_m;
3024         char *l24_v;
3025         uint32_t vtc_m;
3026         uint32_t vtc_v;
3027         int i;
3028         int size;
3029
3030         if (inner) {
3031                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
3032                                          inner_headers);
3033                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
3034         } else {
3035                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
3036                                          outer_headers);
3037                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
3038         }
3039         if (group == 0)
3040                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version, 0xf);
3041         else
3042                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version, 0x6);
3043         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_version, 6);
3044         if (!ipv6_v)
3045                 return;
3046         if (!ipv6_m)
3047                 ipv6_m = &nic_mask;
3048         size = sizeof(ipv6_m->hdr.dst_addr);
3049         l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
3050                              dst_ipv4_dst_ipv6.ipv6_layout.ipv6);
3051         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
3052                              dst_ipv4_dst_ipv6.ipv6_layout.ipv6);
3053         memcpy(l24_m, ipv6_m->hdr.dst_addr, size);
3054         for (i = 0; i < size; ++i)
3055                 l24_v[i] = l24_m[i] & ipv6_v->hdr.dst_addr[i];
3056         l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
3057                              src_ipv4_src_ipv6.ipv6_layout.ipv6);
3058         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
3059                              src_ipv4_src_ipv6.ipv6_layout.ipv6);
3060         memcpy(l24_m, ipv6_m->hdr.src_addr, size);
3061         for (i = 0; i < size; ++i)
3062                 l24_v[i] = l24_m[i] & ipv6_v->hdr.src_addr[i];
3063         /* TOS. */
3064         vtc_m = rte_be_to_cpu_32(ipv6_m->hdr.vtc_flow);
3065         vtc_v = rte_be_to_cpu_32(ipv6_m->hdr.vtc_flow & ipv6_v->hdr.vtc_flow);
3066         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ecn, vtc_m >> 20);
3067         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn, vtc_v >> 20);
3068         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_dscp, vtc_m >> 22);
3069         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp, vtc_v >> 22);
3070         /* Label. */
3071         if (inner) {
3072                 MLX5_SET(fte_match_set_misc, misc_m, inner_ipv6_flow_label,
3073                          vtc_m);
3074                 MLX5_SET(fte_match_set_misc, misc_v, inner_ipv6_flow_label,
3075                          vtc_v);
3076         } else {
3077                 MLX5_SET(fte_match_set_misc, misc_m, outer_ipv6_flow_label,
3078                          vtc_m);
3079                 MLX5_SET(fte_match_set_misc, misc_v, outer_ipv6_flow_label,
3080                          vtc_v);
3081         }
3082         /* Protocol. */
3083         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol,
3084                  ipv6_m->hdr.proto);
3085         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
3086                  ipv6_v->hdr.proto & ipv6_m->hdr.proto);
3087 }
3088
3089 /**
3090  * Add TCP item to matcher and to the value.
3091  *
3092  * @param[in, out] matcher
3093  *   Flow matcher.
3094  * @param[in, out] key
3095  *   Flow matcher value.
3096  * @param[in] item
3097  *   Flow pattern to translate.
3098  * @param[in] inner
3099  *   Item is inner pattern.
3100  */
3101 static void
3102 flow_dv_translate_item_tcp(void *matcher, void *key,
3103                            const struct rte_flow_item *item,
3104                            int inner)
3105 {
3106         const struct rte_flow_item_tcp *tcp_m = item->mask;
3107         const struct rte_flow_item_tcp *tcp_v = item->spec;
3108         void *headers_m;
3109         void *headers_v;
3110
3111         if (inner) {
3112                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
3113                                          inner_headers);
3114                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
3115         } else {
3116                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
3117                                          outer_headers);
3118                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
3119         }
3120         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
3121         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_TCP);
3122         if (!tcp_v)
3123                 return;
3124         if (!tcp_m)
3125                 tcp_m = &rte_flow_item_tcp_mask;
3126         MLX5_SET(fte_match_set_lyr_2_4, headers_m, tcp_sport,
3127                  rte_be_to_cpu_16(tcp_m->hdr.src_port));
3128         MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
3129                  rte_be_to_cpu_16(tcp_v->hdr.src_port & tcp_m->hdr.src_port));
3130         MLX5_SET(fte_match_set_lyr_2_4, headers_m, tcp_dport,
3131                  rte_be_to_cpu_16(tcp_m->hdr.dst_port));
3132         MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
3133                  rte_be_to_cpu_16(tcp_v->hdr.dst_port & tcp_m->hdr.dst_port));
3134         MLX5_SET(fte_match_set_lyr_2_4, headers_m, tcp_flags,
3135                  tcp_m->hdr.tcp_flags);
3136         MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_flags,
3137                  (tcp_v->hdr.tcp_flags & tcp_m->hdr.tcp_flags));
3138 }
3139
3140 /**
3141  * Add UDP item to matcher and to the value.
3142  *
3143  * @param[in, out] matcher
3144  *   Flow matcher.
3145  * @param[in, out] key
3146  *   Flow matcher value.
3147  * @param[in] item
3148  *   Flow pattern to translate.
3149  * @param[in] inner
3150  *   Item is inner pattern.
3151  */
3152 static void
3153 flow_dv_translate_item_udp(void *matcher, void *key,
3154                            const struct rte_flow_item *item,
3155                            int inner)
3156 {
3157         const struct rte_flow_item_udp *udp_m = item->mask;
3158         const struct rte_flow_item_udp *udp_v = item->spec;
3159         void *headers_m;
3160         void *headers_v;
3161
3162         if (inner) {
3163                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
3164                                          inner_headers);
3165                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
3166         } else {
3167                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
3168                                          outer_headers);
3169                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
3170         }
3171         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
3172         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_UDP);
3173         if (!udp_v)
3174                 return;
3175         if (!udp_m)
3176                 udp_m = &rte_flow_item_udp_mask;
3177         MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_sport,
3178                  rte_be_to_cpu_16(udp_m->hdr.src_port));
3179         MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
3180                  rte_be_to_cpu_16(udp_v->hdr.src_port & udp_m->hdr.src_port));
3181         MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport,
3182                  rte_be_to_cpu_16(udp_m->hdr.dst_port));
3183         MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
3184                  rte_be_to_cpu_16(udp_v->hdr.dst_port & udp_m->hdr.dst_port));
3185 }
3186
3187 /**
3188  * Add GRE item to matcher and to the value.
3189  *
3190  * @param[in, out] matcher
3191  *   Flow matcher.
3192  * @param[in, out] key
3193  *   Flow matcher value.
3194  * @param[in] item
3195  *   Flow pattern to translate.
3196  * @param[in] inner
3197  *   Item is inner pattern.
3198  */
3199 static void
3200 flow_dv_translate_item_gre(void *matcher, void *key,
3201                            const struct rte_flow_item *item,
3202                            int inner)
3203 {
3204         const struct rte_flow_item_gre *gre_m = item->mask;
3205         const struct rte_flow_item_gre *gre_v = item->spec;
3206         void *headers_m;
3207         void *headers_v;
3208         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
3209         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
3210
3211         if (inner) {
3212                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
3213                                          inner_headers);
3214                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
3215         } else {
3216                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
3217                                          outer_headers);
3218                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
3219         }
3220         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
3221         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_GRE);
3222         if (!gre_v)
3223                 return;
3224         if (!gre_m)
3225                 gre_m = &rte_flow_item_gre_mask;
3226         MLX5_SET(fte_match_set_misc, misc_m, gre_protocol,
3227                  rte_be_to_cpu_16(gre_m->protocol));
3228         MLX5_SET(fte_match_set_misc, misc_v, gre_protocol,
3229                  rte_be_to_cpu_16(gre_v->protocol & gre_m->protocol));
3230 }
3231
3232 /**
3233  * Add NVGRE item to matcher and to the value.
3234  *
3235  * @param[in, out] matcher
3236  *   Flow matcher.
3237  * @param[in, out] key
3238  *   Flow matcher value.
3239  * @param[in] item
3240  *   Flow pattern to translate.
3241  * @param[in] inner
3242  *   Item is inner pattern.
3243  */
3244 static void
3245 flow_dv_translate_item_nvgre(void *matcher, void *key,
3246                              const struct rte_flow_item *item,
3247                              int inner)
3248 {
3249         const struct rte_flow_item_nvgre *nvgre_m = item->mask;
3250         const struct rte_flow_item_nvgre *nvgre_v = item->spec;
3251         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
3252         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
3253         const char *tni_flow_id_m = (const char *)nvgre_m->tni;
3254         const char *tni_flow_id_v = (const char *)nvgre_v->tni;
3255         char *gre_key_m;
3256         char *gre_key_v;
3257         int size;
3258         int i;
3259
3260         flow_dv_translate_item_gre(matcher, key, item, inner);
3261         if (!nvgre_v)
3262                 return;
3263         if (!nvgre_m)
3264                 nvgre_m = &rte_flow_item_nvgre_mask;
3265         size = sizeof(nvgre_m->tni) + sizeof(nvgre_m->flow_id);
3266         gre_key_m = MLX5_ADDR_OF(fte_match_set_misc, misc_m, gre_key_h);
3267         gre_key_v = MLX5_ADDR_OF(fte_match_set_misc, misc_v, gre_key_h);
3268         memcpy(gre_key_m, tni_flow_id_m, size);
3269         for (i = 0; i < size; ++i)
3270                 gre_key_v[i] = gre_key_m[i] & tni_flow_id_v[i];
3271 }
3272
3273 /**
3274  * Add VXLAN item to matcher and to the value.
3275  *
3276  * @param[in, out] matcher
3277  *   Flow matcher.
3278  * @param[in, out] key
3279  *   Flow matcher value.
3280  * @param[in] item
3281  *   Flow pattern to translate.
3282  * @param[in] inner
3283  *   Item is inner pattern.
3284  */
3285 static void
3286 flow_dv_translate_item_vxlan(void *matcher, void *key,
3287                              const struct rte_flow_item *item,
3288                              int inner)
3289 {
3290         const struct rte_flow_item_vxlan *vxlan_m = item->mask;
3291         const struct rte_flow_item_vxlan *vxlan_v = item->spec;
3292         void *headers_m;
3293         void *headers_v;
3294         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
3295         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
3296         char *vni_m;
3297         char *vni_v;
3298         uint16_t dport;
3299         int size;
3300         int i;
3301
3302         if (inner) {
3303                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
3304                                          inner_headers);
3305                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
3306         } else {
3307                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
3308                                          outer_headers);
3309                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
3310         }
3311         dport = item->type == RTE_FLOW_ITEM_TYPE_VXLAN ?
3312                 MLX5_UDP_PORT_VXLAN : MLX5_UDP_PORT_VXLAN_GPE;
3313         if (!MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport)) {
3314                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xFFFF);
3315                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, dport);
3316         }
3317         if (!vxlan_v)
3318                 return;
3319         if (!vxlan_m)
3320                 vxlan_m = &rte_flow_item_vxlan_mask;
3321         size = sizeof(vxlan_m->vni);
3322         vni_m = MLX5_ADDR_OF(fte_match_set_misc, misc_m, vxlan_vni);
3323         vni_v = MLX5_ADDR_OF(fte_match_set_misc, misc_v, vxlan_vni);
3324         memcpy(vni_m, vxlan_m->vni, size);
3325         for (i = 0; i < size; ++i)
3326                 vni_v[i] = vni_m[i] & vxlan_v->vni[i];
3327 }
3328
3329 /**
3330  * Add MPLS item to matcher and to the value.
3331  *
3332  * @param[in, out] matcher
3333  *   Flow matcher.
3334  * @param[in, out] key
3335  *   Flow matcher value.
3336  * @param[in] item
3337  *   Flow pattern to translate.
3338  * @param[in] prev_layer
3339  *   The protocol layer indicated in previous item.
3340  * @param[in] inner
3341  *   Item is inner pattern.
3342  */
3343 static void
3344 flow_dv_translate_item_mpls(void *matcher, void *key,
3345                             const struct rte_flow_item *item,
3346                             uint64_t prev_layer,
3347                             int inner)
3348 {
3349         const uint32_t *in_mpls_m = item->mask;
3350         const uint32_t *in_mpls_v = item->spec;
3351         uint32_t *out_mpls_m = 0;
3352         uint32_t *out_mpls_v = 0;
3353         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
3354         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
3355         void *misc2_m = MLX5_ADDR_OF(fte_match_param, matcher,
3356                                      misc_parameters_2);
3357         void *misc2_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_2);
3358         void *headers_m = MLX5_ADDR_OF(fte_match_param, matcher, outer_headers);
3359         void *headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
3360
3361         switch (prev_layer) {
3362         case MLX5_FLOW_LAYER_OUTER_L4_UDP:
3363                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xffff);
3364                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
3365                          MLX5_UDP_PORT_MPLS);
3366                 break;
3367         case MLX5_FLOW_LAYER_GRE:
3368                 MLX5_SET(fte_match_set_misc, misc_m, gre_protocol, 0xffff);
3369                 MLX5_SET(fte_match_set_misc, misc_v, gre_protocol,
3370                          RTE_ETHER_TYPE_MPLS);
3371                 break;
3372         default:
3373                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
3374                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
3375                          IPPROTO_MPLS);
3376                 break;
3377         }
3378         if (!in_mpls_v)
3379                 return;
3380         if (!in_mpls_m)
3381                 in_mpls_m = (const uint32_t *)&rte_flow_item_mpls_mask;
3382         switch (prev_layer) {
3383         case MLX5_FLOW_LAYER_OUTER_L4_UDP:
3384                 out_mpls_m =
3385                         (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_m,
3386                                                  outer_first_mpls_over_udp);
3387                 out_mpls_v =
3388                         (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_v,
3389                                                  outer_first_mpls_over_udp);
3390                 break;
3391         case MLX5_FLOW_LAYER_GRE:
3392                 out_mpls_m =
3393                         (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_m,
3394                                                  outer_first_mpls_over_gre);
3395                 out_mpls_v =
3396                         (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_v,
3397                                                  outer_first_mpls_over_gre);
3398                 break;
3399         default:
3400                 /* Inner MPLS not over GRE is not supported. */
3401                 if (!inner) {
3402                         out_mpls_m =
3403                                 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2,
3404                                                          misc2_m,
3405                                                          outer_first_mpls);
3406                         out_mpls_v =
3407                                 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2,
3408                                                          misc2_v,
3409                                                          outer_first_mpls);
3410                 }
3411                 break;
3412         }
3413         if (out_mpls_m && out_mpls_v) {
3414                 *out_mpls_m = *in_mpls_m;
3415                 *out_mpls_v = *in_mpls_v & *in_mpls_m;
3416         }
3417 }
3418
3419 /**
3420  * Add META item to matcher
3421  *
3422  * @param[in, out] matcher
3423  *   Flow matcher.
3424  * @param[in, out] key
3425  *   Flow matcher value.
3426  * @param[in] item
3427  *   Flow pattern to translate.
3428  * @param[in] inner
3429  *   Item is inner pattern.
3430  */
3431 static void
3432 flow_dv_translate_item_meta(void *matcher, void *key,
3433                             const struct rte_flow_item *item)
3434 {
3435         const struct rte_flow_item_meta *meta_m;
3436         const struct rte_flow_item_meta *meta_v;
3437         void *misc2_m =
3438                 MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters_2);
3439         void *misc2_v =
3440                 MLX5_ADDR_OF(fte_match_param, key, misc_parameters_2);
3441
3442         meta_m = (const void *)item->mask;
3443         if (!meta_m)
3444                 meta_m = &rte_flow_item_meta_mask;
3445         meta_v = (const void *)item->spec;
3446         if (meta_v) {
3447                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_a,
3448                          rte_be_to_cpu_32(meta_m->data));
3449                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_a,
3450                          rte_be_to_cpu_32(meta_v->data & meta_m->data));
3451         }
3452 }
3453
3454 /**
3455  * Add source vport match to the specified matcher.
3456  *
3457  * @param[in, out] matcher
3458  *   Flow matcher.
3459  * @param[in, out] key
3460  *   Flow matcher value.
3461  * @param[in] port
3462  *   Source vport value to match
3463  * @param[in] mask
3464  *   Mask
3465  */
3466 static void
3467 flow_dv_translate_item_source_vport(void *matcher, void *key,
3468                                     int16_t port, uint16_t mask)
3469 {
3470         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
3471         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
3472
3473         MLX5_SET(fte_match_set_misc, misc_m, source_port, mask);
3474         MLX5_SET(fte_match_set_misc, misc_v, source_port, port);
3475 }
3476
3477 /**
3478  * Translate port-id item to eswitch match on  port-id.
3479  *
3480  * @param[in] dev
3481  *   The devich to configure through.
3482  * @param[in, out] matcher
3483  *   Flow matcher.
3484  * @param[in, out] key
3485  *   Flow matcher value.
3486  * @param[in] item
3487  *   Flow pattern to translate.
3488  *
3489  * @return
3490  *   0 on success, a negative errno value otherwise.
3491  */
3492 static int
3493 flow_dv_translate_item_port_id(struct rte_eth_dev *dev, void *matcher,
3494                                void *key, const struct rte_flow_item *item)
3495 {
3496         const struct rte_flow_item_port_id *pid_m = item ? item->mask : NULL;
3497         const struct rte_flow_item_port_id *pid_v = item ? item->spec : NULL;
3498         uint16_t mask, val, id;
3499         int ret;
3500
3501         mask = pid_m ? pid_m->id : 0xffff;
3502         id = pid_v ? pid_v->id : dev->data->port_id;
3503         ret = mlx5_port_to_eswitch_info(id, NULL, &val);
3504         if (ret)
3505                 return ret;
3506         flow_dv_translate_item_source_vport(matcher, key, val, mask);
3507         return 0;
3508 }
3509
3510 static uint32_t matcher_zero[MLX5_ST_SZ_DW(fte_match_param)] = { 0 };
3511
3512 #define HEADER_IS_ZERO(match_criteria, headers)                              \
3513         !(memcmp(MLX5_ADDR_OF(fte_match_param, match_criteria, headers),     \
3514                  matcher_zero, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
3515
3516 /**
3517  * Calculate flow matcher enable bitmap.
3518  *
3519  * @param match_criteria
3520  *   Pointer to flow matcher criteria.
3521  *
3522  * @return
3523  *   Bitmap of enabled fields.
3524  */
3525 static uint8_t
3526 flow_dv_matcher_enable(uint32_t *match_criteria)
3527 {
3528         uint8_t match_criteria_enable;
3529
3530         match_criteria_enable =
3531                 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
3532                 MLX5_MATCH_CRITERIA_ENABLE_OUTER_BIT;
3533         match_criteria_enable |=
3534                 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
3535                 MLX5_MATCH_CRITERIA_ENABLE_MISC_BIT;
3536         match_criteria_enable |=
3537                 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
3538                 MLX5_MATCH_CRITERIA_ENABLE_INNER_BIT;
3539         match_criteria_enable |=
3540                 (!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
3541                 MLX5_MATCH_CRITERIA_ENABLE_MISC2_BIT;
3542 #ifdef HAVE_MLX5DV_DR
3543         match_criteria_enable |=
3544                 (!HEADER_IS_ZERO(match_criteria, misc_parameters_3)) <<
3545                 MLX5_MATCH_CRITERIA_ENABLE_MISC3_BIT;
3546 #endif
3547         return match_criteria_enable;
3548 }
3549
3550
3551 /**
3552  * Get a flow table.
3553  *
3554  * @param dev[in, out]
3555  *   Pointer to rte_eth_dev structure.
3556  * @param[in] table_id
3557  *   Table id to use.
3558  * @param[in] egress
3559  *   Direction of the table.
3560  * @param[in] transfer
3561  *   E-Switch or NIC flow.
3562  * @param[out] error
3563  *   pointer to error structure.
3564  *
3565  * @return
3566  *   Returns tables resource based on the index, NULL in case of failed.
3567  */
3568 static struct mlx5_flow_tbl_resource *
3569 flow_dv_tbl_resource_get(struct rte_eth_dev *dev,
3570                          uint32_t table_id, uint8_t egress,
3571                          uint8_t transfer,
3572                          struct rte_flow_error *error)
3573 {
3574         struct mlx5_priv *priv = dev->data->dev_private;
3575         struct mlx5_ibv_shared *sh = priv->sh;
3576         struct mlx5_flow_tbl_resource *tbl;
3577
3578 #ifdef HAVE_MLX5DV_DR
3579         if (transfer) {
3580                 tbl = &sh->fdb_tbl[table_id];
3581                 if (!tbl->obj)
3582                         tbl->obj = mlx5_glue->dr_create_flow_tbl
3583                                 (sh->fdb_domain, table_id);
3584         } else if (egress) {
3585                 tbl = &sh->tx_tbl[table_id];
3586                 if (!tbl->obj)
3587                         tbl->obj = mlx5_glue->dr_create_flow_tbl
3588                                 (sh->tx_domain, table_id);
3589         } else {
3590                 tbl = &sh->rx_tbl[table_id];
3591                 if (!tbl->obj)
3592                         tbl->obj = mlx5_glue->dr_create_flow_tbl
3593                                 (sh->rx_domain, table_id);
3594         }
3595         if (!tbl->obj) {
3596                 rte_flow_error_set(error, ENOMEM,
3597                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3598                                    NULL, "cannot create table");
3599                 return NULL;
3600         }
3601         rte_atomic32_inc(&tbl->refcnt);
3602         return tbl;
3603 #else
3604         (void)error;
3605         (void)tbl;
3606         if (transfer)
3607                 return &sh->fdb_tbl[table_id];
3608         else if (egress)
3609                 return &sh->tx_tbl[table_id];
3610         else
3611                 return &sh->rx_tbl[table_id];
3612 #endif
3613 }
3614
3615 /**
3616  * Release a flow table.
3617  *
3618  * @param[in] tbl
3619  *   Table resource to be released.
3620  *
3621  * @return
3622  *   Returns 0 if table was released, else return 1;
3623  */
3624 static int
3625 flow_dv_tbl_resource_release(struct mlx5_flow_tbl_resource *tbl)
3626 {
3627         if (!tbl)
3628                 return 0;
3629         if (rte_atomic32_dec_and_test(&tbl->refcnt)) {
3630                 mlx5_glue->dr_destroy_flow_tbl(tbl->obj);
3631                 tbl->obj = NULL;
3632                 return 0;
3633         }
3634         return 1;
3635 }
3636
3637 /**
3638  * Register the flow matcher.
3639  *
3640  * @param dev[in, out]
3641  *   Pointer to rte_eth_dev structure.
3642  * @param[in, out] matcher
3643  *   Pointer to flow matcher.
3644  * @parm[in, out] dev_flow
3645  *   Pointer to the dev_flow.
3646  * @param[out] error
3647  *   pointer to error structure.
3648  *
3649  * @return
3650  *   0 on success otherwise -errno and errno is set.
3651  */
3652 static int
3653 flow_dv_matcher_register(struct rte_eth_dev *dev,
3654                          struct mlx5_flow_dv_matcher *matcher,
3655                          struct mlx5_flow *dev_flow,
3656                          struct rte_flow_error *error)
3657 {
3658         struct mlx5_priv *priv = dev->data->dev_private;
3659         struct mlx5_ibv_shared *sh = priv->sh;
3660         struct mlx5_flow_dv_matcher *cache_matcher;
3661         struct mlx5dv_flow_matcher_attr dv_attr = {
3662                 .type = IBV_FLOW_ATTR_NORMAL,
3663                 .match_mask = (void *)&matcher->mask,
3664         };
3665         struct mlx5_flow_tbl_resource *tbl = NULL;
3666
3667         /* Lookup from cache. */
3668         LIST_FOREACH(cache_matcher, &sh->matchers, next) {
3669                 if (matcher->crc == cache_matcher->crc &&
3670                     matcher->priority == cache_matcher->priority &&
3671                     matcher->egress == cache_matcher->egress &&
3672                     matcher->group == cache_matcher->group &&
3673                     matcher->transfer == cache_matcher->transfer &&
3674                     !memcmp((const void *)matcher->mask.buf,
3675                             (const void *)cache_matcher->mask.buf,
3676                             cache_matcher->mask.size)) {
3677                         DRV_LOG(DEBUG,
3678                                 "priority %hd use %s matcher %p: refcnt %d++",
3679                                 cache_matcher->priority,
3680                                 cache_matcher->egress ? "tx" : "rx",
3681                                 (void *)cache_matcher,
3682                                 rte_atomic32_read(&cache_matcher->refcnt));
3683                         rte_atomic32_inc(&cache_matcher->refcnt);
3684                         dev_flow->dv.matcher = cache_matcher;
3685                         return 0;
3686                 }
3687         }
3688         /* Register new matcher. */
3689         cache_matcher = rte_calloc(__func__, 1, sizeof(*cache_matcher), 0);
3690         if (!cache_matcher)
3691                 return rte_flow_error_set(error, ENOMEM,
3692                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
3693                                           "cannot allocate matcher memory");
3694         tbl = flow_dv_tbl_resource_get(dev, matcher->group * MLX5_GROUP_FACTOR,
3695                                        matcher->egress, matcher->transfer,
3696                                        error);
3697         if (!tbl) {
3698                 rte_free(cache_matcher);
3699                 return rte_flow_error_set(error, ENOMEM,
3700                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3701                                           NULL, "cannot create table");
3702         }
3703         *cache_matcher = *matcher;
3704         dv_attr.match_criteria_enable =
3705                 flow_dv_matcher_enable(cache_matcher->mask.buf);
3706         dv_attr.priority = matcher->priority;
3707         if (matcher->egress)
3708                 dv_attr.flags |= IBV_FLOW_ATTR_FLAGS_EGRESS;
3709         cache_matcher->matcher_object =
3710                 mlx5_glue->dv_create_flow_matcher(sh->ctx, &dv_attr, tbl->obj);
3711         if (!cache_matcher->matcher_object) {
3712                 rte_free(cache_matcher);
3713 #ifdef HAVE_MLX5DV_DR
3714                 flow_dv_tbl_resource_release(tbl);
3715 #endif
3716                 return rte_flow_error_set(error, ENOMEM,
3717                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3718                                           NULL, "cannot create matcher");
3719         }
3720         rte_atomic32_inc(&cache_matcher->refcnt);
3721         LIST_INSERT_HEAD(&sh->matchers, cache_matcher, next);
3722         dev_flow->dv.matcher = cache_matcher;
3723         DRV_LOG(DEBUG, "priority %hd new %s matcher %p: refcnt %d",
3724                 cache_matcher->priority,
3725                 cache_matcher->egress ? "tx" : "rx", (void *)cache_matcher,
3726                 rte_atomic32_read(&cache_matcher->refcnt));
3727         rte_atomic32_inc(&tbl->refcnt);
3728         return 0;
3729 }
3730
3731 /**
3732  * Find existing tag resource or create and register a new one.
3733  *
3734  * @param dev[in, out]
3735  *   Pointer to rte_eth_dev structure.
3736  * @param[in, out] resource
3737  *   Pointer to tag resource.
3738  * @parm[in, out] dev_flow
3739  *   Pointer to the dev_flow.
3740  * @param[out] error
3741  *   pointer to error structure.
3742  *
3743  * @return
3744  *   0 on success otherwise -errno and errno is set.
3745  */
3746 static int
3747 flow_dv_tag_resource_register
3748                         (struct rte_eth_dev *dev,
3749                          struct mlx5_flow_dv_tag_resource *resource,
3750                          struct mlx5_flow *dev_flow,
3751                          struct rte_flow_error *error)
3752 {
3753         struct mlx5_priv *priv = dev->data->dev_private;
3754         struct mlx5_ibv_shared *sh = priv->sh;
3755         struct mlx5_flow_dv_tag_resource *cache_resource;
3756
3757         /* Lookup a matching resource from cache. */
3758         LIST_FOREACH(cache_resource, &sh->tags, next) {
3759                 if (resource->tag == cache_resource->tag) {
3760                         DRV_LOG(DEBUG, "tag resource %p: refcnt %d++",
3761                                 (void *)cache_resource,
3762                                 rte_atomic32_read(&cache_resource->refcnt));
3763                         rte_atomic32_inc(&cache_resource->refcnt);
3764                         dev_flow->flow->tag_resource = cache_resource;
3765                         return 0;
3766                 }
3767         }
3768         /* Register new  resource. */
3769         cache_resource = rte_calloc(__func__, 1, sizeof(*cache_resource), 0);
3770         if (!cache_resource)
3771                 return rte_flow_error_set(error, ENOMEM,
3772                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
3773                                           "cannot allocate resource memory");
3774         *cache_resource = *resource;
3775         cache_resource->action = mlx5_glue->dv_create_flow_action_tag
3776                 (resource->tag);
3777         if (!cache_resource->action) {
3778                 rte_free(cache_resource);
3779                 return rte_flow_error_set(error, ENOMEM,
3780                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3781                                           NULL, "cannot create action");
3782         }
3783         rte_atomic32_init(&cache_resource->refcnt);
3784         rte_atomic32_inc(&cache_resource->refcnt);
3785         LIST_INSERT_HEAD(&sh->tags, cache_resource, next);
3786         dev_flow->flow->tag_resource = cache_resource;
3787         DRV_LOG(DEBUG, "new tag resource %p: refcnt %d++",
3788                 (void *)cache_resource,
3789                 rte_atomic32_read(&cache_resource->refcnt));
3790         return 0;
3791 }
3792
3793 /**
3794  * Release the tag.
3795  *
3796  * @param dev
3797  *   Pointer to Ethernet device.
3798  * @param flow
3799  *   Pointer to mlx5_flow.
3800  *
3801  * @return
3802  *   1 while a reference on it exists, 0 when freed.
3803  */
3804 static int
3805 flow_dv_tag_release(struct rte_eth_dev *dev,
3806                     struct mlx5_flow_dv_tag_resource *tag)
3807 {
3808         assert(tag);
3809         DRV_LOG(DEBUG, "port %u tag %p: refcnt %d--",
3810                 dev->data->port_id, (void *)tag,
3811                 rte_atomic32_read(&tag->refcnt));
3812         if (rte_atomic32_dec_and_test(&tag->refcnt)) {
3813                 claim_zero(mlx5_glue->destroy_flow_action(tag->action));
3814                 LIST_REMOVE(tag, next);
3815                 DRV_LOG(DEBUG, "port %u tag %p: removed",
3816                         dev->data->port_id, (void *)tag);
3817                 rte_free(tag);
3818                 return 0;
3819         }
3820         return 1;
3821 }
3822
3823 /**
3824  * Translate port ID action to vport.
3825  *
3826  * @param[in] dev
3827  *   Pointer to rte_eth_dev structure.
3828  * @param[in] action
3829  *   Pointer to the port ID action.
3830  * @param[out] dst_port_id
3831  *   The target port ID.
3832  * @param[out] error
3833  *   Pointer to the error structure.
3834  *
3835  * @return
3836  *   0 on success, a negative errno value otherwise and rte_errno is set.
3837  */
3838 static int
3839 flow_dv_translate_action_port_id(struct rte_eth_dev *dev,
3840                                  const struct rte_flow_action *action,
3841                                  uint32_t *dst_port_id,
3842                                  struct rte_flow_error *error)
3843 {
3844         uint32_t port;
3845         uint16_t port_id;
3846         int ret;
3847         const struct rte_flow_action_port_id *conf =
3848                         (const struct rte_flow_action_port_id *)action->conf;
3849
3850         port = conf->original ? dev->data->port_id : conf->id;
3851         ret = mlx5_port_to_eswitch_info(port, NULL, &port_id);
3852         if (ret)
3853                 return rte_flow_error_set(error, -ret,
3854                                           RTE_FLOW_ERROR_TYPE_ACTION,
3855                                           NULL,
3856                                           "No eswitch info was found for port");
3857         *dst_port_id = port_id;
3858         return 0;
3859 }
3860
3861 /**
3862  * Fill the flow with DV spec.
3863  *
3864  * @param[in] dev
3865  *   Pointer to rte_eth_dev structure.
3866  * @param[in, out] dev_flow
3867  *   Pointer to the sub flow.
3868  * @param[in] attr
3869  *   Pointer to the flow attributes.
3870  * @param[in] items
3871  *   Pointer to the list of items.
3872  * @param[in] actions
3873  *   Pointer to the list of actions.
3874  * @param[out] error
3875  *   Pointer to the error structure.
3876  *
3877  * @return
3878  *   0 on success, a negative errno value otherwise and rte_errno is set.
3879  */
3880 static int
3881 flow_dv_translate(struct rte_eth_dev *dev,
3882                   struct mlx5_flow *dev_flow,
3883                   const struct rte_flow_attr *attr,
3884                   const struct rte_flow_item items[],
3885                   const struct rte_flow_action actions[],
3886                   struct rte_flow_error *error)
3887 {
3888         struct mlx5_priv *priv = dev->data->dev_private;
3889         struct rte_flow *flow = dev_flow->flow;
3890         uint64_t item_flags = 0;
3891         uint64_t last_item = 0;
3892         uint64_t action_flags = 0;
3893         uint64_t priority = attr->priority;
3894         struct mlx5_flow_dv_matcher matcher = {
3895                 .mask = {
3896                         .size = sizeof(matcher.mask.buf),
3897                 },
3898         };
3899         int actions_n = 0;
3900         bool actions_end = false;
3901         struct mlx5_flow_dv_modify_hdr_resource res = {
3902                 .ft_type = attr->egress ? MLX5DV_FLOW_TABLE_TYPE_NIC_TX :
3903                                           MLX5DV_FLOW_TABLE_TYPE_NIC_RX
3904         };
3905         union flow_dv_attr flow_attr = { .attr = 0 };
3906         struct mlx5_flow_dv_tag_resource tag_resource;
3907         uint32_t modify_action_position = UINT32_MAX;
3908         void *match_mask = matcher.mask.buf;
3909         void *match_value = dev_flow->dv.value.buf;
3910
3911         flow->group = attr->group;
3912         if (attr->transfer)
3913                 res.ft_type = MLX5DV_FLOW_TABLE_TYPE_FDB;
3914         if (priority == MLX5_FLOW_PRIO_RSVD)
3915                 priority = priv->config.flow_prio - 1;
3916         for (; !actions_end ; actions++) {
3917                 const struct rte_flow_action_queue *queue;
3918                 const struct rte_flow_action_rss *rss;
3919                 const struct rte_flow_action *action = actions;
3920                 const struct rte_flow_action_count *count = action->conf;
3921                 const uint8_t *rss_key;
3922                 const struct rte_flow_action_jump *jump_data;
3923                 struct mlx5_flow_dv_jump_tbl_resource jump_tbl_resource;
3924                 struct mlx5_flow_tbl_resource *tbl;
3925                 uint32_t port_id = 0;
3926                 struct mlx5_flow_dv_port_id_action_resource port_id_resource;
3927
3928                 switch (actions->type) {
3929                 case RTE_FLOW_ACTION_TYPE_VOID:
3930                         break;
3931                 case RTE_FLOW_ACTION_TYPE_PORT_ID:
3932                         if (flow_dv_translate_action_port_id(dev, action,
3933                                                              &port_id, error))
3934                                 return -rte_errno;
3935                         port_id_resource.port_id = port_id;
3936                         if (flow_dv_port_id_action_resource_register
3937                             (dev, &port_id_resource, dev_flow, error))
3938                                 return -rte_errno;
3939                         dev_flow->dv.actions[actions_n++] =
3940                                 dev_flow->dv.port_id_action->action;
3941                         action_flags |= MLX5_FLOW_ACTION_PORT_ID;
3942                         break;
3943                 case RTE_FLOW_ACTION_TYPE_FLAG:
3944                         tag_resource.tag =
3945                                 mlx5_flow_mark_set(MLX5_FLOW_MARK_DEFAULT);
3946                         if (!flow->tag_resource)
3947                                 if (flow_dv_tag_resource_register
3948                                     (dev, &tag_resource, dev_flow, error))
3949                                         return errno;
3950                         dev_flow->dv.actions[actions_n++] =
3951                                 flow->tag_resource->action;
3952                         action_flags |= MLX5_FLOW_ACTION_FLAG;
3953                         break;
3954                 case RTE_FLOW_ACTION_TYPE_MARK:
3955                         tag_resource.tag = mlx5_flow_mark_set
3956                               (((const struct rte_flow_action_mark *)
3957                                (actions->conf))->id);
3958                         if (!flow->tag_resource)
3959                                 if (flow_dv_tag_resource_register
3960                                     (dev, &tag_resource, dev_flow, error))
3961                                         return errno;
3962                         dev_flow->dv.actions[actions_n++] =
3963                                 flow->tag_resource->action;
3964                         action_flags |= MLX5_FLOW_ACTION_MARK;
3965                         break;
3966                 case RTE_FLOW_ACTION_TYPE_DROP:
3967                         action_flags |= MLX5_FLOW_ACTION_DROP;
3968                         break;
3969                 case RTE_FLOW_ACTION_TYPE_QUEUE:
3970                         queue = actions->conf;
3971                         flow->rss.queue_num = 1;
3972                         (*flow->queue)[0] = queue->index;
3973                         action_flags |= MLX5_FLOW_ACTION_QUEUE;
3974                         break;
3975                 case RTE_FLOW_ACTION_TYPE_RSS:
3976                         rss = actions->conf;
3977                         if (flow->queue)
3978                                 memcpy((*flow->queue), rss->queue,
3979                                        rss->queue_num * sizeof(uint16_t));
3980                         flow->rss.queue_num = rss->queue_num;
3981                         /* NULL RSS key indicates default RSS key. */
3982                         rss_key = !rss->key ? rss_hash_default_key : rss->key;
3983                         memcpy(flow->key, rss_key, MLX5_RSS_HASH_KEY_LEN);
3984                         /* RSS type 0 indicates default RSS type ETH_RSS_IP. */
3985                         flow->rss.types = !rss->types ? ETH_RSS_IP : rss->types;
3986                         flow->rss.level = rss->level;
3987                         action_flags |= MLX5_FLOW_ACTION_RSS;
3988                         break;
3989                 case RTE_FLOW_ACTION_TYPE_COUNT:
3990                         if (!priv->config.devx) {
3991                                 rte_errno = ENOTSUP;
3992                                 goto cnt_err;
3993                         }
3994                         flow->counter = flow_dv_counter_new(dev, count->shared,
3995                                                             count->id);
3996                         if (flow->counter == NULL)
3997                                 goto cnt_err;
3998                         dev_flow->dv.actions[actions_n++] =
3999                                 flow->counter->action;
4000                         action_flags |= MLX5_FLOW_ACTION_COUNT;
4001                         break;
4002 cnt_err:
4003                         if (rte_errno == ENOTSUP)
4004                                 return rte_flow_error_set
4005                                               (error, ENOTSUP,
4006                                                RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
4007                                                NULL,
4008                                                "count action not supported");
4009                         else
4010                                 return rte_flow_error_set
4011                                                 (error, rte_errno,
4012                                                  RTE_FLOW_ERROR_TYPE_ACTION,
4013                                                  action,
4014                                                  "cannot create counter"
4015                                                   " object.");
4016                 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
4017                 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
4018                         if (flow_dv_create_action_l2_encap(dev, actions,
4019                                                            dev_flow,
4020                                                            attr->transfer,
4021                                                            error))
4022                                 return -rte_errno;
4023                         dev_flow->dv.actions[actions_n++] =
4024                                 dev_flow->dv.encap_decap->verbs_action;
4025                         action_flags |= actions->type ==
4026                                         RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP ?
4027                                         MLX5_FLOW_ACTION_VXLAN_ENCAP :
4028                                         MLX5_FLOW_ACTION_NVGRE_ENCAP;
4029                         break;
4030                 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
4031                 case RTE_FLOW_ACTION_TYPE_NVGRE_DECAP:
4032                         if (flow_dv_create_action_l2_decap(dev, dev_flow,
4033                                                            attr->transfer,
4034                                                            error))
4035                                 return -rte_errno;
4036                         dev_flow->dv.actions[actions_n++] =
4037                                 dev_flow->dv.encap_decap->verbs_action;
4038                         action_flags |= actions->type ==
4039                                         RTE_FLOW_ACTION_TYPE_VXLAN_DECAP ?
4040                                         MLX5_FLOW_ACTION_VXLAN_DECAP :
4041                                         MLX5_FLOW_ACTION_NVGRE_DECAP;
4042                         break;
4043                 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
4044                         /* Handle encap with preceding decap. */
4045                         if (action_flags & MLX5_FLOW_ACTION_RAW_DECAP) {
4046                                 if (flow_dv_create_action_raw_encap
4047                                         (dev, actions, dev_flow, attr, error))
4048                                         return -rte_errno;
4049                                 dev_flow->dv.actions[actions_n++] =
4050                                         dev_flow->dv.encap_decap->verbs_action;
4051                         } else {
4052                                 /* Handle encap without preceding decap. */
4053                                 if (flow_dv_create_action_l2_encap
4054                                     (dev, actions, dev_flow, attr->transfer,
4055                                      error))
4056                                         return -rte_errno;
4057                                 dev_flow->dv.actions[actions_n++] =
4058                                         dev_flow->dv.encap_decap->verbs_action;
4059                         }
4060                         action_flags |= MLX5_FLOW_ACTION_RAW_ENCAP;
4061                         break;
4062                 case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
4063                         /* Check if this decap is followed by encap. */
4064                         for (; action->type != RTE_FLOW_ACTION_TYPE_END &&
4065                                action->type != RTE_FLOW_ACTION_TYPE_RAW_ENCAP;
4066                                action++) {
4067                         }
4068                         /* Handle decap only if it isn't followed by encap. */
4069                         if (action->type != RTE_FLOW_ACTION_TYPE_RAW_ENCAP) {
4070                                 if (flow_dv_create_action_l2_decap
4071                                     (dev, dev_flow, attr->transfer, error))
4072                                         return -rte_errno;
4073                                 dev_flow->dv.actions[actions_n++] =
4074                                         dev_flow->dv.encap_decap->verbs_action;
4075                         }
4076                         /* If decap is followed by encap, handle it at encap. */
4077                         action_flags |= MLX5_FLOW_ACTION_RAW_DECAP;
4078                         break;
4079                 case RTE_FLOW_ACTION_TYPE_JUMP:
4080                         jump_data = action->conf;
4081                         tbl = flow_dv_tbl_resource_get(dev, jump_data->group *
4082                                                        MLX5_GROUP_FACTOR,
4083                                                        attr->egress,
4084                                                        attr->transfer, error);
4085                         if (!tbl)
4086                                 return rte_flow_error_set
4087                                                 (error, errno,
4088                                                  RTE_FLOW_ERROR_TYPE_ACTION,
4089                                                  NULL,
4090                                                  "cannot create jump action.");
4091                         jump_tbl_resource.tbl = tbl;
4092                         if (flow_dv_jump_tbl_resource_register
4093                             (dev, &jump_tbl_resource, dev_flow, error)) {
4094                                 flow_dv_tbl_resource_release(tbl);
4095                                 return rte_flow_error_set
4096                                                 (error, errno,
4097                                                  RTE_FLOW_ERROR_TYPE_ACTION,
4098                                                  NULL,
4099                                                  "cannot create jump action.");
4100                         }
4101                         dev_flow->dv.actions[actions_n++] =
4102                                 dev_flow->dv.jump->action;
4103                         action_flags |= MLX5_FLOW_ACTION_JUMP;
4104                         break;
4105                 case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
4106                 case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
4107                         if (flow_dv_convert_action_modify_mac(&res, actions,
4108                                                               error))
4109                                 return -rte_errno;
4110                         action_flags |= actions->type ==
4111                                         RTE_FLOW_ACTION_TYPE_SET_MAC_SRC ?
4112                                         MLX5_FLOW_ACTION_SET_MAC_SRC :
4113                                         MLX5_FLOW_ACTION_SET_MAC_DST;
4114                         break;
4115                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
4116                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
4117                         if (flow_dv_convert_action_modify_ipv4(&res, actions,
4118                                                                error))
4119                                 return -rte_errno;
4120                         action_flags |= actions->type ==
4121                                         RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC ?
4122                                         MLX5_FLOW_ACTION_SET_IPV4_SRC :
4123                                         MLX5_FLOW_ACTION_SET_IPV4_DST;
4124                         break;
4125                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
4126                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
4127                         if (flow_dv_convert_action_modify_ipv6(&res, actions,
4128                                                                error))
4129                                 return -rte_errno;
4130                         action_flags |= actions->type ==
4131                                         RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC ?
4132                                         MLX5_FLOW_ACTION_SET_IPV6_SRC :
4133                                         MLX5_FLOW_ACTION_SET_IPV6_DST;
4134                         break;
4135                 case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
4136                 case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
4137                         if (flow_dv_convert_action_modify_tp(&res, actions,
4138                                                              items, &flow_attr,
4139                                                              error))
4140                                 return -rte_errno;
4141                         action_flags |= actions->type ==
4142                                         RTE_FLOW_ACTION_TYPE_SET_TP_SRC ?
4143                                         MLX5_FLOW_ACTION_SET_TP_SRC :
4144                                         MLX5_FLOW_ACTION_SET_TP_DST;
4145                         break;
4146                 case RTE_FLOW_ACTION_TYPE_DEC_TTL:
4147                         if (flow_dv_convert_action_modify_dec_ttl(&res, items,
4148                                                                   &flow_attr,
4149                                                                   error))
4150                                 return -rte_errno;
4151                         action_flags |= MLX5_FLOW_ACTION_DEC_TTL;
4152                         break;
4153                 case RTE_FLOW_ACTION_TYPE_SET_TTL:
4154                         if (flow_dv_convert_action_modify_ttl(&res, actions,
4155                                                              items, &flow_attr,
4156                                                              error))
4157                                 return -rte_errno;
4158                         action_flags |= MLX5_FLOW_ACTION_SET_TTL;
4159                         break;
4160                 case RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ:
4161                 case RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ:
4162                         if (flow_dv_convert_action_modify_tcp_seq(&res, actions,
4163                                                                   error))
4164                                 return -rte_errno;
4165                         action_flags |= actions->type ==
4166                                         RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ ?
4167                                         MLX5_FLOW_ACTION_INC_TCP_SEQ :
4168                                         MLX5_FLOW_ACTION_DEC_TCP_SEQ;
4169                         break;
4170
4171                 case RTE_FLOW_ACTION_TYPE_INC_TCP_ACK:
4172                 case RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK:
4173                         if (flow_dv_convert_action_modify_tcp_ack(&res, actions,
4174                                                                   error))
4175                                 return -rte_errno;
4176                         action_flags |= actions->type ==
4177                                         RTE_FLOW_ACTION_TYPE_INC_TCP_ACK ?
4178                                         MLX5_FLOW_ACTION_INC_TCP_ACK :
4179                                         MLX5_FLOW_ACTION_DEC_TCP_ACK;
4180                         break;
4181                 case RTE_FLOW_ACTION_TYPE_END:
4182                         actions_end = true;
4183                         if (action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS) {
4184                                 /* create modify action if needed. */
4185                                 if (flow_dv_modify_hdr_resource_register
4186                                                                 (dev, &res,
4187                                                                  dev_flow,
4188                                                                  error))
4189                                         return -rte_errno;
4190                                 dev_flow->dv.actions[modify_action_position] =
4191                                         dev_flow->dv.modify_hdr->verbs_action;
4192                         }
4193                         break;
4194                 default:
4195                         break;
4196                 }
4197                 if ((action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS) &&
4198                     modify_action_position == UINT32_MAX)
4199                         modify_action_position = actions_n++;
4200         }
4201         dev_flow->dv.actions_n = actions_n;
4202         flow->actions = action_flags;
4203         for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
4204                 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
4205
4206                 switch (items->type) {
4207                 case RTE_FLOW_ITEM_TYPE_PORT_ID:
4208                         flow_dv_translate_item_port_id(dev, match_mask,
4209                                                        match_value, items);
4210                         last_item = MLX5_FLOW_ITEM_PORT_ID;
4211                         break;
4212                 case RTE_FLOW_ITEM_TYPE_ETH:
4213                         flow_dv_translate_item_eth(match_mask, match_value,
4214                                                    items, tunnel);
4215                         matcher.priority = MLX5_PRIORITY_MAP_L2;
4216                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
4217                                              MLX5_FLOW_LAYER_OUTER_L2;
4218                         break;
4219                 case RTE_FLOW_ITEM_TYPE_VLAN:
4220                         flow_dv_translate_item_vlan(match_mask, match_value,
4221                                                     items, tunnel);
4222                         matcher.priority = MLX5_PRIORITY_MAP_L2;
4223                         last_item = tunnel ? (MLX5_FLOW_LAYER_INNER_L2 |
4224                                               MLX5_FLOW_LAYER_INNER_VLAN) :
4225                                              (MLX5_FLOW_LAYER_OUTER_L2 |
4226                                               MLX5_FLOW_LAYER_OUTER_VLAN);
4227                         break;
4228                 case RTE_FLOW_ITEM_TYPE_IPV4:
4229                         flow_dv_translate_item_ipv4(match_mask, match_value,
4230                                                     items, tunnel, attr->group);
4231                         matcher.priority = MLX5_PRIORITY_MAP_L3;
4232                         dev_flow->dv.hash_fields |=
4233                                 mlx5_flow_hashfields_adjust
4234                                         (dev_flow, tunnel,
4235                                          MLX5_IPV4_LAYER_TYPES,
4236                                          MLX5_IPV4_IBV_RX_HASH);
4237                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
4238                                              MLX5_FLOW_LAYER_OUTER_L3_IPV4;
4239                         break;
4240                 case RTE_FLOW_ITEM_TYPE_IPV6:
4241                         flow_dv_translate_item_ipv6(match_mask, match_value,
4242                                                     items, tunnel, attr->group);
4243                         matcher.priority = MLX5_PRIORITY_MAP_L3;
4244                         dev_flow->dv.hash_fields |=
4245                                 mlx5_flow_hashfields_adjust
4246                                         (dev_flow, tunnel,
4247                                          MLX5_IPV6_LAYER_TYPES,
4248                                          MLX5_IPV6_IBV_RX_HASH);
4249                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
4250                                              MLX5_FLOW_LAYER_OUTER_L3_IPV6;
4251                         break;
4252                 case RTE_FLOW_ITEM_TYPE_TCP:
4253                         flow_dv_translate_item_tcp(match_mask, match_value,
4254                                                    items, tunnel);
4255                         matcher.priority = MLX5_PRIORITY_MAP_L4;
4256                         dev_flow->dv.hash_fields |=
4257                                 mlx5_flow_hashfields_adjust
4258                                         (dev_flow, tunnel, ETH_RSS_TCP,
4259                                          IBV_RX_HASH_SRC_PORT_TCP |
4260                                          IBV_RX_HASH_DST_PORT_TCP);
4261                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
4262                                              MLX5_FLOW_LAYER_OUTER_L4_TCP;
4263                         break;
4264                 case RTE_FLOW_ITEM_TYPE_UDP:
4265                         flow_dv_translate_item_udp(match_mask, match_value,
4266                                                    items, tunnel);
4267                         matcher.priority = MLX5_PRIORITY_MAP_L4;
4268                         dev_flow->dv.hash_fields |=
4269                                 mlx5_flow_hashfields_adjust
4270                                         (dev_flow, tunnel, ETH_RSS_UDP,
4271                                          IBV_RX_HASH_SRC_PORT_UDP |
4272                                          IBV_RX_HASH_DST_PORT_UDP);
4273                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
4274                                              MLX5_FLOW_LAYER_OUTER_L4_UDP;
4275                         break;
4276                 case RTE_FLOW_ITEM_TYPE_GRE:
4277                         flow_dv_translate_item_gre(match_mask, match_value,
4278                                                    items, tunnel);
4279                         last_item = MLX5_FLOW_LAYER_GRE;
4280                         break;
4281                 case RTE_FLOW_ITEM_TYPE_NVGRE:
4282                         flow_dv_translate_item_nvgre(match_mask, match_value,
4283                                                      items, tunnel);
4284                         last_item = MLX5_FLOW_LAYER_GRE;
4285                         break;
4286                 case RTE_FLOW_ITEM_TYPE_VXLAN:
4287                         flow_dv_translate_item_vxlan(match_mask, match_value,
4288                                                      items, tunnel);
4289                         last_item = MLX5_FLOW_LAYER_VXLAN;
4290                         break;
4291                 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
4292                         flow_dv_translate_item_vxlan(match_mask, match_value,
4293                                                      items, tunnel);
4294                         last_item = MLX5_FLOW_LAYER_VXLAN_GPE;
4295                         break;
4296                 case RTE_FLOW_ITEM_TYPE_MPLS:
4297                         flow_dv_translate_item_mpls(match_mask, match_value,
4298                                                     items, last_item, tunnel);
4299                         last_item = MLX5_FLOW_LAYER_MPLS;
4300                         break;
4301                 case RTE_FLOW_ITEM_TYPE_META:
4302                         flow_dv_translate_item_meta(match_mask, match_value,
4303                                                     items);
4304                         last_item = MLX5_FLOW_ITEM_METADATA;
4305                         break;
4306                 default:
4307                         break;
4308                 }
4309                 item_flags |= last_item;
4310         }
4311         /*
4312          * In case of ingress traffic when E-Switch mode is enabled,
4313          * we have two cases where we need to set the source port manually.
4314          * The first one, is in case of Nic steering rule, and the second is
4315          * E-Switch rule where no port_id item was found. In both cases
4316          * the source port is set according the current port in use.
4317          */
4318         if ((attr->ingress && !(item_flags & MLX5_FLOW_ITEM_PORT_ID)) &&
4319             (priv->representor || priv->master)) {
4320                 if (flow_dv_translate_item_port_id(dev, match_mask,
4321                                                    match_value, NULL))
4322                         return -rte_errno;
4323         }
4324         assert(!flow_dv_check_valid_spec(matcher.mask.buf,
4325                                          dev_flow->dv.value.buf));
4326         dev_flow->layers = item_flags;
4327         /* Register matcher. */
4328         matcher.crc = rte_raw_cksum((const void *)matcher.mask.buf,
4329                                     matcher.mask.size);
4330         matcher.priority = mlx5_flow_adjust_priority(dev, priority,
4331                                                      matcher.priority);
4332         matcher.egress = attr->egress;
4333         matcher.group = attr->group;
4334         matcher.transfer = attr->transfer;
4335         if (flow_dv_matcher_register(dev, &matcher, dev_flow, error))
4336                 return -rte_errno;
4337         return 0;
4338 }
4339
4340 /**
4341  * Apply the flow to the NIC.
4342  *
4343  * @param[in] dev
4344  *   Pointer to the Ethernet device structure.
4345  * @param[in, out] flow
4346  *   Pointer to flow structure.
4347  * @param[out] error
4348  *   Pointer to error structure.
4349  *
4350  * @return
4351  *   0 on success, a negative errno value otherwise and rte_errno is set.
4352  */
4353 static int
4354 flow_dv_apply(struct rte_eth_dev *dev, struct rte_flow *flow,
4355               struct rte_flow_error *error)
4356 {
4357         struct mlx5_flow_dv *dv;
4358         struct mlx5_flow *dev_flow;
4359         struct mlx5_priv *priv = dev->data->dev_private;
4360         int n;
4361         int err;
4362
4363         LIST_FOREACH(dev_flow, &flow->dev_flows, next) {
4364                 dv = &dev_flow->dv;
4365                 n = dv->actions_n;
4366                 if (flow->actions & MLX5_FLOW_ACTION_DROP) {
4367                         if (flow->transfer) {
4368                                 dv->actions[n++] = priv->sh->esw_drop_action;
4369                         } else {
4370                                 dv->hrxq = mlx5_hrxq_drop_new(dev);
4371                                 if (!dv->hrxq) {
4372                                         rte_flow_error_set
4373                                                 (error, errno,
4374                                                  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
4375                                                  NULL,
4376                                                  "cannot get drop hash queue");
4377                                         goto error;
4378                                 }
4379                                 dv->actions[n++] = dv->hrxq->action;
4380                         }
4381                 } else if (flow->actions &
4382                            (MLX5_FLOW_ACTION_QUEUE | MLX5_FLOW_ACTION_RSS)) {
4383                         struct mlx5_hrxq *hrxq;
4384
4385                         hrxq = mlx5_hrxq_get(dev, flow->key,
4386                                              MLX5_RSS_HASH_KEY_LEN,
4387                                              dv->hash_fields,
4388                                              (*flow->queue),
4389                                              flow->rss.queue_num);
4390                         if (!hrxq)
4391                                 hrxq = mlx5_hrxq_new
4392                                         (dev, flow->key, MLX5_RSS_HASH_KEY_LEN,
4393                                          dv->hash_fields, (*flow->queue),
4394                                          flow->rss.queue_num,
4395                                          !!(dev_flow->layers &
4396                                             MLX5_FLOW_LAYER_TUNNEL));
4397                         if (!hrxq) {
4398                                 rte_flow_error_set
4399                                         (error, rte_errno,
4400                                          RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
4401                                          "cannot get hash queue");
4402                                 goto error;
4403                         }
4404                         dv->hrxq = hrxq;
4405                         dv->actions[n++] = dv->hrxq->action;
4406                 }
4407                 dv->flow =
4408                         mlx5_glue->dv_create_flow(dv->matcher->matcher_object,
4409                                                   (void *)&dv->value, n,
4410                                                   dv->actions);
4411                 if (!dv->flow) {
4412                         rte_flow_error_set(error, errno,
4413                                            RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
4414                                            NULL,
4415                                            "hardware refuses to create flow");
4416                         goto error;
4417                 }
4418         }
4419         return 0;
4420 error:
4421         err = rte_errno; /* Save rte_errno before cleanup. */
4422         LIST_FOREACH(dev_flow, &flow->dev_flows, next) {
4423                 struct mlx5_flow_dv *dv = &dev_flow->dv;
4424                 if (dv->hrxq) {
4425                         if (flow->actions & MLX5_FLOW_ACTION_DROP)
4426                                 mlx5_hrxq_drop_release(dev);
4427                         else
4428                                 mlx5_hrxq_release(dev, dv->hrxq);
4429                         dv->hrxq = NULL;
4430                 }
4431         }
4432         rte_errno = err; /* Restore rte_errno. */
4433         return -rte_errno;
4434 }
4435
4436 /**
4437  * Release the flow matcher.
4438  *
4439  * @param dev
4440  *   Pointer to Ethernet device.
4441  * @param flow
4442  *   Pointer to mlx5_flow.
4443  *
4444  * @return
4445  *   1 while a reference on it exists, 0 when freed.
4446  */
4447 static int
4448 flow_dv_matcher_release(struct rte_eth_dev *dev,
4449                         struct mlx5_flow *flow)
4450 {
4451         struct mlx5_flow_dv_matcher *matcher = flow->dv.matcher;
4452         struct mlx5_priv *priv = dev->data->dev_private;
4453         struct mlx5_ibv_shared *sh = priv->sh;
4454         struct mlx5_flow_tbl_resource *tbl;
4455
4456         assert(matcher->matcher_object);
4457         DRV_LOG(DEBUG, "port %u matcher %p: refcnt %d--",
4458                 dev->data->port_id, (void *)matcher,
4459                 rte_atomic32_read(&matcher->refcnt));
4460         if (rte_atomic32_dec_and_test(&matcher->refcnt)) {
4461                 claim_zero(mlx5_glue->dv_destroy_flow_matcher
4462                            (matcher->matcher_object));
4463                 LIST_REMOVE(matcher, next);
4464                 if (matcher->egress)
4465                         tbl = &sh->tx_tbl[matcher->group];
4466                 else
4467                         tbl = &sh->rx_tbl[matcher->group];
4468                 flow_dv_tbl_resource_release(tbl);
4469                 rte_free(matcher);
4470                 DRV_LOG(DEBUG, "port %u matcher %p: removed",
4471                         dev->data->port_id, (void *)matcher);
4472                 return 0;
4473         }
4474         return 1;
4475 }
4476
4477 /**
4478  * Release an encap/decap resource.
4479  *
4480  * @param flow
4481  *   Pointer to mlx5_flow.
4482  *
4483  * @return
4484  *   1 while a reference on it exists, 0 when freed.
4485  */
4486 static int
4487 flow_dv_encap_decap_resource_release(struct mlx5_flow *flow)
4488 {
4489         struct mlx5_flow_dv_encap_decap_resource *cache_resource =
4490                                                 flow->dv.encap_decap;
4491
4492         assert(cache_resource->verbs_action);
4493         DRV_LOG(DEBUG, "encap/decap resource %p: refcnt %d--",
4494                 (void *)cache_resource,
4495                 rte_atomic32_read(&cache_resource->refcnt));
4496         if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
4497                 claim_zero(mlx5_glue->destroy_flow_action
4498                                 (cache_resource->verbs_action));
4499                 LIST_REMOVE(cache_resource, next);
4500                 rte_free(cache_resource);
4501                 DRV_LOG(DEBUG, "encap/decap resource %p: removed",
4502                         (void *)cache_resource);
4503                 return 0;
4504         }
4505         return 1;
4506 }
4507
4508 /**
4509  * Release an jump to table action resource.
4510  *
4511  * @param flow
4512  *   Pointer to mlx5_flow.
4513  *
4514  * @return
4515  *   1 while a reference on it exists, 0 when freed.
4516  */
4517 static int
4518 flow_dv_jump_tbl_resource_release(struct mlx5_flow *flow)
4519 {
4520         struct mlx5_flow_dv_jump_tbl_resource *cache_resource =
4521                                                 flow->dv.jump;
4522
4523         assert(cache_resource->action);
4524         DRV_LOG(DEBUG, "jump table resource %p: refcnt %d--",
4525                 (void *)cache_resource,
4526                 rte_atomic32_read(&cache_resource->refcnt));
4527         if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
4528                 claim_zero(mlx5_glue->destroy_flow_action
4529                                 (cache_resource->action));
4530                 LIST_REMOVE(cache_resource, next);
4531                 flow_dv_tbl_resource_release(cache_resource->tbl);
4532                 rte_free(cache_resource);
4533                 DRV_LOG(DEBUG, "jump table resource %p: removed",
4534                         (void *)cache_resource);
4535                 return 0;
4536         }
4537         return 1;
4538 }
4539
4540 /**
4541  * Release a modify-header resource.
4542  *
4543  * @param flow
4544  *   Pointer to mlx5_flow.
4545  *
4546  * @return
4547  *   1 while a reference on it exists, 0 when freed.
4548  */
4549 static int
4550 flow_dv_modify_hdr_resource_release(struct mlx5_flow *flow)
4551 {
4552         struct mlx5_flow_dv_modify_hdr_resource *cache_resource =
4553                                                 flow->dv.modify_hdr;
4554
4555         assert(cache_resource->verbs_action);
4556         DRV_LOG(DEBUG, "modify-header resource %p: refcnt %d--",
4557                 (void *)cache_resource,
4558                 rte_atomic32_read(&cache_resource->refcnt));
4559         if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
4560                 claim_zero(mlx5_glue->destroy_flow_action
4561                                 (cache_resource->verbs_action));
4562                 LIST_REMOVE(cache_resource, next);
4563                 rte_free(cache_resource);
4564                 DRV_LOG(DEBUG, "modify-header resource %p: removed",
4565                         (void *)cache_resource);
4566                 return 0;
4567         }
4568         return 1;
4569 }
4570
4571 /**
4572  * Release port ID action resource.
4573  *
4574  * @param flow
4575  *   Pointer to mlx5_flow.
4576  *
4577  * @return
4578  *   1 while a reference on it exists, 0 when freed.
4579  */
4580 static int
4581 flow_dv_port_id_action_resource_release(struct mlx5_flow *flow)
4582 {
4583         struct mlx5_flow_dv_port_id_action_resource *cache_resource =
4584                 flow->dv.port_id_action;
4585
4586         assert(cache_resource->action);
4587         DRV_LOG(DEBUG, "port ID action resource %p: refcnt %d--",
4588                 (void *)cache_resource,
4589                 rte_atomic32_read(&cache_resource->refcnt));
4590         if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
4591                 claim_zero(mlx5_glue->destroy_flow_action
4592                                 (cache_resource->action));
4593                 LIST_REMOVE(cache_resource, next);
4594                 rte_free(cache_resource);
4595                 DRV_LOG(DEBUG, "port id action resource %p: removed",
4596                         (void *)cache_resource);
4597                 return 0;
4598         }
4599         return 1;
4600 }
4601
4602 /**
4603  * Remove the flow from the NIC but keeps it in memory.
4604  *
4605  * @param[in] dev
4606  *   Pointer to Ethernet device.
4607  * @param[in, out] flow
4608  *   Pointer to flow structure.
4609  */
4610 static void
4611 flow_dv_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
4612 {
4613         struct mlx5_flow_dv *dv;
4614         struct mlx5_flow *dev_flow;
4615
4616         if (!flow)
4617                 return;
4618         LIST_FOREACH(dev_flow, &flow->dev_flows, next) {
4619                 dv = &dev_flow->dv;
4620                 if (dv->flow) {
4621                         claim_zero(mlx5_glue->dv_destroy_flow(dv->flow));
4622                         dv->flow = NULL;
4623                 }
4624                 if (dv->hrxq) {
4625                         if (flow->actions & MLX5_FLOW_ACTION_DROP)
4626                                 mlx5_hrxq_drop_release(dev);
4627                         else
4628                                 mlx5_hrxq_release(dev, dv->hrxq);
4629                         dv->hrxq = NULL;
4630                 }
4631         }
4632 }
4633
4634 /**
4635  * Remove the flow from the NIC and the memory.
4636  *
4637  * @param[in] dev
4638  *   Pointer to the Ethernet device structure.
4639  * @param[in, out] flow
4640  *   Pointer to flow structure.
4641  */
4642 static void
4643 flow_dv_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
4644 {
4645         struct mlx5_flow *dev_flow;
4646
4647         if (!flow)
4648                 return;
4649         flow_dv_remove(dev, flow);
4650         if (flow->counter) {
4651                 flow_dv_counter_release(flow->counter);
4652                 flow->counter = NULL;
4653         }
4654         if (flow->tag_resource) {
4655                 flow_dv_tag_release(dev, flow->tag_resource);
4656                 flow->tag_resource = NULL;
4657         }
4658         while (!LIST_EMPTY(&flow->dev_flows)) {
4659                 dev_flow = LIST_FIRST(&flow->dev_flows);
4660                 LIST_REMOVE(dev_flow, next);
4661                 if (dev_flow->dv.matcher)
4662                         flow_dv_matcher_release(dev, dev_flow);
4663                 if (dev_flow->dv.encap_decap)
4664                         flow_dv_encap_decap_resource_release(dev_flow);
4665                 if (dev_flow->dv.modify_hdr)
4666                         flow_dv_modify_hdr_resource_release(dev_flow);
4667                 if (dev_flow->dv.jump)
4668                         flow_dv_jump_tbl_resource_release(dev_flow);
4669                 if (dev_flow->dv.port_id_action)
4670                         flow_dv_port_id_action_resource_release(dev_flow);
4671                 rte_free(dev_flow);
4672         }
4673 }
4674
4675 /**
4676  * Query a dv flow  rule for its statistics via devx.
4677  *
4678  * @param[in] dev
4679  *   Pointer to Ethernet device.
4680  * @param[in] flow
4681  *   Pointer to the sub flow.
4682  * @param[out] data
4683  *   data retrieved by the query.
4684  * @param[out] error
4685  *   Perform verbose error reporting if not NULL.
4686  *
4687  * @return
4688  *   0 on success, a negative errno value otherwise and rte_errno is set.
4689  */
4690 static int
4691 flow_dv_query_count(struct rte_eth_dev *dev, struct rte_flow *flow,
4692                     void *data, struct rte_flow_error *error)
4693 {
4694         struct mlx5_priv *priv = dev->data->dev_private;
4695         struct rte_flow_query_count *qc = data;
4696         uint64_t pkts = 0;
4697         uint64_t bytes = 0;
4698         int err;
4699
4700         if (!priv->config.devx)
4701                 return rte_flow_error_set(error, ENOTSUP,
4702                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
4703                                           NULL,
4704                                           "counters are not supported");
4705         if (flow->counter) {
4706                 err = mlx5_devx_cmd_flow_counter_query
4707                                                 (flow->counter->dcs,
4708                                                  qc->reset, &pkts, &bytes);
4709                 if (err)
4710                         return rte_flow_error_set
4711                                 (error, err,
4712                                  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
4713                                  NULL,
4714                                  "cannot read counters");
4715                 qc->hits_set = 1;
4716                 qc->bytes_set = 1;
4717                 qc->hits = pkts - flow->counter->hits;
4718                 qc->bytes = bytes - flow->counter->bytes;
4719                 if (qc->reset) {
4720                         flow->counter->hits = pkts;
4721                         flow->counter->bytes = bytes;
4722                 }
4723                 return 0;
4724         }
4725         return rte_flow_error_set(error, EINVAL,
4726                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
4727                                   NULL,
4728                                   "counters are not available");
4729 }
4730
4731 /**
4732  * Query a flow.
4733  *
4734  * @see rte_flow_query()
4735  * @see rte_flow_ops
4736  */
4737 static int
4738 flow_dv_query(struct rte_eth_dev *dev,
4739               struct rte_flow *flow __rte_unused,
4740               const struct rte_flow_action *actions __rte_unused,
4741               void *data __rte_unused,
4742               struct rte_flow_error *error __rte_unused)
4743 {
4744         int ret = -EINVAL;
4745
4746         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
4747                 switch (actions->type) {
4748                 case RTE_FLOW_ACTION_TYPE_VOID:
4749                         break;
4750                 case RTE_FLOW_ACTION_TYPE_COUNT:
4751                         ret = flow_dv_query_count(dev, flow, data, error);
4752                         break;
4753                 default:
4754                         return rte_flow_error_set(error, ENOTSUP,
4755                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4756                                                   actions,
4757                                                   "action not supported");
4758                 }
4759         }
4760         return ret;
4761 }
4762
4763 /*
4764  * Mutex-protected thunk to flow_dv_translate().
4765  */
4766 static int
4767 flow_d_translate(struct rte_eth_dev *dev,
4768                  struct mlx5_flow *dev_flow,
4769                  const struct rte_flow_attr *attr,
4770                  const struct rte_flow_item items[],
4771                  const struct rte_flow_action actions[],
4772                  struct rte_flow_error *error)
4773 {
4774         int ret;
4775
4776         flow_d_shared_lock(dev);
4777         ret = flow_dv_translate(dev, dev_flow, attr, items, actions, error);
4778         flow_d_shared_unlock(dev);
4779         return ret;
4780 }
4781
4782 /*
4783  * Mutex-protected thunk to flow_dv_apply().
4784  */
4785 static int
4786 flow_d_apply(struct rte_eth_dev *dev,
4787              struct rte_flow *flow,
4788              struct rte_flow_error *error)
4789 {
4790         int ret;
4791
4792         flow_d_shared_lock(dev);
4793         ret = flow_dv_apply(dev, flow, error);
4794         flow_d_shared_unlock(dev);
4795         return ret;
4796 }
4797
4798 /*
4799  * Mutex-protected thunk to flow_dv_remove().
4800  */
4801 static void
4802 flow_d_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
4803 {
4804         flow_d_shared_lock(dev);
4805         flow_dv_remove(dev, flow);
4806         flow_d_shared_unlock(dev);
4807 }
4808
4809 /*
4810  * Mutex-protected thunk to flow_dv_destroy().
4811  */
4812 static void
4813 flow_d_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
4814 {
4815         flow_d_shared_lock(dev);
4816         flow_dv_destroy(dev, flow);
4817         flow_d_shared_unlock(dev);
4818 }
4819
4820 const struct mlx5_flow_driver_ops mlx5_flow_dv_drv_ops = {
4821         .validate = flow_dv_validate,
4822         .prepare = flow_dv_prepare,
4823         .translate = flow_d_translate,
4824         .apply = flow_d_apply,
4825         .remove = flow_d_remove,
4826         .destroy = flow_d_destroy,
4827         .query = flow_dv_query,
4828 };
4829
4830 #endif /* HAVE_IBV_FLOW_DV_SUPPORT */