1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2018 Mellanox Technologies, Ltd
12 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
14 #pragma GCC diagnostic ignored "-Wpedantic"
16 #include <infiniband/verbs.h>
18 #pragma GCC diagnostic error "-Wpedantic"
21 #include <rte_common.h>
22 #include <rte_ether.h>
23 #include <rte_ethdev_driver.h>
25 #include <rte_flow_driver.h>
26 #include <rte_malloc.h>
31 #include "mlx5_defs.h"
32 #include "mlx5_glue.h"
33 #include "mlx5_flow.h"
35 #include "mlx5_rxtx.h"
37 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
39 #ifndef HAVE_IBV_FLOW_DEVX_COUNTERS
40 #define MLX5DV_FLOW_ACTION_COUNTERS_DEVX 0
43 #ifndef HAVE_MLX5DV_DR_ESWITCH
44 #ifndef MLX5DV_FLOW_TABLE_TYPE_FDB
45 #define MLX5DV_FLOW_TABLE_TYPE_FDB 0
49 #ifndef HAVE_MLX5DV_DR
50 #define MLX5DV_DR_ACTION_FLAGS_ROOT_LEVEL 1
66 * Initialize flow attributes structure according to flow items' types.
69 * Pointer to item specification.
71 * Pointer to flow attributes structure.
74 flow_dv_attr_init(const struct rte_flow_item *item, union flow_dv_attr *attr)
76 for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
78 case RTE_FLOW_ITEM_TYPE_IPV4:
81 case RTE_FLOW_ITEM_TYPE_IPV6:
84 case RTE_FLOW_ITEM_TYPE_UDP:
87 case RTE_FLOW_ITEM_TYPE_TCP:
97 struct field_modify_info {
98 uint32_t size; /* Size of field in protocol header, in bytes. */
99 uint32_t offset; /* Offset of field in protocol header, in bytes. */
100 enum mlx5_modification_field id;
103 struct field_modify_info modify_eth[] = {
104 {4, 0, MLX5_MODI_OUT_DMAC_47_16},
105 {2, 4, MLX5_MODI_OUT_DMAC_15_0},
106 {4, 6, MLX5_MODI_OUT_SMAC_47_16},
107 {2, 10, MLX5_MODI_OUT_SMAC_15_0},
111 struct field_modify_info modify_ipv4[] = {
112 {1, 8, MLX5_MODI_OUT_IPV4_TTL},
113 {4, 12, MLX5_MODI_OUT_SIPV4},
114 {4, 16, MLX5_MODI_OUT_DIPV4},
118 struct field_modify_info modify_ipv6[] = {
119 {1, 7, MLX5_MODI_OUT_IPV6_HOPLIMIT},
120 {4, 8, MLX5_MODI_OUT_SIPV6_127_96},
121 {4, 12, MLX5_MODI_OUT_SIPV6_95_64},
122 {4, 16, MLX5_MODI_OUT_SIPV6_63_32},
123 {4, 20, MLX5_MODI_OUT_SIPV6_31_0},
124 {4, 24, MLX5_MODI_OUT_DIPV6_127_96},
125 {4, 28, MLX5_MODI_OUT_DIPV6_95_64},
126 {4, 32, MLX5_MODI_OUT_DIPV6_63_32},
127 {4, 36, MLX5_MODI_OUT_DIPV6_31_0},
131 struct field_modify_info modify_udp[] = {
132 {2, 0, MLX5_MODI_OUT_UDP_SPORT},
133 {2, 2, MLX5_MODI_OUT_UDP_DPORT},
137 struct field_modify_info modify_tcp[] = {
138 {2, 0, MLX5_MODI_OUT_TCP_SPORT},
139 {2, 2, MLX5_MODI_OUT_TCP_DPORT},
140 {4, 4, MLX5_MODI_OUT_TCP_SEQ_NUM},
141 {4, 8, MLX5_MODI_OUT_TCP_ACK_NUM},
146 mlx5_flow_tunnel_ip_check(const struct rte_flow_item *item, uint64_t *flags)
148 uint8_t next_protocol = 0xFF;
150 if (item->mask != NULL) {
151 switch (item->type) {
152 case RTE_FLOW_ITEM_TYPE_IPV4:
154 ((const struct rte_flow_item_ipv4 *)
155 (item->spec))->hdr.next_proto_id;
157 ((const struct rte_flow_item_ipv4 *)
158 (item->mask))->hdr.next_proto_id;
160 case RTE_FLOW_ITEM_TYPE_IPV6:
162 ((const struct rte_flow_item_ipv6 *)
163 (item->spec))->hdr.proto;
165 ((const struct rte_flow_item_ipv6 *)
166 (item->mask))->hdr.proto;
172 if (next_protocol == IPPROTO_IPIP)
173 *flags |= MLX5_FLOW_LAYER_IPIP;
174 if (next_protocol == IPPROTO_IPV6)
175 *flags |= MLX5_FLOW_LAYER_IPV6_ENCAP;
179 * Acquire the synchronizing object to protect multithreaded access
180 * to shared dv context. Lock occurs only if context is actually
181 * shared, i.e. we have multiport IB device and representors are
185 * Pointer to the rte_eth_dev structure.
188 flow_d_shared_lock(struct rte_eth_dev *dev)
190 struct mlx5_priv *priv = dev->data->dev_private;
191 struct mlx5_ibv_shared *sh = priv->sh;
193 if (sh->dv_refcnt > 1) {
196 ret = pthread_mutex_lock(&sh->dv_mutex);
203 flow_d_shared_unlock(struct rte_eth_dev *dev)
205 struct mlx5_priv *priv = dev->data->dev_private;
206 struct mlx5_ibv_shared *sh = priv->sh;
208 if (sh->dv_refcnt > 1) {
211 ret = pthread_mutex_unlock(&sh->dv_mutex);
218 * Convert modify-header action to DV specification.
221 * Pointer to item specification.
223 * Pointer to field modification information.
224 * @param[in,out] resource
225 * Pointer to the modify-header resource.
227 * Type of modification.
229 * Pointer to the error structure.
232 * 0 on success, a negative errno value otherwise and rte_errno is set.
235 flow_dv_convert_modify_action(struct rte_flow_item *item,
236 struct field_modify_info *field,
237 struct mlx5_flow_dv_modify_hdr_resource *resource,
239 struct rte_flow_error *error)
241 uint32_t i = resource->actions_num;
242 struct mlx5_modification_cmd *actions = resource->actions;
243 const uint8_t *spec = item->spec;
244 const uint8_t *mask = item->mask;
247 while (field->size) {
249 /* Generate modify command for each mask segment. */
250 memcpy(&set, &mask[field->offset], field->size);
252 if (i >= MLX5_MODIFY_NUM)
253 return rte_flow_error_set(error, EINVAL,
254 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
255 "too many items to modify");
256 actions[i].action_type = type;
257 actions[i].field = field->id;
258 actions[i].length = field->size ==
259 4 ? 0 : field->size * 8;
260 rte_memcpy(&actions[i].data[4 - field->size],
261 &spec[field->offset], field->size);
262 actions[i].data0 = rte_cpu_to_be_32(actions[i].data0);
265 if (resource->actions_num != i)
266 resource->actions_num = i;
269 if (!resource->actions_num)
270 return rte_flow_error_set(error, EINVAL,
271 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
272 "invalid modification flow item");
277 * Convert modify-header set IPv4 address action to DV specification.
279 * @param[in,out] resource
280 * Pointer to the modify-header resource.
282 * Pointer to action specification.
284 * Pointer to the error structure.
287 * 0 on success, a negative errno value otherwise and rte_errno is set.
290 flow_dv_convert_action_modify_ipv4
291 (struct mlx5_flow_dv_modify_hdr_resource *resource,
292 const struct rte_flow_action *action,
293 struct rte_flow_error *error)
295 const struct rte_flow_action_set_ipv4 *conf =
296 (const struct rte_flow_action_set_ipv4 *)(action->conf);
297 struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_IPV4 };
298 struct rte_flow_item_ipv4 ipv4;
299 struct rte_flow_item_ipv4 ipv4_mask;
301 memset(&ipv4, 0, sizeof(ipv4));
302 memset(&ipv4_mask, 0, sizeof(ipv4_mask));
303 if (action->type == RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC) {
304 ipv4.hdr.src_addr = conf->ipv4_addr;
305 ipv4_mask.hdr.src_addr = rte_flow_item_ipv4_mask.hdr.src_addr;
307 ipv4.hdr.dst_addr = conf->ipv4_addr;
308 ipv4_mask.hdr.dst_addr = rte_flow_item_ipv4_mask.hdr.dst_addr;
311 item.mask = &ipv4_mask;
312 return flow_dv_convert_modify_action(&item, modify_ipv4, resource,
313 MLX5_MODIFICATION_TYPE_SET, error);
317 * Convert modify-header set IPv6 address action to DV specification.
319 * @param[in,out] resource
320 * Pointer to the modify-header resource.
322 * Pointer to action specification.
324 * Pointer to the error structure.
327 * 0 on success, a negative errno value otherwise and rte_errno is set.
330 flow_dv_convert_action_modify_ipv6
331 (struct mlx5_flow_dv_modify_hdr_resource *resource,
332 const struct rte_flow_action *action,
333 struct rte_flow_error *error)
335 const struct rte_flow_action_set_ipv6 *conf =
336 (const struct rte_flow_action_set_ipv6 *)(action->conf);
337 struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_IPV6 };
338 struct rte_flow_item_ipv6 ipv6;
339 struct rte_flow_item_ipv6 ipv6_mask;
341 memset(&ipv6, 0, sizeof(ipv6));
342 memset(&ipv6_mask, 0, sizeof(ipv6_mask));
343 if (action->type == RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC) {
344 memcpy(&ipv6.hdr.src_addr, &conf->ipv6_addr,
345 sizeof(ipv6.hdr.src_addr));
346 memcpy(&ipv6_mask.hdr.src_addr,
347 &rte_flow_item_ipv6_mask.hdr.src_addr,
348 sizeof(ipv6.hdr.src_addr));
350 memcpy(&ipv6.hdr.dst_addr, &conf->ipv6_addr,
351 sizeof(ipv6.hdr.dst_addr));
352 memcpy(&ipv6_mask.hdr.dst_addr,
353 &rte_flow_item_ipv6_mask.hdr.dst_addr,
354 sizeof(ipv6.hdr.dst_addr));
357 item.mask = &ipv6_mask;
358 return flow_dv_convert_modify_action(&item, modify_ipv6, resource,
359 MLX5_MODIFICATION_TYPE_SET, error);
363 * Convert modify-header set MAC address action to DV specification.
365 * @param[in,out] resource
366 * Pointer to the modify-header resource.
368 * Pointer to action specification.
370 * Pointer to the error structure.
373 * 0 on success, a negative errno value otherwise and rte_errno is set.
376 flow_dv_convert_action_modify_mac
377 (struct mlx5_flow_dv_modify_hdr_resource *resource,
378 const struct rte_flow_action *action,
379 struct rte_flow_error *error)
381 const struct rte_flow_action_set_mac *conf =
382 (const struct rte_flow_action_set_mac *)(action->conf);
383 struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_ETH };
384 struct rte_flow_item_eth eth;
385 struct rte_flow_item_eth eth_mask;
387 memset(ð, 0, sizeof(eth));
388 memset(ð_mask, 0, sizeof(eth_mask));
389 if (action->type == RTE_FLOW_ACTION_TYPE_SET_MAC_SRC) {
390 memcpy(ð.src.addr_bytes, &conf->mac_addr,
391 sizeof(eth.src.addr_bytes));
392 memcpy(ð_mask.src.addr_bytes,
393 &rte_flow_item_eth_mask.src.addr_bytes,
394 sizeof(eth_mask.src.addr_bytes));
396 memcpy(ð.dst.addr_bytes, &conf->mac_addr,
397 sizeof(eth.dst.addr_bytes));
398 memcpy(ð_mask.dst.addr_bytes,
399 &rte_flow_item_eth_mask.dst.addr_bytes,
400 sizeof(eth_mask.dst.addr_bytes));
403 item.mask = ð_mask;
404 return flow_dv_convert_modify_action(&item, modify_eth, resource,
405 MLX5_MODIFICATION_TYPE_SET, error);
409 * Convert modify-header set TP action to DV specification.
411 * @param[in,out] resource
412 * Pointer to the modify-header resource.
414 * Pointer to action specification.
416 * Pointer to rte_flow_item objects list.
418 * Pointer to flow attributes structure.
420 * Pointer to the error structure.
423 * 0 on success, a negative errno value otherwise and rte_errno is set.
426 flow_dv_convert_action_modify_tp
427 (struct mlx5_flow_dv_modify_hdr_resource *resource,
428 const struct rte_flow_action *action,
429 const struct rte_flow_item *items,
430 union flow_dv_attr *attr,
431 struct rte_flow_error *error)
433 const struct rte_flow_action_set_tp *conf =
434 (const struct rte_flow_action_set_tp *)(action->conf);
435 struct rte_flow_item item;
436 struct rte_flow_item_udp udp;
437 struct rte_flow_item_udp udp_mask;
438 struct rte_flow_item_tcp tcp;
439 struct rte_flow_item_tcp tcp_mask;
440 struct field_modify_info *field;
443 flow_dv_attr_init(items, attr);
445 memset(&udp, 0, sizeof(udp));
446 memset(&udp_mask, 0, sizeof(udp_mask));
447 if (action->type == RTE_FLOW_ACTION_TYPE_SET_TP_SRC) {
448 udp.hdr.src_port = conf->port;
449 udp_mask.hdr.src_port =
450 rte_flow_item_udp_mask.hdr.src_port;
452 udp.hdr.dst_port = conf->port;
453 udp_mask.hdr.dst_port =
454 rte_flow_item_udp_mask.hdr.dst_port;
456 item.type = RTE_FLOW_ITEM_TYPE_UDP;
458 item.mask = &udp_mask;
462 memset(&tcp, 0, sizeof(tcp));
463 memset(&tcp_mask, 0, sizeof(tcp_mask));
464 if (action->type == RTE_FLOW_ACTION_TYPE_SET_TP_SRC) {
465 tcp.hdr.src_port = conf->port;
466 tcp_mask.hdr.src_port =
467 rte_flow_item_tcp_mask.hdr.src_port;
469 tcp.hdr.dst_port = conf->port;
470 tcp_mask.hdr.dst_port =
471 rte_flow_item_tcp_mask.hdr.dst_port;
473 item.type = RTE_FLOW_ITEM_TYPE_TCP;
475 item.mask = &tcp_mask;
478 return flow_dv_convert_modify_action(&item, field, resource,
479 MLX5_MODIFICATION_TYPE_SET, error);
483 * Convert modify-header set TTL action to DV specification.
485 * @param[in,out] resource
486 * Pointer to the modify-header resource.
488 * Pointer to action specification.
490 * Pointer to rte_flow_item objects list.
492 * Pointer to flow attributes structure.
494 * Pointer to the error structure.
497 * 0 on success, a negative errno value otherwise and rte_errno is set.
500 flow_dv_convert_action_modify_ttl
501 (struct mlx5_flow_dv_modify_hdr_resource *resource,
502 const struct rte_flow_action *action,
503 const struct rte_flow_item *items,
504 union flow_dv_attr *attr,
505 struct rte_flow_error *error)
507 const struct rte_flow_action_set_ttl *conf =
508 (const struct rte_flow_action_set_ttl *)(action->conf);
509 struct rte_flow_item item;
510 struct rte_flow_item_ipv4 ipv4;
511 struct rte_flow_item_ipv4 ipv4_mask;
512 struct rte_flow_item_ipv6 ipv6;
513 struct rte_flow_item_ipv6 ipv6_mask;
514 struct field_modify_info *field;
517 flow_dv_attr_init(items, attr);
519 memset(&ipv4, 0, sizeof(ipv4));
520 memset(&ipv4_mask, 0, sizeof(ipv4_mask));
521 ipv4.hdr.time_to_live = conf->ttl_value;
522 ipv4_mask.hdr.time_to_live = 0xFF;
523 item.type = RTE_FLOW_ITEM_TYPE_IPV4;
525 item.mask = &ipv4_mask;
529 memset(&ipv6, 0, sizeof(ipv6));
530 memset(&ipv6_mask, 0, sizeof(ipv6_mask));
531 ipv6.hdr.hop_limits = conf->ttl_value;
532 ipv6_mask.hdr.hop_limits = 0xFF;
533 item.type = RTE_FLOW_ITEM_TYPE_IPV6;
535 item.mask = &ipv6_mask;
538 return flow_dv_convert_modify_action(&item, field, resource,
539 MLX5_MODIFICATION_TYPE_SET, error);
543 * Convert modify-header decrement TTL action to DV specification.
545 * @param[in,out] resource
546 * Pointer to the modify-header resource.
548 * Pointer to action specification.
550 * Pointer to rte_flow_item objects list.
552 * Pointer to flow attributes structure.
554 * Pointer to the error structure.
557 * 0 on success, a negative errno value otherwise and rte_errno is set.
560 flow_dv_convert_action_modify_dec_ttl
561 (struct mlx5_flow_dv_modify_hdr_resource *resource,
562 const struct rte_flow_item *items,
563 union flow_dv_attr *attr,
564 struct rte_flow_error *error)
566 struct rte_flow_item item;
567 struct rte_flow_item_ipv4 ipv4;
568 struct rte_flow_item_ipv4 ipv4_mask;
569 struct rte_flow_item_ipv6 ipv6;
570 struct rte_flow_item_ipv6 ipv6_mask;
571 struct field_modify_info *field;
574 flow_dv_attr_init(items, attr);
576 memset(&ipv4, 0, sizeof(ipv4));
577 memset(&ipv4_mask, 0, sizeof(ipv4_mask));
578 ipv4.hdr.time_to_live = 0xFF;
579 ipv4_mask.hdr.time_to_live = 0xFF;
580 item.type = RTE_FLOW_ITEM_TYPE_IPV4;
582 item.mask = &ipv4_mask;
586 memset(&ipv6, 0, sizeof(ipv6));
587 memset(&ipv6_mask, 0, sizeof(ipv6_mask));
588 ipv6.hdr.hop_limits = 0xFF;
589 ipv6_mask.hdr.hop_limits = 0xFF;
590 item.type = RTE_FLOW_ITEM_TYPE_IPV6;
592 item.mask = &ipv6_mask;
595 return flow_dv_convert_modify_action(&item, field, resource,
596 MLX5_MODIFICATION_TYPE_ADD, error);
600 * Convert modify-header increment/decrement TCP Sequence number
601 * to DV specification.
603 * @param[in,out] resource
604 * Pointer to the modify-header resource.
606 * Pointer to action specification.
608 * Pointer to the error structure.
611 * 0 on success, a negative errno value otherwise and rte_errno is set.
614 flow_dv_convert_action_modify_tcp_seq
615 (struct mlx5_flow_dv_modify_hdr_resource *resource,
616 const struct rte_flow_action *action,
617 struct rte_flow_error *error)
619 const rte_be32_t *conf = (const rte_be32_t *)(action->conf);
620 uint64_t value = rte_be_to_cpu_32(*conf);
621 struct rte_flow_item item;
622 struct rte_flow_item_tcp tcp;
623 struct rte_flow_item_tcp tcp_mask;
625 memset(&tcp, 0, sizeof(tcp));
626 memset(&tcp_mask, 0, sizeof(tcp_mask));
627 if (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ)
629 * The HW has no decrement operation, only increment operation.
630 * To simulate decrement X from Y using increment operation
631 * we need to add UINT32_MAX X times to Y.
632 * Each adding of UINT32_MAX decrements Y by 1.
635 tcp.hdr.sent_seq = rte_cpu_to_be_32((uint32_t)value);
636 tcp_mask.hdr.sent_seq = RTE_BE32(UINT32_MAX);
637 item.type = RTE_FLOW_ITEM_TYPE_TCP;
639 item.mask = &tcp_mask;
640 return flow_dv_convert_modify_action(&item, modify_tcp, resource,
641 MLX5_MODIFICATION_TYPE_ADD, error);
645 * Convert modify-header increment/decrement TCP Acknowledgment number
646 * to DV specification.
648 * @param[in,out] resource
649 * Pointer to the modify-header resource.
651 * Pointer to action specification.
653 * Pointer to the error structure.
656 * 0 on success, a negative errno value otherwise and rte_errno is set.
659 flow_dv_convert_action_modify_tcp_ack
660 (struct mlx5_flow_dv_modify_hdr_resource *resource,
661 const struct rte_flow_action *action,
662 struct rte_flow_error *error)
664 const rte_be32_t *conf = (const rte_be32_t *)(action->conf);
665 uint64_t value = rte_be_to_cpu_32(*conf);
666 struct rte_flow_item item;
667 struct rte_flow_item_tcp tcp;
668 struct rte_flow_item_tcp tcp_mask;
670 memset(&tcp, 0, sizeof(tcp));
671 memset(&tcp_mask, 0, sizeof(tcp_mask));
672 if (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK)
674 * The HW has no decrement operation, only increment operation.
675 * To simulate decrement X from Y using increment operation
676 * we need to add UINT32_MAX X times to Y.
677 * Each adding of UINT32_MAX decrements Y by 1.
680 tcp.hdr.recv_ack = rte_cpu_to_be_32((uint32_t)value);
681 tcp_mask.hdr.recv_ack = RTE_BE32(UINT32_MAX);
682 item.type = RTE_FLOW_ITEM_TYPE_TCP;
684 item.mask = &tcp_mask;
685 return flow_dv_convert_modify_action(&item, modify_tcp, resource,
686 MLX5_MODIFICATION_TYPE_ADD, error);
690 * Validate META item.
693 * Pointer to the rte_eth_dev structure.
695 * Item specification.
697 * Attributes of flow that includes this item.
699 * Pointer to error structure.
702 * 0 on success, a negative errno value otherwise and rte_errno is set.
705 flow_dv_validate_item_meta(struct rte_eth_dev *dev,
706 const struct rte_flow_item *item,
707 const struct rte_flow_attr *attr,
708 struct rte_flow_error *error)
710 const struct rte_flow_item_meta *spec = item->spec;
711 const struct rte_flow_item_meta *mask = item->mask;
712 const struct rte_flow_item_meta nic_mask = {
713 .data = RTE_BE32(UINT32_MAX)
716 uint64_t offloads = dev->data->dev_conf.txmode.offloads;
718 if (!(offloads & DEV_TX_OFFLOAD_MATCH_METADATA))
719 return rte_flow_error_set(error, EPERM,
720 RTE_FLOW_ERROR_TYPE_ITEM,
722 "match on metadata offload "
723 "configuration is off for this port");
725 return rte_flow_error_set(error, EINVAL,
726 RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
728 "data cannot be empty");
730 return rte_flow_error_set(error, EINVAL,
731 RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
733 "data cannot be zero");
735 mask = &rte_flow_item_meta_mask;
736 ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
737 (const uint8_t *)&nic_mask,
738 sizeof(struct rte_flow_item_meta),
743 return rte_flow_error_set(error, ENOTSUP,
744 RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
746 "pattern not supported for ingress");
751 * Validate vport item.
754 * Pointer to the rte_eth_dev structure.
756 * Item specification.
758 * Attributes of flow that includes this item.
759 * @param[in] item_flags
760 * Bit-fields that holds the items detected until now.
762 * Pointer to error structure.
765 * 0 on success, a negative errno value otherwise and rte_errno is set.
768 flow_dv_validate_item_port_id(struct rte_eth_dev *dev,
769 const struct rte_flow_item *item,
770 const struct rte_flow_attr *attr,
772 struct rte_flow_error *error)
774 const struct rte_flow_item_port_id *spec = item->spec;
775 const struct rte_flow_item_port_id *mask = item->mask;
776 const struct rte_flow_item_port_id switch_mask = {
779 uint16_t esw_domain_id;
780 uint16_t item_port_esw_domain_id;
784 return rte_flow_error_set(error, EINVAL,
785 RTE_FLOW_ERROR_TYPE_ITEM,
787 "match on port id is valid only"
788 " when transfer flag is enabled");
789 if (item_flags & MLX5_FLOW_ITEM_PORT_ID)
790 return rte_flow_error_set(error, ENOTSUP,
791 RTE_FLOW_ERROR_TYPE_ITEM, item,
792 "multiple source ports are not"
796 if (mask->id != 0xffffffff)
797 return rte_flow_error_set(error, ENOTSUP,
798 RTE_FLOW_ERROR_TYPE_ITEM_MASK,
800 "no support for partial mask on"
802 ret = mlx5_flow_item_acceptable
803 (item, (const uint8_t *)mask,
804 (const uint8_t *)&rte_flow_item_port_id_mask,
805 sizeof(struct rte_flow_item_port_id),
811 ret = mlx5_port_to_eswitch_info(spec->id, &item_port_esw_domain_id,
814 return rte_flow_error_set(error, -ret,
815 RTE_FLOW_ERROR_TYPE_ITEM_SPEC, spec,
816 "failed to obtain E-Switch info for"
818 ret = mlx5_port_to_eswitch_info(dev->data->port_id,
819 &esw_domain_id, NULL);
821 return rte_flow_error_set(error, -ret,
822 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
824 "failed to obtain E-Switch info");
825 if (item_port_esw_domain_id != esw_domain_id)
826 return rte_flow_error_set(error, -ret,
827 RTE_FLOW_ERROR_TYPE_ITEM_SPEC, spec,
828 "cannot match on a port from a"
829 " different E-Switch");
834 * Validate count action.
839 * Pointer to error structure.
842 * 0 on success, a negative errno value otherwise and rte_errno is set.
845 flow_dv_validate_action_count(struct rte_eth_dev *dev,
846 struct rte_flow_error *error)
848 struct mlx5_priv *priv = dev->data->dev_private;
850 if (!priv->config.devx)
852 #ifdef HAVE_IBV_FLOW_DEVX_COUNTERS
856 return rte_flow_error_set
858 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
860 "count action not supported");
864 * Validate the L2 encap action.
866 * @param[in] action_flags
867 * Holds the actions detected until now.
869 * Pointer to the encap action.
871 * Pointer to flow attributes
873 * Pointer to error structure.
876 * 0 on success, a negative errno value otherwise and rte_errno is set.
879 flow_dv_validate_action_l2_encap(uint64_t action_flags,
880 const struct rte_flow_action *action,
881 const struct rte_flow_attr *attr,
882 struct rte_flow_error *error)
885 return rte_flow_error_set(error, EINVAL,
886 RTE_FLOW_ERROR_TYPE_ACTION, action,
887 "configuration cannot be null");
888 if (action_flags & MLX5_FLOW_ACTION_DROP)
889 return rte_flow_error_set(error, EINVAL,
890 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
891 "can't drop and encap in same flow");
892 if (action_flags & (MLX5_FLOW_ENCAP_ACTIONS | MLX5_FLOW_DECAP_ACTIONS))
893 return rte_flow_error_set(error, EINVAL,
894 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
895 "can only have a single encap or"
896 " decap action in a flow");
897 if (!attr->transfer && attr->ingress)
898 return rte_flow_error_set(error, ENOTSUP,
899 RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
901 "encap action not supported for "
907 * Validate the L2 decap action.
909 * @param[in] action_flags
910 * Holds the actions detected until now.
912 * Pointer to flow attributes
914 * Pointer to error structure.
917 * 0 on success, a negative errno value otherwise and rte_errno is set.
920 flow_dv_validate_action_l2_decap(uint64_t action_flags,
921 const struct rte_flow_attr *attr,
922 struct rte_flow_error *error)
924 if (action_flags & MLX5_FLOW_ACTION_DROP)
925 return rte_flow_error_set(error, EINVAL,
926 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
927 "can't drop and decap in same flow");
928 if (action_flags & (MLX5_FLOW_ENCAP_ACTIONS | MLX5_FLOW_DECAP_ACTIONS))
929 return rte_flow_error_set(error, EINVAL,
930 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
931 "can only have a single encap or"
932 " decap action in a flow");
933 if (action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS)
934 return rte_flow_error_set(error, EINVAL,
935 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
936 "can't have decap action after"
939 return rte_flow_error_set(error, ENOTSUP,
940 RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
942 "decap action not supported for "
948 * Validate the raw encap action.
950 * @param[in] action_flags
951 * Holds the actions detected until now.
953 * Pointer to the encap action.
955 * Pointer to flow attributes
957 * Pointer to error structure.
960 * 0 on success, a negative errno value otherwise and rte_errno is set.
963 flow_dv_validate_action_raw_encap(uint64_t action_flags,
964 const struct rte_flow_action *action,
965 const struct rte_flow_attr *attr,
966 struct rte_flow_error *error)
969 return rte_flow_error_set(error, EINVAL,
970 RTE_FLOW_ERROR_TYPE_ACTION, action,
971 "configuration cannot be null");
972 if (action_flags & MLX5_FLOW_ACTION_DROP)
973 return rte_flow_error_set(error, EINVAL,
974 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
975 "can't drop and encap in same flow");
976 if (action_flags & MLX5_FLOW_ENCAP_ACTIONS)
977 return rte_flow_error_set(error, EINVAL,
978 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
979 "can only have a single encap"
980 " action in a flow");
981 /* encap without preceding decap is not supported for ingress */
982 if (!attr->transfer && attr->ingress &&
983 !(action_flags & MLX5_FLOW_ACTION_RAW_DECAP))
984 return rte_flow_error_set(error, ENOTSUP,
985 RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
987 "encap action not supported for "
993 * Validate the raw decap action.
995 * @param[in] action_flags
996 * Holds the actions detected until now.
998 * Pointer to the encap action.
1000 * Pointer to flow attributes
1002 * Pointer to error structure.
1005 * 0 on success, a negative errno value otherwise and rte_errno is set.
1008 flow_dv_validate_action_raw_decap(uint64_t action_flags,
1009 const struct rte_flow_action *action,
1010 const struct rte_flow_attr *attr,
1011 struct rte_flow_error *error)
1013 if (action_flags & MLX5_FLOW_ACTION_DROP)
1014 return rte_flow_error_set(error, EINVAL,
1015 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1016 "can't drop and decap in same flow");
1017 if (action_flags & MLX5_FLOW_ENCAP_ACTIONS)
1018 return rte_flow_error_set(error, EINVAL,
1019 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1020 "can't have encap action before"
1022 if (action_flags & MLX5_FLOW_DECAP_ACTIONS)
1023 return rte_flow_error_set(error, EINVAL,
1024 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1025 "can only have a single decap"
1026 " action in a flow");
1027 if (action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS)
1028 return rte_flow_error_set(error, EINVAL,
1029 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1030 "can't have decap action after"
1032 /* decap action is valid on egress only if it is followed by encap */
1034 for (; action->type != RTE_FLOW_ACTION_TYPE_END &&
1035 action->type != RTE_FLOW_ACTION_TYPE_RAW_ENCAP;
1038 if (action->type != RTE_FLOW_ACTION_TYPE_RAW_ENCAP)
1039 return rte_flow_error_set
1041 RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
1042 NULL, "decap action not supported"
1049 * Find existing encap/decap resource or create and register a new one.
1051 * @param dev[in, out]
1052 * Pointer to rte_eth_dev structure.
1053 * @param[in, out] resource
1054 * Pointer to encap/decap resource.
1055 * @parm[in, out] dev_flow
1056 * Pointer to the dev_flow.
1058 * pointer to error structure.
1061 * 0 on success otherwise -errno and errno is set.
1064 flow_dv_encap_decap_resource_register
1065 (struct rte_eth_dev *dev,
1066 struct mlx5_flow_dv_encap_decap_resource *resource,
1067 struct mlx5_flow *dev_flow,
1068 struct rte_flow_error *error)
1070 struct mlx5_priv *priv = dev->data->dev_private;
1071 struct mlx5_ibv_shared *sh = priv->sh;
1072 struct mlx5_flow_dv_encap_decap_resource *cache_resource;
1073 struct rte_flow *flow = dev_flow->flow;
1074 struct mlx5dv_dr_domain *domain;
1076 resource->flags = flow->group ? 0 : 1;
1077 if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB)
1078 domain = sh->fdb_domain;
1079 else if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_NIC_RX)
1080 domain = sh->rx_domain;
1082 domain = sh->tx_domain;
1084 /* Lookup a matching resource from cache. */
1085 LIST_FOREACH(cache_resource, &sh->encaps_decaps, next) {
1086 if (resource->reformat_type == cache_resource->reformat_type &&
1087 resource->ft_type == cache_resource->ft_type &&
1088 resource->flags == cache_resource->flags &&
1089 resource->size == cache_resource->size &&
1090 !memcmp((const void *)resource->buf,
1091 (const void *)cache_resource->buf,
1093 DRV_LOG(DEBUG, "encap/decap resource %p: refcnt %d++",
1094 (void *)cache_resource,
1095 rte_atomic32_read(&cache_resource->refcnt));
1096 rte_atomic32_inc(&cache_resource->refcnt);
1097 dev_flow->dv.encap_decap = cache_resource;
1101 /* Register new encap/decap resource. */
1102 cache_resource = rte_calloc(__func__, 1, sizeof(*cache_resource), 0);
1103 if (!cache_resource)
1104 return rte_flow_error_set(error, ENOMEM,
1105 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1106 "cannot allocate resource memory");
1107 *cache_resource = *resource;
1108 cache_resource->verbs_action =
1109 mlx5_glue->dv_create_flow_action_packet_reformat
1110 (sh->ctx, cache_resource->reformat_type,
1111 cache_resource->ft_type, domain, cache_resource->flags,
1112 cache_resource->size,
1113 (cache_resource->size ? cache_resource->buf : NULL));
1114 if (!cache_resource->verbs_action) {
1115 rte_free(cache_resource);
1116 return rte_flow_error_set(error, ENOMEM,
1117 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1118 NULL, "cannot create action");
1120 rte_atomic32_init(&cache_resource->refcnt);
1121 rte_atomic32_inc(&cache_resource->refcnt);
1122 LIST_INSERT_HEAD(&sh->encaps_decaps, cache_resource, next);
1123 dev_flow->dv.encap_decap = cache_resource;
1124 DRV_LOG(DEBUG, "new encap/decap resource %p: refcnt %d++",
1125 (void *)cache_resource,
1126 rte_atomic32_read(&cache_resource->refcnt));
1131 * Find existing table jump resource or create and register a new one.
1133 * @param dev[in, out]
1134 * Pointer to rte_eth_dev structure.
1135 * @param[in, out] resource
1136 * Pointer to jump table resource.
1137 * @parm[in, out] dev_flow
1138 * Pointer to the dev_flow.
1140 * pointer to error structure.
1143 * 0 on success otherwise -errno and errno is set.
1146 flow_dv_jump_tbl_resource_register
1147 (struct rte_eth_dev *dev,
1148 struct mlx5_flow_dv_jump_tbl_resource *resource,
1149 struct mlx5_flow *dev_flow,
1150 struct rte_flow_error *error)
1152 struct mlx5_priv *priv = dev->data->dev_private;
1153 struct mlx5_ibv_shared *sh = priv->sh;
1154 struct mlx5_flow_dv_jump_tbl_resource *cache_resource;
1156 /* Lookup a matching resource from cache. */
1157 LIST_FOREACH(cache_resource, &sh->jump_tbl, next) {
1158 if (resource->tbl == cache_resource->tbl) {
1159 DRV_LOG(DEBUG, "jump table resource resource %p: refcnt %d++",
1160 (void *)cache_resource,
1161 rte_atomic32_read(&cache_resource->refcnt));
1162 rte_atomic32_inc(&cache_resource->refcnt);
1163 dev_flow->dv.jump = cache_resource;
1167 /* Register new jump table resource. */
1168 cache_resource = rte_calloc(__func__, 1, sizeof(*cache_resource), 0);
1169 if (!cache_resource)
1170 return rte_flow_error_set(error, ENOMEM,
1171 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1172 "cannot allocate resource memory");
1173 *cache_resource = *resource;
1174 cache_resource->action =
1175 mlx5_glue->dr_create_flow_action_dest_flow_tbl
1176 (resource->tbl->obj);
1177 if (!cache_resource->action) {
1178 rte_free(cache_resource);
1179 return rte_flow_error_set(error, ENOMEM,
1180 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1181 NULL, "cannot create action");
1183 rte_atomic32_init(&cache_resource->refcnt);
1184 rte_atomic32_inc(&cache_resource->refcnt);
1185 LIST_INSERT_HEAD(&sh->jump_tbl, cache_resource, next);
1186 dev_flow->dv.jump = cache_resource;
1187 DRV_LOG(DEBUG, "new jump table resource %p: refcnt %d++",
1188 (void *)cache_resource,
1189 rte_atomic32_read(&cache_resource->refcnt));
1194 * Find existing table port ID resource or create and register a new one.
1196 * @param dev[in, out]
1197 * Pointer to rte_eth_dev structure.
1198 * @param[in, out] resource
1199 * Pointer to port ID action resource.
1200 * @parm[in, out] dev_flow
1201 * Pointer to the dev_flow.
1203 * pointer to error structure.
1206 * 0 on success otherwise -errno and errno is set.
1209 flow_dv_port_id_action_resource_register
1210 (struct rte_eth_dev *dev,
1211 struct mlx5_flow_dv_port_id_action_resource *resource,
1212 struct mlx5_flow *dev_flow,
1213 struct rte_flow_error *error)
1215 struct mlx5_priv *priv = dev->data->dev_private;
1216 struct mlx5_ibv_shared *sh = priv->sh;
1217 struct mlx5_flow_dv_port_id_action_resource *cache_resource;
1219 /* Lookup a matching resource from cache. */
1220 LIST_FOREACH(cache_resource, &sh->port_id_action_list, next) {
1221 if (resource->port_id == cache_resource->port_id) {
1222 DRV_LOG(DEBUG, "port id action resource resource %p: "
1224 (void *)cache_resource,
1225 rte_atomic32_read(&cache_resource->refcnt));
1226 rte_atomic32_inc(&cache_resource->refcnt);
1227 dev_flow->dv.port_id_action = cache_resource;
1231 /* Register new port id action resource. */
1232 cache_resource = rte_calloc(__func__, 1, sizeof(*cache_resource), 0);
1233 if (!cache_resource)
1234 return rte_flow_error_set(error, ENOMEM,
1235 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1236 "cannot allocate resource memory");
1237 *cache_resource = *resource;
1238 cache_resource->action =
1239 mlx5_glue->dr_create_flow_action_dest_vport
1240 (priv->sh->fdb_domain, resource->port_id);
1241 if (!cache_resource->action) {
1242 rte_free(cache_resource);
1243 return rte_flow_error_set(error, ENOMEM,
1244 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1245 NULL, "cannot create action");
1247 rte_atomic32_init(&cache_resource->refcnt);
1248 rte_atomic32_inc(&cache_resource->refcnt);
1249 LIST_INSERT_HEAD(&sh->port_id_action_list, cache_resource, next);
1250 dev_flow->dv.port_id_action = cache_resource;
1251 DRV_LOG(DEBUG, "new port id action resource %p: refcnt %d++",
1252 (void *)cache_resource,
1253 rte_atomic32_read(&cache_resource->refcnt));
1258 * Get the size of specific rte_flow_item_type
1260 * @param[in] item_type
1261 * Tested rte_flow_item_type.
1264 * sizeof struct item_type, 0 if void or irrelevant.
1267 flow_dv_get_item_len(const enum rte_flow_item_type item_type)
1271 switch (item_type) {
1272 case RTE_FLOW_ITEM_TYPE_ETH:
1273 retval = sizeof(struct rte_flow_item_eth);
1275 case RTE_FLOW_ITEM_TYPE_VLAN:
1276 retval = sizeof(struct rte_flow_item_vlan);
1278 case RTE_FLOW_ITEM_TYPE_IPV4:
1279 retval = sizeof(struct rte_flow_item_ipv4);
1281 case RTE_FLOW_ITEM_TYPE_IPV6:
1282 retval = sizeof(struct rte_flow_item_ipv6);
1284 case RTE_FLOW_ITEM_TYPE_UDP:
1285 retval = sizeof(struct rte_flow_item_udp);
1287 case RTE_FLOW_ITEM_TYPE_TCP:
1288 retval = sizeof(struct rte_flow_item_tcp);
1290 case RTE_FLOW_ITEM_TYPE_VXLAN:
1291 retval = sizeof(struct rte_flow_item_vxlan);
1293 case RTE_FLOW_ITEM_TYPE_GRE:
1294 retval = sizeof(struct rte_flow_item_gre);
1296 case RTE_FLOW_ITEM_TYPE_NVGRE:
1297 retval = sizeof(struct rte_flow_item_nvgre);
1299 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
1300 retval = sizeof(struct rte_flow_item_vxlan_gpe);
1302 case RTE_FLOW_ITEM_TYPE_MPLS:
1303 retval = sizeof(struct rte_flow_item_mpls);
1305 case RTE_FLOW_ITEM_TYPE_VOID: /* Fall through. */
1313 #define MLX5_ENCAP_IPV4_VERSION 0x40
1314 #define MLX5_ENCAP_IPV4_IHL_MIN 0x05
1315 #define MLX5_ENCAP_IPV4_TTL_DEF 0x40
1316 #define MLX5_ENCAP_IPV6_VTC_FLOW 0x60000000
1317 #define MLX5_ENCAP_IPV6_HOP_LIMIT 0xff
1318 #define MLX5_ENCAP_VXLAN_FLAGS 0x08000000
1319 #define MLX5_ENCAP_VXLAN_GPE_FLAGS 0x04
1322 * Convert the encap action data from list of rte_flow_item to raw buffer
1325 * Pointer to rte_flow_item objects list.
1327 * Pointer to the output buffer.
1329 * Pointer to the output buffer size.
1331 * Pointer to the error structure.
1334 * 0 on success, a negative errno value otherwise and rte_errno is set.
1337 flow_dv_convert_encap_data(const struct rte_flow_item *items, uint8_t *buf,
1338 size_t *size, struct rte_flow_error *error)
1340 struct rte_ether_hdr *eth = NULL;
1341 struct rte_vlan_hdr *vlan = NULL;
1342 struct rte_ipv4_hdr *ipv4 = NULL;
1343 struct rte_ipv6_hdr *ipv6 = NULL;
1344 struct rte_udp_hdr *udp = NULL;
1345 struct rte_vxlan_hdr *vxlan = NULL;
1346 struct rte_vxlan_gpe_hdr *vxlan_gpe = NULL;
1347 struct rte_gre_hdr *gre = NULL;
1349 size_t temp_size = 0;
1352 return rte_flow_error_set(error, EINVAL,
1353 RTE_FLOW_ERROR_TYPE_ACTION,
1354 NULL, "invalid empty data");
1355 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
1356 len = flow_dv_get_item_len(items->type);
1357 if (len + temp_size > MLX5_ENCAP_MAX_LEN)
1358 return rte_flow_error_set(error, EINVAL,
1359 RTE_FLOW_ERROR_TYPE_ACTION,
1360 (void *)items->type,
1361 "items total size is too big"
1362 " for encap action");
1363 rte_memcpy((void *)&buf[temp_size], items->spec, len);
1364 switch (items->type) {
1365 case RTE_FLOW_ITEM_TYPE_ETH:
1366 eth = (struct rte_ether_hdr *)&buf[temp_size];
1368 case RTE_FLOW_ITEM_TYPE_VLAN:
1369 vlan = (struct rte_vlan_hdr *)&buf[temp_size];
1371 return rte_flow_error_set(error, EINVAL,
1372 RTE_FLOW_ERROR_TYPE_ACTION,
1373 (void *)items->type,
1374 "eth header not found");
1375 if (!eth->ether_type)
1376 eth->ether_type = RTE_BE16(RTE_ETHER_TYPE_VLAN);
1378 case RTE_FLOW_ITEM_TYPE_IPV4:
1379 ipv4 = (struct rte_ipv4_hdr *)&buf[temp_size];
1381 return rte_flow_error_set(error, EINVAL,
1382 RTE_FLOW_ERROR_TYPE_ACTION,
1383 (void *)items->type,
1384 "neither eth nor vlan"
1386 if (vlan && !vlan->eth_proto)
1387 vlan->eth_proto = RTE_BE16(RTE_ETHER_TYPE_IPV4);
1388 else if (eth && !eth->ether_type)
1389 eth->ether_type = RTE_BE16(RTE_ETHER_TYPE_IPV4);
1390 if (!ipv4->version_ihl)
1391 ipv4->version_ihl = MLX5_ENCAP_IPV4_VERSION |
1392 MLX5_ENCAP_IPV4_IHL_MIN;
1393 if (!ipv4->time_to_live)
1394 ipv4->time_to_live = MLX5_ENCAP_IPV4_TTL_DEF;
1396 case RTE_FLOW_ITEM_TYPE_IPV6:
1397 ipv6 = (struct rte_ipv6_hdr *)&buf[temp_size];
1399 return rte_flow_error_set(error, EINVAL,
1400 RTE_FLOW_ERROR_TYPE_ACTION,
1401 (void *)items->type,
1402 "neither eth nor vlan"
1404 if (vlan && !vlan->eth_proto)
1405 vlan->eth_proto = RTE_BE16(RTE_ETHER_TYPE_IPV6);
1406 else if (eth && !eth->ether_type)
1407 eth->ether_type = RTE_BE16(RTE_ETHER_TYPE_IPV6);
1408 if (!ipv6->vtc_flow)
1410 RTE_BE32(MLX5_ENCAP_IPV6_VTC_FLOW);
1411 if (!ipv6->hop_limits)
1412 ipv6->hop_limits = MLX5_ENCAP_IPV6_HOP_LIMIT;
1414 case RTE_FLOW_ITEM_TYPE_UDP:
1415 udp = (struct rte_udp_hdr *)&buf[temp_size];
1417 return rte_flow_error_set(error, EINVAL,
1418 RTE_FLOW_ERROR_TYPE_ACTION,
1419 (void *)items->type,
1420 "ip header not found");
1421 if (ipv4 && !ipv4->next_proto_id)
1422 ipv4->next_proto_id = IPPROTO_UDP;
1423 else if (ipv6 && !ipv6->proto)
1424 ipv6->proto = IPPROTO_UDP;
1426 case RTE_FLOW_ITEM_TYPE_VXLAN:
1427 vxlan = (struct rte_vxlan_hdr *)&buf[temp_size];
1429 return rte_flow_error_set(error, EINVAL,
1430 RTE_FLOW_ERROR_TYPE_ACTION,
1431 (void *)items->type,
1432 "udp header not found");
1434 udp->dst_port = RTE_BE16(MLX5_UDP_PORT_VXLAN);
1435 if (!vxlan->vx_flags)
1437 RTE_BE32(MLX5_ENCAP_VXLAN_FLAGS);
1439 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
1440 vxlan_gpe = (struct rte_vxlan_gpe_hdr *)&buf[temp_size];
1442 return rte_flow_error_set(error, EINVAL,
1443 RTE_FLOW_ERROR_TYPE_ACTION,
1444 (void *)items->type,
1445 "udp header not found");
1446 if (!vxlan_gpe->proto)
1447 return rte_flow_error_set(error, EINVAL,
1448 RTE_FLOW_ERROR_TYPE_ACTION,
1449 (void *)items->type,
1450 "next protocol not found");
1453 RTE_BE16(MLX5_UDP_PORT_VXLAN_GPE);
1454 if (!vxlan_gpe->vx_flags)
1455 vxlan_gpe->vx_flags =
1456 MLX5_ENCAP_VXLAN_GPE_FLAGS;
1458 case RTE_FLOW_ITEM_TYPE_GRE:
1459 case RTE_FLOW_ITEM_TYPE_NVGRE:
1460 gre = (struct rte_gre_hdr *)&buf[temp_size];
1462 return rte_flow_error_set(error, EINVAL,
1463 RTE_FLOW_ERROR_TYPE_ACTION,
1464 (void *)items->type,
1465 "next protocol not found");
1467 return rte_flow_error_set(error, EINVAL,
1468 RTE_FLOW_ERROR_TYPE_ACTION,
1469 (void *)items->type,
1470 "ip header not found");
1471 if (ipv4 && !ipv4->next_proto_id)
1472 ipv4->next_proto_id = IPPROTO_GRE;
1473 else if (ipv6 && !ipv6->proto)
1474 ipv6->proto = IPPROTO_GRE;
1476 case RTE_FLOW_ITEM_TYPE_VOID:
1479 return rte_flow_error_set(error, EINVAL,
1480 RTE_FLOW_ERROR_TYPE_ACTION,
1481 (void *)items->type,
1482 "unsupported item type");
1492 flow_dv_zero_encap_udp_csum(void *data, struct rte_flow_error *error)
1494 struct rte_ether_hdr *eth = NULL;
1495 struct rte_vlan_hdr *vlan = NULL;
1496 struct rte_ipv6_hdr *ipv6 = NULL;
1497 struct rte_udp_hdr *udp = NULL;
1501 eth = (struct rte_ether_hdr *)data;
1502 next_hdr = (char *)(eth + 1);
1503 proto = RTE_BE16(eth->ether_type);
1506 while (proto == RTE_ETHER_TYPE_VLAN || proto == RTE_ETHER_TYPE_QINQ) {
1507 next_hdr += sizeof(struct rte_vlan_hdr);
1508 vlan = (struct rte_vlan_hdr *)next_hdr;
1509 proto = RTE_BE16(vlan->eth_proto);
1512 /* HW calculates IPv4 csum. no need to proceed */
1513 if (proto == RTE_ETHER_TYPE_IPV4)
1516 /* non IPv4/IPv6 header. not supported */
1517 if (proto != RTE_ETHER_TYPE_IPV6) {
1518 return rte_flow_error_set(error, ENOTSUP,
1519 RTE_FLOW_ERROR_TYPE_ACTION,
1520 NULL, "Cannot offload non IPv4/IPv6");
1523 ipv6 = (struct rte_ipv6_hdr *)next_hdr;
1525 /* ignore non UDP */
1526 if (ipv6->proto != IPPROTO_UDP)
1529 udp = (struct rte_udp_hdr *)(ipv6 + 1);
1530 udp->dgram_cksum = 0;
1536 * Convert L2 encap action to DV specification.
1539 * Pointer to rte_eth_dev structure.
1541 * Pointer to action structure.
1542 * @param[in, out] dev_flow
1543 * Pointer to the mlx5_flow.
1544 * @param[in] transfer
1545 * Mark if the flow is E-Switch flow.
1547 * Pointer to the error structure.
1550 * 0 on success, a negative errno value otherwise and rte_errno is set.
1553 flow_dv_create_action_l2_encap(struct rte_eth_dev *dev,
1554 const struct rte_flow_action *action,
1555 struct mlx5_flow *dev_flow,
1557 struct rte_flow_error *error)
1559 const struct rte_flow_item *encap_data;
1560 const struct rte_flow_action_raw_encap *raw_encap_data;
1561 struct mlx5_flow_dv_encap_decap_resource res = {
1563 MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TO_L2_TUNNEL,
1564 .ft_type = transfer ? MLX5DV_FLOW_TABLE_TYPE_FDB :
1565 MLX5DV_FLOW_TABLE_TYPE_NIC_TX,
1568 if (action->type == RTE_FLOW_ACTION_TYPE_RAW_ENCAP) {
1570 (const struct rte_flow_action_raw_encap *)action->conf;
1571 res.size = raw_encap_data->size;
1572 memcpy(res.buf, raw_encap_data->data, res.size);
1573 if (flow_dv_zero_encap_udp_csum(res.buf, error))
1576 if (action->type == RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP)
1578 ((const struct rte_flow_action_vxlan_encap *)
1579 action->conf)->definition;
1582 ((const struct rte_flow_action_nvgre_encap *)
1583 action->conf)->definition;
1584 if (flow_dv_convert_encap_data(encap_data, res.buf,
1588 if (flow_dv_encap_decap_resource_register(dev, &res, dev_flow, error))
1589 return rte_flow_error_set(error, EINVAL,
1590 RTE_FLOW_ERROR_TYPE_ACTION,
1591 NULL, "can't create L2 encap action");
1596 * Convert L2 decap action to DV specification.
1599 * Pointer to rte_eth_dev structure.
1600 * @param[in, out] dev_flow
1601 * Pointer to the mlx5_flow.
1602 * @param[in] transfer
1603 * Mark if the flow is E-Switch flow.
1605 * Pointer to the error structure.
1608 * 0 on success, a negative errno value otherwise and rte_errno is set.
1611 flow_dv_create_action_l2_decap(struct rte_eth_dev *dev,
1612 struct mlx5_flow *dev_flow,
1614 struct rte_flow_error *error)
1616 struct mlx5_flow_dv_encap_decap_resource res = {
1619 MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TUNNEL_TO_L2,
1620 .ft_type = transfer ? MLX5DV_FLOW_TABLE_TYPE_FDB :
1621 MLX5DV_FLOW_TABLE_TYPE_NIC_RX,
1624 if (flow_dv_encap_decap_resource_register(dev, &res, dev_flow, error))
1625 return rte_flow_error_set(error, EINVAL,
1626 RTE_FLOW_ERROR_TYPE_ACTION,
1627 NULL, "can't create L2 decap action");
1632 * Convert raw decap/encap (L3 tunnel) action to DV specification.
1635 * Pointer to rte_eth_dev structure.
1637 * Pointer to action structure.
1638 * @param[in, out] dev_flow
1639 * Pointer to the mlx5_flow.
1641 * Pointer to the flow attributes.
1643 * Pointer to the error structure.
1646 * 0 on success, a negative errno value otherwise and rte_errno is set.
1649 flow_dv_create_action_raw_encap(struct rte_eth_dev *dev,
1650 const struct rte_flow_action *action,
1651 struct mlx5_flow *dev_flow,
1652 const struct rte_flow_attr *attr,
1653 struct rte_flow_error *error)
1655 const struct rte_flow_action_raw_encap *encap_data;
1656 struct mlx5_flow_dv_encap_decap_resource res;
1658 encap_data = (const struct rte_flow_action_raw_encap *)action->conf;
1659 res.size = encap_data->size;
1660 memcpy(res.buf, encap_data->data, res.size);
1661 res.reformat_type = attr->egress ?
1662 MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TO_L3_TUNNEL :
1663 MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L3_TUNNEL_TO_L2;
1665 res.ft_type = MLX5DV_FLOW_TABLE_TYPE_FDB;
1667 res.ft_type = attr->egress ? MLX5DV_FLOW_TABLE_TYPE_NIC_TX :
1668 MLX5DV_FLOW_TABLE_TYPE_NIC_RX;
1669 if (flow_dv_encap_decap_resource_register(dev, &res, dev_flow, error))
1670 return rte_flow_error_set(error, EINVAL,
1671 RTE_FLOW_ERROR_TYPE_ACTION,
1672 NULL, "can't create encap action");
1677 * Validate the modify-header actions.
1679 * @param[in] action_flags
1680 * Holds the actions detected until now.
1682 * Pointer to the modify action.
1684 * Pointer to error structure.
1687 * 0 on success, a negative errno value otherwise and rte_errno is set.
1690 flow_dv_validate_action_modify_hdr(const uint64_t action_flags,
1691 const struct rte_flow_action *action,
1692 struct rte_flow_error *error)
1694 if (action->type != RTE_FLOW_ACTION_TYPE_DEC_TTL && !action->conf)
1695 return rte_flow_error_set(error, EINVAL,
1696 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1697 NULL, "action configuration not set");
1698 if (action_flags & MLX5_FLOW_ENCAP_ACTIONS)
1699 return rte_flow_error_set(error, EINVAL,
1700 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1701 "can't have encap action before"
1707 * Validate the modify-header MAC address actions.
1709 * @param[in] action_flags
1710 * Holds the actions detected until now.
1712 * Pointer to the modify action.
1713 * @param[in] item_flags
1714 * Holds the items detected.
1716 * Pointer to error structure.
1719 * 0 on success, a negative errno value otherwise and rte_errno is set.
1722 flow_dv_validate_action_modify_mac(const uint64_t action_flags,
1723 const struct rte_flow_action *action,
1724 const uint64_t item_flags,
1725 struct rte_flow_error *error)
1729 ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
1731 if (!(item_flags & MLX5_FLOW_LAYER_L2))
1732 return rte_flow_error_set(error, EINVAL,
1733 RTE_FLOW_ERROR_TYPE_ACTION,
1735 "no L2 item in pattern");
1741 * Validate the modify-header IPv4 address actions.
1743 * @param[in] action_flags
1744 * Holds the actions detected until now.
1746 * Pointer to the modify action.
1747 * @param[in] item_flags
1748 * Holds the items detected.
1750 * Pointer to error structure.
1753 * 0 on success, a negative errno value otherwise and rte_errno is set.
1756 flow_dv_validate_action_modify_ipv4(const uint64_t action_flags,
1757 const struct rte_flow_action *action,
1758 const uint64_t item_flags,
1759 struct rte_flow_error *error)
1763 ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
1765 if (!(item_flags & MLX5_FLOW_LAYER_L3_IPV4))
1766 return rte_flow_error_set(error, EINVAL,
1767 RTE_FLOW_ERROR_TYPE_ACTION,
1769 "no ipv4 item in pattern");
1775 * Validate the modify-header IPv6 address actions.
1777 * @param[in] action_flags
1778 * Holds the actions detected until now.
1780 * Pointer to the modify action.
1781 * @param[in] item_flags
1782 * Holds the items detected.
1784 * Pointer to error structure.
1787 * 0 on success, a negative errno value otherwise and rte_errno is set.
1790 flow_dv_validate_action_modify_ipv6(const uint64_t action_flags,
1791 const struct rte_flow_action *action,
1792 const uint64_t item_flags,
1793 struct rte_flow_error *error)
1797 ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
1799 if (!(item_flags & MLX5_FLOW_LAYER_L3_IPV6))
1800 return rte_flow_error_set(error, EINVAL,
1801 RTE_FLOW_ERROR_TYPE_ACTION,
1803 "no ipv6 item in pattern");
1809 * Validate the modify-header TP actions.
1811 * @param[in] action_flags
1812 * Holds the actions detected until now.
1814 * Pointer to the modify action.
1815 * @param[in] item_flags
1816 * Holds the items detected.
1818 * Pointer to error structure.
1821 * 0 on success, a negative errno value otherwise and rte_errno is set.
1824 flow_dv_validate_action_modify_tp(const uint64_t action_flags,
1825 const struct rte_flow_action *action,
1826 const uint64_t item_flags,
1827 struct rte_flow_error *error)
1831 ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
1833 if (!(item_flags & MLX5_FLOW_LAYER_L4))
1834 return rte_flow_error_set(error, EINVAL,
1835 RTE_FLOW_ERROR_TYPE_ACTION,
1836 NULL, "no transport layer "
1843 * Validate the modify-header actions of increment/decrement
1844 * TCP Sequence-number.
1846 * @param[in] action_flags
1847 * Holds the actions detected until now.
1849 * Pointer to the modify action.
1850 * @param[in] item_flags
1851 * Holds the items detected.
1853 * Pointer to error structure.
1856 * 0 on success, a negative errno value otherwise and rte_errno is set.
1859 flow_dv_validate_action_modify_tcp_seq(const uint64_t action_flags,
1860 const struct rte_flow_action *action,
1861 const uint64_t item_flags,
1862 struct rte_flow_error *error)
1866 ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
1868 if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_TCP))
1869 return rte_flow_error_set(error, EINVAL,
1870 RTE_FLOW_ERROR_TYPE_ACTION,
1871 NULL, "no TCP item in"
1873 if ((action->type == RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ &&
1874 (action_flags & MLX5_FLOW_ACTION_DEC_TCP_SEQ)) ||
1875 (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ &&
1876 (action_flags & MLX5_FLOW_ACTION_INC_TCP_SEQ)))
1877 return rte_flow_error_set(error, EINVAL,
1878 RTE_FLOW_ERROR_TYPE_ACTION,
1880 "cannot decrease and increase"
1881 " TCP sequence number"
1882 " at the same time");
1888 * Validate the modify-header actions of increment/decrement
1889 * TCP Acknowledgment number.
1891 * @param[in] action_flags
1892 * Holds the actions detected until now.
1894 * Pointer to the modify action.
1895 * @param[in] item_flags
1896 * Holds the items detected.
1898 * Pointer to error structure.
1901 * 0 on success, a negative errno value otherwise and rte_errno is set.
1904 flow_dv_validate_action_modify_tcp_ack(const uint64_t action_flags,
1905 const struct rte_flow_action *action,
1906 const uint64_t item_flags,
1907 struct rte_flow_error *error)
1911 ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
1913 if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_TCP))
1914 return rte_flow_error_set(error, EINVAL,
1915 RTE_FLOW_ERROR_TYPE_ACTION,
1916 NULL, "no TCP item in"
1918 if ((action->type == RTE_FLOW_ACTION_TYPE_INC_TCP_ACK &&
1919 (action_flags & MLX5_FLOW_ACTION_DEC_TCP_ACK)) ||
1920 (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK &&
1921 (action_flags & MLX5_FLOW_ACTION_INC_TCP_ACK)))
1922 return rte_flow_error_set(error, EINVAL,
1923 RTE_FLOW_ERROR_TYPE_ACTION,
1925 "cannot decrease and increase"
1926 " TCP acknowledgment number"
1927 " at the same time");
1933 * Validate the modify-header TTL actions.
1935 * @param[in] action_flags
1936 * Holds the actions detected until now.
1938 * Pointer to the modify action.
1939 * @param[in] item_flags
1940 * Holds the items detected.
1942 * Pointer to error structure.
1945 * 0 on success, a negative errno value otherwise and rte_errno is set.
1948 flow_dv_validate_action_modify_ttl(const uint64_t action_flags,
1949 const struct rte_flow_action *action,
1950 const uint64_t item_flags,
1951 struct rte_flow_error *error)
1955 ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
1957 if (!(item_flags & MLX5_FLOW_LAYER_L3))
1958 return rte_flow_error_set(error, EINVAL,
1959 RTE_FLOW_ERROR_TYPE_ACTION,
1961 "no IP protocol in pattern");
1967 * Validate jump action.
1970 * Pointer to the modify action.
1972 * The group of the current flow.
1974 * Pointer to error structure.
1977 * 0 on success, a negative errno value otherwise and rte_errno is set.
1980 flow_dv_validate_action_jump(const struct rte_flow_action *action,
1982 struct rte_flow_error *error)
1984 if (action->type != RTE_FLOW_ACTION_TYPE_JUMP && !action->conf)
1985 return rte_flow_error_set(error, EINVAL,
1986 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1987 NULL, "action configuration not set");
1988 if (group >= ((const struct rte_flow_action_jump *)action->conf)->group)
1989 return rte_flow_error_set(error, EINVAL,
1990 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1991 "target group must be higher then"
1992 " the current flow group");
1997 * Validate the port_id action.
2000 * Pointer to rte_eth_dev structure.
2001 * @param[in] action_flags
2002 * Bit-fields that holds the actions detected until now.
2004 * Port_id RTE action structure.
2006 * Attributes of flow that includes this action.
2008 * Pointer to error structure.
2011 * 0 on success, a negative errno value otherwise and rte_errno is set.
2014 flow_dv_validate_action_port_id(struct rte_eth_dev *dev,
2015 uint64_t action_flags,
2016 const struct rte_flow_action *action,
2017 const struct rte_flow_attr *attr,
2018 struct rte_flow_error *error)
2020 const struct rte_flow_action_port_id *port_id;
2022 uint16_t esw_domain_id;
2023 uint16_t act_port_domain_id;
2026 if (!attr->transfer)
2027 return rte_flow_error_set(error, ENOTSUP,
2028 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2030 "port id action is valid in transfer"
2032 if (!action || !action->conf)
2033 return rte_flow_error_set(error, ENOTSUP,
2034 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
2036 "port id action parameters must be"
2038 if (action_flags & (MLX5_FLOW_FATE_ACTIONS |
2039 MLX5_FLOW_FATE_ESWITCH_ACTIONS))
2040 return rte_flow_error_set(error, EINVAL,
2041 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2042 "can have only one fate actions in"
2044 ret = mlx5_port_to_eswitch_info(dev->data->port_id,
2045 &esw_domain_id, NULL);
2047 return rte_flow_error_set(error, -ret,
2048 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2050 "failed to obtain E-Switch info");
2051 port_id = action->conf;
2052 port = port_id->original ? dev->data->port_id : port_id->id;
2053 ret = mlx5_port_to_eswitch_info(port, &act_port_domain_id, NULL);
2055 return rte_flow_error_set
2057 RTE_FLOW_ERROR_TYPE_ACTION_CONF, port_id,
2058 "failed to obtain E-Switch port id for port");
2059 if (act_port_domain_id != esw_domain_id)
2060 return rte_flow_error_set
2062 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2063 "port does not belong to"
2064 " E-Switch being configured");
2069 * Find existing modify-header resource or create and register a new one.
2071 * @param dev[in, out]
2072 * Pointer to rte_eth_dev structure.
2073 * @param[in, out] resource
2074 * Pointer to modify-header resource.
2075 * @parm[in, out] dev_flow
2076 * Pointer to the dev_flow.
2078 * pointer to error structure.
2081 * 0 on success otherwise -errno and errno is set.
2084 flow_dv_modify_hdr_resource_register
2085 (struct rte_eth_dev *dev,
2086 struct mlx5_flow_dv_modify_hdr_resource *resource,
2087 struct mlx5_flow *dev_flow,
2088 struct rte_flow_error *error)
2090 struct mlx5_priv *priv = dev->data->dev_private;
2091 struct mlx5_ibv_shared *sh = priv->sh;
2092 struct mlx5_flow_dv_modify_hdr_resource *cache_resource;
2093 struct mlx5dv_dr_domain *ns;
2095 if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB)
2096 ns = sh->fdb_domain;
2097 else if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_NIC_TX)
2102 dev_flow->flow->group ? 0 : MLX5DV_DR_ACTION_FLAGS_ROOT_LEVEL;
2103 /* Lookup a matching resource from cache. */
2104 LIST_FOREACH(cache_resource, &sh->modify_cmds, next) {
2105 if (resource->ft_type == cache_resource->ft_type &&
2106 resource->actions_num == cache_resource->actions_num &&
2107 resource->flags == cache_resource->flags &&
2108 !memcmp((const void *)resource->actions,
2109 (const void *)cache_resource->actions,
2110 (resource->actions_num *
2111 sizeof(resource->actions[0])))) {
2112 DRV_LOG(DEBUG, "modify-header resource %p: refcnt %d++",
2113 (void *)cache_resource,
2114 rte_atomic32_read(&cache_resource->refcnt));
2115 rte_atomic32_inc(&cache_resource->refcnt);
2116 dev_flow->dv.modify_hdr = cache_resource;
2120 /* Register new modify-header resource. */
2121 cache_resource = rte_calloc(__func__, 1, sizeof(*cache_resource), 0);
2122 if (!cache_resource)
2123 return rte_flow_error_set(error, ENOMEM,
2124 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2125 "cannot allocate resource memory");
2126 *cache_resource = *resource;
2127 cache_resource->verbs_action =
2128 mlx5_glue->dv_create_flow_action_modify_header
2129 (sh->ctx, cache_resource->ft_type,
2130 ns, cache_resource->flags,
2131 cache_resource->actions_num *
2132 sizeof(cache_resource->actions[0]),
2133 (uint64_t *)cache_resource->actions);
2134 if (!cache_resource->verbs_action) {
2135 rte_free(cache_resource);
2136 return rte_flow_error_set(error, ENOMEM,
2137 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2138 NULL, "cannot create action");
2140 rte_atomic32_init(&cache_resource->refcnt);
2141 rte_atomic32_inc(&cache_resource->refcnt);
2142 LIST_INSERT_HEAD(&sh->modify_cmds, cache_resource, next);
2143 dev_flow->dv.modify_hdr = cache_resource;
2144 DRV_LOG(DEBUG, "new modify-header resource %p: refcnt %d++",
2145 (void *)cache_resource,
2146 rte_atomic32_read(&cache_resource->refcnt));
2150 #define MLX5_CNT_CONTAINER_RESIZE 64
2153 * Get or create a flow counter.
2156 * Pointer to the Ethernet device structure.
2158 * Indicate if this counter is shared with other flows.
2160 * Counter identifier.
2163 * pointer to flow counter on success, NULL otherwise and rte_errno is set.
2165 static struct mlx5_flow_counter *
2166 flow_dv_counter_alloc_fallback(struct rte_eth_dev *dev, uint32_t shared,
2169 struct mlx5_priv *priv = dev->data->dev_private;
2170 struct mlx5_flow_counter *cnt = NULL;
2171 struct mlx5_devx_obj *dcs = NULL;
2173 if (!priv->config.devx) {
2174 rte_errno = ENOTSUP;
2178 TAILQ_FOREACH(cnt, &priv->sh->cmng.flow_counters, next) {
2179 if (cnt->shared && cnt->id == id) {
2185 dcs = mlx5_devx_cmd_flow_counter_alloc(priv->sh->ctx, 0);
2188 cnt = rte_calloc(__func__, 1, sizeof(*cnt), 0);
2190 claim_zero(mlx5_devx_cmd_destroy(cnt->dcs));
2194 struct mlx5_flow_counter tmpl = {
2200 tmpl.action = mlx5_glue->dv_create_flow_action_counter(dcs->obj, 0);
2202 claim_zero(mlx5_devx_cmd_destroy(cnt->dcs));
2208 TAILQ_INSERT_HEAD(&priv->sh->cmng.flow_counters, cnt, next);
2213 * Release a flow counter.
2216 * Pointer to the Ethernet device structure.
2217 * @param[in] counter
2218 * Pointer to the counter handler.
2221 flow_dv_counter_release_fallback(struct rte_eth_dev *dev,
2222 struct mlx5_flow_counter *counter)
2224 struct mlx5_priv *priv = dev->data->dev_private;
2228 if (--counter->ref_cnt == 0) {
2229 TAILQ_REMOVE(&priv->sh->cmng.flow_counters, counter, next);
2230 claim_zero(mlx5_devx_cmd_destroy(counter->dcs));
2236 * Query a devx flow counter.
2239 * Pointer to the Ethernet device structure.
2241 * Pointer to the flow counter.
2243 * The statistics value of packets.
2245 * The statistics value of bytes.
2248 * 0 on success, otherwise a negative errno value and rte_errno is set.
2251 _flow_dv_query_count_fallback(struct rte_eth_dev *dev __rte_unused,
2252 struct mlx5_flow_counter *cnt, uint64_t *pkts,
2255 return mlx5_devx_cmd_flow_counter_query(cnt->dcs, 0, 0, pkts, bytes,
2260 * Get a pool by a counter.
2263 * Pointer to the counter.
2268 static struct mlx5_flow_counter_pool *
2269 flow_dv_counter_pool_get(struct mlx5_flow_counter *cnt)
2272 cnt -= cnt->dcs->id % MLX5_COUNTERS_PER_POOL;
2273 return (struct mlx5_flow_counter_pool *)cnt - 1;
2279 * Get a pool by devx counter ID.
2282 * Pointer to the counter container.
2284 * The counter devx ID.
2287 * The counter pool pointer if exists, NULL otherwise,
2289 static struct mlx5_flow_counter_pool *
2290 flow_dv_find_pool_by_id(struct mlx5_pools_container *cont, int id)
2292 struct mlx5_flow_counter_pool *pool;
2294 TAILQ_FOREACH(pool, &cont->pool_list, next) {
2295 int base = (pool->min_dcs->id / MLX5_COUNTERS_PER_POOL) *
2296 MLX5_COUNTERS_PER_POOL;
2298 if (id >= base && id < base + MLX5_COUNTERS_PER_POOL)
2305 * Allocate a new memory for the counter values wrapped by all the needed
2309 * Pointer to the Ethernet device structure.
2311 * The raw memory areas - each one for MLX5_COUNTERS_PER_POOL counters.
2314 * The new memory management pointer on success, otherwise NULL and rte_errno
2317 static struct mlx5_counter_stats_mem_mng *
2318 flow_dv_create_counter_stat_mem_mng(struct rte_eth_dev *dev, int raws_n)
2320 struct mlx5_ibv_shared *sh = ((struct mlx5_priv *)
2321 (dev->data->dev_private))->sh;
2322 struct mlx5dv_pd dv_pd;
2323 struct mlx5dv_obj dv_obj;
2324 struct mlx5_devx_mkey_attr mkey_attr;
2325 struct mlx5_counter_stats_mem_mng *mem_mng;
2326 volatile struct flow_counter_stats *raw_data;
2327 int size = (sizeof(struct flow_counter_stats) *
2328 MLX5_COUNTERS_PER_POOL +
2329 sizeof(struct mlx5_counter_stats_raw)) * raws_n +
2330 sizeof(struct mlx5_counter_stats_mem_mng);
2331 uint8_t *mem = rte_calloc(__func__, 1, size, sysconf(_SC_PAGESIZE));
2338 mem_mng = (struct mlx5_counter_stats_mem_mng *)(mem + size) - 1;
2339 size = sizeof(*raw_data) * MLX5_COUNTERS_PER_POOL * raws_n;
2340 mem_mng->umem = mlx5_glue->devx_umem_reg(sh->ctx, mem, size,
2341 IBV_ACCESS_LOCAL_WRITE);
2342 if (!mem_mng->umem) {
2347 dv_obj.pd.in = sh->pd;
2348 dv_obj.pd.out = &dv_pd;
2349 mlx5_glue->dv_init_obj(&dv_obj, MLX5DV_OBJ_PD);
2350 mkey_attr.addr = (uintptr_t)mem;
2351 mkey_attr.size = size;
2352 mkey_attr.umem_id = mem_mng->umem->umem_id;
2353 mkey_attr.pd = dv_pd.pdn;
2354 mem_mng->dm = mlx5_devx_cmd_mkey_create(sh->ctx, &mkey_attr);
2356 mlx5_glue->devx_umem_dereg(mem_mng->umem);
2361 mem_mng->raws = (struct mlx5_counter_stats_raw *)(mem + size);
2362 raw_data = (volatile struct flow_counter_stats *)mem;
2363 for (i = 0; i < raws_n; ++i) {
2364 mem_mng->raws[i].mem_mng = mem_mng;
2365 mem_mng->raws[i].data = raw_data + i * MLX5_COUNTERS_PER_POOL;
2367 LIST_INSERT_HEAD(&sh->cmng.mem_mngs, mem_mng, next);
2372 * Resize a counter container.
2375 * Pointer to the Ethernet device structure.
2377 * Whether the pool is for counter that was allocated by batch command.
2380 * The new container pointer on success, otherwise NULL and rte_errno is set.
2382 static struct mlx5_pools_container *
2383 flow_dv_container_resize(struct rte_eth_dev *dev, uint32_t batch)
2385 struct mlx5_priv *priv = dev->data->dev_private;
2386 struct mlx5_pools_container *cont =
2387 MLX5_CNT_CONTAINER(priv->sh, batch, 0);
2388 struct mlx5_pools_container *new_cont =
2389 MLX5_CNT_CONTAINER_UNUSED(priv->sh, batch, 0);
2390 struct mlx5_counter_stats_mem_mng *mem_mng;
2391 uint32_t resize = cont->n + MLX5_CNT_CONTAINER_RESIZE;
2392 uint32_t mem_size = sizeof(struct mlx5_flow_counter_pool *) * resize;
2395 if (cont != MLX5_CNT_CONTAINER(priv->sh, batch, 1)) {
2396 /* The last resize still hasn't detected by the host thread. */
2400 new_cont->pools = rte_calloc(__func__, 1, mem_size, 0);
2401 if (!new_cont->pools) {
2406 memcpy(new_cont->pools, cont->pools, cont->n *
2407 sizeof(struct mlx5_flow_counter_pool *));
2408 mem_mng = flow_dv_create_counter_stat_mem_mng(dev,
2409 MLX5_CNT_CONTAINER_RESIZE + MLX5_MAX_PENDING_QUERIES);
2411 rte_free(new_cont->pools);
2414 for (i = 0; i < MLX5_MAX_PENDING_QUERIES; ++i)
2415 LIST_INSERT_HEAD(&priv->sh->cmng.free_stat_raws,
2416 mem_mng->raws + MLX5_CNT_CONTAINER_RESIZE +
2418 new_cont->n = resize;
2419 rte_atomic16_set(&new_cont->n_valid, rte_atomic16_read(&cont->n_valid));
2420 TAILQ_INIT(&new_cont->pool_list);
2421 TAILQ_CONCAT(&new_cont->pool_list, &cont->pool_list, next);
2422 new_cont->init_mem_mng = mem_mng;
2424 /* Flip the master container. */
2425 priv->sh->cmng.mhi[batch] ^= (uint8_t)1;
2430 * Query a devx flow counter.
2433 * Pointer to the Ethernet device structure.
2435 * Pointer to the flow counter.
2437 * The statistics value of packets.
2439 * The statistics value of bytes.
2442 * 0 on success, otherwise a negative errno value and rte_errno is set.
2445 _flow_dv_query_count(struct rte_eth_dev *dev,
2446 struct mlx5_flow_counter *cnt, uint64_t *pkts,
2449 struct mlx5_priv *priv = dev->data->dev_private;
2450 struct mlx5_flow_counter_pool *pool =
2451 flow_dv_counter_pool_get(cnt);
2452 int offset = cnt - &pool->counters_raw[0];
2454 if (priv->counter_fallback)
2455 return _flow_dv_query_count_fallback(dev, cnt, pkts, bytes);
2457 rte_spinlock_lock(&pool->sl);
2459 * The single counters allocation may allocate smaller ID than the
2460 * current allocated in parallel to the host reading.
2461 * In this case the new counter values must be reported as 0.
2463 if (unlikely(!cnt->batch && cnt->dcs->id < pool->raw->min_dcs_id)) {
2467 *pkts = rte_be_to_cpu_64(pool->raw->data[offset].hits);
2468 *bytes = rte_be_to_cpu_64(pool->raw->data[offset].bytes);
2470 rte_spinlock_unlock(&pool->sl);
2475 * Create and initialize a new counter pool.
2478 * Pointer to the Ethernet device structure.
2480 * The devX counter handle.
2482 * Whether the pool is for counter that was allocated by batch command.
2485 * A new pool pointer on success, NULL otherwise and rte_errno is set.
2487 static struct mlx5_flow_counter_pool *
2488 flow_dv_pool_create(struct rte_eth_dev *dev, struct mlx5_devx_obj *dcs,
2491 struct mlx5_priv *priv = dev->data->dev_private;
2492 struct mlx5_flow_counter_pool *pool;
2493 struct mlx5_pools_container *cont = MLX5_CNT_CONTAINER(priv->sh, batch,
2495 int16_t n_valid = rte_atomic16_read(&cont->n_valid);
2498 if (cont->n == n_valid) {
2499 cont = flow_dv_container_resize(dev, batch);
2503 size = sizeof(*pool) + MLX5_COUNTERS_PER_POOL *
2504 sizeof(struct mlx5_flow_counter);
2505 pool = rte_calloc(__func__, 1, size, 0);
2510 pool->min_dcs = dcs;
2511 pool->raw = cont->init_mem_mng->raws + n_valid %
2512 MLX5_CNT_CONTAINER_RESIZE;
2513 pool->raw_hw = NULL;
2514 rte_spinlock_init(&pool->sl);
2516 * The generation of the new allocated counters in this pool is 0, 2 in
2517 * the pool generation makes all the counters valid for allocation.
2519 rte_atomic64_set(&pool->query_gen, 0x2);
2520 TAILQ_INIT(&pool->counters);
2521 TAILQ_INSERT_TAIL(&cont->pool_list, pool, next);
2522 cont->pools[n_valid] = pool;
2523 /* Pool initialization must be updated before host thread access. */
2525 rte_atomic16_add(&cont->n_valid, 1);
2530 * Prepare a new counter and/or a new counter pool.
2533 * Pointer to the Ethernet device structure.
2534 * @param[out] cnt_free
2535 * Where to put the pointer of a new counter.
2537 * Whether the pool is for counter that was allocated by batch command.
2540 * The free counter pool pointer and @p cnt_free is set on success,
2541 * NULL otherwise and rte_errno is set.
2543 static struct mlx5_flow_counter_pool *
2544 flow_dv_counter_pool_prepare(struct rte_eth_dev *dev,
2545 struct mlx5_flow_counter **cnt_free,
2548 struct mlx5_priv *priv = dev->data->dev_private;
2549 struct mlx5_flow_counter_pool *pool;
2550 struct mlx5_devx_obj *dcs = NULL;
2551 struct mlx5_flow_counter *cnt;
2555 /* bulk_bitmap must be 0 for single counter allocation. */
2556 dcs = mlx5_devx_cmd_flow_counter_alloc(priv->sh->ctx, 0);
2559 pool = flow_dv_find_pool_by_id
2560 (MLX5_CNT_CONTAINER(priv->sh, batch, 0), dcs->id);
2562 pool = flow_dv_pool_create(dev, dcs, batch);
2564 mlx5_devx_cmd_destroy(dcs);
2567 } else if (dcs->id < pool->min_dcs->id) {
2568 rte_atomic64_set(&pool->a64_dcs,
2569 (int64_t)(uintptr_t)dcs);
2571 cnt = &pool->counters_raw[dcs->id % MLX5_COUNTERS_PER_POOL];
2572 TAILQ_INSERT_HEAD(&pool->counters, cnt, next);
2577 /* bulk_bitmap is in 128 counters units. */
2578 if (priv->config.hca_attr.flow_counter_bulk_alloc_bitmap & 0x4)
2579 dcs = mlx5_devx_cmd_flow_counter_alloc(priv->sh->ctx, 0x4);
2581 rte_errno = ENODATA;
2584 pool = flow_dv_pool_create(dev, dcs, batch);
2586 mlx5_devx_cmd_destroy(dcs);
2589 for (i = 0; i < MLX5_COUNTERS_PER_POOL; ++i) {
2590 cnt = &pool->counters_raw[i];
2592 TAILQ_INSERT_HEAD(&pool->counters, cnt, next);
2594 *cnt_free = &pool->counters_raw[0];
2599 * Search for existed shared counter.
2602 * Pointer to the relevant counter pool container.
2604 * The shared counter ID to search.
2607 * NULL if not existed, otherwise pointer to the shared counter.
2609 static struct mlx5_flow_counter *
2610 flow_dv_counter_shared_search(struct mlx5_pools_container *cont,
2613 static struct mlx5_flow_counter *cnt;
2614 struct mlx5_flow_counter_pool *pool;
2617 TAILQ_FOREACH(pool, &cont->pool_list, next) {
2618 for (i = 0; i < MLX5_COUNTERS_PER_POOL; ++i) {
2619 cnt = &pool->counters_raw[i];
2620 if (cnt->ref_cnt && cnt->shared && cnt->id == id)
2628 * Allocate a flow counter.
2631 * Pointer to the Ethernet device structure.
2633 * Indicate if this counter is shared with other flows.
2635 * Counter identifier.
2637 * Counter flow group.
2640 * pointer to flow counter on success, NULL otherwise and rte_errno is set.
2642 static struct mlx5_flow_counter *
2643 flow_dv_counter_alloc(struct rte_eth_dev *dev, uint32_t shared, uint32_t id,
2646 struct mlx5_priv *priv = dev->data->dev_private;
2647 struct mlx5_flow_counter_pool *pool = NULL;
2648 struct mlx5_flow_counter *cnt_free = NULL;
2650 * Currently group 0 flow counter cannot be assigned to a flow if it is
2651 * not the first one in the batch counter allocation, so it is better
2652 * to allocate counters one by one for these flows in a separate
2654 * A counter can be shared between different groups so need to take
2655 * shared counters from the single container.
2657 uint32_t batch = (group && !shared) ? 1 : 0;
2658 struct mlx5_pools_container *cont = MLX5_CNT_CONTAINER(priv->sh, batch,
2661 if (priv->counter_fallback)
2662 return flow_dv_counter_alloc_fallback(dev, shared, id);
2663 if (!priv->config.devx) {
2664 rte_errno = ENOTSUP;
2668 cnt_free = flow_dv_counter_shared_search(cont, id);
2670 if (cnt_free->ref_cnt + 1 == 0) {
2674 cnt_free->ref_cnt++;
2678 /* Pools which has a free counters are in the start. */
2679 TAILQ_FOREACH(pool, &cont->pool_list, next) {
2681 * The free counter reset values must be updated between the
2682 * counter release to the counter allocation, so, at least one
2683 * query must be done in this time. ensure it by saving the
2684 * query generation in the release time.
2685 * The free list is sorted according to the generation - so if
2686 * the first one is not updated, all the others are not
2689 cnt_free = TAILQ_FIRST(&pool->counters);
2690 if (cnt_free && cnt_free->query_gen + 1 <
2691 rte_atomic64_read(&pool->query_gen))
2696 pool = flow_dv_counter_pool_prepare(dev, &cnt_free, batch);
2700 cnt_free->batch = batch;
2701 /* Create a DV counter action only in the first time usage. */
2702 if (!cnt_free->action) {
2704 struct mlx5_devx_obj *dcs;
2707 offset = cnt_free - &pool->counters_raw[0];
2708 dcs = pool->min_dcs;
2711 dcs = cnt_free->dcs;
2713 cnt_free->action = mlx5_glue->dv_create_flow_action_counter
2715 if (!cnt_free->action) {
2720 /* Update the counter reset values. */
2721 if (_flow_dv_query_count(dev, cnt_free, &cnt_free->hits,
2724 cnt_free->shared = shared;
2725 cnt_free->ref_cnt = 1;
2727 if (!priv->sh->cmng.query_thread_on)
2728 /* Start the asynchronous batch query by the host thread. */
2729 mlx5_set_query_alarm(priv->sh);
2730 TAILQ_REMOVE(&pool->counters, cnt_free, next);
2731 if (TAILQ_EMPTY(&pool->counters)) {
2732 /* Move the pool to the end of the container pool list. */
2733 TAILQ_REMOVE(&cont->pool_list, pool, next);
2734 TAILQ_INSERT_TAIL(&cont->pool_list, pool, next);
2740 * Release a flow counter.
2743 * Pointer to the Ethernet device structure.
2744 * @param[in] counter
2745 * Pointer to the counter handler.
2748 flow_dv_counter_release(struct rte_eth_dev *dev,
2749 struct mlx5_flow_counter *counter)
2751 struct mlx5_priv *priv = dev->data->dev_private;
2755 if (priv->counter_fallback) {
2756 flow_dv_counter_release_fallback(dev, counter);
2759 if (--counter->ref_cnt == 0) {
2760 struct mlx5_flow_counter_pool *pool =
2761 flow_dv_counter_pool_get(counter);
2763 /* Put the counter in the end - the last updated one. */
2764 TAILQ_INSERT_TAIL(&pool->counters, counter, next);
2765 counter->query_gen = rte_atomic64_read(&pool->query_gen);
2770 * Verify the @p attributes will be correctly understood by the NIC and store
2771 * them in the @p flow if everything is correct.
2774 * Pointer to dev struct.
2775 * @param[in] attributes
2776 * Pointer to flow attributes
2778 * Pointer to error structure.
2781 * 0 on success, a negative errno value otherwise and rte_errno is set.
2784 flow_dv_validate_attributes(struct rte_eth_dev *dev,
2785 const struct rte_flow_attr *attributes,
2786 struct rte_flow_error *error)
2788 struct mlx5_priv *priv = dev->data->dev_private;
2789 uint32_t priority_max = priv->config.flow_prio - 1;
2791 #ifndef HAVE_MLX5DV_DR
2792 if (attributes->group)
2793 return rte_flow_error_set(error, ENOTSUP,
2794 RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
2796 "groups is not supported");
2798 if (attributes->priority != MLX5_FLOW_PRIO_RSVD &&
2799 attributes->priority >= priority_max)
2800 return rte_flow_error_set(error, ENOTSUP,
2801 RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
2803 "priority out of range");
2804 if (attributes->transfer) {
2805 if (!priv->config.dv_esw_en)
2806 return rte_flow_error_set
2808 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2809 "E-Switch dr is not supported");
2810 if (!(priv->representor || priv->master))
2811 return rte_flow_error_set
2812 (error, EINVAL, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2813 NULL, "E-Switch configurationd can only be"
2814 " done by a master or a representor device");
2815 if (attributes->egress)
2816 return rte_flow_error_set
2818 RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, attributes,
2819 "egress is not supported");
2820 if (attributes->group >= MLX5_MAX_TABLES_FDB)
2821 return rte_flow_error_set
2823 RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
2824 NULL, "group must be smaller than "
2825 RTE_STR(MLX5_MAX_FDB_TABLES));
2827 if (!(attributes->egress ^ attributes->ingress))
2828 return rte_flow_error_set(error, ENOTSUP,
2829 RTE_FLOW_ERROR_TYPE_ATTR, NULL,
2830 "must specify exactly one of "
2831 "ingress or egress");
2836 * Internal validation function. For validating both actions and items.
2839 * Pointer to the rte_eth_dev structure.
2841 * Pointer to the flow attributes.
2843 * Pointer to the list of items.
2844 * @param[in] actions
2845 * Pointer to the list of actions.
2847 * Pointer to the error structure.
2850 * 0 on success, a negative errno value otherwise and rte_errno is set.
2853 flow_dv_validate(struct rte_eth_dev *dev, const struct rte_flow_attr *attr,
2854 const struct rte_flow_item items[],
2855 const struct rte_flow_action actions[],
2856 struct rte_flow_error *error)
2859 uint64_t action_flags = 0;
2860 uint64_t item_flags = 0;
2861 uint64_t last_item = 0;
2862 uint8_t next_protocol = 0xff;
2864 const struct rte_flow_item *gre_item = NULL;
2865 struct rte_flow_item_tcp nic_tcp_mask = {
2868 .src_port = RTE_BE16(UINT16_MAX),
2869 .dst_port = RTE_BE16(UINT16_MAX),
2875 ret = flow_dv_validate_attributes(dev, attr, error);
2878 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
2879 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
2880 switch (items->type) {
2881 case RTE_FLOW_ITEM_TYPE_VOID:
2883 case RTE_FLOW_ITEM_TYPE_PORT_ID:
2884 ret = flow_dv_validate_item_port_id
2885 (dev, items, attr, item_flags, error);
2888 last_item = MLX5_FLOW_ITEM_PORT_ID;
2890 case RTE_FLOW_ITEM_TYPE_ETH:
2891 ret = mlx5_flow_validate_item_eth(items, item_flags,
2895 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
2896 MLX5_FLOW_LAYER_OUTER_L2;
2898 case RTE_FLOW_ITEM_TYPE_VLAN:
2899 ret = mlx5_flow_validate_item_vlan(items, item_flags,
2903 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_VLAN :
2904 MLX5_FLOW_LAYER_OUTER_VLAN;
2906 case RTE_FLOW_ITEM_TYPE_IPV4:
2907 ret = mlx5_flow_validate_item_ipv4(items, item_flags,
2911 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
2912 MLX5_FLOW_LAYER_OUTER_L3_IPV4;
2913 if (items->mask != NULL &&
2914 ((const struct rte_flow_item_ipv4 *)
2915 items->mask)->hdr.next_proto_id) {
2917 ((const struct rte_flow_item_ipv4 *)
2918 (items->spec))->hdr.next_proto_id;
2920 ((const struct rte_flow_item_ipv4 *)
2921 (items->mask))->hdr.next_proto_id;
2923 /* Reset for inner layer. */
2924 next_protocol = 0xff;
2926 mlx5_flow_tunnel_ip_check(items, &last_item);
2928 case RTE_FLOW_ITEM_TYPE_IPV6:
2929 ret = mlx5_flow_validate_item_ipv6(items, item_flags,
2933 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
2934 MLX5_FLOW_LAYER_OUTER_L3_IPV6;
2935 if (items->mask != NULL &&
2936 ((const struct rte_flow_item_ipv6 *)
2937 items->mask)->hdr.proto) {
2939 ((const struct rte_flow_item_ipv6 *)
2940 items->spec)->hdr.proto;
2942 ((const struct rte_flow_item_ipv6 *)
2943 items->mask)->hdr.proto;
2945 /* Reset for inner layer. */
2946 next_protocol = 0xff;
2948 mlx5_flow_tunnel_ip_check(items, &last_item);
2950 case RTE_FLOW_ITEM_TYPE_TCP:
2951 ret = mlx5_flow_validate_item_tcp
2958 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
2959 MLX5_FLOW_LAYER_OUTER_L4_TCP;
2961 case RTE_FLOW_ITEM_TYPE_UDP:
2962 ret = mlx5_flow_validate_item_udp(items, item_flags,
2967 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
2968 MLX5_FLOW_LAYER_OUTER_L4_UDP;
2970 case RTE_FLOW_ITEM_TYPE_GRE:
2971 case RTE_FLOW_ITEM_TYPE_NVGRE:
2972 ret = mlx5_flow_validate_item_gre(items, item_flags,
2973 next_protocol, error);
2977 last_item = MLX5_FLOW_LAYER_GRE;
2979 case RTE_FLOW_ITEM_TYPE_GRE_KEY:
2980 ret = mlx5_flow_validate_item_gre_key
2981 (items, item_flags, gre_item, error);
2984 last_item = MLX5_FLOW_LAYER_GRE_KEY;
2986 case RTE_FLOW_ITEM_TYPE_VXLAN:
2987 ret = mlx5_flow_validate_item_vxlan(items, item_flags,
2991 last_item = MLX5_FLOW_LAYER_VXLAN;
2993 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
2994 ret = mlx5_flow_validate_item_vxlan_gpe(items,
2999 last_item = MLX5_FLOW_LAYER_VXLAN_GPE;
3001 case RTE_FLOW_ITEM_TYPE_MPLS:
3002 ret = mlx5_flow_validate_item_mpls(dev, items,
3007 last_item = MLX5_FLOW_LAYER_MPLS;
3009 case RTE_FLOW_ITEM_TYPE_META:
3010 ret = flow_dv_validate_item_meta(dev, items, attr,
3014 last_item = MLX5_FLOW_ITEM_METADATA;
3016 case RTE_FLOW_ITEM_TYPE_ICMP:
3017 ret = mlx5_flow_validate_item_icmp(items, item_flags,
3022 last_item = MLX5_FLOW_LAYER_ICMP;
3024 case RTE_FLOW_ITEM_TYPE_ICMP6:
3025 ret = mlx5_flow_validate_item_icmp6(items, item_flags,
3030 last_item = MLX5_FLOW_LAYER_ICMP6;
3033 return rte_flow_error_set(error, ENOTSUP,
3034 RTE_FLOW_ERROR_TYPE_ITEM,
3035 NULL, "item not supported");
3037 item_flags |= last_item;
3039 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
3040 if (actions_n == MLX5_DV_MAX_NUMBER_OF_ACTIONS)
3041 return rte_flow_error_set(error, ENOTSUP,
3042 RTE_FLOW_ERROR_TYPE_ACTION,
3043 actions, "too many actions");
3044 switch (actions->type) {
3045 case RTE_FLOW_ACTION_TYPE_VOID:
3047 case RTE_FLOW_ACTION_TYPE_PORT_ID:
3048 ret = flow_dv_validate_action_port_id(dev,
3055 action_flags |= MLX5_FLOW_ACTION_PORT_ID;
3058 case RTE_FLOW_ACTION_TYPE_FLAG:
3059 ret = mlx5_flow_validate_action_flag(action_flags,
3063 action_flags |= MLX5_FLOW_ACTION_FLAG;
3066 case RTE_FLOW_ACTION_TYPE_MARK:
3067 ret = mlx5_flow_validate_action_mark(actions,
3072 action_flags |= MLX5_FLOW_ACTION_MARK;
3075 case RTE_FLOW_ACTION_TYPE_DROP:
3076 ret = mlx5_flow_validate_action_drop(action_flags,
3080 action_flags |= MLX5_FLOW_ACTION_DROP;
3083 case RTE_FLOW_ACTION_TYPE_QUEUE:
3084 ret = mlx5_flow_validate_action_queue(actions,
3089 action_flags |= MLX5_FLOW_ACTION_QUEUE;
3092 case RTE_FLOW_ACTION_TYPE_RSS:
3093 ret = mlx5_flow_validate_action_rss(actions,
3099 action_flags |= MLX5_FLOW_ACTION_RSS;
3102 case RTE_FLOW_ACTION_TYPE_COUNT:
3103 ret = flow_dv_validate_action_count(dev, error);
3106 action_flags |= MLX5_FLOW_ACTION_COUNT;
3109 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
3110 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
3111 ret = flow_dv_validate_action_l2_encap(action_flags,
3116 action_flags |= actions->type ==
3117 RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP ?
3118 MLX5_FLOW_ACTION_VXLAN_ENCAP :
3119 MLX5_FLOW_ACTION_NVGRE_ENCAP;
3122 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
3123 case RTE_FLOW_ACTION_TYPE_NVGRE_DECAP:
3124 ret = flow_dv_validate_action_l2_decap(action_flags,
3128 action_flags |= actions->type ==
3129 RTE_FLOW_ACTION_TYPE_VXLAN_DECAP ?
3130 MLX5_FLOW_ACTION_VXLAN_DECAP :
3131 MLX5_FLOW_ACTION_NVGRE_DECAP;
3134 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
3135 ret = flow_dv_validate_action_raw_encap(action_flags,
3140 action_flags |= MLX5_FLOW_ACTION_RAW_ENCAP;
3143 case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
3144 ret = flow_dv_validate_action_raw_decap(action_flags,
3149 action_flags |= MLX5_FLOW_ACTION_RAW_DECAP;
3152 case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
3153 case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
3154 ret = flow_dv_validate_action_modify_mac(action_flags,
3160 /* Count all modify-header actions as one action. */
3161 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
3163 action_flags |= actions->type ==
3164 RTE_FLOW_ACTION_TYPE_SET_MAC_SRC ?
3165 MLX5_FLOW_ACTION_SET_MAC_SRC :
3166 MLX5_FLOW_ACTION_SET_MAC_DST;
3169 case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
3170 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
3171 ret = flow_dv_validate_action_modify_ipv4(action_flags,
3177 /* Count all modify-header actions as one action. */
3178 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
3180 action_flags |= actions->type ==
3181 RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC ?
3182 MLX5_FLOW_ACTION_SET_IPV4_SRC :
3183 MLX5_FLOW_ACTION_SET_IPV4_DST;
3185 case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
3186 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
3187 ret = flow_dv_validate_action_modify_ipv6(action_flags,
3193 /* Count all modify-header actions as one action. */
3194 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
3196 action_flags |= actions->type ==
3197 RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC ?
3198 MLX5_FLOW_ACTION_SET_IPV6_SRC :
3199 MLX5_FLOW_ACTION_SET_IPV6_DST;
3201 case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
3202 case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
3203 ret = flow_dv_validate_action_modify_tp(action_flags,
3209 /* Count all modify-header actions as one action. */
3210 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
3212 action_flags |= actions->type ==
3213 RTE_FLOW_ACTION_TYPE_SET_TP_SRC ?
3214 MLX5_FLOW_ACTION_SET_TP_SRC :
3215 MLX5_FLOW_ACTION_SET_TP_DST;
3217 case RTE_FLOW_ACTION_TYPE_DEC_TTL:
3218 case RTE_FLOW_ACTION_TYPE_SET_TTL:
3219 ret = flow_dv_validate_action_modify_ttl(action_flags,
3225 /* Count all modify-header actions as one action. */
3226 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
3228 action_flags |= actions->type ==
3229 RTE_FLOW_ACTION_TYPE_SET_TTL ?
3230 MLX5_FLOW_ACTION_SET_TTL :
3231 MLX5_FLOW_ACTION_DEC_TTL;
3233 case RTE_FLOW_ACTION_TYPE_JUMP:
3234 ret = flow_dv_validate_action_jump(actions,
3235 attr->group, error);
3239 action_flags |= MLX5_FLOW_ACTION_JUMP;
3241 case RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ:
3242 case RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ:
3243 ret = flow_dv_validate_action_modify_tcp_seq
3250 /* Count all modify-header actions as one action. */
3251 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
3253 action_flags |= actions->type ==
3254 RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ ?
3255 MLX5_FLOW_ACTION_INC_TCP_SEQ :
3256 MLX5_FLOW_ACTION_DEC_TCP_SEQ;
3258 case RTE_FLOW_ACTION_TYPE_INC_TCP_ACK:
3259 case RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK:
3260 ret = flow_dv_validate_action_modify_tcp_ack
3267 /* Count all modify-header actions as one action. */
3268 if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
3270 action_flags |= actions->type ==
3271 RTE_FLOW_ACTION_TYPE_INC_TCP_ACK ?
3272 MLX5_FLOW_ACTION_INC_TCP_ACK :
3273 MLX5_FLOW_ACTION_DEC_TCP_ACK;
3276 return rte_flow_error_set(error, ENOTSUP,
3277 RTE_FLOW_ERROR_TYPE_ACTION,
3279 "action not supported");
3282 /* Eswitch has few restrictions on using items and actions */
3283 if (attr->transfer) {
3284 if (action_flags & MLX5_FLOW_ACTION_FLAG)
3285 return rte_flow_error_set(error, ENOTSUP,
3286 RTE_FLOW_ERROR_TYPE_ACTION,
3288 "unsupported action FLAG");
3289 if (action_flags & MLX5_FLOW_ACTION_MARK)
3290 return rte_flow_error_set(error, ENOTSUP,
3291 RTE_FLOW_ERROR_TYPE_ACTION,
3293 "unsupported action MARK");
3294 if (action_flags & MLX5_FLOW_ACTION_QUEUE)
3295 return rte_flow_error_set(error, ENOTSUP,
3296 RTE_FLOW_ERROR_TYPE_ACTION,
3298 "unsupported action QUEUE");
3299 if (action_flags & MLX5_FLOW_ACTION_RSS)
3300 return rte_flow_error_set(error, ENOTSUP,
3301 RTE_FLOW_ERROR_TYPE_ACTION,
3303 "unsupported action RSS");
3304 if (!(action_flags & MLX5_FLOW_FATE_ESWITCH_ACTIONS))
3305 return rte_flow_error_set(error, EINVAL,
3306 RTE_FLOW_ERROR_TYPE_ACTION,
3308 "no fate action is found");
3310 if (!(action_flags & MLX5_FLOW_FATE_ACTIONS) && attr->ingress)
3311 return rte_flow_error_set(error, EINVAL,
3312 RTE_FLOW_ERROR_TYPE_ACTION,
3314 "no fate action is found");
3320 * Internal preparation function. Allocates the DV flow size,
3321 * this size is constant.
3324 * Pointer to the flow attributes.
3326 * Pointer to the list of items.
3327 * @param[in] actions
3328 * Pointer to the list of actions.
3330 * Pointer to the error structure.
3333 * Pointer to mlx5_flow object on success,
3334 * otherwise NULL and rte_errno is set.
3336 static struct mlx5_flow *
3337 flow_dv_prepare(const struct rte_flow_attr *attr __rte_unused,
3338 const struct rte_flow_item items[] __rte_unused,
3339 const struct rte_flow_action actions[] __rte_unused,
3340 struct rte_flow_error *error)
3342 uint32_t size = sizeof(struct mlx5_flow);
3343 struct mlx5_flow *flow;
3345 flow = rte_calloc(__func__, 1, size, 0);
3347 rte_flow_error_set(error, ENOMEM,
3348 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
3349 "not enough memory to create flow");
3352 flow->dv.value.size = MLX5_ST_SZ_BYTES(fte_match_param);
3358 * Sanity check for match mask and value. Similar to check_valid_spec() in
3359 * kernel driver. If unmasked bit is present in value, it returns failure.
3362 * pointer to match mask buffer.
3363 * @param match_value
3364 * pointer to match value buffer.
3367 * 0 if valid, -EINVAL otherwise.
3370 flow_dv_check_valid_spec(void *match_mask, void *match_value)
3372 uint8_t *m = match_mask;
3373 uint8_t *v = match_value;
3376 for (i = 0; i < MLX5_ST_SZ_BYTES(fte_match_param); ++i) {
3379 "match_value differs from match_criteria"
3380 " %p[%u] != %p[%u]",
3381 match_value, i, match_mask, i);
3390 * Add Ethernet item to matcher and to the value.
3392 * @param[in, out] matcher
3394 * @param[in, out] key
3395 * Flow matcher value.
3397 * Flow pattern to translate.
3399 * Item is inner pattern.
3402 flow_dv_translate_item_eth(void *matcher, void *key,
3403 const struct rte_flow_item *item, int inner)
3405 const struct rte_flow_item_eth *eth_m = item->mask;
3406 const struct rte_flow_item_eth *eth_v = item->spec;
3407 const struct rte_flow_item_eth nic_mask = {
3408 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
3409 .src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
3410 .type = RTE_BE16(0xffff),
3422 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
3424 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
3426 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
3428 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
3430 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m, dmac_47_16),
3431 ð_m->dst, sizeof(eth_m->dst));
3432 /* The value must be in the range of the mask. */
3433 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, dmac_47_16);
3434 for (i = 0; i < sizeof(eth_m->dst); ++i)
3435 l24_v[i] = eth_m->dst.addr_bytes[i] & eth_v->dst.addr_bytes[i];
3436 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m, smac_47_16),
3437 ð_m->src, sizeof(eth_m->src));
3438 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, smac_47_16);
3439 /* The value must be in the range of the mask. */
3440 for (i = 0; i < sizeof(eth_m->dst); ++i)
3441 l24_v[i] = eth_m->src.addr_bytes[i] & eth_v->src.addr_bytes[i];
3442 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ethertype,
3443 rte_be_to_cpu_16(eth_m->type));
3444 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, ethertype);
3445 *(uint16_t *)(l24_v) = eth_m->type & eth_v->type;
3449 * Add VLAN item to matcher and to the value.
3451 * @param[in, out] matcher
3453 * @param[in, out] key
3454 * Flow matcher value.
3456 * Flow pattern to translate.
3458 * Item is inner pattern.
3461 flow_dv_translate_item_vlan(void *matcher, void *key,
3462 const struct rte_flow_item *item,
3465 const struct rte_flow_item_vlan *vlan_m = item->mask;
3466 const struct rte_flow_item_vlan *vlan_v = item->spec;
3467 const struct rte_flow_item_vlan nic_mask = {
3468 .tci = RTE_BE16(0x0fff),
3469 .inner_type = RTE_BE16(0xffff),
3481 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
3483 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
3485 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
3487 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
3489 tci_m = rte_be_to_cpu_16(vlan_m->tci);
3490 tci_v = rte_be_to_cpu_16(vlan_m->tci & vlan_v->tci);
3491 MLX5_SET(fte_match_set_lyr_2_4, headers_m, cvlan_tag, 1);
3492 MLX5_SET(fte_match_set_lyr_2_4, headers_v, cvlan_tag, 1);
3493 MLX5_SET(fte_match_set_lyr_2_4, headers_m, first_vid, tci_m);
3494 MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_vid, tci_v);
3495 MLX5_SET(fte_match_set_lyr_2_4, headers_m, first_cfi, tci_m >> 12);
3496 MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_cfi, tci_v >> 12);
3497 MLX5_SET(fte_match_set_lyr_2_4, headers_m, first_prio, tci_m >> 13);
3498 MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_prio, tci_v >> 13);
3502 * Add IPV4 item to matcher and to the value.
3504 * @param[in, out] matcher
3506 * @param[in, out] key
3507 * Flow matcher value.
3509 * Flow pattern to translate.
3511 * Item is inner pattern.
3513 * The group to insert the rule.
3516 flow_dv_translate_item_ipv4(void *matcher, void *key,
3517 const struct rte_flow_item *item,
3518 int inner, uint32_t group)
3520 const struct rte_flow_item_ipv4 *ipv4_m = item->mask;
3521 const struct rte_flow_item_ipv4 *ipv4_v = item->spec;
3522 const struct rte_flow_item_ipv4 nic_mask = {
3524 .src_addr = RTE_BE32(0xffffffff),
3525 .dst_addr = RTE_BE32(0xffffffff),
3526 .type_of_service = 0xff,
3527 .next_proto_id = 0xff,
3537 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
3539 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
3541 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
3543 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
3546 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version, 0xf);
3548 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version, 0x4);
3549 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_version, 4);
3554 l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
3555 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
3556 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
3557 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
3558 *(uint32_t *)l24_m = ipv4_m->hdr.dst_addr;
3559 *(uint32_t *)l24_v = ipv4_m->hdr.dst_addr & ipv4_v->hdr.dst_addr;
3560 l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
3561 src_ipv4_src_ipv6.ipv4_layout.ipv4);
3562 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
3563 src_ipv4_src_ipv6.ipv4_layout.ipv4);
3564 *(uint32_t *)l24_m = ipv4_m->hdr.src_addr;
3565 *(uint32_t *)l24_v = ipv4_m->hdr.src_addr & ipv4_v->hdr.src_addr;
3566 tos = ipv4_m->hdr.type_of_service & ipv4_v->hdr.type_of_service;
3567 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ecn,
3568 ipv4_m->hdr.type_of_service);
3569 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn, tos);
3570 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_dscp,
3571 ipv4_m->hdr.type_of_service >> 2);
3572 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp, tos >> 2);
3573 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol,
3574 ipv4_m->hdr.next_proto_id);
3575 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
3576 ipv4_v->hdr.next_proto_id & ipv4_m->hdr.next_proto_id);
3580 * Add IPV6 item to matcher and to the value.
3582 * @param[in, out] matcher
3584 * @param[in, out] key
3585 * Flow matcher value.
3587 * Flow pattern to translate.
3589 * Item is inner pattern.
3591 * The group to insert the rule.
3594 flow_dv_translate_item_ipv6(void *matcher, void *key,
3595 const struct rte_flow_item *item,
3596 int inner, uint32_t group)
3598 const struct rte_flow_item_ipv6 *ipv6_m = item->mask;
3599 const struct rte_flow_item_ipv6 *ipv6_v = item->spec;
3600 const struct rte_flow_item_ipv6 nic_mask = {
3603 "\xff\xff\xff\xff\xff\xff\xff\xff"
3604 "\xff\xff\xff\xff\xff\xff\xff\xff",
3606 "\xff\xff\xff\xff\xff\xff\xff\xff"
3607 "\xff\xff\xff\xff\xff\xff\xff\xff",
3608 .vtc_flow = RTE_BE32(0xffffffff),
3615 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
3616 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
3625 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
3627 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
3629 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
3631 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
3634 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version, 0xf);
3636 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version, 0x6);
3637 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_version, 6);
3642 size = sizeof(ipv6_m->hdr.dst_addr);
3643 l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
3644 dst_ipv4_dst_ipv6.ipv6_layout.ipv6);
3645 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
3646 dst_ipv4_dst_ipv6.ipv6_layout.ipv6);
3647 memcpy(l24_m, ipv6_m->hdr.dst_addr, size);
3648 for (i = 0; i < size; ++i)
3649 l24_v[i] = l24_m[i] & ipv6_v->hdr.dst_addr[i];
3650 l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
3651 src_ipv4_src_ipv6.ipv6_layout.ipv6);
3652 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
3653 src_ipv4_src_ipv6.ipv6_layout.ipv6);
3654 memcpy(l24_m, ipv6_m->hdr.src_addr, size);
3655 for (i = 0; i < size; ++i)
3656 l24_v[i] = l24_m[i] & ipv6_v->hdr.src_addr[i];
3658 vtc_m = rte_be_to_cpu_32(ipv6_m->hdr.vtc_flow);
3659 vtc_v = rte_be_to_cpu_32(ipv6_m->hdr.vtc_flow & ipv6_v->hdr.vtc_flow);
3660 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ecn, vtc_m >> 20);
3661 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn, vtc_v >> 20);
3662 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_dscp, vtc_m >> 22);
3663 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp, vtc_v >> 22);
3666 MLX5_SET(fte_match_set_misc, misc_m, inner_ipv6_flow_label,
3668 MLX5_SET(fte_match_set_misc, misc_v, inner_ipv6_flow_label,
3671 MLX5_SET(fte_match_set_misc, misc_m, outer_ipv6_flow_label,
3673 MLX5_SET(fte_match_set_misc, misc_v, outer_ipv6_flow_label,
3677 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol,
3679 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
3680 ipv6_v->hdr.proto & ipv6_m->hdr.proto);
3684 * Add TCP item to matcher and to the value.
3686 * @param[in, out] matcher
3688 * @param[in, out] key
3689 * Flow matcher value.
3691 * Flow pattern to translate.
3693 * Item is inner pattern.
3696 flow_dv_translate_item_tcp(void *matcher, void *key,
3697 const struct rte_flow_item *item,
3700 const struct rte_flow_item_tcp *tcp_m = item->mask;
3701 const struct rte_flow_item_tcp *tcp_v = item->spec;
3706 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
3708 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
3710 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
3712 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
3714 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
3715 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_TCP);
3719 tcp_m = &rte_flow_item_tcp_mask;
3720 MLX5_SET(fte_match_set_lyr_2_4, headers_m, tcp_sport,
3721 rte_be_to_cpu_16(tcp_m->hdr.src_port));
3722 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
3723 rte_be_to_cpu_16(tcp_v->hdr.src_port & tcp_m->hdr.src_port));
3724 MLX5_SET(fte_match_set_lyr_2_4, headers_m, tcp_dport,
3725 rte_be_to_cpu_16(tcp_m->hdr.dst_port));
3726 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
3727 rte_be_to_cpu_16(tcp_v->hdr.dst_port & tcp_m->hdr.dst_port));
3728 MLX5_SET(fte_match_set_lyr_2_4, headers_m, tcp_flags,
3729 tcp_m->hdr.tcp_flags);
3730 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_flags,
3731 (tcp_v->hdr.tcp_flags & tcp_m->hdr.tcp_flags));
3735 * Add UDP item to matcher and to the value.
3737 * @param[in, out] matcher
3739 * @param[in, out] key
3740 * Flow matcher value.
3742 * Flow pattern to translate.
3744 * Item is inner pattern.
3747 flow_dv_translate_item_udp(void *matcher, void *key,
3748 const struct rte_flow_item *item,
3751 const struct rte_flow_item_udp *udp_m = item->mask;
3752 const struct rte_flow_item_udp *udp_v = item->spec;
3757 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
3759 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
3761 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
3763 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
3765 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
3766 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_UDP);
3770 udp_m = &rte_flow_item_udp_mask;
3771 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_sport,
3772 rte_be_to_cpu_16(udp_m->hdr.src_port));
3773 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
3774 rte_be_to_cpu_16(udp_v->hdr.src_port & udp_m->hdr.src_port));
3775 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport,
3776 rte_be_to_cpu_16(udp_m->hdr.dst_port));
3777 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
3778 rte_be_to_cpu_16(udp_v->hdr.dst_port & udp_m->hdr.dst_port));
3782 * Add GRE optional Key item to matcher and to the value.
3784 * @param[in, out] matcher
3786 * @param[in, out] key
3787 * Flow matcher value.
3789 * Flow pattern to translate.
3791 * Item is inner pattern.
3794 flow_dv_translate_item_gre_key(void *matcher, void *key,
3795 const struct rte_flow_item *item)
3797 const rte_be32_t *key_m = item->mask;
3798 const rte_be32_t *key_v = item->spec;
3799 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
3800 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
3801 rte_be32_t gre_key_default_mask = RTE_BE32(UINT32_MAX);
3806 key_m = &gre_key_default_mask;
3807 /* GRE K bit must be on and should already be validated */
3808 MLX5_SET(fte_match_set_misc, misc_m, gre_k_present, 1);
3809 MLX5_SET(fte_match_set_misc, misc_v, gre_k_present, 1);
3810 MLX5_SET(fte_match_set_misc, misc_m, gre_key_h,
3811 rte_be_to_cpu_32(*key_m) >> 8);
3812 MLX5_SET(fte_match_set_misc, misc_v, gre_key_h,
3813 rte_be_to_cpu_32((*key_v) & (*key_m)) >> 8);
3814 MLX5_SET(fte_match_set_misc, misc_m, gre_key_l,
3815 rte_be_to_cpu_32(*key_m) & 0xFF);
3816 MLX5_SET(fte_match_set_misc, misc_v, gre_key_l,
3817 rte_be_to_cpu_32((*key_v) & (*key_m)) & 0xFF);
3821 * Add GRE item to matcher and to the value.
3823 * @param[in, out] matcher
3825 * @param[in, out] key
3826 * Flow matcher value.
3828 * Flow pattern to translate.
3830 * Item is inner pattern.
3833 flow_dv_translate_item_gre(void *matcher, void *key,
3834 const struct rte_flow_item *item,
3837 const struct rte_flow_item_gre *gre_m = item->mask;
3838 const struct rte_flow_item_gre *gre_v = item->spec;
3841 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
3842 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
3849 uint16_t s_present:1;
3850 uint16_t k_present:1;
3851 uint16_t rsvd_bit1:1;
3852 uint16_t c_present:1;
3856 } gre_crks_rsvd0_ver_m, gre_crks_rsvd0_ver_v;
3859 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
3861 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
3863 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
3865 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
3867 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
3868 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_GRE);
3872 gre_m = &rte_flow_item_gre_mask;
3873 MLX5_SET(fte_match_set_misc, misc_m, gre_protocol,
3874 rte_be_to_cpu_16(gre_m->protocol));
3875 MLX5_SET(fte_match_set_misc, misc_v, gre_protocol,
3876 rte_be_to_cpu_16(gre_v->protocol & gre_m->protocol));
3877 gre_crks_rsvd0_ver_m.value = rte_be_to_cpu_16(gre_m->c_rsvd0_ver);
3878 gre_crks_rsvd0_ver_v.value = rte_be_to_cpu_16(gre_v->c_rsvd0_ver);
3879 MLX5_SET(fte_match_set_misc, misc_m, gre_c_present,
3880 gre_crks_rsvd0_ver_m.c_present);
3881 MLX5_SET(fte_match_set_misc, misc_v, gre_c_present,
3882 gre_crks_rsvd0_ver_v.c_present &
3883 gre_crks_rsvd0_ver_m.c_present);
3884 MLX5_SET(fte_match_set_misc, misc_m, gre_k_present,
3885 gre_crks_rsvd0_ver_m.k_present);
3886 MLX5_SET(fte_match_set_misc, misc_v, gre_k_present,
3887 gre_crks_rsvd0_ver_v.k_present &
3888 gre_crks_rsvd0_ver_m.k_present);
3889 MLX5_SET(fte_match_set_misc, misc_m, gre_s_present,
3890 gre_crks_rsvd0_ver_m.s_present);
3891 MLX5_SET(fte_match_set_misc, misc_v, gre_s_present,
3892 gre_crks_rsvd0_ver_v.s_present &
3893 gre_crks_rsvd0_ver_m.s_present);
3897 * Add NVGRE item to matcher and to the value.
3899 * @param[in, out] matcher
3901 * @param[in, out] key
3902 * Flow matcher value.
3904 * Flow pattern to translate.
3906 * Item is inner pattern.
3909 flow_dv_translate_item_nvgre(void *matcher, void *key,
3910 const struct rte_flow_item *item,
3913 const struct rte_flow_item_nvgre *nvgre_m = item->mask;
3914 const struct rte_flow_item_nvgre *nvgre_v = item->spec;
3915 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
3916 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
3917 const char *tni_flow_id_m = (const char *)nvgre_m->tni;
3918 const char *tni_flow_id_v = (const char *)nvgre_v->tni;
3924 flow_dv_translate_item_gre(matcher, key, item, inner);
3928 nvgre_m = &rte_flow_item_nvgre_mask;
3929 size = sizeof(nvgre_m->tni) + sizeof(nvgre_m->flow_id);
3930 gre_key_m = MLX5_ADDR_OF(fte_match_set_misc, misc_m, gre_key_h);
3931 gre_key_v = MLX5_ADDR_OF(fte_match_set_misc, misc_v, gre_key_h);
3932 memcpy(gre_key_m, tni_flow_id_m, size);
3933 for (i = 0; i < size; ++i)
3934 gre_key_v[i] = gre_key_m[i] & tni_flow_id_v[i];
3938 * Add VXLAN item to matcher and to the value.
3940 * @param[in, out] matcher
3942 * @param[in, out] key
3943 * Flow matcher value.
3945 * Flow pattern to translate.
3947 * Item is inner pattern.
3950 flow_dv_translate_item_vxlan(void *matcher, void *key,
3951 const struct rte_flow_item *item,
3954 const struct rte_flow_item_vxlan *vxlan_m = item->mask;
3955 const struct rte_flow_item_vxlan *vxlan_v = item->spec;
3958 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
3959 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
3967 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
3969 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
3971 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
3973 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
3975 dport = item->type == RTE_FLOW_ITEM_TYPE_VXLAN ?
3976 MLX5_UDP_PORT_VXLAN : MLX5_UDP_PORT_VXLAN_GPE;
3977 if (!MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport)) {
3978 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xFFFF);
3979 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, dport);
3984 vxlan_m = &rte_flow_item_vxlan_mask;
3985 size = sizeof(vxlan_m->vni);
3986 vni_m = MLX5_ADDR_OF(fte_match_set_misc, misc_m, vxlan_vni);
3987 vni_v = MLX5_ADDR_OF(fte_match_set_misc, misc_v, vxlan_vni);
3988 memcpy(vni_m, vxlan_m->vni, size);
3989 for (i = 0; i < size; ++i)
3990 vni_v[i] = vni_m[i] & vxlan_v->vni[i];
3994 * Add MPLS item to matcher and to the value.
3996 * @param[in, out] matcher
3998 * @param[in, out] key
3999 * Flow matcher value.
4001 * Flow pattern to translate.
4002 * @param[in] prev_layer
4003 * The protocol layer indicated in previous item.
4005 * Item is inner pattern.
4008 flow_dv_translate_item_mpls(void *matcher, void *key,
4009 const struct rte_flow_item *item,
4010 uint64_t prev_layer,
4013 const uint32_t *in_mpls_m = item->mask;
4014 const uint32_t *in_mpls_v = item->spec;
4015 uint32_t *out_mpls_m = 0;
4016 uint32_t *out_mpls_v = 0;
4017 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
4018 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
4019 void *misc2_m = MLX5_ADDR_OF(fte_match_param, matcher,
4021 void *misc2_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_2);
4022 void *headers_m = MLX5_ADDR_OF(fte_match_param, matcher, outer_headers);
4023 void *headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
4025 switch (prev_layer) {
4026 case MLX5_FLOW_LAYER_OUTER_L4_UDP:
4027 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xffff);
4028 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
4029 MLX5_UDP_PORT_MPLS);
4031 case MLX5_FLOW_LAYER_GRE:
4032 MLX5_SET(fte_match_set_misc, misc_m, gre_protocol, 0xffff);
4033 MLX5_SET(fte_match_set_misc, misc_v, gre_protocol,
4034 RTE_ETHER_TYPE_MPLS);
4037 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
4038 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
4045 in_mpls_m = (const uint32_t *)&rte_flow_item_mpls_mask;
4046 switch (prev_layer) {
4047 case MLX5_FLOW_LAYER_OUTER_L4_UDP:
4049 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_m,
4050 outer_first_mpls_over_udp);
4052 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_v,
4053 outer_first_mpls_over_udp);
4055 case MLX5_FLOW_LAYER_GRE:
4057 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_m,
4058 outer_first_mpls_over_gre);
4060 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_v,
4061 outer_first_mpls_over_gre);
4064 /* Inner MPLS not over GRE is not supported. */
4067 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2,
4071 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2,
4077 if (out_mpls_m && out_mpls_v) {
4078 *out_mpls_m = *in_mpls_m;
4079 *out_mpls_v = *in_mpls_v & *in_mpls_m;
4084 * Add META item to matcher
4086 * @param[in, out] matcher
4088 * @param[in, out] key
4089 * Flow matcher value.
4091 * Flow pattern to translate.
4093 * Item is inner pattern.
4096 flow_dv_translate_item_meta(void *matcher, void *key,
4097 const struct rte_flow_item *item)
4099 const struct rte_flow_item_meta *meta_m;
4100 const struct rte_flow_item_meta *meta_v;
4102 MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters_2);
4104 MLX5_ADDR_OF(fte_match_param, key, misc_parameters_2);
4106 meta_m = (const void *)item->mask;
4108 meta_m = &rte_flow_item_meta_mask;
4109 meta_v = (const void *)item->spec;
4111 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_a,
4112 rte_be_to_cpu_32(meta_m->data));
4113 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_a,
4114 rte_be_to_cpu_32(meta_v->data & meta_m->data));
4119 * Add source vport match to the specified matcher.
4121 * @param[in, out] matcher
4123 * @param[in, out] key
4124 * Flow matcher value.
4126 * Source vport value to match
4131 flow_dv_translate_item_source_vport(void *matcher, void *key,
4132 int16_t port, uint16_t mask)
4134 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
4135 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
4137 MLX5_SET(fte_match_set_misc, misc_m, source_port, mask);
4138 MLX5_SET(fte_match_set_misc, misc_v, source_port, port);
4142 * Translate port-id item to eswitch match on port-id.
4145 * The devich to configure through.
4146 * @param[in, out] matcher
4148 * @param[in, out] key
4149 * Flow matcher value.
4151 * Flow pattern to translate.
4154 * 0 on success, a negative errno value otherwise.
4157 flow_dv_translate_item_port_id(struct rte_eth_dev *dev, void *matcher,
4158 void *key, const struct rte_flow_item *item)
4160 const struct rte_flow_item_port_id *pid_m = item ? item->mask : NULL;
4161 const struct rte_flow_item_port_id *pid_v = item ? item->spec : NULL;
4162 uint16_t mask, val, id;
4165 mask = pid_m ? pid_m->id : 0xffff;
4166 id = pid_v ? pid_v->id : dev->data->port_id;
4167 ret = mlx5_port_to_eswitch_info(id, NULL, &val);
4170 flow_dv_translate_item_source_vport(matcher, key, val, mask);
4175 * Add ICMP6 item to matcher and to the value.
4177 * @param[in, out] matcher
4179 * @param[in, out] key
4180 * Flow matcher value.
4182 * Flow pattern to translate.
4184 * Item is inner pattern.
4187 flow_dv_translate_item_icmp6(void *matcher, void *key,
4188 const struct rte_flow_item *item,
4191 const struct rte_flow_item_icmp6 *icmp6_m = item->mask;
4192 const struct rte_flow_item_icmp6 *icmp6_v = item->spec;
4195 void *misc3_m = MLX5_ADDR_OF(fte_match_param, matcher,
4197 void *misc3_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
4199 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
4201 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
4203 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
4205 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
4207 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xFF);
4208 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_ICMPV6);
4212 icmp6_m = &rte_flow_item_icmp6_mask;
4213 MLX5_SET(fte_match_set_misc3, misc3_m, icmpv6_type, icmp6_m->type);
4214 MLX5_SET(fte_match_set_misc3, misc3_v, icmpv6_type,
4215 icmp6_v->type & icmp6_m->type);
4216 MLX5_SET(fte_match_set_misc3, misc3_m, icmpv6_code, icmp6_m->code);
4217 MLX5_SET(fte_match_set_misc3, misc3_v, icmpv6_code,
4218 icmp6_v->code & icmp6_m->code);
4222 * Add ICMP item to matcher and to the value.
4224 * @param[in, out] matcher
4226 * @param[in, out] key
4227 * Flow matcher value.
4229 * Flow pattern to translate.
4231 * Item is inner pattern.
4234 flow_dv_translate_item_icmp(void *matcher, void *key,
4235 const struct rte_flow_item *item,
4238 const struct rte_flow_item_icmp *icmp_m = item->mask;
4239 const struct rte_flow_item_icmp *icmp_v = item->spec;
4242 void *misc3_m = MLX5_ADDR_OF(fte_match_param, matcher,
4244 void *misc3_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
4246 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
4248 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
4250 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
4252 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
4254 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xFF);
4255 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_ICMP);
4259 icmp_m = &rte_flow_item_icmp_mask;
4260 MLX5_SET(fte_match_set_misc3, misc3_m, icmp_type,
4261 icmp_m->hdr.icmp_type);
4262 MLX5_SET(fte_match_set_misc3, misc3_v, icmp_type,
4263 icmp_v->hdr.icmp_type & icmp_m->hdr.icmp_type);
4264 MLX5_SET(fte_match_set_misc3, misc3_m, icmp_code,
4265 icmp_m->hdr.icmp_code);
4266 MLX5_SET(fte_match_set_misc3, misc3_v, icmp_code,
4267 icmp_v->hdr.icmp_code & icmp_m->hdr.icmp_code);
4270 static uint32_t matcher_zero[MLX5_ST_SZ_DW(fte_match_param)] = { 0 };
4272 #define HEADER_IS_ZERO(match_criteria, headers) \
4273 !(memcmp(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
4274 matcher_zero, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
4277 * Calculate flow matcher enable bitmap.
4279 * @param match_criteria
4280 * Pointer to flow matcher criteria.
4283 * Bitmap of enabled fields.
4286 flow_dv_matcher_enable(uint32_t *match_criteria)
4288 uint8_t match_criteria_enable;
4290 match_criteria_enable =
4291 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
4292 MLX5_MATCH_CRITERIA_ENABLE_OUTER_BIT;
4293 match_criteria_enable |=
4294 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
4295 MLX5_MATCH_CRITERIA_ENABLE_MISC_BIT;
4296 match_criteria_enable |=
4297 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
4298 MLX5_MATCH_CRITERIA_ENABLE_INNER_BIT;
4299 match_criteria_enable |=
4300 (!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
4301 MLX5_MATCH_CRITERIA_ENABLE_MISC2_BIT;
4302 #ifdef HAVE_MLX5DV_DR
4303 match_criteria_enable |=
4304 (!HEADER_IS_ZERO(match_criteria, misc_parameters_3)) <<
4305 MLX5_MATCH_CRITERIA_ENABLE_MISC3_BIT;
4307 return match_criteria_enable;
4314 * @param dev[in, out]
4315 * Pointer to rte_eth_dev structure.
4316 * @param[in] table_id
4319 * Direction of the table.
4320 * @param[in] transfer
4321 * E-Switch or NIC flow.
4323 * pointer to error structure.
4326 * Returns tables resource based on the index, NULL in case of failed.
4328 static struct mlx5_flow_tbl_resource *
4329 flow_dv_tbl_resource_get(struct rte_eth_dev *dev,
4330 uint32_t table_id, uint8_t egress,
4332 struct rte_flow_error *error)
4334 struct mlx5_priv *priv = dev->data->dev_private;
4335 struct mlx5_ibv_shared *sh = priv->sh;
4336 struct mlx5_flow_tbl_resource *tbl;
4338 #ifdef HAVE_MLX5DV_DR
4340 tbl = &sh->fdb_tbl[table_id];
4342 tbl->obj = mlx5_glue->dr_create_flow_tbl
4343 (sh->fdb_domain, table_id);
4344 } else if (egress) {
4345 tbl = &sh->tx_tbl[table_id];
4347 tbl->obj = mlx5_glue->dr_create_flow_tbl
4348 (sh->tx_domain, table_id);
4350 tbl = &sh->rx_tbl[table_id];
4352 tbl->obj = mlx5_glue->dr_create_flow_tbl
4353 (sh->rx_domain, table_id);
4356 rte_flow_error_set(error, ENOMEM,
4357 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
4358 NULL, "cannot create table");
4361 rte_atomic32_inc(&tbl->refcnt);
4367 return &sh->fdb_tbl[table_id];
4369 return &sh->tx_tbl[table_id];
4371 return &sh->rx_tbl[table_id];
4376 * Release a flow table.
4379 * Table resource to be released.
4382 * Returns 0 if table was released, else return 1;
4385 flow_dv_tbl_resource_release(struct mlx5_flow_tbl_resource *tbl)
4389 if (rte_atomic32_dec_and_test(&tbl->refcnt)) {
4390 mlx5_glue->dr_destroy_flow_tbl(tbl->obj);
4398 * Register the flow matcher.
4400 * @param dev[in, out]
4401 * Pointer to rte_eth_dev structure.
4402 * @param[in, out] matcher
4403 * Pointer to flow matcher.
4404 * @parm[in, out] dev_flow
4405 * Pointer to the dev_flow.
4407 * pointer to error structure.
4410 * 0 on success otherwise -errno and errno is set.
4413 flow_dv_matcher_register(struct rte_eth_dev *dev,
4414 struct mlx5_flow_dv_matcher *matcher,
4415 struct mlx5_flow *dev_flow,
4416 struct rte_flow_error *error)
4418 struct mlx5_priv *priv = dev->data->dev_private;
4419 struct mlx5_ibv_shared *sh = priv->sh;
4420 struct mlx5_flow_dv_matcher *cache_matcher;
4421 struct mlx5dv_flow_matcher_attr dv_attr = {
4422 .type = IBV_FLOW_ATTR_NORMAL,
4423 .match_mask = (void *)&matcher->mask,
4425 struct mlx5_flow_tbl_resource *tbl = NULL;
4427 /* Lookup from cache. */
4428 LIST_FOREACH(cache_matcher, &sh->matchers, next) {
4429 if (matcher->crc == cache_matcher->crc &&
4430 matcher->priority == cache_matcher->priority &&
4431 matcher->egress == cache_matcher->egress &&
4432 matcher->group == cache_matcher->group &&
4433 matcher->transfer == cache_matcher->transfer &&
4434 !memcmp((const void *)matcher->mask.buf,
4435 (const void *)cache_matcher->mask.buf,
4436 cache_matcher->mask.size)) {
4438 "priority %hd use %s matcher %p: refcnt %d++",
4439 cache_matcher->priority,
4440 cache_matcher->egress ? "tx" : "rx",
4441 (void *)cache_matcher,
4442 rte_atomic32_read(&cache_matcher->refcnt));
4443 rte_atomic32_inc(&cache_matcher->refcnt);
4444 dev_flow->dv.matcher = cache_matcher;
4448 /* Register new matcher. */
4449 cache_matcher = rte_calloc(__func__, 1, sizeof(*cache_matcher), 0);
4451 return rte_flow_error_set(error, ENOMEM,
4452 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
4453 "cannot allocate matcher memory");
4454 tbl = flow_dv_tbl_resource_get(dev, matcher->group * MLX5_GROUP_FACTOR,
4455 matcher->egress, matcher->transfer,
4458 rte_free(cache_matcher);
4459 return rte_flow_error_set(error, ENOMEM,
4460 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
4461 NULL, "cannot create table");
4463 *cache_matcher = *matcher;
4464 dv_attr.match_criteria_enable =
4465 flow_dv_matcher_enable(cache_matcher->mask.buf);
4466 dv_attr.priority = matcher->priority;
4467 if (matcher->egress)
4468 dv_attr.flags |= IBV_FLOW_ATTR_FLAGS_EGRESS;
4469 cache_matcher->matcher_object =
4470 mlx5_glue->dv_create_flow_matcher(sh->ctx, &dv_attr, tbl->obj);
4471 if (!cache_matcher->matcher_object) {
4472 rte_free(cache_matcher);
4473 #ifdef HAVE_MLX5DV_DR
4474 flow_dv_tbl_resource_release(tbl);
4476 return rte_flow_error_set(error, ENOMEM,
4477 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
4478 NULL, "cannot create matcher");
4480 rte_atomic32_inc(&cache_matcher->refcnt);
4481 LIST_INSERT_HEAD(&sh->matchers, cache_matcher, next);
4482 dev_flow->dv.matcher = cache_matcher;
4483 DRV_LOG(DEBUG, "priority %hd new %s matcher %p: refcnt %d",
4484 cache_matcher->priority,
4485 cache_matcher->egress ? "tx" : "rx", (void *)cache_matcher,
4486 rte_atomic32_read(&cache_matcher->refcnt));
4487 rte_atomic32_inc(&tbl->refcnt);
4492 * Find existing tag resource or create and register a new one.
4494 * @param dev[in, out]
4495 * Pointer to rte_eth_dev structure.
4496 * @param[in, out] resource
4497 * Pointer to tag resource.
4498 * @parm[in, out] dev_flow
4499 * Pointer to the dev_flow.
4501 * pointer to error structure.
4504 * 0 on success otherwise -errno and errno is set.
4507 flow_dv_tag_resource_register
4508 (struct rte_eth_dev *dev,
4509 struct mlx5_flow_dv_tag_resource *resource,
4510 struct mlx5_flow *dev_flow,
4511 struct rte_flow_error *error)
4513 struct mlx5_priv *priv = dev->data->dev_private;
4514 struct mlx5_ibv_shared *sh = priv->sh;
4515 struct mlx5_flow_dv_tag_resource *cache_resource;
4517 /* Lookup a matching resource from cache. */
4518 LIST_FOREACH(cache_resource, &sh->tags, next) {
4519 if (resource->tag == cache_resource->tag) {
4520 DRV_LOG(DEBUG, "tag resource %p: refcnt %d++",
4521 (void *)cache_resource,
4522 rte_atomic32_read(&cache_resource->refcnt));
4523 rte_atomic32_inc(&cache_resource->refcnt);
4524 dev_flow->flow->tag_resource = cache_resource;
4528 /* Register new resource. */
4529 cache_resource = rte_calloc(__func__, 1, sizeof(*cache_resource), 0);
4530 if (!cache_resource)
4531 return rte_flow_error_set(error, ENOMEM,
4532 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
4533 "cannot allocate resource memory");
4534 *cache_resource = *resource;
4535 cache_resource->action = mlx5_glue->dv_create_flow_action_tag
4537 if (!cache_resource->action) {
4538 rte_free(cache_resource);
4539 return rte_flow_error_set(error, ENOMEM,
4540 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
4541 NULL, "cannot create action");
4543 rte_atomic32_init(&cache_resource->refcnt);
4544 rte_atomic32_inc(&cache_resource->refcnt);
4545 LIST_INSERT_HEAD(&sh->tags, cache_resource, next);
4546 dev_flow->flow->tag_resource = cache_resource;
4547 DRV_LOG(DEBUG, "new tag resource %p: refcnt %d++",
4548 (void *)cache_resource,
4549 rte_atomic32_read(&cache_resource->refcnt));
4557 * Pointer to Ethernet device.
4559 * Pointer to mlx5_flow.
4562 * 1 while a reference on it exists, 0 when freed.
4565 flow_dv_tag_release(struct rte_eth_dev *dev,
4566 struct mlx5_flow_dv_tag_resource *tag)
4569 DRV_LOG(DEBUG, "port %u tag %p: refcnt %d--",
4570 dev->data->port_id, (void *)tag,
4571 rte_atomic32_read(&tag->refcnt));
4572 if (rte_atomic32_dec_and_test(&tag->refcnt)) {
4573 claim_zero(mlx5_glue->destroy_flow_action(tag->action));
4574 LIST_REMOVE(tag, next);
4575 DRV_LOG(DEBUG, "port %u tag %p: removed",
4576 dev->data->port_id, (void *)tag);
4584 * Translate port ID action to vport.
4587 * Pointer to rte_eth_dev structure.
4589 * Pointer to the port ID action.
4590 * @param[out] dst_port_id
4591 * The target port ID.
4593 * Pointer to the error structure.
4596 * 0 on success, a negative errno value otherwise and rte_errno is set.
4599 flow_dv_translate_action_port_id(struct rte_eth_dev *dev,
4600 const struct rte_flow_action *action,
4601 uint32_t *dst_port_id,
4602 struct rte_flow_error *error)
4607 const struct rte_flow_action_port_id *conf =
4608 (const struct rte_flow_action_port_id *)action->conf;
4610 port = conf->original ? dev->data->port_id : conf->id;
4611 ret = mlx5_port_to_eswitch_info(port, NULL, &port_id);
4613 return rte_flow_error_set(error, -ret,
4614 RTE_FLOW_ERROR_TYPE_ACTION,
4616 "No eswitch info was found for port");
4617 *dst_port_id = port_id;
4622 * Fill the flow with DV spec.
4625 * Pointer to rte_eth_dev structure.
4626 * @param[in, out] dev_flow
4627 * Pointer to the sub flow.
4629 * Pointer to the flow attributes.
4631 * Pointer to the list of items.
4632 * @param[in] actions
4633 * Pointer to the list of actions.
4635 * Pointer to the error structure.
4638 * 0 on success, a negative errno value otherwise and rte_errno is set.
4641 flow_dv_translate(struct rte_eth_dev *dev,
4642 struct mlx5_flow *dev_flow,
4643 const struct rte_flow_attr *attr,
4644 const struct rte_flow_item items[],
4645 const struct rte_flow_action actions[],
4646 struct rte_flow_error *error)
4648 struct mlx5_priv *priv = dev->data->dev_private;
4649 struct rte_flow *flow = dev_flow->flow;
4650 uint64_t item_flags = 0;
4651 uint64_t last_item = 0;
4652 uint64_t action_flags = 0;
4653 uint64_t priority = attr->priority;
4654 struct mlx5_flow_dv_matcher matcher = {
4656 .size = sizeof(matcher.mask.buf),
4660 bool actions_end = false;
4661 struct mlx5_flow_dv_modify_hdr_resource res = {
4662 .ft_type = attr->egress ? MLX5DV_FLOW_TABLE_TYPE_NIC_TX :
4663 MLX5DV_FLOW_TABLE_TYPE_NIC_RX
4665 union flow_dv_attr flow_attr = { .attr = 0 };
4666 struct mlx5_flow_dv_tag_resource tag_resource;
4667 uint32_t modify_action_position = UINT32_MAX;
4668 void *match_mask = matcher.mask.buf;
4669 void *match_value = dev_flow->dv.value.buf;
4671 flow->group = attr->group;
4673 res.ft_type = MLX5DV_FLOW_TABLE_TYPE_FDB;
4674 if (priority == MLX5_FLOW_PRIO_RSVD)
4675 priority = priv->config.flow_prio - 1;
4676 for (; !actions_end ; actions++) {
4677 const struct rte_flow_action_queue *queue;
4678 const struct rte_flow_action_rss *rss;
4679 const struct rte_flow_action *action = actions;
4680 const struct rte_flow_action_count *count = action->conf;
4681 const uint8_t *rss_key;
4682 const struct rte_flow_action_jump *jump_data;
4683 struct mlx5_flow_dv_jump_tbl_resource jump_tbl_resource;
4684 struct mlx5_flow_tbl_resource *tbl;
4685 uint32_t port_id = 0;
4686 struct mlx5_flow_dv_port_id_action_resource port_id_resource;
4688 switch (actions->type) {
4689 case RTE_FLOW_ACTION_TYPE_VOID:
4691 case RTE_FLOW_ACTION_TYPE_PORT_ID:
4692 if (flow_dv_translate_action_port_id(dev, action,
4695 port_id_resource.port_id = port_id;
4696 if (flow_dv_port_id_action_resource_register
4697 (dev, &port_id_resource, dev_flow, error))
4699 dev_flow->dv.actions[actions_n++] =
4700 dev_flow->dv.port_id_action->action;
4701 action_flags |= MLX5_FLOW_ACTION_PORT_ID;
4703 case RTE_FLOW_ACTION_TYPE_FLAG:
4705 mlx5_flow_mark_set(MLX5_FLOW_MARK_DEFAULT);
4706 if (!flow->tag_resource)
4707 if (flow_dv_tag_resource_register
4708 (dev, &tag_resource, dev_flow, error))
4710 dev_flow->dv.actions[actions_n++] =
4711 flow->tag_resource->action;
4712 action_flags |= MLX5_FLOW_ACTION_FLAG;
4714 case RTE_FLOW_ACTION_TYPE_MARK:
4715 tag_resource.tag = mlx5_flow_mark_set
4716 (((const struct rte_flow_action_mark *)
4717 (actions->conf))->id);
4718 if (!flow->tag_resource)
4719 if (flow_dv_tag_resource_register
4720 (dev, &tag_resource, dev_flow, error))
4722 dev_flow->dv.actions[actions_n++] =
4723 flow->tag_resource->action;
4724 action_flags |= MLX5_FLOW_ACTION_MARK;
4726 case RTE_FLOW_ACTION_TYPE_DROP:
4727 action_flags |= MLX5_FLOW_ACTION_DROP;
4729 case RTE_FLOW_ACTION_TYPE_QUEUE:
4730 queue = actions->conf;
4731 flow->rss.queue_num = 1;
4732 (*flow->queue)[0] = queue->index;
4733 action_flags |= MLX5_FLOW_ACTION_QUEUE;
4735 case RTE_FLOW_ACTION_TYPE_RSS:
4736 rss = actions->conf;
4738 memcpy((*flow->queue), rss->queue,
4739 rss->queue_num * sizeof(uint16_t));
4740 flow->rss.queue_num = rss->queue_num;
4741 /* NULL RSS key indicates default RSS key. */
4742 rss_key = !rss->key ? rss_hash_default_key : rss->key;
4743 memcpy(flow->key, rss_key, MLX5_RSS_HASH_KEY_LEN);
4744 /* RSS type 0 indicates default RSS type ETH_RSS_IP. */
4745 flow->rss.types = !rss->types ? ETH_RSS_IP : rss->types;
4746 flow->rss.level = rss->level;
4747 action_flags |= MLX5_FLOW_ACTION_RSS;
4749 case RTE_FLOW_ACTION_TYPE_COUNT:
4750 if (!priv->config.devx) {
4751 rte_errno = ENOTSUP;
4754 flow->counter = flow_dv_counter_alloc(dev,
4758 if (flow->counter == NULL)
4760 dev_flow->dv.actions[actions_n++] =
4761 flow->counter->action;
4762 action_flags |= MLX5_FLOW_ACTION_COUNT;
4765 if (rte_errno == ENOTSUP)
4766 return rte_flow_error_set
4768 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
4770 "count action not supported");
4772 return rte_flow_error_set
4774 RTE_FLOW_ERROR_TYPE_ACTION,
4776 "cannot create counter"
4778 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
4779 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
4780 if (flow_dv_create_action_l2_encap(dev, actions,
4785 dev_flow->dv.actions[actions_n++] =
4786 dev_flow->dv.encap_decap->verbs_action;
4787 action_flags |= actions->type ==
4788 RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP ?
4789 MLX5_FLOW_ACTION_VXLAN_ENCAP :
4790 MLX5_FLOW_ACTION_NVGRE_ENCAP;
4792 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
4793 case RTE_FLOW_ACTION_TYPE_NVGRE_DECAP:
4794 if (flow_dv_create_action_l2_decap(dev, dev_flow,
4798 dev_flow->dv.actions[actions_n++] =
4799 dev_flow->dv.encap_decap->verbs_action;
4800 action_flags |= actions->type ==
4801 RTE_FLOW_ACTION_TYPE_VXLAN_DECAP ?
4802 MLX5_FLOW_ACTION_VXLAN_DECAP :
4803 MLX5_FLOW_ACTION_NVGRE_DECAP;
4805 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
4806 /* Handle encap with preceding decap. */
4807 if (action_flags & MLX5_FLOW_ACTION_RAW_DECAP) {
4808 if (flow_dv_create_action_raw_encap
4809 (dev, actions, dev_flow, attr, error))
4811 dev_flow->dv.actions[actions_n++] =
4812 dev_flow->dv.encap_decap->verbs_action;
4814 /* Handle encap without preceding decap. */
4815 if (flow_dv_create_action_l2_encap
4816 (dev, actions, dev_flow, attr->transfer,
4819 dev_flow->dv.actions[actions_n++] =
4820 dev_flow->dv.encap_decap->verbs_action;
4822 action_flags |= MLX5_FLOW_ACTION_RAW_ENCAP;
4824 case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
4825 /* Check if this decap is followed by encap. */
4826 for (; action->type != RTE_FLOW_ACTION_TYPE_END &&
4827 action->type != RTE_FLOW_ACTION_TYPE_RAW_ENCAP;
4830 /* Handle decap only if it isn't followed by encap. */
4831 if (action->type != RTE_FLOW_ACTION_TYPE_RAW_ENCAP) {
4832 if (flow_dv_create_action_l2_decap
4833 (dev, dev_flow, attr->transfer, error))
4835 dev_flow->dv.actions[actions_n++] =
4836 dev_flow->dv.encap_decap->verbs_action;
4838 /* If decap is followed by encap, handle it at encap. */
4839 action_flags |= MLX5_FLOW_ACTION_RAW_DECAP;
4841 case RTE_FLOW_ACTION_TYPE_JUMP:
4842 jump_data = action->conf;
4843 tbl = flow_dv_tbl_resource_get(dev, jump_data->group *
4846 attr->transfer, error);
4848 return rte_flow_error_set
4850 RTE_FLOW_ERROR_TYPE_ACTION,
4852 "cannot create jump action.");
4853 jump_tbl_resource.tbl = tbl;
4854 if (flow_dv_jump_tbl_resource_register
4855 (dev, &jump_tbl_resource, dev_flow, error)) {
4856 flow_dv_tbl_resource_release(tbl);
4857 return rte_flow_error_set
4859 RTE_FLOW_ERROR_TYPE_ACTION,
4861 "cannot create jump action.");
4863 dev_flow->dv.actions[actions_n++] =
4864 dev_flow->dv.jump->action;
4865 action_flags |= MLX5_FLOW_ACTION_JUMP;
4867 case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
4868 case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
4869 if (flow_dv_convert_action_modify_mac(&res, actions,
4872 action_flags |= actions->type ==
4873 RTE_FLOW_ACTION_TYPE_SET_MAC_SRC ?
4874 MLX5_FLOW_ACTION_SET_MAC_SRC :
4875 MLX5_FLOW_ACTION_SET_MAC_DST;
4877 case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
4878 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
4879 if (flow_dv_convert_action_modify_ipv4(&res, actions,
4882 action_flags |= actions->type ==
4883 RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC ?
4884 MLX5_FLOW_ACTION_SET_IPV4_SRC :
4885 MLX5_FLOW_ACTION_SET_IPV4_DST;
4887 case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
4888 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
4889 if (flow_dv_convert_action_modify_ipv6(&res, actions,
4892 action_flags |= actions->type ==
4893 RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC ?
4894 MLX5_FLOW_ACTION_SET_IPV6_SRC :
4895 MLX5_FLOW_ACTION_SET_IPV6_DST;
4897 case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
4898 case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
4899 if (flow_dv_convert_action_modify_tp(&res, actions,
4903 action_flags |= actions->type ==
4904 RTE_FLOW_ACTION_TYPE_SET_TP_SRC ?
4905 MLX5_FLOW_ACTION_SET_TP_SRC :
4906 MLX5_FLOW_ACTION_SET_TP_DST;
4908 case RTE_FLOW_ACTION_TYPE_DEC_TTL:
4909 if (flow_dv_convert_action_modify_dec_ttl(&res, items,
4913 action_flags |= MLX5_FLOW_ACTION_DEC_TTL;
4915 case RTE_FLOW_ACTION_TYPE_SET_TTL:
4916 if (flow_dv_convert_action_modify_ttl(&res, actions,
4920 action_flags |= MLX5_FLOW_ACTION_SET_TTL;
4922 case RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ:
4923 case RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ:
4924 if (flow_dv_convert_action_modify_tcp_seq(&res, actions,
4927 action_flags |= actions->type ==
4928 RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ ?
4929 MLX5_FLOW_ACTION_INC_TCP_SEQ :
4930 MLX5_FLOW_ACTION_DEC_TCP_SEQ;
4933 case RTE_FLOW_ACTION_TYPE_INC_TCP_ACK:
4934 case RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK:
4935 if (flow_dv_convert_action_modify_tcp_ack(&res, actions,
4938 action_flags |= actions->type ==
4939 RTE_FLOW_ACTION_TYPE_INC_TCP_ACK ?
4940 MLX5_FLOW_ACTION_INC_TCP_ACK :
4941 MLX5_FLOW_ACTION_DEC_TCP_ACK;
4943 case RTE_FLOW_ACTION_TYPE_END:
4945 if (action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS) {
4946 /* create modify action if needed. */
4947 if (flow_dv_modify_hdr_resource_register
4952 dev_flow->dv.actions[modify_action_position] =
4953 dev_flow->dv.modify_hdr->verbs_action;
4959 if ((action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS) &&
4960 modify_action_position == UINT32_MAX)
4961 modify_action_position = actions_n++;
4963 dev_flow->dv.actions_n = actions_n;
4964 flow->actions = action_flags;
4965 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
4966 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
4968 switch (items->type) {
4969 case RTE_FLOW_ITEM_TYPE_PORT_ID:
4970 flow_dv_translate_item_port_id(dev, match_mask,
4971 match_value, items);
4972 last_item = MLX5_FLOW_ITEM_PORT_ID;
4974 case RTE_FLOW_ITEM_TYPE_ETH:
4975 flow_dv_translate_item_eth(match_mask, match_value,
4977 matcher.priority = MLX5_PRIORITY_MAP_L2;
4978 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
4979 MLX5_FLOW_LAYER_OUTER_L2;
4981 case RTE_FLOW_ITEM_TYPE_VLAN:
4982 flow_dv_translate_item_vlan(match_mask, match_value,
4984 matcher.priority = MLX5_PRIORITY_MAP_L2;
4985 last_item = tunnel ? (MLX5_FLOW_LAYER_INNER_L2 |
4986 MLX5_FLOW_LAYER_INNER_VLAN) :
4987 (MLX5_FLOW_LAYER_OUTER_L2 |
4988 MLX5_FLOW_LAYER_OUTER_VLAN);
4990 case RTE_FLOW_ITEM_TYPE_IPV4:
4991 flow_dv_translate_item_ipv4(match_mask, match_value,
4992 items, tunnel, attr->group);
4993 matcher.priority = MLX5_PRIORITY_MAP_L3;
4994 dev_flow->dv.hash_fields |=
4995 mlx5_flow_hashfields_adjust
4997 MLX5_IPV4_LAYER_TYPES,
4998 MLX5_IPV4_IBV_RX_HASH);
4999 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
5000 MLX5_FLOW_LAYER_OUTER_L3_IPV4;
5001 mlx5_flow_tunnel_ip_check(items, &last_item);
5003 case RTE_FLOW_ITEM_TYPE_IPV6:
5004 flow_dv_translate_item_ipv6(match_mask, match_value,
5005 items, tunnel, attr->group);
5006 matcher.priority = MLX5_PRIORITY_MAP_L3;
5007 dev_flow->dv.hash_fields |=
5008 mlx5_flow_hashfields_adjust
5010 MLX5_IPV6_LAYER_TYPES,
5011 MLX5_IPV6_IBV_RX_HASH);
5012 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
5013 MLX5_FLOW_LAYER_OUTER_L3_IPV6;
5014 mlx5_flow_tunnel_ip_check(items, &last_item);
5016 case RTE_FLOW_ITEM_TYPE_TCP:
5017 flow_dv_translate_item_tcp(match_mask, match_value,
5019 matcher.priority = MLX5_PRIORITY_MAP_L4;
5020 dev_flow->dv.hash_fields |=
5021 mlx5_flow_hashfields_adjust
5022 (dev_flow, tunnel, ETH_RSS_TCP,
5023 IBV_RX_HASH_SRC_PORT_TCP |
5024 IBV_RX_HASH_DST_PORT_TCP);
5025 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
5026 MLX5_FLOW_LAYER_OUTER_L4_TCP;
5028 case RTE_FLOW_ITEM_TYPE_UDP:
5029 flow_dv_translate_item_udp(match_mask, match_value,
5031 matcher.priority = MLX5_PRIORITY_MAP_L4;
5032 dev_flow->dv.hash_fields |=
5033 mlx5_flow_hashfields_adjust
5034 (dev_flow, tunnel, ETH_RSS_UDP,
5035 IBV_RX_HASH_SRC_PORT_UDP |
5036 IBV_RX_HASH_DST_PORT_UDP);
5037 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
5038 MLX5_FLOW_LAYER_OUTER_L4_UDP;
5040 case RTE_FLOW_ITEM_TYPE_GRE:
5041 flow_dv_translate_item_gre(match_mask, match_value,
5043 last_item = MLX5_FLOW_LAYER_GRE;
5045 case RTE_FLOW_ITEM_TYPE_GRE_KEY:
5046 flow_dv_translate_item_gre_key(match_mask,
5047 match_value, items);
5048 last_item = MLX5_FLOW_LAYER_GRE_KEY;
5050 case RTE_FLOW_ITEM_TYPE_NVGRE:
5051 flow_dv_translate_item_nvgre(match_mask, match_value,
5053 last_item = MLX5_FLOW_LAYER_GRE;
5055 case RTE_FLOW_ITEM_TYPE_VXLAN:
5056 flow_dv_translate_item_vxlan(match_mask, match_value,
5058 last_item = MLX5_FLOW_LAYER_VXLAN;
5060 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
5061 flow_dv_translate_item_vxlan(match_mask, match_value,
5063 last_item = MLX5_FLOW_LAYER_VXLAN_GPE;
5065 case RTE_FLOW_ITEM_TYPE_MPLS:
5066 flow_dv_translate_item_mpls(match_mask, match_value,
5067 items, last_item, tunnel);
5068 last_item = MLX5_FLOW_LAYER_MPLS;
5070 case RTE_FLOW_ITEM_TYPE_META:
5071 flow_dv_translate_item_meta(match_mask, match_value,
5073 last_item = MLX5_FLOW_ITEM_METADATA;
5075 case RTE_FLOW_ITEM_TYPE_ICMP:
5076 flow_dv_translate_item_icmp(match_mask, match_value,
5078 last_item = MLX5_FLOW_LAYER_ICMP;
5080 case RTE_FLOW_ITEM_TYPE_ICMP6:
5081 flow_dv_translate_item_icmp6(match_mask, match_value,
5083 last_item = MLX5_FLOW_LAYER_ICMP6;
5088 item_flags |= last_item;
5091 * In case of ingress traffic when E-Switch mode is enabled,
5092 * we have two cases where we need to set the source port manually.
5093 * The first one, is in case of Nic steering rule, and the second is
5094 * E-Switch rule where no port_id item was found. In both cases
5095 * the source port is set according the current port in use.
5097 if ((attr->ingress && !(item_flags & MLX5_FLOW_ITEM_PORT_ID)) &&
5098 (priv->representor || priv->master)) {
5099 if (flow_dv_translate_item_port_id(dev, match_mask,
5103 assert(!flow_dv_check_valid_spec(matcher.mask.buf,
5104 dev_flow->dv.value.buf));
5105 dev_flow->layers = item_flags;
5106 /* Register matcher. */
5107 matcher.crc = rte_raw_cksum((const void *)matcher.mask.buf,
5109 matcher.priority = mlx5_flow_adjust_priority(dev, priority,
5111 matcher.egress = attr->egress;
5112 matcher.group = attr->group;
5113 matcher.transfer = attr->transfer;
5114 if (flow_dv_matcher_register(dev, &matcher, dev_flow, error))
5120 * Apply the flow to the NIC.
5123 * Pointer to the Ethernet device structure.
5124 * @param[in, out] flow
5125 * Pointer to flow structure.
5127 * Pointer to error structure.
5130 * 0 on success, a negative errno value otherwise and rte_errno is set.
5133 flow_dv_apply(struct rte_eth_dev *dev, struct rte_flow *flow,
5134 struct rte_flow_error *error)
5136 struct mlx5_flow_dv *dv;
5137 struct mlx5_flow *dev_flow;
5138 struct mlx5_priv *priv = dev->data->dev_private;
5142 LIST_FOREACH(dev_flow, &flow->dev_flows, next) {
5145 if (flow->actions & MLX5_FLOW_ACTION_DROP) {
5146 if (flow->transfer) {
5147 dv->actions[n++] = priv->sh->esw_drop_action;
5149 dv->hrxq = mlx5_hrxq_drop_new(dev);
5153 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
5155 "cannot get drop hash queue");
5158 dv->actions[n++] = dv->hrxq->action;
5160 } else if (flow->actions &
5161 (MLX5_FLOW_ACTION_QUEUE | MLX5_FLOW_ACTION_RSS)) {
5162 struct mlx5_hrxq *hrxq;
5164 hrxq = mlx5_hrxq_get(dev, flow->key,
5165 MLX5_RSS_HASH_KEY_LEN,
5168 flow->rss.queue_num);
5170 hrxq = mlx5_hrxq_new
5171 (dev, flow->key, MLX5_RSS_HASH_KEY_LEN,
5172 dv->hash_fields, (*flow->queue),
5173 flow->rss.queue_num,
5174 !!(dev_flow->layers &
5175 MLX5_FLOW_LAYER_TUNNEL));
5179 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
5180 "cannot get hash queue");
5184 dv->actions[n++] = dv->hrxq->action;
5187 mlx5_glue->dv_create_flow(dv->matcher->matcher_object,
5188 (void *)&dv->value, n,
5191 rte_flow_error_set(error, errno,
5192 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
5194 "hardware refuses to create flow");
5200 err = rte_errno; /* Save rte_errno before cleanup. */
5201 LIST_FOREACH(dev_flow, &flow->dev_flows, next) {
5202 struct mlx5_flow_dv *dv = &dev_flow->dv;
5204 if (flow->actions & MLX5_FLOW_ACTION_DROP)
5205 mlx5_hrxq_drop_release(dev);
5207 mlx5_hrxq_release(dev, dv->hrxq);
5211 rte_errno = err; /* Restore rte_errno. */
5216 * Release the flow matcher.
5219 * Pointer to Ethernet device.
5221 * Pointer to mlx5_flow.
5224 * 1 while a reference on it exists, 0 when freed.
5227 flow_dv_matcher_release(struct rte_eth_dev *dev,
5228 struct mlx5_flow *flow)
5230 struct mlx5_flow_dv_matcher *matcher = flow->dv.matcher;
5231 struct mlx5_priv *priv = dev->data->dev_private;
5232 struct mlx5_ibv_shared *sh = priv->sh;
5233 struct mlx5_flow_tbl_resource *tbl;
5235 assert(matcher->matcher_object);
5236 DRV_LOG(DEBUG, "port %u matcher %p: refcnt %d--",
5237 dev->data->port_id, (void *)matcher,
5238 rte_atomic32_read(&matcher->refcnt));
5239 if (rte_atomic32_dec_and_test(&matcher->refcnt)) {
5240 claim_zero(mlx5_glue->dv_destroy_flow_matcher
5241 (matcher->matcher_object));
5242 LIST_REMOVE(matcher, next);
5243 if (matcher->egress)
5244 tbl = &sh->tx_tbl[matcher->group];
5246 tbl = &sh->rx_tbl[matcher->group];
5247 flow_dv_tbl_resource_release(tbl);
5249 DRV_LOG(DEBUG, "port %u matcher %p: removed",
5250 dev->data->port_id, (void *)matcher);
5257 * Release an encap/decap resource.
5260 * Pointer to mlx5_flow.
5263 * 1 while a reference on it exists, 0 when freed.
5266 flow_dv_encap_decap_resource_release(struct mlx5_flow *flow)
5268 struct mlx5_flow_dv_encap_decap_resource *cache_resource =
5269 flow->dv.encap_decap;
5271 assert(cache_resource->verbs_action);
5272 DRV_LOG(DEBUG, "encap/decap resource %p: refcnt %d--",
5273 (void *)cache_resource,
5274 rte_atomic32_read(&cache_resource->refcnt));
5275 if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
5276 claim_zero(mlx5_glue->destroy_flow_action
5277 (cache_resource->verbs_action));
5278 LIST_REMOVE(cache_resource, next);
5279 rte_free(cache_resource);
5280 DRV_LOG(DEBUG, "encap/decap resource %p: removed",
5281 (void *)cache_resource);
5288 * Release an jump to table action resource.
5291 * Pointer to mlx5_flow.
5294 * 1 while a reference on it exists, 0 when freed.
5297 flow_dv_jump_tbl_resource_release(struct mlx5_flow *flow)
5299 struct mlx5_flow_dv_jump_tbl_resource *cache_resource =
5302 assert(cache_resource->action);
5303 DRV_LOG(DEBUG, "jump table resource %p: refcnt %d--",
5304 (void *)cache_resource,
5305 rte_atomic32_read(&cache_resource->refcnt));
5306 if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
5307 claim_zero(mlx5_glue->destroy_flow_action
5308 (cache_resource->action));
5309 LIST_REMOVE(cache_resource, next);
5310 flow_dv_tbl_resource_release(cache_resource->tbl);
5311 rte_free(cache_resource);
5312 DRV_LOG(DEBUG, "jump table resource %p: removed",
5313 (void *)cache_resource);
5320 * Release a modify-header resource.
5323 * Pointer to mlx5_flow.
5326 * 1 while a reference on it exists, 0 when freed.
5329 flow_dv_modify_hdr_resource_release(struct mlx5_flow *flow)
5331 struct mlx5_flow_dv_modify_hdr_resource *cache_resource =
5332 flow->dv.modify_hdr;
5334 assert(cache_resource->verbs_action);
5335 DRV_LOG(DEBUG, "modify-header resource %p: refcnt %d--",
5336 (void *)cache_resource,
5337 rte_atomic32_read(&cache_resource->refcnt));
5338 if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
5339 claim_zero(mlx5_glue->destroy_flow_action
5340 (cache_resource->verbs_action));
5341 LIST_REMOVE(cache_resource, next);
5342 rte_free(cache_resource);
5343 DRV_LOG(DEBUG, "modify-header resource %p: removed",
5344 (void *)cache_resource);
5351 * Release port ID action resource.
5354 * Pointer to mlx5_flow.
5357 * 1 while a reference on it exists, 0 when freed.
5360 flow_dv_port_id_action_resource_release(struct mlx5_flow *flow)
5362 struct mlx5_flow_dv_port_id_action_resource *cache_resource =
5363 flow->dv.port_id_action;
5365 assert(cache_resource->action);
5366 DRV_LOG(DEBUG, "port ID action resource %p: refcnt %d--",
5367 (void *)cache_resource,
5368 rte_atomic32_read(&cache_resource->refcnt));
5369 if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
5370 claim_zero(mlx5_glue->destroy_flow_action
5371 (cache_resource->action));
5372 LIST_REMOVE(cache_resource, next);
5373 rte_free(cache_resource);
5374 DRV_LOG(DEBUG, "port id action resource %p: removed",
5375 (void *)cache_resource);
5382 * Remove the flow from the NIC but keeps it in memory.
5385 * Pointer to Ethernet device.
5386 * @param[in, out] flow
5387 * Pointer to flow structure.
5390 flow_dv_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
5392 struct mlx5_flow_dv *dv;
5393 struct mlx5_flow *dev_flow;
5397 LIST_FOREACH(dev_flow, &flow->dev_flows, next) {
5400 claim_zero(mlx5_glue->dv_destroy_flow(dv->flow));
5404 if (flow->actions & MLX5_FLOW_ACTION_DROP)
5405 mlx5_hrxq_drop_release(dev);
5407 mlx5_hrxq_release(dev, dv->hrxq);
5414 * Remove the flow from the NIC and the memory.
5417 * Pointer to the Ethernet device structure.
5418 * @param[in, out] flow
5419 * Pointer to flow structure.
5422 flow_dv_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
5424 struct mlx5_flow *dev_flow;
5428 flow_dv_remove(dev, flow);
5429 if (flow->counter) {
5430 flow_dv_counter_release(dev, flow->counter);
5431 flow->counter = NULL;
5433 if (flow->tag_resource) {
5434 flow_dv_tag_release(dev, flow->tag_resource);
5435 flow->tag_resource = NULL;
5437 while (!LIST_EMPTY(&flow->dev_flows)) {
5438 dev_flow = LIST_FIRST(&flow->dev_flows);
5439 LIST_REMOVE(dev_flow, next);
5440 if (dev_flow->dv.matcher)
5441 flow_dv_matcher_release(dev, dev_flow);
5442 if (dev_flow->dv.encap_decap)
5443 flow_dv_encap_decap_resource_release(dev_flow);
5444 if (dev_flow->dv.modify_hdr)
5445 flow_dv_modify_hdr_resource_release(dev_flow);
5446 if (dev_flow->dv.jump)
5447 flow_dv_jump_tbl_resource_release(dev_flow);
5448 if (dev_flow->dv.port_id_action)
5449 flow_dv_port_id_action_resource_release(dev_flow);
5455 * Query a dv flow rule for its statistics via devx.
5458 * Pointer to Ethernet device.
5460 * Pointer to the sub flow.
5462 * data retrieved by the query.
5464 * Perform verbose error reporting if not NULL.
5467 * 0 on success, a negative errno value otherwise and rte_errno is set.
5470 flow_dv_query_count(struct rte_eth_dev *dev, struct rte_flow *flow,
5471 void *data, struct rte_flow_error *error)
5473 struct mlx5_priv *priv = dev->data->dev_private;
5474 struct rte_flow_query_count *qc = data;
5476 if (!priv->config.devx)
5477 return rte_flow_error_set(error, ENOTSUP,
5478 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
5480 "counters are not supported");
5481 if (flow->counter) {
5482 uint64_t pkts, bytes;
5483 int err = _flow_dv_query_count(dev, flow->counter, &pkts,
5487 return rte_flow_error_set(error, -err,
5488 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
5489 NULL, "cannot read counters");
5492 qc->hits = pkts - flow->counter->hits;
5493 qc->bytes = bytes - flow->counter->bytes;
5495 flow->counter->hits = pkts;
5496 flow->counter->bytes = bytes;
5500 return rte_flow_error_set(error, EINVAL,
5501 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
5503 "counters are not available");
5509 * @see rte_flow_query()
5513 flow_dv_query(struct rte_eth_dev *dev,
5514 struct rte_flow *flow __rte_unused,
5515 const struct rte_flow_action *actions __rte_unused,
5516 void *data __rte_unused,
5517 struct rte_flow_error *error __rte_unused)
5521 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
5522 switch (actions->type) {
5523 case RTE_FLOW_ACTION_TYPE_VOID:
5525 case RTE_FLOW_ACTION_TYPE_COUNT:
5526 ret = flow_dv_query_count(dev, flow, data, error);
5529 return rte_flow_error_set(error, ENOTSUP,
5530 RTE_FLOW_ERROR_TYPE_ACTION,
5532 "action not supported");
5539 * Mutex-protected thunk to flow_dv_translate().
5542 flow_d_translate(struct rte_eth_dev *dev,
5543 struct mlx5_flow *dev_flow,
5544 const struct rte_flow_attr *attr,
5545 const struct rte_flow_item items[],
5546 const struct rte_flow_action actions[],
5547 struct rte_flow_error *error)
5551 flow_d_shared_lock(dev);
5552 ret = flow_dv_translate(dev, dev_flow, attr, items, actions, error);
5553 flow_d_shared_unlock(dev);
5558 * Mutex-protected thunk to flow_dv_apply().
5561 flow_d_apply(struct rte_eth_dev *dev,
5562 struct rte_flow *flow,
5563 struct rte_flow_error *error)
5567 flow_d_shared_lock(dev);
5568 ret = flow_dv_apply(dev, flow, error);
5569 flow_d_shared_unlock(dev);
5574 * Mutex-protected thunk to flow_dv_remove().
5577 flow_d_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
5579 flow_d_shared_lock(dev);
5580 flow_dv_remove(dev, flow);
5581 flow_d_shared_unlock(dev);
5585 * Mutex-protected thunk to flow_dv_destroy().
5588 flow_d_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
5590 flow_d_shared_lock(dev);
5591 flow_dv_destroy(dev, flow);
5592 flow_d_shared_unlock(dev);
5595 const struct mlx5_flow_driver_ops mlx5_flow_dv_drv_ops = {
5596 .validate = flow_dv_validate,
5597 .prepare = flow_dv_prepare,
5598 .translate = flow_d_translate,
5599 .apply = flow_d_apply,
5600 .remove = flow_d_remove,
5601 .destroy = flow_d_destroy,
5602 .query = flow_dv_query,
5605 #endif /* HAVE_IBV_FLOW_DV_SUPPORT */