1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2018 Mellanox Technologies, Ltd
5 #include <netinet/in.h>
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>
29 #include <mlx5_glue.h>
32 #include "mlx5_defs.h"
34 #include "mlx5_flow.h"
35 #include "mlx5_rxtx.h"
37 #define VERBS_SPEC_INNER(item_flags) \
38 (!!((item_flags) & MLX5_FLOW_LAYER_TUNNEL) ? IBV_FLOW_SPEC_INNER : 0)
41 * Get Verbs flow counter by index.
44 * Pointer to the Ethernet device structure.
46 * mlx5 flow counter index in the container.
48 * mlx5 flow counter pool in the container,
51 * A pointer to the counter, NULL otherwise.
53 static struct mlx5_flow_counter *
54 flow_verbs_counter_get_by_idx(struct rte_eth_dev *dev,
56 struct mlx5_flow_counter_pool **ppool)
58 struct mlx5_priv *priv = dev->data->dev_private;
59 struct mlx5_pools_container *cont = MLX5_CNT_CONTAINER(priv->sh, 0, 0,
61 struct mlx5_flow_counter_pool *pool;
64 pool = cont->pools[idx / MLX5_COUNTERS_PER_POOL];
68 return MLX5_POOL_GET_CNT(pool, idx % MLX5_COUNTERS_PER_POOL);
72 * Create Verbs flow counter with Verbs library.
75 * Pointer to the Ethernet device structure.
76 * @param[in, out] counter
77 * mlx5 flow counter object, contains the counter id,
78 * handle of created Verbs flow counter is returned
79 * in cs field (if counters are supported).
82 * 0 On success else a negative errno value is returned
83 * and rte_errno is set.
86 flow_verbs_counter_create(struct rte_eth_dev *dev,
87 struct mlx5_flow_counter_ext *counter)
89 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42)
90 struct mlx5_priv *priv = dev->data->dev_private;
91 struct ibv_context *ctx = priv->sh->ctx;
92 struct ibv_counter_set_init_attr init = {
93 .counter_set_id = counter->id};
95 counter->cs = mlx5_glue->create_counter_set(ctx, &init);
101 #elif defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
102 struct mlx5_priv *priv = dev->data->dev_private;
103 struct ibv_context *ctx = priv->sh->ctx;
104 struct ibv_counters_init_attr init = {0};
105 struct ibv_counter_attach_attr attach;
108 memset(&attach, 0, sizeof(attach));
109 counter->cs = mlx5_glue->create_counters(ctx, &init);
114 attach.counter_desc = IBV_COUNTER_PACKETS;
116 ret = mlx5_glue->attach_counters(counter->cs, &attach, NULL);
118 attach.counter_desc = IBV_COUNTER_BYTES;
120 ret = mlx5_glue->attach_counters
121 (counter->cs, &attach, NULL);
124 claim_zero(mlx5_glue->destroy_counters(counter->cs));
139 * Get a flow counter.
142 * Pointer to the Ethernet device structure.
144 * Indicate if this counter is shared with other flows.
146 * Counter identifier.
149 * Index to the counter, 0 otherwise and rte_errno is set.
152 flow_verbs_counter_new(struct rte_eth_dev *dev, uint32_t shared, uint32_t id)
154 struct mlx5_priv *priv = dev->data->dev_private;
155 struct mlx5_pools_container *cont = MLX5_CNT_CONTAINER(priv->sh, 0, 0,
157 struct mlx5_flow_counter_pool *pool = NULL;
158 struct mlx5_flow_counter_ext *cnt_ext = NULL;
159 struct mlx5_flow_counter *cnt = NULL;
160 uint32_t n_valid = rte_atomic16_read(&cont->n_valid);
166 for (pool_idx = 0; pool_idx < n_valid; ++pool_idx) {
167 pool = cont->pools[pool_idx];
168 for (i = 0; i < MLX5_COUNTERS_PER_POOL; ++i) {
169 cnt_ext = MLX5_GET_POOL_CNT_EXT(pool, i);
170 if (cnt_ext->shared && cnt_ext->id == id) {
172 return MLX5_MAKE_CNT_IDX(pool_idx, i);
177 for (pool_idx = 0; pool_idx < n_valid; ++pool_idx) {
178 pool = cont->pools[pool_idx];
181 cnt = TAILQ_FIRST(&pool->counters);
186 struct mlx5_flow_counter_pool **pools;
189 if (n_valid == cont->n) {
190 /* Resize the container pool array. */
191 size = sizeof(struct mlx5_flow_counter_pool *) *
192 (n_valid + MLX5_CNT_CONTAINER_RESIZE);
193 pools = rte_zmalloc(__func__, size, 0);
197 memcpy(pools, cont->pools,
198 sizeof(struct mlx5_flow_counter_pool *) *
200 rte_free(cont->pools);
203 cont->n += MLX5_CNT_CONTAINER_RESIZE;
205 /* Allocate memory for new pool*/
206 size = sizeof(*pool) + sizeof(*cnt_ext) *
207 MLX5_COUNTERS_PER_POOL;
208 pool = rte_calloc(__func__, 1, size, 0);
211 for (i = 0; i < MLX5_COUNTERS_PER_POOL; ++i) {
212 cnt = MLX5_POOL_GET_CNT(pool, i);
213 TAILQ_INSERT_HEAD(&pool->counters, cnt, next);
215 cnt = MLX5_POOL_GET_CNT(pool, 0);
216 cont->pools[n_valid] = pool;
218 rte_atomic16_add(&cont->n_valid, 1);
219 TAILQ_INSERT_HEAD(&cont->pool_list, pool, next);
221 i = MLX5_CNT_ARRAY_IDX(pool, cnt);
222 cnt_ext = MLX5_GET_POOL_CNT_EXT(pool, i);
224 cnt_ext->shared = shared;
225 cnt_ext->ref_cnt = 1;
228 /* Create counter with Verbs. */
229 ret = flow_verbs_counter_create(dev, cnt_ext);
231 TAILQ_REMOVE(&pool->counters, cnt, next);
232 return MLX5_MAKE_CNT_IDX(pool_idx, i);
234 /* Some error occurred in Verbs library. */
240 * Release a flow counter.
243 * Pointer to the Ethernet device structure.
245 * Index to the counter handler.
248 flow_verbs_counter_release(struct rte_eth_dev *dev, uint32_t counter)
250 struct mlx5_flow_counter_pool *pool;
251 struct mlx5_flow_counter *cnt;
252 struct mlx5_flow_counter_ext *cnt_ext;
254 cnt = flow_verbs_counter_get_by_idx(dev, counter,
256 cnt_ext = MLX5_CNT_TO_CNT_EXT(pool, cnt);
257 if (--cnt_ext->ref_cnt == 0) {
258 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42)
259 claim_zero(mlx5_glue->destroy_counter_set(cnt_ext->cs));
261 #elif defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
262 claim_zero(mlx5_glue->destroy_counters(cnt_ext->cs));
265 TAILQ_INSERT_HEAD(&pool->counters, cnt, next);
270 * Query a flow counter via Verbs library call.
272 * @see rte_flow_query()
276 flow_verbs_counter_query(struct rte_eth_dev *dev __rte_unused,
277 struct rte_flow *flow, void *data,
278 struct rte_flow_error *error)
280 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42) || \
281 defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
283 struct mlx5_flow_counter_pool *pool;
284 struct mlx5_flow_counter *cnt = flow_verbs_counter_get_by_idx
285 (dev, flow->counter, &pool);
286 struct mlx5_flow_counter_ext *cnt_ext = MLX5_CNT_TO_CNT_EXT
288 struct rte_flow_query_count *qc = data;
289 uint64_t counters[2] = {0, 0};
290 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42)
291 struct ibv_query_counter_set_attr query_cs_attr = {
293 .query_flags = IBV_COUNTER_SET_FORCE_UPDATE,
295 struct ibv_counter_set_data query_out = {
297 .outlen = 2 * sizeof(uint64_t),
299 int err = mlx5_glue->query_counter_set(&query_cs_attr,
301 #elif defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
302 int err = mlx5_glue->query_counters
303 (cnt_ext->cs, counters,
305 IBV_READ_COUNTERS_ATTR_PREFER_CACHED);
308 return rte_flow_error_set
310 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
312 "cannot read counter");
315 qc->hits = counters[0] - cnt->hits;
316 qc->bytes = counters[1] - cnt->bytes;
318 cnt->hits = counters[0];
319 cnt->bytes = counters[1];
323 return rte_flow_error_set(error, EINVAL,
324 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
326 "flow does not have counter");
330 return rte_flow_error_set(error, ENOTSUP,
331 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
333 "counters are not available");
338 * Add a verbs item specification into @p verbs.
341 * Pointer to verbs structure.
343 * Create specification.
345 * Size in bytes of the specification to copy.
348 flow_verbs_spec_add(struct mlx5_flow_verbs_workspace *verbs,
349 void *src, unsigned int size)
355 MLX5_ASSERT(verbs->specs);
356 dst = (void *)(verbs->specs + verbs->size);
357 memcpy(dst, src, size);
358 ++verbs->attr.num_of_specs;
363 * Convert the @p item into a Verbs specification. This function assumes that
364 * the input is valid and that there is space to insert the requested item
367 * @param[in, out] dev_flow
368 * Pointer to dev_flow structure.
370 * Item specification.
371 * @param[in] item_flags
375 flow_verbs_translate_item_eth(struct mlx5_flow *dev_flow,
376 const struct rte_flow_item *item,
379 const struct rte_flow_item_eth *spec = item->spec;
380 const struct rte_flow_item_eth *mask = item->mask;
381 const unsigned int size = sizeof(struct ibv_flow_spec_eth);
382 struct ibv_flow_spec_eth eth = {
383 .type = IBV_FLOW_SPEC_ETH | VERBS_SPEC_INNER(item_flags),
388 mask = &rte_flow_item_eth_mask;
392 memcpy(ð.val.dst_mac, spec->dst.addr_bytes,
394 memcpy(ð.val.src_mac, spec->src.addr_bytes,
396 eth.val.ether_type = spec->type;
397 memcpy(ð.mask.dst_mac, mask->dst.addr_bytes,
399 memcpy(ð.mask.src_mac, mask->src.addr_bytes,
401 eth.mask.ether_type = mask->type;
402 /* Remove unwanted bits from values. */
403 for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i) {
404 eth.val.dst_mac[i] &= eth.mask.dst_mac[i];
405 eth.val.src_mac[i] &= eth.mask.src_mac[i];
407 eth.val.ether_type &= eth.mask.ether_type;
409 flow_verbs_spec_add(&dev_flow->verbs, ð, size);
413 * Update the VLAN tag in the Verbs Ethernet specification.
414 * This function assumes that the input is valid and there is space to add
415 * the requested item.
417 * @param[in, out] attr
418 * Pointer to Verbs attributes structure.
420 * Verbs structure containing the VLAN information to copy.
423 flow_verbs_item_vlan_update(struct ibv_flow_attr *attr,
424 struct ibv_flow_spec_eth *eth)
427 const enum ibv_flow_spec_type search = eth->type;
428 struct ibv_spec_header *hdr = (struct ibv_spec_header *)
429 ((uint8_t *)attr + sizeof(struct ibv_flow_attr));
431 for (i = 0; i != attr->num_of_specs; ++i) {
432 if (hdr->type == search) {
433 struct ibv_flow_spec_eth *e =
434 (struct ibv_flow_spec_eth *)hdr;
436 e->val.vlan_tag = eth->val.vlan_tag;
437 e->mask.vlan_tag = eth->mask.vlan_tag;
438 e->val.ether_type = eth->val.ether_type;
439 e->mask.ether_type = eth->mask.ether_type;
442 hdr = (struct ibv_spec_header *)((uint8_t *)hdr + hdr->size);
447 * Convert the @p item into a Verbs specification. This function assumes that
448 * the input is valid and that there is space to insert the requested item
451 * @param[in, out] dev_flow
452 * Pointer to dev_flow structure.
454 * Item specification.
455 * @param[in] item_flags
459 flow_verbs_translate_item_vlan(struct mlx5_flow *dev_flow,
460 const struct rte_flow_item *item,
463 const struct rte_flow_item_vlan *spec = item->spec;
464 const struct rte_flow_item_vlan *mask = item->mask;
465 unsigned int size = sizeof(struct ibv_flow_spec_eth);
466 const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
467 struct ibv_flow_spec_eth eth = {
468 .type = IBV_FLOW_SPEC_ETH | VERBS_SPEC_INNER(item_flags),
471 const uint32_t l2m = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
472 MLX5_FLOW_LAYER_OUTER_L2;
475 mask = &rte_flow_item_vlan_mask;
477 eth.val.vlan_tag = spec->tci;
478 eth.mask.vlan_tag = mask->tci;
479 eth.val.vlan_tag &= eth.mask.vlan_tag;
480 eth.val.ether_type = spec->inner_type;
481 eth.mask.ether_type = mask->inner_type;
482 eth.val.ether_type &= eth.mask.ether_type;
484 if (!(item_flags & l2m))
485 flow_verbs_spec_add(&dev_flow->verbs, ð, size);
487 flow_verbs_item_vlan_update(&dev_flow->verbs.attr, ð);
489 dev_flow->handle->vf_vlan.tag =
490 rte_be_to_cpu_16(spec->tci) & 0x0fff;
494 * Convert the @p item into a Verbs specification. This function assumes that
495 * the input is valid and that there is space to insert the requested item
498 * @param[in, out] dev_flow
499 * Pointer to dev_flow structure.
501 * Item specification.
502 * @param[in] item_flags
506 flow_verbs_translate_item_ipv4(struct mlx5_flow *dev_flow,
507 const struct rte_flow_item *item,
510 const struct rte_flow_item_ipv4 *spec = item->spec;
511 const struct rte_flow_item_ipv4 *mask = item->mask;
512 unsigned int size = sizeof(struct ibv_flow_spec_ipv4_ext);
513 struct ibv_flow_spec_ipv4_ext ipv4 = {
514 .type = IBV_FLOW_SPEC_IPV4_EXT | VERBS_SPEC_INNER(item_flags),
519 mask = &rte_flow_item_ipv4_mask;
521 ipv4.val = (struct ibv_flow_ipv4_ext_filter){
522 .src_ip = spec->hdr.src_addr,
523 .dst_ip = spec->hdr.dst_addr,
524 .proto = spec->hdr.next_proto_id,
525 .tos = spec->hdr.type_of_service,
527 ipv4.mask = (struct ibv_flow_ipv4_ext_filter){
528 .src_ip = mask->hdr.src_addr,
529 .dst_ip = mask->hdr.dst_addr,
530 .proto = mask->hdr.next_proto_id,
531 .tos = mask->hdr.type_of_service,
533 /* Remove unwanted bits from values. */
534 ipv4.val.src_ip &= ipv4.mask.src_ip;
535 ipv4.val.dst_ip &= ipv4.mask.dst_ip;
536 ipv4.val.proto &= ipv4.mask.proto;
537 ipv4.val.tos &= ipv4.mask.tos;
539 flow_verbs_spec_add(&dev_flow->verbs, &ipv4, size);
543 * Convert the @p item into a Verbs specification. This function assumes that
544 * the input is valid and that there is space to insert the requested item
547 * @param[in, out] dev_flow
548 * Pointer to dev_flow structure.
550 * Item specification.
551 * @param[in] item_flags
555 flow_verbs_translate_item_ipv6(struct mlx5_flow *dev_flow,
556 const struct rte_flow_item *item,
559 const struct rte_flow_item_ipv6 *spec = item->spec;
560 const struct rte_flow_item_ipv6 *mask = item->mask;
561 unsigned int size = sizeof(struct ibv_flow_spec_ipv6);
562 struct ibv_flow_spec_ipv6 ipv6 = {
563 .type = IBV_FLOW_SPEC_IPV6 | VERBS_SPEC_INNER(item_flags),
568 mask = &rte_flow_item_ipv6_mask;
571 uint32_t vtc_flow_val;
572 uint32_t vtc_flow_mask;
574 memcpy(&ipv6.val.src_ip, spec->hdr.src_addr,
575 RTE_DIM(ipv6.val.src_ip));
576 memcpy(&ipv6.val.dst_ip, spec->hdr.dst_addr,
577 RTE_DIM(ipv6.val.dst_ip));
578 memcpy(&ipv6.mask.src_ip, mask->hdr.src_addr,
579 RTE_DIM(ipv6.mask.src_ip));
580 memcpy(&ipv6.mask.dst_ip, mask->hdr.dst_addr,
581 RTE_DIM(ipv6.mask.dst_ip));
582 vtc_flow_val = rte_be_to_cpu_32(spec->hdr.vtc_flow);
583 vtc_flow_mask = rte_be_to_cpu_32(mask->hdr.vtc_flow);
584 ipv6.val.flow_label =
585 rte_cpu_to_be_32((vtc_flow_val & RTE_IPV6_HDR_FL_MASK) >>
586 RTE_IPV6_HDR_FL_SHIFT);
587 ipv6.val.traffic_class = (vtc_flow_val & RTE_IPV6_HDR_TC_MASK) >>
588 RTE_IPV6_HDR_TC_SHIFT;
589 ipv6.val.next_hdr = spec->hdr.proto;
590 ipv6.mask.flow_label =
591 rte_cpu_to_be_32((vtc_flow_mask & RTE_IPV6_HDR_FL_MASK) >>
592 RTE_IPV6_HDR_FL_SHIFT);
593 ipv6.mask.traffic_class = (vtc_flow_mask & RTE_IPV6_HDR_TC_MASK) >>
594 RTE_IPV6_HDR_TC_SHIFT;
595 ipv6.mask.next_hdr = mask->hdr.proto;
596 /* Remove unwanted bits from values. */
597 for (i = 0; i < RTE_DIM(ipv6.val.src_ip); ++i) {
598 ipv6.val.src_ip[i] &= ipv6.mask.src_ip[i];
599 ipv6.val.dst_ip[i] &= ipv6.mask.dst_ip[i];
601 ipv6.val.flow_label &= ipv6.mask.flow_label;
602 ipv6.val.traffic_class &= ipv6.mask.traffic_class;
603 ipv6.val.next_hdr &= ipv6.mask.next_hdr;
605 flow_verbs_spec_add(&dev_flow->verbs, &ipv6, size);
609 * Convert the @p item into a Verbs specification. This function assumes that
610 * the input is valid and that there is space to insert the requested item
613 * @param[in, out] dev_flow
614 * Pointer to dev_flow structure.
616 * Item specification.
617 * @param[in] item_flags
621 flow_verbs_translate_item_tcp(struct mlx5_flow *dev_flow,
622 const struct rte_flow_item *item,
623 uint64_t item_flags __rte_unused)
625 const struct rte_flow_item_tcp *spec = item->spec;
626 const struct rte_flow_item_tcp *mask = item->mask;
627 unsigned int size = sizeof(struct ibv_flow_spec_tcp_udp);
628 struct ibv_flow_spec_tcp_udp tcp = {
629 .type = IBV_FLOW_SPEC_TCP | VERBS_SPEC_INNER(item_flags),
634 mask = &rte_flow_item_tcp_mask;
636 tcp.val.dst_port = spec->hdr.dst_port;
637 tcp.val.src_port = spec->hdr.src_port;
638 tcp.mask.dst_port = mask->hdr.dst_port;
639 tcp.mask.src_port = mask->hdr.src_port;
640 /* Remove unwanted bits from values. */
641 tcp.val.src_port &= tcp.mask.src_port;
642 tcp.val.dst_port &= tcp.mask.dst_port;
644 flow_verbs_spec_add(&dev_flow->verbs, &tcp, size);
648 * Convert the @p item into a Verbs specification. This function assumes that
649 * the input is valid and that there is space to insert the requested item
652 * @param[in, out] dev_flow
653 * Pointer to dev_flow structure.
655 * Item specification.
656 * @param[in] item_flags
660 flow_verbs_translate_item_udp(struct mlx5_flow *dev_flow,
661 const struct rte_flow_item *item,
662 uint64_t item_flags __rte_unused)
664 const struct rte_flow_item_udp *spec = item->spec;
665 const struct rte_flow_item_udp *mask = item->mask;
666 unsigned int size = sizeof(struct ibv_flow_spec_tcp_udp);
667 struct ibv_flow_spec_tcp_udp udp = {
668 .type = IBV_FLOW_SPEC_UDP | VERBS_SPEC_INNER(item_flags),
673 mask = &rte_flow_item_udp_mask;
675 udp.val.dst_port = spec->hdr.dst_port;
676 udp.val.src_port = spec->hdr.src_port;
677 udp.mask.dst_port = mask->hdr.dst_port;
678 udp.mask.src_port = mask->hdr.src_port;
679 /* Remove unwanted bits from values. */
680 udp.val.src_port &= udp.mask.src_port;
681 udp.val.dst_port &= udp.mask.dst_port;
683 flow_verbs_spec_add(&dev_flow->verbs, &udp, size);
687 * Convert the @p item into a Verbs specification. This function assumes that
688 * the input is valid and that there is space to insert the requested item
691 * @param[in, out] dev_flow
692 * Pointer to dev_flow structure.
694 * Item specification.
695 * @param[in] item_flags
699 flow_verbs_translate_item_vxlan(struct mlx5_flow *dev_flow,
700 const struct rte_flow_item *item,
701 uint64_t item_flags __rte_unused)
703 const struct rte_flow_item_vxlan *spec = item->spec;
704 const struct rte_flow_item_vxlan *mask = item->mask;
705 unsigned int size = sizeof(struct ibv_flow_spec_tunnel);
706 struct ibv_flow_spec_tunnel vxlan = {
707 .type = IBV_FLOW_SPEC_VXLAN_TUNNEL,
713 } id = { .vlan_id = 0, };
716 mask = &rte_flow_item_vxlan_mask;
718 memcpy(&id.vni[1], spec->vni, 3);
719 vxlan.val.tunnel_id = id.vlan_id;
720 memcpy(&id.vni[1], mask->vni, 3);
721 vxlan.mask.tunnel_id = id.vlan_id;
722 /* Remove unwanted bits from values. */
723 vxlan.val.tunnel_id &= vxlan.mask.tunnel_id;
725 flow_verbs_spec_add(&dev_flow->verbs, &vxlan, size);
729 * Convert the @p item into a Verbs specification. This function assumes that
730 * the input is valid and that there is space to insert the requested item
733 * @param[in, out] dev_flow
734 * Pointer to dev_flow structure.
736 * Item specification.
737 * @param[in] item_flags
741 flow_verbs_translate_item_vxlan_gpe(struct mlx5_flow *dev_flow,
742 const struct rte_flow_item *item,
743 uint64_t item_flags __rte_unused)
745 const struct rte_flow_item_vxlan_gpe *spec = item->spec;
746 const struct rte_flow_item_vxlan_gpe *mask = item->mask;
747 unsigned int size = sizeof(struct ibv_flow_spec_tunnel);
748 struct ibv_flow_spec_tunnel vxlan_gpe = {
749 .type = IBV_FLOW_SPEC_VXLAN_TUNNEL,
755 } id = { .vlan_id = 0, };
758 mask = &rte_flow_item_vxlan_gpe_mask;
760 memcpy(&id.vni[1], spec->vni, 3);
761 vxlan_gpe.val.tunnel_id = id.vlan_id;
762 memcpy(&id.vni[1], mask->vni, 3);
763 vxlan_gpe.mask.tunnel_id = id.vlan_id;
764 /* Remove unwanted bits from values. */
765 vxlan_gpe.val.tunnel_id &= vxlan_gpe.mask.tunnel_id;
767 flow_verbs_spec_add(&dev_flow->verbs, &vxlan_gpe, size);
771 * Update the protocol in Verbs IPv4/IPv6 spec.
773 * @param[in, out] attr
774 * Pointer to Verbs attributes structure.
776 * Specification type to search in order to update the IP protocol.
777 * @param[in] protocol
778 * Protocol value to set if none is present in the specification.
781 flow_verbs_item_gre_ip_protocol_update(struct ibv_flow_attr *attr,
782 enum ibv_flow_spec_type search,
786 struct ibv_spec_header *hdr = (struct ibv_spec_header *)
787 ((uint8_t *)attr + sizeof(struct ibv_flow_attr));
791 for (i = 0; i != attr->num_of_specs; ++i) {
792 if (hdr->type == search) {
794 struct ibv_flow_spec_ipv4_ext *ipv4;
795 struct ibv_flow_spec_ipv6 *ipv6;
799 case IBV_FLOW_SPEC_IPV4_EXT:
800 ip.ipv4 = (struct ibv_flow_spec_ipv4_ext *)hdr;
801 if (!ip.ipv4->val.proto) {
802 ip.ipv4->val.proto = protocol;
803 ip.ipv4->mask.proto = 0xff;
806 case IBV_FLOW_SPEC_IPV6:
807 ip.ipv6 = (struct ibv_flow_spec_ipv6 *)hdr;
808 if (!ip.ipv6->val.next_hdr) {
809 ip.ipv6->val.next_hdr = protocol;
810 ip.ipv6->mask.next_hdr = 0xff;
818 hdr = (struct ibv_spec_header *)((uint8_t *)hdr + hdr->size);
823 * Convert the @p item into a Verbs specification. This function assumes that
824 * the input is valid and that there is space to insert the requested item
827 * @param[in, out] dev_flow
828 * Pointer to dev_flow structure.
830 * Item specification.
831 * @param[in] item_flags
835 flow_verbs_translate_item_gre(struct mlx5_flow *dev_flow,
836 const struct rte_flow_item *item __rte_unused,
839 struct mlx5_flow_verbs_workspace *verbs = &dev_flow->verbs;
840 #ifndef HAVE_IBV_DEVICE_MPLS_SUPPORT
841 unsigned int size = sizeof(struct ibv_flow_spec_tunnel);
842 struct ibv_flow_spec_tunnel tunnel = {
843 .type = IBV_FLOW_SPEC_VXLAN_TUNNEL,
847 const struct rte_flow_item_gre *spec = item->spec;
848 const struct rte_flow_item_gre *mask = item->mask;
849 unsigned int size = sizeof(struct ibv_flow_spec_gre);
850 struct ibv_flow_spec_gre tunnel = {
851 .type = IBV_FLOW_SPEC_GRE,
856 mask = &rte_flow_item_gre_mask;
858 tunnel.val.c_ks_res0_ver = spec->c_rsvd0_ver;
859 tunnel.val.protocol = spec->protocol;
860 tunnel.mask.c_ks_res0_ver = mask->c_rsvd0_ver;
861 tunnel.mask.protocol = mask->protocol;
862 /* Remove unwanted bits from values. */
863 tunnel.val.c_ks_res0_ver &= tunnel.mask.c_ks_res0_ver;
864 tunnel.val.protocol &= tunnel.mask.protocol;
865 tunnel.val.key &= tunnel.mask.key;
868 if (item_flags & MLX5_FLOW_LAYER_OUTER_L3_IPV4)
869 flow_verbs_item_gre_ip_protocol_update(&verbs->attr,
870 IBV_FLOW_SPEC_IPV4_EXT,
873 flow_verbs_item_gre_ip_protocol_update(&verbs->attr,
876 flow_verbs_spec_add(verbs, &tunnel, size);
880 * Convert the @p action into a Verbs specification. This function assumes that
881 * the input is valid and that there is space to insert the requested action
882 * into the flow. This function also return the action that was added.
884 * @param[in, out] dev_flow
885 * Pointer to dev_flow structure.
887 * Item specification.
888 * @param[in] item_flags
892 flow_verbs_translate_item_mpls(struct mlx5_flow *dev_flow __rte_unused,
893 const struct rte_flow_item *item __rte_unused,
894 uint64_t item_flags __rte_unused)
896 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
897 const struct rte_flow_item_mpls *spec = item->spec;
898 const struct rte_flow_item_mpls *mask = item->mask;
899 unsigned int size = sizeof(struct ibv_flow_spec_mpls);
900 struct ibv_flow_spec_mpls mpls = {
901 .type = IBV_FLOW_SPEC_MPLS,
906 mask = &rte_flow_item_mpls_mask;
908 memcpy(&mpls.val.label, spec, sizeof(mpls.val.label));
909 memcpy(&mpls.mask.label, mask, sizeof(mpls.mask.label));
910 /* Remove unwanted bits from values. */
911 mpls.val.label &= mpls.mask.label;
913 flow_verbs_spec_add(&dev_flow->verbs, &mpls, size);
918 * Convert the @p action into a Verbs specification. This function assumes that
919 * the input is valid and that there is space to insert the requested action
922 * @param[in] dev_flow
923 * Pointer to mlx5_flow.
925 * Action configuration.
928 flow_verbs_translate_action_drop
929 (struct mlx5_flow *dev_flow,
930 const struct rte_flow_action *action __rte_unused)
932 unsigned int size = sizeof(struct ibv_flow_spec_action_drop);
933 struct ibv_flow_spec_action_drop drop = {
934 .type = IBV_FLOW_SPEC_ACTION_DROP,
938 flow_verbs_spec_add(&dev_flow->verbs, &drop, size);
942 * Convert the @p action into a Verbs specification. This function assumes that
943 * the input is valid and that there is space to insert the requested action
946 * @param[in] rss_desc
947 * Pointer to mlx5_flow_rss_desc.
949 * Action configuration.
952 flow_verbs_translate_action_queue(struct mlx5_flow_rss_desc *rss_desc,
953 const struct rte_flow_action *action)
955 const struct rte_flow_action_queue *queue = action->conf;
957 rss_desc->queue[0] = queue->index;
958 rss_desc->queue_num = 1;
962 * Convert the @p action into a Verbs specification. This function assumes that
963 * the input is valid and that there is space to insert the requested action
966 * @param[in] rss_desc
967 * Pointer to mlx5_flow_rss_desc.
969 * Action configuration.
972 flow_verbs_translate_action_rss(struct mlx5_flow_rss_desc *rss_desc,
973 const struct rte_flow_action *action)
975 const struct rte_flow_action_rss *rss = action->conf;
976 const uint8_t *rss_key;
978 memcpy(rss_desc->queue, rss->queue, rss->queue_num * sizeof(uint16_t));
979 rss_desc->queue_num = rss->queue_num;
980 /* NULL RSS key indicates default RSS key. */
981 rss_key = !rss->key ? rss_hash_default_key : rss->key;
982 memcpy(rss_desc->key, rss_key, MLX5_RSS_HASH_KEY_LEN);
984 * rss->level and rss.types should be set in advance when expanding
990 * Convert the @p action into a Verbs specification. This function assumes that
991 * the input is valid and that there is space to insert the requested action
994 * @param[in] dev_flow
995 * Pointer to mlx5_flow.
997 * Action configuration.
1000 flow_verbs_translate_action_flag
1001 (struct mlx5_flow *dev_flow,
1002 const struct rte_flow_action *action __rte_unused)
1004 unsigned int size = sizeof(struct ibv_flow_spec_action_tag);
1005 struct ibv_flow_spec_action_tag tag = {
1006 .type = IBV_FLOW_SPEC_ACTION_TAG,
1008 .tag_id = mlx5_flow_mark_set(MLX5_FLOW_MARK_DEFAULT),
1011 flow_verbs_spec_add(&dev_flow->verbs, &tag, size);
1015 * Convert the @p action into a Verbs specification. This function assumes that
1016 * the input is valid and that there is space to insert the requested action
1019 * @param[in] dev_flow
1020 * Pointer to mlx5_flow.
1022 * Action configuration.
1025 flow_verbs_translate_action_mark(struct mlx5_flow *dev_flow,
1026 const struct rte_flow_action *action)
1028 const struct rte_flow_action_mark *mark = action->conf;
1029 unsigned int size = sizeof(struct ibv_flow_spec_action_tag);
1030 struct ibv_flow_spec_action_tag tag = {
1031 .type = IBV_FLOW_SPEC_ACTION_TAG,
1033 .tag_id = mlx5_flow_mark_set(mark->id),
1036 flow_verbs_spec_add(&dev_flow->verbs, &tag, size);
1040 * Convert the @p action into a Verbs specification. This function assumes that
1041 * the input is valid and that there is space to insert the requested action
1045 * Pointer to the Ethernet device structure.
1047 * Action configuration.
1048 * @param[in] dev_flow
1049 * Pointer to mlx5_flow.
1051 * Pointer to error structure.
1054 * 0 On success else a negative errno value is returned and rte_errno is set.
1057 flow_verbs_translate_action_count(struct mlx5_flow *dev_flow,
1058 const struct rte_flow_action *action,
1059 struct rte_eth_dev *dev,
1060 struct rte_flow_error *error)
1062 const struct rte_flow_action_count *count = action->conf;
1063 struct rte_flow *flow = dev_flow->flow;
1064 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42) || \
1065 defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
1066 struct mlx5_flow_counter_pool *pool;
1067 struct mlx5_flow_counter *cnt = NULL;
1068 struct mlx5_flow_counter_ext *cnt_ext;
1069 unsigned int size = sizeof(struct ibv_flow_spec_counter_action);
1070 struct ibv_flow_spec_counter_action counter = {
1071 .type = IBV_FLOW_SPEC_ACTION_COUNT,
1076 if (!flow->counter) {
1077 flow->counter = flow_verbs_counter_new(dev, count->shared,
1080 return rte_flow_error_set(error, rte_errno,
1081 RTE_FLOW_ERROR_TYPE_ACTION,
1083 "cannot get counter"
1086 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42)
1087 cnt = flow_verbs_counter_get_by_idx(dev, flow->counter, &pool);
1088 cnt_ext = MLX5_CNT_TO_CNT_EXT(pool, cnt);
1089 counter.counter_set_handle = cnt_ext->cs->handle;
1090 flow_verbs_spec_add(&dev_flow->verbs, &counter, size);
1091 #elif defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
1092 cnt = flow_verbs_counter_get_by_idx(dev, flow->counter, &pool);
1093 cnt_ext = MLX5_CNT_TO_CNT_EXT(pool, cnt);
1094 counter.counters = cnt_ext->cs;
1095 flow_verbs_spec_add(&dev_flow->verbs, &counter, size);
1101 * Internal validation function. For validating both actions and items.
1104 * Pointer to the Ethernet device structure.
1106 * Pointer to the flow attributes.
1108 * Pointer to the list of items.
1109 * @param[in] actions
1110 * Pointer to the list of actions.
1111 * @param[in] external
1112 * This flow rule is created by request external to PMD.
1113 * @param[in] hairpin
1114 * Number of hairpin TX actions, 0 means classic flow.
1116 * Pointer to the error structure.
1119 * 0 on success, a negative errno value otherwise and rte_errno is set.
1122 flow_verbs_validate(struct rte_eth_dev *dev,
1123 const struct rte_flow_attr *attr,
1124 const struct rte_flow_item items[],
1125 const struct rte_flow_action actions[],
1126 bool external __rte_unused,
1127 int hairpin __rte_unused,
1128 struct rte_flow_error *error)
1131 uint64_t action_flags = 0;
1132 uint64_t item_flags = 0;
1133 uint64_t last_item = 0;
1134 uint8_t next_protocol = 0xff;
1135 uint16_t ether_type = 0;
1139 ret = mlx5_flow_validate_attributes(dev, attr, error);
1142 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
1143 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1146 switch (items->type) {
1147 case RTE_FLOW_ITEM_TYPE_VOID:
1149 case RTE_FLOW_ITEM_TYPE_ETH:
1150 ret = mlx5_flow_validate_item_eth(items, item_flags,
1154 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
1155 MLX5_FLOW_LAYER_OUTER_L2;
1156 if (items->mask != NULL && items->spec != NULL) {
1158 ((const struct rte_flow_item_eth *)
1161 ((const struct rte_flow_item_eth *)
1163 ether_type = rte_be_to_cpu_16(ether_type);
1168 case RTE_FLOW_ITEM_TYPE_VLAN:
1169 ret = mlx5_flow_validate_item_vlan(items, item_flags,
1173 last_item = tunnel ? (MLX5_FLOW_LAYER_INNER_L2 |
1174 MLX5_FLOW_LAYER_INNER_VLAN) :
1175 (MLX5_FLOW_LAYER_OUTER_L2 |
1176 MLX5_FLOW_LAYER_OUTER_VLAN);
1177 if (items->mask != NULL && items->spec != NULL) {
1179 ((const struct rte_flow_item_vlan *)
1180 items->spec)->inner_type;
1182 ((const struct rte_flow_item_vlan *)
1183 items->mask)->inner_type;
1184 ether_type = rte_be_to_cpu_16(ether_type);
1189 case RTE_FLOW_ITEM_TYPE_IPV4:
1190 ret = mlx5_flow_validate_item_ipv4(items, item_flags,
1196 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
1197 MLX5_FLOW_LAYER_OUTER_L3_IPV4;
1198 if (items->mask != NULL &&
1199 ((const struct rte_flow_item_ipv4 *)
1200 items->mask)->hdr.next_proto_id) {
1202 ((const struct rte_flow_item_ipv4 *)
1203 (items->spec))->hdr.next_proto_id;
1205 ((const struct rte_flow_item_ipv4 *)
1206 (items->mask))->hdr.next_proto_id;
1208 /* Reset for inner layer. */
1209 next_protocol = 0xff;
1212 case RTE_FLOW_ITEM_TYPE_IPV6:
1213 ret = mlx5_flow_validate_item_ipv6(items, item_flags,
1219 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
1220 MLX5_FLOW_LAYER_OUTER_L3_IPV6;
1221 if (items->mask != NULL &&
1222 ((const struct rte_flow_item_ipv6 *)
1223 items->mask)->hdr.proto) {
1225 ((const struct rte_flow_item_ipv6 *)
1226 items->spec)->hdr.proto;
1228 ((const struct rte_flow_item_ipv6 *)
1229 items->mask)->hdr.proto;
1231 /* Reset for inner layer. */
1232 next_protocol = 0xff;
1235 case RTE_FLOW_ITEM_TYPE_UDP:
1236 ret = mlx5_flow_validate_item_udp(items, item_flags,
1241 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
1242 MLX5_FLOW_LAYER_OUTER_L4_UDP;
1244 case RTE_FLOW_ITEM_TYPE_TCP:
1245 ret = mlx5_flow_validate_item_tcp
1248 &rte_flow_item_tcp_mask,
1252 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
1253 MLX5_FLOW_LAYER_OUTER_L4_TCP;
1255 case RTE_FLOW_ITEM_TYPE_VXLAN:
1256 ret = mlx5_flow_validate_item_vxlan(items, item_flags,
1260 last_item = MLX5_FLOW_LAYER_VXLAN;
1262 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
1263 ret = mlx5_flow_validate_item_vxlan_gpe(items,
1268 last_item = MLX5_FLOW_LAYER_VXLAN_GPE;
1270 case RTE_FLOW_ITEM_TYPE_GRE:
1271 ret = mlx5_flow_validate_item_gre(items, item_flags,
1272 next_protocol, error);
1275 last_item = MLX5_FLOW_LAYER_GRE;
1277 case RTE_FLOW_ITEM_TYPE_MPLS:
1278 ret = mlx5_flow_validate_item_mpls(dev, items,
1283 last_item = MLX5_FLOW_LAYER_MPLS;
1286 return rte_flow_error_set(error, ENOTSUP,
1287 RTE_FLOW_ERROR_TYPE_ITEM,
1288 NULL, "item not supported");
1290 item_flags |= last_item;
1292 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1293 switch (actions->type) {
1294 case RTE_FLOW_ACTION_TYPE_VOID:
1296 case RTE_FLOW_ACTION_TYPE_FLAG:
1297 ret = mlx5_flow_validate_action_flag(action_flags,
1302 action_flags |= MLX5_FLOW_ACTION_FLAG;
1304 case RTE_FLOW_ACTION_TYPE_MARK:
1305 ret = mlx5_flow_validate_action_mark(actions,
1311 action_flags |= MLX5_FLOW_ACTION_MARK;
1313 case RTE_FLOW_ACTION_TYPE_DROP:
1314 ret = mlx5_flow_validate_action_drop(action_flags,
1319 action_flags |= MLX5_FLOW_ACTION_DROP;
1321 case RTE_FLOW_ACTION_TYPE_QUEUE:
1322 ret = mlx5_flow_validate_action_queue(actions,
1328 action_flags |= MLX5_FLOW_ACTION_QUEUE;
1330 case RTE_FLOW_ACTION_TYPE_RSS:
1331 ret = mlx5_flow_validate_action_rss(actions,
1337 action_flags |= MLX5_FLOW_ACTION_RSS;
1339 case RTE_FLOW_ACTION_TYPE_COUNT:
1340 ret = mlx5_flow_validate_action_count(dev, attr, error);
1343 action_flags |= MLX5_FLOW_ACTION_COUNT;
1346 return rte_flow_error_set(error, ENOTSUP,
1347 RTE_FLOW_ERROR_TYPE_ACTION,
1349 "action not supported");
1353 * Validate the drop action mutual exclusion with other actions.
1354 * Drop action is mutually-exclusive with any other action, except for
1357 if ((action_flags & MLX5_FLOW_ACTION_DROP) &&
1358 (action_flags & ~(MLX5_FLOW_ACTION_DROP | MLX5_FLOW_ACTION_COUNT)))
1359 return rte_flow_error_set(error, EINVAL,
1360 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1361 "Drop action is mutually-exclusive "
1362 "with any other action, except for "
1364 if (!(action_flags & MLX5_FLOW_FATE_ACTIONS))
1365 return rte_flow_error_set(error, EINVAL,
1366 RTE_FLOW_ERROR_TYPE_ACTION, actions,
1367 "no fate action is found");
1372 * Calculate the required bytes that are needed for the action part of the verbs
1375 * @param[in] actions
1376 * Pointer to the list of actions.
1379 * The size of the memory needed for all actions.
1382 flow_verbs_get_actions_size(const struct rte_flow_action actions[])
1386 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1387 switch (actions->type) {
1388 case RTE_FLOW_ACTION_TYPE_VOID:
1390 case RTE_FLOW_ACTION_TYPE_FLAG:
1391 size += sizeof(struct ibv_flow_spec_action_tag);
1393 case RTE_FLOW_ACTION_TYPE_MARK:
1394 size += sizeof(struct ibv_flow_spec_action_tag);
1396 case RTE_FLOW_ACTION_TYPE_DROP:
1397 size += sizeof(struct ibv_flow_spec_action_drop);
1399 case RTE_FLOW_ACTION_TYPE_QUEUE:
1401 case RTE_FLOW_ACTION_TYPE_RSS:
1403 case RTE_FLOW_ACTION_TYPE_COUNT:
1404 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42) || \
1405 defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
1406 size += sizeof(struct ibv_flow_spec_counter_action);
1417 * Calculate the required bytes that are needed for the item part of the verbs
1421 * Pointer to the list of items.
1424 * The size of the memory needed for all items.
1427 flow_verbs_get_items_size(const struct rte_flow_item items[])
1431 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
1432 switch (items->type) {
1433 case RTE_FLOW_ITEM_TYPE_VOID:
1435 case RTE_FLOW_ITEM_TYPE_ETH:
1436 size += sizeof(struct ibv_flow_spec_eth);
1438 case RTE_FLOW_ITEM_TYPE_VLAN:
1439 size += sizeof(struct ibv_flow_spec_eth);
1441 case RTE_FLOW_ITEM_TYPE_IPV4:
1442 size += sizeof(struct ibv_flow_spec_ipv4_ext);
1444 case RTE_FLOW_ITEM_TYPE_IPV6:
1445 size += sizeof(struct ibv_flow_spec_ipv6);
1447 case RTE_FLOW_ITEM_TYPE_UDP:
1448 size += sizeof(struct ibv_flow_spec_tcp_udp);
1450 case RTE_FLOW_ITEM_TYPE_TCP:
1451 size += sizeof(struct ibv_flow_spec_tcp_udp);
1453 case RTE_FLOW_ITEM_TYPE_VXLAN:
1454 size += sizeof(struct ibv_flow_spec_tunnel);
1456 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
1457 size += sizeof(struct ibv_flow_spec_tunnel);
1459 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
1460 case RTE_FLOW_ITEM_TYPE_GRE:
1461 size += sizeof(struct ibv_flow_spec_gre);
1463 case RTE_FLOW_ITEM_TYPE_MPLS:
1464 size += sizeof(struct ibv_flow_spec_mpls);
1467 case RTE_FLOW_ITEM_TYPE_GRE:
1468 size += sizeof(struct ibv_flow_spec_tunnel);
1479 * Internal preparation function. Allocate mlx5_flow with the required size.
1480 * The required size is calculate based on the actions and items. This function
1481 * also returns the detected actions and items for later use.
1484 * Pointer to Ethernet device.
1486 * Pointer to the flow attributes.
1488 * Pointer to the list of items.
1489 * @param[in] actions
1490 * Pointer to the list of actions.
1492 * Pointer to the error structure.
1495 * Pointer to mlx5_flow object on success, otherwise NULL and rte_errno
1498 static struct mlx5_flow *
1499 flow_verbs_prepare(struct rte_eth_dev *dev,
1500 const struct rte_flow_attr *attr __rte_unused,
1501 const struct rte_flow_item items[],
1502 const struct rte_flow_action actions[],
1503 struct rte_flow_error *error)
1506 uint32_t handle_idx = 0;
1507 struct mlx5_flow *dev_flow;
1508 struct mlx5_flow_handle *dev_handle;
1509 struct mlx5_priv *priv = dev->data->dev_private;
1511 size += flow_verbs_get_actions_size(actions);
1512 size += flow_verbs_get_items_size(items);
1513 if (size > MLX5_VERBS_MAX_SPEC_ACT_SIZE) {
1514 rte_flow_error_set(error, E2BIG,
1515 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1516 "Verbs spec/action size too large");
1519 /* In case of corrupting the memory. */
1520 if (priv->flow_idx >= MLX5_NUM_MAX_DEV_FLOWS) {
1521 rte_flow_error_set(error, ENOSPC,
1522 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1523 "not free temporary device flow");
1526 dev_handle = mlx5_ipool_zmalloc(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
1529 rte_flow_error_set(error, ENOMEM,
1530 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1531 "not enough memory to create flow handle");
1534 /* No multi-thread supporting. */
1535 dev_flow = &((struct mlx5_flow *)priv->inter_flows)[priv->flow_idx++];
1536 dev_flow->handle = dev_handle;
1537 dev_flow->handle_idx = handle_idx;
1538 /* Memcpy is used, only size needs to be cleared to 0. */
1539 dev_flow->verbs.size = 0;
1540 dev_flow->verbs.attr.num_of_specs = 0;
1541 dev_flow->ingress = attr->ingress;
1542 dev_flow->hash_fields = 0;
1543 /* Need to set transfer attribute: not supported in Verbs mode. */
1548 * Fill the flow with verb spec.
1551 * Pointer to Ethernet device.
1552 * @param[in, out] dev_flow
1553 * Pointer to the mlx5 flow.
1555 * Pointer to the flow attributes.
1557 * Pointer to the list of items.
1558 * @param[in] actions
1559 * Pointer to the list of actions.
1561 * Pointer to the error structure.
1564 * 0 on success, else a negative errno value otherwise and rte_errno is set.
1567 flow_verbs_translate(struct rte_eth_dev *dev,
1568 struct mlx5_flow *dev_flow,
1569 const struct rte_flow_attr *attr,
1570 const struct rte_flow_item items[],
1571 const struct rte_flow_action actions[],
1572 struct rte_flow_error *error)
1574 uint64_t item_flags = 0;
1575 uint64_t action_flags = 0;
1576 uint64_t priority = attr->priority;
1577 uint32_t subpriority = 0;
1578 struct mlx5_priv *priv = dev->data->dev_private;
1579 struct mlx5_flow_rss_desc *rss_desc = &((struct mlx5_flow_rss_desc *)
1581 [!!priv->flow_nested_idx];
1583 if (priority == MLX5_FLOW_PRIO_RSVD)
1584 priority = priv->config.flow_prio - 1;
1585 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1588 switch (actions->type) {
1589 case RTE_FLOW_ACTION_TYPE_VOID:
1591 case RTE_FLOW_ACTION_TYPE_FLAG:
1592 flow_verbs_translate_action_flag(dev_flow, actions);
1593 action_flags |= MLX5_FLOW_ACTION_FLAG;
1594 dev_flow->handle->mark = 1;
1596 case RTE_FLOW_ACTION_TYPE_MARK:
1597 flow_verbs_translate_action_mark(dev_flow, actions);
1598 action_flags |= MLX5_FLOW_ACTION_MARK;
1599 dev_flow->handle->mark = 1;
1601 case RTE_FLOW_ACTION_TYPE_DROP:
1602 flow_verbs_translate_action_drop(dev_flow, actions);
1603 action_flags |= MLX5_FLOW_ACTION_DROP;
1604 dev_flow->handle->fate_action = MLX5_FLOW_FATE_DROP;
1606 case RTE_FLOW_ACTION_TYPE_QUEUE:
1607 flow_verbs_translate_action_queue(rss_desc, actions);
1608 action_flags |= MLX5_FLOW_ACTION_QUEUE;
1609 dev_flow->handle->fate_action = MLX5_FLOW_FATE_QUEUE;
1611 case RTE_FLOW_ACTION_TYPE_RSS:
1612 flow_verbs_translate_action_rss(rss_desc, actions);
1613 action_flags |= MLX5_FLOW_ACTION_RSS;
1614 dev_flow->handle->fate_action = MLX5_FLOW_FATE_QUEUE;
1616 case RTE_FLOW_ACTION_TYPE_COUNT:
1617 ret = flow_verbs_translate_action_count(dev_flow,
1622 action_flags |= MLX5_FLOW_ACTION_COUNT;
1625 return rte_flow_error_set(error, ENOTSUP,
1626 RTE_FLOW_ERROR_TYPE_ACTION,
1628 "action not supported");
1631 dev_flow->act_flags = action_flags;
1632 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
1633 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1635 switch (items->type) {
1636 case RTE_FLOW_ITEM_TYPE_VOID:
1638 case RTE_FLOW_ITEM_TYPE_ETH:
1639 flow_verbs_translate_item_eth(dev_flow, items,
1641 subpriority = MLX5_PRIORITY_MAP_L2;
1642 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
1643 MLX5_FLOW_LAYER_OUTER_L2;
1645 case RTE_FLOW_ITEM_TYPE_VLAN:
1646 flow_verbs_translate_item_vlan(dev_flow, items,
1648 subpriority = MLX5_PRIORITY_MAP_L2;
1649 item_flags |= tunnel ? (MLX5_FLOW_LAYER_INNER_L2 |
1650 MLX5_FLOW_LAYER_INNER_VLAN) :
1651 (MLX5_FLOW_LAYER_OUTER_L2 |
1652 MLX5_FLOW_LAYER_OUTER_VLAN);
1654 case RTE_FLOW_ITEM_TYPE_IPV4:
1655 flow_verbs_translate_item_ipv4(dev_flow, items,
1657 subpriority = MLX5_PRIORITY_MAP_L3;
1658 dev_flow->hash_fields |=
1659 mlx5_flow_hashfields_adjust
1661 MLX5_IPV4_LAYER_TYPES,
1662 MLX5_IPV4_IBV_RX_HASH);
1663 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
1664 MLX5_FLOW_LAYER_OUTER_L3_IPV4;
1666 case RTE_FLOW_ITEM_TYPE_IPV6:
1667 flow_verbs_translate_item_ipv6(dev_flow, items,
1669 subpriority = MLX5_PRIORITY_MAP_L3;
1670 dev_flow->hash_fields |=
1671 mlx5_flow_hashfields_adjust
1673 MLX5_IPV6_LAYER_TYPES,
1674 MLX5_IPV6_IBV_RX_HASH);
1675 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
1676 MLX5_FLOW_LAYER_OUTER_L3_IPV6;
1678 case RTE_FLOW_ITEM_TYPE_TCP:
1679 flow_verbs_translate_item_tcp(dev_flow, items,
1681 subpriority = MLX5_PRIORITY_MAP_L4;
1682 dev_flow->hash_fields |=
1683 mlx5_flow_hashfields_adjust
1684 (rss_desc, tunnel, ETH_RSS_TCP,
1685 (IBV_RX_HASH_SRC_PORT_TCP |
1686 IBV_RX_HASH_DST_PORT_TCP));
1687 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
1688 MLX5_FLOW_LAYER_OUTER_L4_TCP;
1690 case RTE_FLOW_ITEM_TYPE_UDP:
1691 flow_verbs_translate_item_udp(dev_flow, items,
1693 subpriority = MLX5_PRIORITY_MAP_L4;
1694 dev_flow->hash_fields |=
1695 mlx5_flow_hashfields_adjust
1696 (rss_desc, tunnel, ETH_RSS_UDP,
1697 (IBV_RX_HASH_SRC_PORT_UDP |
1698 IBV_RX_HASH_DST_PORT_UDP));
1699 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
1700 MLX5_FLOW_LAYER_OUTER_L4_UDP;
1702 case RTE_FLOW_ITEM_TYPE_VXLAN:
1703 flow_verbs_translate_item_vxlan(dev_flow, items,
1705 subpriority = MLX5_PRIORITY_MAP_L2;
1706 item_flags |= MLX5_FLOW_LAYER_VXLAN;
1708 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
1709 flow_verbs_translate_item_vxlan_gpe(dev_flow, items,
1711 subpriority = MLX5_PRIORITY_MAP_L2;
1712 item_flags |= MLX5_FLOW_LAYER_VXLAN_GPE;
1714 case RTE_FLOW_ITEM_TYPE_GRE:
1715 flow_verbs_translate_item_gre(dev_flow, items,
1717 subpriority = MLX5_PRIORITY_MAP_L2;
1718 item_flags |= MLX5_FLOW_LAYER_GRE;
1720 case RTE_FLOW_ITEM_TYPE_MPLS:
1721 flow_verbs_translate_item_mpls(dev_flow, items,
1723 subpriority = MLX5_PRIORITY_MAP_L2;
1724 item_flags |= MLX5_FLOW_LAYER_MPLS;
1727 return rte_flow_error_set(error, ENOTSUP,
1728 RTE_FLOW_ERROR_TYPE_ITEM,
1730 "item not supported");
1733 dev_flow->handle->layers = item_flags;
1734 /* Other members of attr will be ignored. */
1735 dev_flow->verbs.attr.priority =
1736 mlx5_flow_adjust_priority(dev, priority, subpriority);
1737 dev_flow->verbs.attr.port = (uint8_t)priv->ibv_port;
1742 * Remove the flow from the NIC but keeps it in memory.
1745 * Pointer to the Ethernet device structure.
1746 * @param[in, out] flow
1747 * Pointer to flow structure.
1750 flow_verbs_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
1752 struct mlx5_priv *priv = dev->data->dev_private;
1753 struct mlx5_flow_handle *handle;
1754 uint32_t handle_idx;
1758 SILIST_FOREACH(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW], flow->dev_handles,
1759 handle_idx, handle, next) {
1760 if (handle->ib_flow) {
1761 claim_zero(mlx5_glue->destroy_flow(handle->ib_flow));
1762 handle->ib_flow = NULL;
1764 /* hrxq is union, don't touch it only the flag is set. */
1765 if (handle->rix_hrxq) {
1766 if (handle->fate_action == MLX5_FLOW_FATE_DROP) {
1767 mlx5_hrxq_drop_release(dev);
1768 handle->rix_hrxq = 0;
1769 } else if (handle->fate_action ==
1770 MLX5_FLOW_FATE_QUEUE) {
1771 mlx5_hrxq_release(dev, handle->rix_hrxq);
1772 handle->rix_hrxq = 0;
1775 if (handle->vf_vlan.tag && handle->vf_vlan.created)
1776 mlx5_vlan_vmwa_release(dev, &handle->vf_vlan);
1781 * Remove the flow from the NIC and the memory.
1784 * Pointer to the Ethernet device structure.
1785 * @param[in, out] flow
1786 * Pointer to flow structure.
1789 flow_verbs_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
1791 struct mlx5_priv *priv = dev->data->dev_private;
1792 struct mlx5_flow_handle *handle;
1796 flow_verbs_remove(dev, flow);
1797 while (flow->dev_handles) {
1798 uint32_t tmp_idx = flow->dev_handles;
1800 handle = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
1804 flow->dev_handles = handle->next.next;
1805 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
1808 if (flow->counter) {
1809 flow_verbs_counter_release(dev, flow->counter);
1815 * Apply the flow to the NIC.
1818 * Pointer to the Ethernet device structure.
1819 * @param[in, out] flow
1820 * Pointer to flow structure.
1822 * Pointer to error structure.
1825 * 0 on success, a negative errno value otherwise and rte_errno is set.
1828 flow_verbs_apply(struct rte_eth_dev *dev, struct rte_flow *flow,
1829 struct rte_flow_error *error)
1831 struct mlx5_priv *priv = dev->data->dev_private;
1832 struct mlx5_flow_handle *handle;
1833 struct mlx5_flow *dev_flow;
1834 struct mlx5_hrxq *hrxq;
1835 uint32_t dev_handles;
1839 for (idx = priv->flow_idx - 1; idx >= priv->flow_nested_idx; idx--) {
1840 dev_flow = &((struct mlx5_flow *)priv->inter_flows)[idx];
1841 handle = dev_flow->handle;
1842 if (handle->fate_action == MLX5_FLOW_FATE_DROP) {
1843 hrxq = mlx5_hrxq_drop_new(dev);
1847 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1848 "cannot get drop hash queue");
1853 struct mlx5_flow_rss_desc *rss_desc =
1854 &((struct mlx5_flow_rss_desc *)priv->rss_desc)
1855 [!!priv->flow_nested_idx];
1857 MLX5_ASSERT(rss_desc->queue_num);
1858 hrxq_idx = mlx5_hrxq_get(dev, rss_desc->key,
1859 MLX5_RSS_HASH_KEY_LEN,
1860 dev_flow->hash_fields,
1862 rss_desc->queue_num);
1864 hrxq_idx = mlx5_hrxq_new(dev, rss_desc->key,
1865 MLX5_RSS_HASH_KEY_LEN,
1866 dev_flow->hash_fields,
1868 rss_desc->queue_num,
1870 MLX5_FLOW_LAYER_TUNNEL));
1871 hrxq = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_HRXQ],
1876 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1877 "cannot get hash queue");
1880 handle->rix_hrxq = hrxq_idx;
1883 handle->ib_flow = mlx5_glue->create_flow(hrxq->qp,
1884 &dev_flow->verbs.attr);
1885 if (!handle->ib_flow) {
1886 rte_flow_error_set(error, errno,
1887 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1889 "hardware refuses to create flow");
1892 if (priv->vmwa_context &&
1893 handle->vf_vlan.tag && !handle->vf_vlan.created) {
1895 * The rule contains the VLAN pattern.
1896 * For VF we are going to create VLAN
1897 * interface to make hypervisor set correct
1898 * e-Switch vport context.
1900 mlx5_vlan_vmwa_acquire(dev, &handle->vf_vlan);
1905 err = rte_errno; /* Save rte_errno before cleanup. */
1906 SILIST_FOREACH(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW], flow->dev_handles,
1907 dev_handles, handle, next) {
1908 /* hrxq is union, don't touch it only the flag is set. */
1909 if (handle->rix_hrxq) {
1910 if (handle->fate_action == MLX5_FLOW_FATE_DROP) {
1911 mlx5_hrxq_drop_release(dev);
1912 handle->rix_hrxq = 0;
1913 } else if (handle->fate_action ==
1914 MLX5_FLOW_FATE_QUEUE) {
1915 mlx5_hrxq_release(dev, handle->rix_hrxq);
1916 handle->rix_hrxq = 0;
1919 if (handle->vf_vlan.tag && handle->vf_vlan.created)
1920 mlx5_vlan_vmwa_release(dev, &handle->vf_vlan);
1922 rte_errno = err; /* Restore rte_errno. */
1929 * @see rte_flow_query()
1933 flow_verbs_query(struct rte_eth_dev *dev,
1934 struct rte_flow *flow,
1935 const struct rte_flow_action *actions,
1937 struct rte_flow_error *error)
1941 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1942 switch (actions->type) {
1943 case RTE_FLOW_ACTION_TYPE_VOID:
1945 case RTE_FLOW_ACTION_TYPE_COUNT:
1946 ret = flow_verbs_counter_query(dev, flow, data, error);
1949 return rte_flow_error_set(error, ENOTSUP,
1950 RTE_FLOW_ERROR_TYPE_ACTION,
1952 "action not supported");
1958 const struct mlx5_flow_driver_ops mlx5_flow_verbs_drv_ops = {
1959 .validate = flow_verbs_validate,
1960 .prepare = flow_verbs_prepare,
1961 .translate = flow_verbs_translate,
1962 .apply = flow_verbs_apply,
1963 .remove = flow_verbs_remove,
1964 .destroy = flow_verbs_destroy,
1965 .query = flow_verbs_query,