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);
60 struct mlx5_flow_counter_pool *pool;
63 pool = cont->pools[idx / MLX5_COUNTERS_PER_POOL];
67 return MLX5_POOL_GET_CNT(pool, idx % MLX5_COUNTERS_PER_POOL);
71 * Create Verbs flow counter with Verbs library.
74 * Pointer to the Ethernet device structure.
75 * @param[in, out] counter
76 * mlx5 flow counter object, contains the counter id,
77 * handle of created Verbs flow counter is returned
78 * in cs field (if counters are supported).
81 * 0 On success else a negative errno value is returned
82 * and rte_errno is set.
85 flow_verbs_counter_create(struct rte_eth_dev *dev,
86 struct mlx5_flow_counter_ext *counter)
88 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42)
89 struct mlx5_priv *priv = dev->data->dev_private;
90 struct ibv_context *ctx = priv->sh->ctx;
91 struct ibv_counter_set_init_attr init = {
92 .counter_set_id = counter->id};
94 counter->cs = mlx5_glue->create_counter_set(ctx, &init);
100 #elif defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
101 struct mlx5_priv *priv = dev->data->dev_private;
102 struct ibv_context *ctx = priv->sh->ctx;
103 struct ibv_counters_init_attr init = {0};
104 struct ibv_counter_attach_attr attach;
107 memset(&attach, 0, sizeof(attach));
108 counter->cs = mlx5_glue->create_counters(ctx, &init);
113 attach.counter_desc = IBV_COUNTER_PACKETS;
115 ret = mlx5_glue->attach_counters(counter->cs, &attach, NULL);
117 attach.counter_desc = IBV_COUNTER_BYTES;
119 ret = mlx5_glue->attach_counters
120 (counter->cs, &attach, NULL);
123 claim_zero(mlx5_glue->destroy_counters(counter->cs));
138 * Get a flow counter.
141 * Pointer to the Ethernet device structure.
143 * Indicate if this counter is shared with other flows.
145 * Counter identifier.
148 * Index to the counter, 0 otherwise and rte_errno is set.
151 flow_verbs_counter_new(struct rte_eth_dev *dev, uint32_t shared, uint32_t id)
153 struct mlx5_priv *priv = dev->data->dev_private;
154 struct mlx5_pools_container *cont = MLX5_CNT_CONTAINER(priv->sh, 0, 0);
155 struct mlx5_flow_counter_pool *pool = NULL;
156 struct mlx5_flow_counter_ext *cnt_ext = NULL;
157 struct mlx5_flow_counter *cnt = NULL;
158 uint32_t n_valid = rte_atomic16_read(&cont->n_valid);
164 for (pool_idx = 0; pool_idx < n_valid; ++pool_idx) {
165 pool = cont->pools[pool_idx];
166 for (i = 0; i < MLX5_COUNTERS_PER_POOL; ++i) {
167 cnt_ext = MLX5_GET_POOL_CNT_EXT(pool, i);
168 if (cnt_ext->shared && cnt_ext->id == id) {
170 return MLX5_MAKE_CNT_IDX(pool_idx, i);
175 for (pool_idx = 0; pool_idx < n_valid; ++pool_idx) {
176 pool = cont->pools[pool_idx];
179 cnt = TAILQ_FIRST(&pool->counters[0]);
184 struct mlx5_flow_counter_pool **pools;
187 if (n_valid == cont->n) {
188 /* Resize the container pool array. */
189 size = sizeof(struct mlx5_flow_counter_pool *) *
190 (n_valid + MLX5_CNT_CONTAINER_RESIZE);
191 pools = rte_zmalloc(__func__, size, 0);
195 memcpy(pools, cont->pools,
196 sizeof(struct mlx5_flow_counter_pool *) *
198 rte_free(cont->pools);
201 cont->n += MLX5_CNT_CONTAINER_RESIZE;
203 /* Allocate memory for new pool*/
204 size = sizeof(*pool) + (sizeof(*cnt_ext) + sizeof(*cnt)) *
205 MLX5_COUNTERS_PER_POOL;
206 pool = rte_calloc(__func__, 1, size, 0);
209 pool->type |= CNT_POOL_TYPE_EXT;
210 for (i = 0; i < MLX5_COUNTERS_PER_POOL; ++i) {
211 cnt = MLX5_POOL_GET_CNT(pool, i);
212 TAILQ_INSERT_HEAD(&pool->counters[0], cnt, next);
214 cnt = MLX5_POOL_GET_CNT(pool, 0);
215 cont->pools[n_valid] = pool;
217 rte_atomic16_add(&cont->n_valid, 1);
218 TAILQ_INSERT_HEAD(&cont->pool_list, pool, next);
220 i = MLX5_CNT_ARRAY_IDX(pool, cnt);
221 cnt_ext = MLX5_GET_POOL_CNT_EXT(pool, i);
223 cnt_ext->shared = shared;
224 cnt_ext->ref_cnt = 1;
227 /* Create counter with Verbs. */
228 ret = flow_verbs_counter_create(dev, cnt_ext);
230 TAILQ_REMOVE(&pool->counters[0], cnt, next);
231 return MLX5_MAKE_CNT_IDX(pool_idx, i);
233 /* Some error occurred in Verbs library. */
239 * Release a flow counter.
242 * Pointer to the Ethernet device structure.
244 * Index to the counter handler.
247 flow_verbs_counter_release(struct rte_eth_dev *dev, uint32_t counter)
249 struct mlx5_flow_counter_pool *pool;
250 struct mlx5_flow_counter *cnt;
251 struct mlx5_flow_counter_ext *cnt_ext;
253 cnt = flow_verbs_counter_get_by_idx(dev, counter,
255 cnt_ext = MLX5_CNT_TO_CNT_EXT(pool, cnt);
256 if (--cnt_ext->ref_cnt == 0) {
257 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42)
258 claim_zero(mlx5_glue->destroy_counter_set(cnt_ext->cs));
260 #elif defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
261 claim_zero(mlx5_glue->destroy_counters(cnt_ext->cs));
264 TAILQ_INSERT_HEAD(&pool->counters[0], cnt, next);
269 * Query a flow counter via Verbs library call.
271 * @see rte_flow_query()
275 flow_verbs_counter_query(struct rte_eth_dev *dev __rte_unused,
276 struct rte_flow *flow, void *data,
277 struct rte_flow_error *error)
279 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42) || \
280 defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
282 struct mlx5_flow_counter_pool *pool;
283 struct mlx5_flow_counter *cnt = flow_verbs_counter_get_by_idx
284 (dev, flow->counter, &pool);
285 struct mlx5_flow_counter_ext *cnt_ext = MLX5_CNT_TO_CNT_EXT
287 struct rte_flow_query_count *qc = data;
288 uint64_t counters[2] = {0, 0};
289 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42)
290 struct ibv_query_counter_set_attr query_cs_attr = {
292 .query_flags = IBV_COUNTER_SET_FORCE_UPDATE,
294 struct ibv_counter_set_data query_out = {
296 .outlen = 2 * sizeof(uint64_t),
298 int err = mlx5_glue->query_counter_set(&query_cs_attr,
300 #elif defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
301 int err = mlx5_glue->query_counters
302 (cnt_ext->cs, counters,
304 IBV_READ_COUNTERS_ATTR_PREFER_CACHED);
307 return rte_flow_error_set
309 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
311 "cannot read counter");
314 qc->hits = counters[0] - cnt->hits;
315 qc->bytes = counters[1] - cnt->bytes;
317 cnt->hits = counters[0];
318 cnt->bytes = counters[1];
322 return rte_flow_error_set(error, EINVAL,
323 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
325 "flow does not have counter");
329 return rte_flow_error_set(error, ENOTSUP,
330 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
332 "counters are not available");
337 * Add a verbs item specification into @p verbs.
340 * Pointer to verbs structure.
342 * Create specification.
344 * Size in bytes of the specification to copy.
347 flow_verbs_spec_add(struct mlx5_flow_verbs_workspace *verbs,
348 void *src, unsigned int size)
354 MLX5_ASSERT(verbs->specs);
355 dst = (void *)(verbs->specs + verbs->size);
356 memcpy(dst, src, size);
357 ++verbs->attr.num_of_specs;
362 * Convert the @p item into a Verbs specification. This function assumes that
363 * the input is valid and that there is space to insert the requested item
366 * @param[in, out] dev_flow
367 * Pointer to dev_flow structure.
369 * Item specification.
370 * @param[in] item_flags
374 flow_verbs_translate_item_eth(struct mlx5_flow *dev_flow,
375 const struct rte_flow_item *item,
378 const struct rte_flow_item_eth *spec = item->spec;
379 const struct rte_flow_item_eth *mask = item->mask;
380 const unsigned int size = sizeof(struct ibv_flow_spec_eth);
381 struct ibv_flow_spec_eth eth = {
382 .type = IBV_FLOW_SPEC_ETH | VERBS_SPEC_INNER(item_flags),
387 mask = &rte_flow_item_eth_mask;
391 memcpy(ð.val.dst_mac, spec->dst.addr_bytes,
393 memcpy(ð.val.src_mac, spec->src.addr_bytes,
395 eth.val.ether_type = spec->type;
396 memcpy(ð.mask.dst_mac, mask->dst.addr_bytes,
398 memcpy(ð.mask.src_mac, mask->src.addr_bytes,
400 eth.mask.ether_type = mask->type;
401 /* Remove unwanted bits from values. */
402 for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i) {
403 eth.val.dst_mac[i] &= eth.mask.dst_mac[i];
404 eth.val.src_mac[i] &= eth.mask.src_mac[i];
406 eth.val.ether_type &= eth.mask.ether_type;
408 flow_verbs_spec_add(&dev_flow->verbs, ð, size);
412 * Update the VLAN tag in the Verbs Ethernet specification.
413 * This function assumes that the input is valid and there is space to add
414 * the requested item.
416 * @param[in, out] attr
417 * Pointer to Verbs attributes structure.
419 * Verbs structure containing the VLAN information to copy.
422 flow_verbs_item_vlan_update(struct ibv_flow_attr *attr,
423 struct ibv_flow_spec_eth *eth)
426 const enum ibv_flow_spec_type search = eth->type;
427 struct ibv_spec_header *hdr = (struct ibv_spec_header *)
428 ((uint8_t *)attr + sizeof(struct ibv_flow_attr));
430 for (i = 0; i != attr->num_of_specs; ++i) {
431 if (hdr->type == search) {
432 struct ibv_flow_spec_eth *e =
433 (struct ibv_flow_spec_eth *)hdr;
435 e->val.vlan_tag = eth->val.vlan_tag;
436 e->mask.vlan_tag = eth->mask.vlan_tag;
437 e->val.ether_type = eth->val.ether_type;
438 e->mask.ether_type = eth->mask.ether_type;
441 hdr = (struct ibv_spec_header *)((uint8_t *)hdr + hdr->size);
446 * Convert the @p item into a Verbs specification. This function assumes that
447 * the input is valid and that there is space to insert the requested item
450 * @param[in, out] dev_flow
451 * Pointer to dev_flow structure.
453 * Item specification.
454 * @param[in] item_flags
458 flow_verbs_translate_item_vlan(struct mlx5_flow *dev_flow,
459 const struct rte_flow_item *item,
462 const struct rte_flow_item_vlan *spec = item->spec;
463 const struct rte_flow_item_vlan *mask = item->mask;
464 unsigned int size = sizeof(struct ibv_flow_spec_eth);
465 const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
466 struct ibv_flow_spec_eth eth = {
467 .type = IBV_FLOW_SPEC_ETH | VERBS_SPEC_INNER(item_flags),
470 const uint32_t l2m = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
471 MLX5_FLOW_LAYER_OUTER_L2;
474 mask = &rte_flow_item_vlan_mask;
476 eth.val.vlan_tag = spec->tci;
477 eth.mask.vlan_tag = mask->tci;
478 eth.val.vlan_tag &= eth.mask.vlan_tag;
479 eth.val.ether_type = spec->inner_type;
480 eth.mask.ether_type = mask->inner_type;
481 eth.val.ether_type &= eth.mask.ether_type;
483 if (!(item_flags & l2m))
484 flow_verbs_spec_add(&dev_flow->verbs, ð, size);
486 flow_verbs_item_vlan_update(&dev_flow->verbs.attr, ð);
488 dev_flow->handle->vf_vlan.tag =
489 rte_be_to_cpu_16(spec->tci) & 0x0fff;
493 * Convert the @p item into a Verbs specification. This function assumes that
494 * the input is valid and that there is space to insert the requested item
497 * @param[in, out] dev_flow
498 * Pointer to dev_flow structure.
500 * Item specification.
501 * @param[in] item_flags
505 flow_verbs_translate_item_ipv4(struct mlx5_flow *dev_flow,
506 const struct rte_flow_item *item,
509 const struct rte_flow_item_ipv4 *spec = item->spec;
510 const struct rte_flow_item_ipv4 *mask = item->mask;
511 unsigned int size = sizeof(struct ibv_flow_spec_ipv4_ext);
512 struct ibv_flow_spec_ipv4_ext ipv4 = {
513 .type = IBV_FLOW_SPEC_IPV4_EXT | VERBS_SPEC_INNER(item_flags),
518 mask = &rte_flow_item_ipv4_mask;
520 ipv4.val = (struct ibv_flow_ipv4_ext_filter){
521 .src_ip = spec->hdr.src_addr,
522 .dst_ip = spec->hdr.dst_addr,
523 .proto = spec->hdr.next_proto_id,
524 .tos = spec->hdr.type_of_service,
526 ipv4.mask = (struct ibv_flow_ipv4_ext_filter){
527 .src_ip = mask->hdr.src_addr,
528 .dst_ip = mask->hdr.dst_addr,
529 .proto = mask->hdr.next_proto_id,
530 .tos = mask->hdr.type_of_service,
532 /* Remove unwanted bits from values. */
533 ipv4.val.src_ip &= ipv4.mask.src_ip;
534 ipv4.val.dst_ip &= ipv4.mask.dst_ip;
535 ipv4.val.proto &= ipv4.mask.proto;
536 ipv4.val.tos &= ipv4.mask.tos;
538 flow_verbs_spec_add(&dev_flow->verbs, &ipv4, size);
542 * Convert the @p item into a Verbs specification. This function assumes that
543 * the input is valid and that there is space to insert the requested item
546 * @param[in, out] dev_flow
547 * Pointer to dev_flow structure.
549 * Item specification.
550 * @param[in] item_flags
554 flow_verbs_translate_item_ipv6(struct mlx5_flow *dev_flow,
555 const struct rte_flow_item *item,
558 const struct rte_flow_item_ipv6 *spec = item->spec;
559 const struct rte_flow_item_ipv6 *mask = item->mask;
560 unsigned int size = sizeof(struct ibv_flow_spec_ipv6);
561 struct ibv_flow_spec_ipv6 ipv6 = {
562 .type = IBV_FLOW_SPEC_IPV6 | VERBS_SPEC_INNER(item_flags),
567 mask = &rte_flow_item_ipv6_mask;
570 uint32_t vtc_flow_val;
571 uint32_t vtc_flow_mask;
573 memcpy(&ipv6.val.src_ip, spec->hdr.src_addr,
574 RTE_DIM(ipv6.val.src_ip));
575 memcpy(&ipv6.val.dst_ip, spec->hdr.dst_addr,
576 RTE_DIM(ipv6.val.dst_ip));
577 memcpy(&ipv6.mask.src_ip, mask->hdr.src_addr,
578 RTE_DIM(ipv6.mask.src_ip));
579 memcpy(&ipv6.mask.dst_ip, mask->hdr.dst_addr,
580 RTE_DIM(ipv6.mask.dst_ip));
581 vtc_flow_val = rte_be_to_cpu_32(spec->hdr.vtc_flow);
582 vtc_flow_mask = rte_be_to_cpu_32(mask->hdr.vtc_flow);
583 ipv6.val.flow_label =
584 rte_cpu_to_be_32((vtc_flow_val & RTE_IPV6_HDR_FL_MASK) >>
585 RTE_IPV6_HDR_FL_SHIFT);
586 ipv6.val.traffic_class = (vtc_flow_val & RTE_IPV6_HDR_TC_MASK) >>
587 RTE_IPV6_HDR_TC_SHIFT;
588 ipv6.val.next_hdr = spec->hdr.proto;
589 ipv6.mask.flow_label =
590 rte_cpu_to_be_32((vtc_flow_mask & RTE_IPV6_HDR_FL_MASK) >>
591 RTE_IPV6_HDR_FL_SHIFT);
592 ipv6.mask.traffic_class = (vtc_flow_mask & RTE_IPV6_HDR_TC_MASK) >>
593 RTE_IPV6_HDR_TC_SHIFT;
594 ipv6.mask.next_hdr = mask->hdr.proto;
595 /* Remove unwanted bits from values. */
596 for (i = 0; i < RTE_DIM(ipv6.val.src_ip); ++i) {
597 ipv6.val.src_ip[i] &= ipv6.mask.src_ip[i];
598 ipv6.val.dst_ip[i] &= ipv6.mask.dst_ip[i];
600 ipv6.val.flow_label &= ipv6.mask.flow_label;
601 ipv6.val.traffic_class &= ipv6.mask.traffic_class;
602 ipv6.val.next_hdr &= ipv6.mask.next_hdr;
604 flow_verbs_spec_add(&dev_flow->verbs, &ipv6, size);
608 * Convert the @p item into a Verbs specification. This function assumes that
609 * the input is valid and that there is space to insert the requested item
612 * @param[in, out] dev_flow
613 * Pointer to dev_flow structure.
615 * Item specification.
616 * @param[in] item_flags
620 flow_verbs_translate_item_tcp(struct mlx5_flow *dev_flow,
621 const struct rte_flow_item *item,
622 uint64_t item_flags __rte_unused)
624 const struct rte_flow_item_tcp *spec = item->spec;
625 const struct rte_flow_item_tcp *mask = item->mask;
626 unsigned int size = sizeof(struct ibv_flow_spec_tcp_udp);
627 struct ibv_flow_spec_tcp_udp tcp = {
628 .type = IBV_FLOW_SPEC_TCP | VERBS_SPEC_INNER(item_flags),
633 mask = &rte_flow_item_tcp_mask;
635 tcp.val.dst_port = spec->hdr.dst_port;
636 tcp.val.src_port = spec->hdr.src_port;
637 tcp.mask.dst_port = mask->hdr.dst_port;
638 tcp.mask.src_port = mask->hdr.src_port;
639 /* Remove unwanted bits from values. */
640 tcp.val.src_port &= tcp.mask.src_port;
641 tcp.val.dst_port &= tcp.mask.dst_port;
643 flow_verbs_spec_add(&dev_flow->verbs, &tcp, size);
647 * Convert the @p item into a Verbs specification. This function assumes that
648 * the input is valid and that there is space to insert the requested item
651 * @param[in, out] dev_flow
652 * Pointer to dev_flow structure.
654 * Item specification.
655 * @param[in] item_flags
659 flow_verbs_translate_item_udp(struct mlx5_flow *dev_flow,
660 const struct rte_flow_item *item,
661 uint64_t item_flags __rte_unused)
663 const struct rte_flow_item_udp *spec = item->spec;
664 const struct rte_flow_item_udp *mask = item->mask;
665 unsigned int size = sizeof(struct ibv_flow_spec_tcp_udp);
666 struct ibv_flow_spec_tcp_udp udp = {
667 .type = IBV_FLOW_SPEC_UDP | VERBS_SPEC_INNER(item_flags),
672 mask = &rte_flow_item_udp_mask;
674 udp.val.dst_port = spec->hdr.dst_port;
675 udp.val.src_port = spec->hdr.src_port;
676 udp.mask.dst_port = mask->hdr.dst_port;
677 udp.mask.src_port = mask->hdr.src_port;
678 /* Remove unwanted bits from values. */
679 udp.val.src_port &= udp.mask.src_port;
680 udp.val.dst_port &= udp.mask.dst_port;
683 while (item->type == RTE_FLOW_ITEM_TYPE_VOID)
685 if (!(udp.val.dst_port & udp.mask.dst_port)) {
686 switch ((item)->type) {
687 case RTE_FLOW_ITEM_TYPE_VXLAN:
688 udp.val.dst_port = htons(MLX5_UDP_PORT_VXLAN);
689 udp.mask.dst_port = 0xffff;
691 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
692 udp.val.dst_port = htons(MLX5_UDP_PORT_VXLAN_GPE);
693 udp.mask.dst_port = 0xffff;
695 case RTE_FLOW_ITEM_TYPE_MPLS:
696 udp.val.dst_port = htons(MLX5_UDP_PORT_MPLS);
697 udp.mask.dst_port = 0xffff;
704 flow_verbs_spec_add(&dev_flow->verbs, &udp, size);
708 * Convert the @p item into a Verbs specification. This function assumes that
709 * the input is valid and that there is space to insert the requested item
712 * @param[in, out] dev_flow
713 * Pointer to dev_flow structure.
715 * Item specification.
716 * @param[in] item_flags
720 flow_verbs_translate_item_vxlan(struct mlx5_flow *dev_flow,
721 const struct rte_flow_item *item,
722 uint64_t item_flags __rte_unused)
724 const struct rte_flow_item_vxlan *spec = item->spec;
725 const struct rte_flow_item_vxlan *mask = item->mask;
726 unsigned int size = sizeof(struct ibv_flow_spec_tunnel);
727 struct ibv_flow_spec_tunnel vxlan = {
728 .type = IBV_FLOW_SPEC_VXLAN_TUNNEL,
734 } id = { .vlan_id = 0, };
737 mask = &rte_flow_item_vxlan_mask;
739 memcpy(&id.vni[1], spec->vni, 3);
740 vxlan.val.tunnel_id = id.vlan_id;
741 memcpy(&id.vni[1], mask->vni, 3);
742 vxlan.mask.tunnel_id = id.vlan_id;
743 /* Remove unwanted bits from values. */
744 vxlan.val.tunnel_id &= vxlan.mask.tunnel_id;
746 flow_verbs_spec_add(&dev_flow->verbs, &vxlan, size);
750 * Convert the @p item into a Verbs specification. This function assumes that
751 * the input is valid and that there is space to insert the requested item
754 * @param[in, out] dev_flow
755 * Pointer to dev_flow structure.
757 * Item specification.
758 * @param[in] item_flags
762 flow_verbs_translate_item_vxlan_gpe(struct mlx5_flow *dev_flow,
763 const struct rte_flow_item *item,
764 uint64_t item_flags __rte_unused)
766 const struct rte_flow_item_vxlan_gpe *spec = item->spec;
767 const struct rte_flow_item_vxlan_gpe *mask = item->mask;
768 unsigned int size = sizeof(struct ibv_flow_spec_tunnel);
769 struct ibv_flow_spec_tunnel vxlan_gpe = {
770 .type = IBV_FLOW_SPEC_VXLAN_TUNNEL,
776 } id = { .vlan_id = 0, };
779 mask = &rte_flow_item_vxlan_gpe_mask;
781 memcpy(&id.vni[1], spec->vni, 3);
782 vxlan_gpe.val.tunnel_id = id.vlan_id;
783 memcpy(&id.vni[1], mask->vni, 3);
784 vxlan_gpe.mask.tunnel_id = id.vlan_id;
785 /* Remove unwanted bits from values. */
786 vxlan_gpe.val.tunnel_id &= vxlan_gpe.mask.tunnel_id;
788 flow_verbs_spec_add(&dev_flow->verbs, &vxlan_gpe, size);
792 * Update the protocol in Verbs IPv4/IPv6 spec.
794 * @param[in, out] attr
795 * Pointer to Verbs attributes structure.
797 * Specification type to search in order to update the IP protocol.
798 * @param[in] protocol
799 * Protocol value to set if none is present in the specification.
802 flow_verbs_item_gre_ip_protocol_update(struct ibv_flow_attr *attr,
803 enum ibv_flow_spec_type search,
807 struct ibv_spec_header *hdr = (struct ibv_spec_header *)
808 ((uint8_t *)attr + sizeof(struct ibv_flow_attr));
812 for (i = 0; i != attr->num_of_specs; ++i) {
813 if (hdr->type == search) {
815 struct ibv_flow_spec_ipv4_ext *ipv4;
816 struct ibv_flow_spec_ipv6 *ipv6;
820 case IBV_FLOW_SPEC_IPV4_EXT:
821 ip.ipv4 = (struct ibv_flow_spec_ipv4_ext *)hdr;
822 if (!ip.ipv4->val.proto) {
823 ip.ipv4->val.proto = protocol;
824 ip.ipv4->mask.proto = 0xff;
827 case IBV_FLOW_SPEC_IPV6:
828 ip.ipv6 = (struct ibv_flow_spec_ipv6 *)hdr;
829 if (!ip.ipv6->val.next_hdr) {
830 ip.ipv6->val.next_hdr = protocol;
831 ip.ipv6->mask.next_hdr = 0xff;
839 hdr = (struct ibv_spec_header *)((uint8_t *)hdr + hdr->size);
844 * Convert the @p item into a Verbs specification. This function assumes that
845 * the input is valid and that there is space to insert the requested item
848 * @param[in, out] dev_flow
849 * Pointer to dev_flow structure.
851 * Item specification.
852 * @param[in] item_flags
856 flow_verbs_translate_item_gre(struct mlx5_flow *dev_flow,
857 const struct rte_flow_item *item __rte_unused,
860 struct mlx5_flow_verbs_workspace *verbs = &dev_flow->verbs;
861 #ifndef HAVE_IBV_DEVICE_MPLS_SUPPORT
862 unsigned int size = sizeof(struct ibv_flow_spec_tunnel);
863 struct ibv_flow_spec_tunnel tunnel = {
864 .type = IBV_FLOW_SPEC_VXLAN_TUNNEL,
868 const struct rte_flow_item_gre *spec = item->spec;
869 const struct rte_flow_item_gre *mask = item->mask;
870 unsigned int size = sizeof(struct ibv_flow_spec_gre);
871 struct ibv_flow_spec_gre tunnel = {
872 .type = IBV_FLOW_SPEC_GRE,
877 mask = &rte_flow_item_gre_mask;
879 tunnel.val.c_ks_res0_ver = spec->c_rsvd0_ver;
880 tunnel.val.protocol = spec->protocol;
881 tunnel.mask.c_ks_res0_ver = mask->c_rsvd0_ver;
882 tunnel.mask.protocol = mask->protocol;
883 /* Remove unwanted bits from values. */
884 tunnel.val.c_ks_res0_ver &= tunnel.mask.c_ks_res0_ver;
885 tunnel.val.protocol &= tunnel.mask.protocol;
886 tunnel.val.key &= tunnel.mask.key;
889 if (item_flags & MLX5_FLOW_LAYER_OUTER_L3_IPV4)
890 flow_verbs_item_gre_ip_protocol_update(&verbs->attr,
891 IBV_FLOW_SPEC_IPV4_EXT,
894 flow_verbs_item_gre_ip_protocol_update(&verbs->attr,
897 flow_verbs_spec_add(verbs, &tunnel, size);
901 * Convert the @p action into a Verbs specification. This function assumes that
902 * the input is valid and that there is space to insert the requested action
903 * into the flow. This function also return the action that was added.
905 * @param[in, out] dev_flow
906 * Pointer to dev_flow structure.
908 * Item specification.
909 * @param[in] item_flags
913 flow_verbs_translate_item_mpls(struct mlx5_flow *dev_flow __rte_unused,
914 const struct rte_flow_item *item __rte_unused,
915 uint64_t item_flags __rte_unused)
917 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
918 const struct rte_flow_item_mpls *spec = item->spec;
919 const struct rte_flow_item_mpls *mask = item->mask;
920 unsigned int size = sizeof(struct ibv_flow_spec_mpls);
921 struct ibv_flow_spec_mpls mpls = {
922 .type = IBV_FLOW_SPEC_MPLS,
927 mask = &rte_flow_item_mpls_mask;
929 memcpy(&mpls.val.label, spec, sizeof(mpls.val.label));
930 memcpy(&mpls.mask.label, mask, sizeof(mpls.mask.label));
931 /* Remove unwanted bits from values. */
932 mpls.val.label &= mpls.mask.label;
934 flow_verbs_spec_add(&dev_flow->verbs, &mpls, size);
939 * Convert the @p action into a Verbs specification. This function assumes that
940 * the input is valid and that there is space to insert the requested action
943 * @param[in] dev_flow
944 * Pointer to mlx5_flow.
946 * Action configuration.
949 flow_verbs_translate_action_drop
950 (struct mlx5_flow *dev_flow,
951 const struct rte_flow_action *action __rte_unused)
953 unsigned int size = sizeof(struct ibv_flow_spec_action_drop);
954 struct ibv_flow_spec_action_drop drop = {
955 .type = IBV_FLOW_SPEC_ACTION_DROP,
959 flow_verbs_spec_add(&dev_flow->verbs, &drop, size);
963 * Convert the @p action into a Verbs specification. This function assumes that
964 * the input is valid and that there is space to insert the requested action
967 * @param[in] rss_desc
968 * Pointer to mlx5_flow_rss_desc.
970 * Action configuration.
973 flow_verbs_translate_action_queue(struct mlx5_flow_rss_desc *rss_desc,
974 const struct rte_flow_action *action)
976 const struct rte_flow_action_queue *queue = action->conf;
978 rss_desc->queue[0] = queue->index;
979 rss_desc->queue_num = 1;
983 * Convert the @p action into a Verbs specification. This function assumes that
984 * the input is valid and that there is space to insert the requested action
987 * @param[in] rss_desc
988 * Pointer to mlx5_flow_rss_desc.
990 * Action configuration.
993 flow_verbs_translate_action_rss(struct mlx5_flow_rss_desc *rss_desc,
994 const struct rte_flow_action *action)
996 const struct rte_flow_action_rss *rss = action->conf;
997 const uint8_t *rss_key;
999 memcpy(rss_desc->queue, rss->queue, rss->queue_num * sizeof(uint16_t));
1000 rss_desc->queue_num = rss->queue_num;
1001 /* NULL RSS key indicates default RSS key. */
1002 rss_key = !rss->key ? rss_hash_default_key : rss->key;
1003 memcpy(rss_desc->key, rss_key, MLX5_RSS_HASH_KEY_LEN);
1005 * rss->level and rss.types should be set in advance when expanding
1011 * Convert the @p action into a Verbs specification. This function assumes that
1012 * the input is valid and that there is space to insert the requested action
1015 * @param[in] dev_flow
1016 * Pointer to mlx5_flow.
1018 * Action configuration.
1021 flow_verbs_translate_action_flag
1022 (struct mlx5_flow *dev_flow,
1023 const struct rte_flow_action *action __rte_unused)
1025 unsigned int size = sizeof(struct ibv_flow_spec_action_tag);
1026 struct ibv_flow_spec_action_tag tag = {
1027 .type = IBV_FLOW_SPEC_ACTION_TAG,
1029 .tag_id = mlx5_flow_mark_set(MLX5_FLOW_MARK_DEFAULT),
1032 flow_verbs_spec_add(&dev_flow->verbs, &tag, size);
1036 * Convert the @p action into a Verbs specification. This function assumes that
1037 * the input is valid and that there is space to insert the requested action
1040 * @param[in] dev_flow
1041 * Pointer to mlx5_flow.
1043 * Action configuration.
1046 flow_verbs_translate_action_mark(struct mlx5_flow *dev_flow,
1047 const struct rte_flow_action *action)
1049 const struct rte_flow_action_mark *mark = action->conf;
1050 unsigned int size = sizeof(struct ibv_flow_spec_action_tag);
1051 struct ibv_flow_spec_action_tag tag = {
1052 .type = IBV_FLOW_SPEC_ACTION_TAG,
1054 .tag_id = mlx5_flow_mark_set(mark->id),
1057 flow_verbs_spec_add(&dev_flow->verbs, &tag, size);
1061 * Convert the @p action into a Verbs specification. This function assumes that
1062 * the input is valid and that there is space to insert the requested action
1066 * Pointer to the Ethernet device structure.
1068 * Action configuration.
1069 * @param[in] dev_flow
1070 * Pointer to mlx5_flow.
1072 * Pointer to error structure.
1075 * 0 On success else a negative errno value is returned and rte_errno is set.
1078 flow_verbs_translate_action_count(struct mlx5_flow *dev_flow,
1079 const struct rte_flow_action *action,
1080 struct rte_eth_dev *dev,
1081 struct rte_flow_error *error)
1083 const struct rte_flow_action_count *count = action->conf;
1084 struct rte_flow *flow = dev_flow->flow;
1085 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42) || \
1086 defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
1087 struct mlx5_flow_counter_pool *pool;
1088 struct mlx5_flow_counter *cnt = NULL;
1089 struct mlx5_flow_counter_ext *cnt_ext;
1090 unsigned int size = sizeof(struct ibv_flow_spec_counter_action);
1091 struct ibv_flow_spec_counter_action counter = {
1092 .type = IBV_FLOW_SPEC_ACTION_COUNT,
1097 if (!flow->counter) {
1098 flow->counter = flow_verbs_counter_new(dev, count->shared,
1101 return rte_flow_error_set(error, rte_errno,
1102 RTE_FLOW_ERROR_TYPE_ACTION,
1104 "cannot get counter"
1107 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42)
1108 cnt = flow_verbs_counter_get_by_idx(dev, flow->counter, &pool);
1109 cnt_ext = MLX5_CNT_TO_CNT_EXT(pool, cnt);
1110 counter.counter_set_handle = cnt_ext->cs->handle;
1111 flow_verbs_spec_add(&dev_flow->verbs, &counter, size);
1112 #elif defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
1113 cnt = flow_verbs_counter_get_by_idx(dev, flow->counter, &pool);
1114 cnt_ext = MLX5_CNT_TO_CNT_EXT(pool, cnt);
1115 counter.counters = cnt_ext->cs;
1116 flow_verbs_spec_add(&dev_flow->verbs, &counter, size);
1122 * Internal validation function. For validating both actions and items.
1125 * Pointer to the Ethernet device structure.
1127 * Pointer to the flow attributes.
1129 * Pointer to the list of items.
1130 * @param[in] actions
1131 * Pointer to the list of actions.
1132 * @param[in] external
1133 * This flow rule is created by request external to PMD.
1134 * @param[in] hairpin
1135 * Number of hairpin TX actions, 0 means classic flow.
1137 * Pointer to the error structure.
1140 * 0 on success, a negative errno value otherwise and rte_errno is set.
1143 flow_verbs_validate(struct rte_eth_dev *dev,
1144 const struct rte_flow_attr *attr,
1145 const struct rte_flow_item items[],
1146 const struct rte_flow_action actions[],
1147 bool external __rte_unused,
1148 int hairpin __rte_unused,
1149 struct rte_flow_error *error)
1152 uint64_t action_flags = 0;
1153 uint64_t item_flags = 0;
1154 uint64_t last_item = 0;
1155 uint8_t next_protocol = 0xff;
1156 uint16_t ether_type = 0;
1160 ret = mlx5_flow_validate_attributes(dev, attr, error);
1163 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
1164 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1167 switch (items->type) {
1168 case RTE_FLOW_ITEM_TYPE_VOID:
1170 case RTE_FLOW_ITEM_TYPE_ETH:
1171 ret = mlx5_flow_validate_item_eth(items, item_flags,
1175 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
1176 MLX5_FLOW_LAYER_OUTER_L2;
1177 if (items->mask != NULL && items->spec != NULL) {
1179 ((const struct rte_flow_item_eth *)
1182 ((const struct rte_flow_item_eth *)
1184 ether_type = rte_be_to_cpu_16(ether_type);
1189 case RTE_FLOW_ITEM_TYPE_VLAN:
1190 ret = mlx5_flow_validate_item_vlan(items, item_flags,
1194 last_item = tunnel ? (MLX5_FLOW_LAYER_INNER_L2 |
1195 MLX5_FLOW_LAYER_INNER_VLAN) :
1196 (MLX5_FLOW_LAYER_OUTER_L2 |
1197 MLX5_FLOW_LAYER_OUTER_VLAN);
1198 if (items->mask != NULL && items->spec != NULL) {
1200 ((const struct rte_flow_item_vlan *)
1201 items->spec)->inner_type;
1203 ((const struct rte_flow_item_vlan *)
1204 items->mask)->inner_type;
1205 ether_type = rte_be_to_cpu_16(ether_type);
1210 case RTE_FLOW_ITEM_TYPE_IPV4:
1211 ret = mlx5_flow_validate_item_ipv4(items, item_flags,
1217 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
1218 MLX5_FLOW_LAYER_OUTER_L3_IPV4;
1219 if (items->mask != NULL &&
1220 ((const struct rte_flow_item_ipv4 *)
1221 items->mask)->hdr.next_proto_id) {
1223 ((const struct rte_flow_item_ipv4 *)
1224 (items->spec))->hdr.next_proto_id;
1226 ((const struct rte_flow_item_ipv4 *)
1227 (items->mask))->hdr.next_proto_id;
1229 /* Reset for inner layer. */
1230 next_protocol = 0xff;
1233 case RTE_FLOW_ITEM_TYPE_IPV6:
1234 ret = mlx5_flow_validate_item_ipv6(items, item_flags,
1240 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
1241 MLX5_FLOW_LAYER_OUTER_L3_IPV6;
1242 if (items->mask != NULL &&
1243 ((const struct rte_flow_item_ipv6 *)
1244 items->mask)->hdr.proto) {
1246 ((const struct rte_flow_item_ipv6 *)
1247 items->spec)->hdr.proto;
1249 ((const struct rte_flow_item_ipv6 *)
1250 items->mask)->hdr.proto;
1252 /* Reset for inner layer. */
1253 next_protocol = 0xff;
1256 case RTE_FLOW_ITEM_TYPE_UDP:
1257 ret = mlx5_flow_validate_item_udp(items, item_flags,
1262 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
1263 MLX5_FLOW_LAYER_OUTER_L4_UDP;
1265 case RTE_FLOW_ITEM_TYPE_TCP:
1266 ret = mlx5_flow_validate_item_tcp
1269 &rte_flow_item_tcp_mask,
1273 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
1274 MLX5_FLOW_LAYER_OUTER_L4_TCP;
1276 case RTE_FLOW_ITEM_TYPE_VXLAN:
1277 ret = mlx5_flow_validate_item_vxlan(items, item_flags,
1281 last_item = MLX5_FLOW_LAYER_VXLAN;
1283 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
1284 ret = mlx5_flow_validate_item_vxlan_gpe(items,
1289 last_item = MLX5_FLOW_LAYER_VXLAN_GPE;
1291 case RTE_FLOW_ITEM_TYPE_GRE:
1292 ret = mlx5_flow_validate_item_gre(items, item_flags,
1293 next_protocol, error);
1296 last_item = MLX5_FLOW_LAYER_GRE;
1298 case RTE_FLOW_ITEM_TYPE_MPLS:
1299 ret = mlx5_flow_validate_item_mpls(dev, items,
1304 last_item = MLX5_FLOW_LAYER_MPLS;
1307 return rte_flow_error_set(error, ENOTSUP,
1308 RTE_FLOW_ERROR_TYPE_ITEM,
1309 NULL, "item not supported");
1311 item_flags |= last_item;
1313 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1314 switch (actions->type) {
1315 case RTE_FLOW_ACTION_TYPE_VOID:
1317 case RTE_FLOW_ACTION_TYPE_FLAG:
1318 ret = mlx5_flow_validate_action_flag(action_flags,
1323 action_flags |= MLX5_FLOW_ACTION_FLAG;
1325 case RTE_FLOW_ACTION_TYPE_MARK:
1326 ret = mlx5_flow_validate_action_mark(actions,
1332 action_flags |= MLX5_FLOW_ACTION_MARK;
1334 case RTE_FLOW_ACTION_TYPE_DROP:
1335 ret = mlx5_flow_validate_action_drop(action_flags,
1340 action_flags |= MLX5_FLOW_ACTION_DROP;
1342 case RTE_FLOW_ACTION_TYPE_QUEUE:
1343 ret = mlx5_flow_validate_action_queue(actions,
1349 action_flags |= MLX5_FLOW_ACTION_QUEUE;
1351 case RTE_FLOW_ACTION_TYPE_RSS:
1352 ret = mlx5_flow_validate_action_rss(actions,
1358 action_flags |= MLX5_FLOW_ACTION_RSS;
1360 case RTE_FLOW_ACTION_TYPE_COUNT:
1361 ret = mlx5_flow_validate_action_count(dev, attr, error);
1364 action_flags |= MLX5_FLOW_ACTION_COUNT;
1367 return rte_flow_error_set(error, ENOTSUP,
1368 RTE_FLOW_ERROR_TYPE_ACTION,
1370 "action not supported");
1374 * Validate the drop action mutual exclusion with other actions.
1375 * Drop action is mutually-exclusive with any other action, except for
1378 if ((action_flags & MLX5_FLOW_ACTION_DROP) &&
1379 (action_flags & ~(MLX5_FLOW_ACTION_DROP | MLX5_FLOW_ACTION_COUNT)))
1380 return rte_flow_error_set(error, EINVAL,
1381 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1382 "Drop action is mutually-exclusive "
1383 "with any other action, except for "
1385 if (!(action_flags & MLX5_FLOW_FATE_ACTIONS))
1386 return rte_flow_error_set(error, EINVAL,
1387 RTE_FLOW_ERROR_TYPE_ACTION, actions,
1388 "no fate action is found");
1393 * Calculate the required bytes that are needed for the action part of the verbs
1396 * @param[in] actions
1397 * Pointer to the list of actions.
1400 * The size of the memory needed for all actions.
1403 flow_verbs_get_actions_size(const struct rte_flow_action actions[])
1407 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1408 switch (actions->type) {
1409 case RTE_FLOW_ACTION_TYPE_VOID:
1411 case RTE_FLOW_ACTION_TYPE_FLAG:
1412 size += sizeof(struct ibv_flow_spec_action_tag);
1414 case RTE_FLOW_ACTION_TYPE_MARK:
1415 size += sizeof(struct ibv_flow_spec_action_tag);
1417 case RTE_FLOW_ACTION_TYPE_DROP:
1418 size += sizeof(struct ibv_flow_spec_action_drop);
1420 case RTE_FLOW_ACTION_TYPE_QUEUE:
1422 case RTE_FLOW_ACTION_TYPE_RSS:
1424 case RTE_FLOW_ACTION_TYPE_COUNT:
1425 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42) || \
1426 defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
1427 size += sizeof(struct ibv_flow_spec_counter_action);
1438 * Calculate the required bytes that are needed for the item part of the verbs
1442 * Pointer to the list of items.
1445 * The size of the memory needed for all items.
1448 flow_verbs_get_items_size(const struct rte_flow_item items[])
1452 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
1453 switch (items->type) {
1454 case RTE_FLOW_ITEM_TYPE_VOID:
1456 case RTE_FLOW_ITEM_TYPE_ETH:
1457 size += sizeof(struct ibv_flow_spec_eth);
1459 case RTE_FLOW_ITEM_TYPE_VLAN:
1460 size += sizeof(struct ibv_flow_spec_eth);
1462 case RTE_FLOW_ITEM_TYPE_IPV4:
1463 size += sizeof(struct ibv_flow_spec_ipv4_ext);
1465 case RTE_FLOW_ITEM_TYPE_IPV6:
1466 size += sizeof(struct ibv_flow_spec_ipv6);
1468 case RTE_FLOW_ITEM_TYPE_UDP:
1469 size += sizeof(struct ibv_flow_spec_tcp_udp);
1471 case RTE_FLOW_ITEM_TYPE_TCP:
1472 size += sizeof(struct ibv_flow_spec_tcp_udp);
1474 case RTE_FLOW_ITEM_TYPE_VXLAN:
1475 size += sizeof(struct ibv_flow_spec_tunnel);
1477 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
1478 size += sizeof(struct ibv_flow_spec_tunnel);
1480 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
1481 case RTE_FLOW_ITEM_TYPE_GRE:
1482 size += sizeof(struct ibv_flow_spec_gre);
1484 case RTE_FLOW_ITEM_TYPE_MPLS:
1485 size += sizeof(struct ibv_flow_spec_mpls);
1488 case RTE_FLOW_ITEM_TYPE_GRE:
1489 size += sizeof(struct ibv_flow_spec_tunnel);
1500 * Internal preparation function. Allocate mlx5_flow with the required size.
1501 * The required size is calculate based on the actions and items. This function
1502 * also returns the detected actions and items for later use.
1505 * Pointer to Ethernet device.
1507 * Pointer to the flow attributes.
1509 * Pointer to the list of items.
1510 * @param[in] actions
1511 * Pointer to the list of actions.
1513 * Pointer to the error structure.
1516 * Pointer to mlx5_flow object on success, otherwise NULL and rte_errno
1519 static struct mlx5_flow *
1520 flow_verbs_prepare(struct rte_eth_dev *dev,
1521 const struct rte_flow_attr *attr __rte_unused,
1522 const struct rte_flow_item items[],
1523 const struct rte_flow_action actions[],
1524 struct rte_flow_error *error)
1527 uint32_t handle_idx = 0;
1528 struct mlx5_flow *dev_flow;
1529 struct mlx5_flow_handle *dev_handle;
1530 struct mlx5_priv *priv = dev->data->dev_private;
1532 size += flow_verbs_get_actions_size(actions);
1533 size += flow_verbs_get_items_size(items);
1534 if (size > MLX5_VERBS_MAX_SPEC_ACT_SIZE) {
1535 rte_flow_error_set(error, E2BIG,
1536 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1537 "Verbs spec/action size too large");
1540 /* In case of corrupting the memory. */
1541 if (priv->flow_idx >= MLX5_NUM_MAX_DEV_FLOWS) {
1542 rte_flow_error_set(error, ENOSPC,
1543 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1544 "not free temporary device flow");
1547 dev_handle = mlx5_ipool_zmalloc(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
1550 rte_flow_error_set(error, ENOMEM,
1551 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1552 "not enough memory to create flow handle");
1555 /* No multi-thread supporting. */
1556 dev_flow = &((struct mlx5_flow *)priv->inter_flows)[priv->flow_idx++];
1557 dev_flow->handle = dev_handle;
1558 dev_flow->handle_idx = handle_idx;
1559 /* Memcpy is used, only size needs to be cleared to 0. */
1560 dev_flow->verbs.size = 0;
1561 dev_flow->verbs.attr.num_of_specs = 0;
1562 dev_flow->ingress = attr->ingress;
1563 dev_flow->hash_fields = 0;
1564 /* Need to set transfer attribute: not supported in Verbs mode. */
1569 * Fill the flow with verb spec.
1572 * Pointer to Ethernet device.
1573 * @param[in, out] dev_flow
1574 * Pointer to the mlx5 flow.
1576 * Pointer to the flow attributes.
1578 * Pointer to the list of items.
1579 * @param[in] actions
1580 * Pointer to the list of actions.
1582 * Pointer to the error structure.
1585 * 0 on success, else a negative errno value otherwise and rte_errno is set.
1588 flow_verbs_translate(struct rte_eth_dev *dev,
1589 struct mlx5_flow *dev_flow,
1590 const struct rte_flow_attr *attr,
1591 const struct rte_flow_item items[],
1592 const struct rte_flow_action actions[],
1593 struct rte_flow_error *error)
1595 uint64_t item_flags = 0;
1596 uint64_t action_flags = 0;
1597 uint64_t priority = attr->priority;
1598 uint32_t subpriority = 0;
1599 struct mlx5_priv *priv = dev->data->dev_private;
1600 struct mlx5_flow_rss_desc *rss_desc = &((struct mlx5_flow_rss_desc *)
1602 [!!priv->flow_nested_idx];
1604 if (priority == MLX5_FLOW_PRIO_RSVD)
1605 priority = priv->config.flow_prio - 1;
1606 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1609 switch (actions->type) {
1610 case RTE_FLOW_ACTION_TYPE_VOID:
1612 case RTE_FLOW_ACTION_TYPE_FLAG:
1613 flow_verbs_translate_action_flag(dev_flow, actions);
1614 action_flags |= MLX5_FLOW_ACTION_FLAG;
1615 dev_flow->handle->mark = 1;
1617 case RTE_FLOW_ACTION_TYPE_MARK:
1618 flow_verbs_translate_action_mark(dev_flow, actions);
1619 action_flags |= MLX5_FLOW_ACTION_MARK;
1620 dev_flow->handle->mark = 1;
1622 case RTE_FLOW_ACTION_TYPE_DROP:
1623 flow_verbs_translate_action_drop(dev_flow, actions);
1624 action_flags |= MLX5_FLOW_ACTION_DROP;
1625 dev_flow->handle->fate_action = MLX5_FLOW_FATE_DROP;
1627 case RTE_FLOW_ACTION_TYPE_QUEUE:
1628 flow_verbs_translate_action_queue(rss_desc, actions);
1629 action_flags |= MLX5_FLOW_ACTION_QUEUE;
1630 dev_flow->handle->fate_action = MLX5_FLOW_FATE_QUEUE;
1632 case RTE_FLOW_ACTION_TYPE_RSS:
1633 flow_verbs_translate_action_rss(rss_desc, actions);
1634 action_flags |= MLX5_FLOW_ACTION_RSS;
1635 dev_flow->handle->fate_action = MLX5_FLOW_FATE_QUEUE;
1637 case RTE_FLOW_ACTION_TYPE_COUNT:
1638 ret = flow_verbs_translate_action_count(dev_flow,
1643 action_flags |= MLX5_FLOW_ACTION_COUNT;
1646 return rte_flow_error_set(error, ENOTSUP,
1647 RTE_FLOW_ERROR_TYPE_ACTION,
1649 "action not supported");
1652 dev_flow->act_flags = action_flags;
1653 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
1654 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1656 switch (items->type) {
1657 case RTE_FLOW_ITEM_TYPE_VOID:
1659 case RTE_FLOW_ITEM_TYPE_ETH:
1660 flow_verbs_translate_item_eth(dev_flow, items,
1662 subpriority = MLX5_PRIORITY_MAP_L2;
1663 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
1664 MLX5_FLOW_LAYER_OUTER_L2;
1666 case RTE_FLOW_ITEM_TYPE_VLAN:
1667 flow_verbs_translate_item_vlan(dev_flow, items,
1669 subpriority = MLX5_PRIORITY_MAP_L2;
1670 item_flags |= tunnel ? (MLX5_FLOW_LAYER_INNER_L2 |
1671 MLX5_FLOW_LAYER_INNER_VLAN) :
1672 (MLX5_FLOW_LAYER_OUTER_L2 |
1673 MLX5_FLOW_LAYER_OUTER_VLAN);
1675 case RTE_FLOW_ITEM_TYPE_IPV4:
1676 flow_verbs_translate_item_ipv4(dev_flow, items,
1678 subpriority = MLX5_PRIORITY_MAP_L3;
1679 dev_flow->hash_fields |=
1680 mlx5_flow_hashfields_adjust
1682 MLX5_IPV4_LAYER_TYPES,
1683 MLX5_IPV4_IBV_RX_HASH);
1684 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
1685 MLX5_FLOW_LAYER_OUTER_L3_IPV4;
1687 case RTE_FLOW_ITEM_TYPE_IPV6:
1688 flow_verbs_translate_item_ipv6(dev_flow, items,
1690 subpriority = MLX5_PRIORITY_MAP_L3;
1691 dev_flow->hash_fields |=
1692 mlx5_flow_hashfields_adjust
1694 MLX5_IPV6_LAYER_TYPES,
1695 MLX5_IPV6_IBV_RX_HASH);
1696 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
1697 MLX5_FLOW_LAYER_OUTER_L3_IPV6;
1699 case RTE_FLOW_ITEM_TYPE_TCP:
1700 flow_verbs_translate_item_tcp(dev_flow, items,
1702 subpriority = MLX5_PRIORITY_MAP_L4;
1703 dev_flow->hash_fields |=
1704 mlx5_flow_hashfields_adjust
1705 (rss_desc, tunnel, ETH_RSS_TCP,
1706 (IBV_RX_HASH_SRC_PORT_TCP |
1707 IBV_RX_HASH_DST_PORT_TCP));
1708 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
1709 MLX5_FLOW_LAYER_OUTER_L4_TCP;
1711 case RTE_FLOW_ITEM_TYPE_UDP:
1712 flow_verbs_translate_item_udp(dev_flow, items,
1714 subpriority = MLX5_PRIORITY_MAP_L4;
1715 dev_flow->hash_fields |=
1716 mlx5_flow_hashfields_adjust
1717 (rss_desc, tunnel, ETH_RSS_UDP,
1718 (IBV_RX_HASH_SRC_PORT_UDP |
1719 IBV_RX_HASH_DST_PORT_UDP));
1720 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
1721 MLX5_FLOW_LAYER_OUTER_L4_UDP;
1723 case RTE_FLOW_ITEM_TYPE_VXLAN:
1724 flow_verbs_translate_item_vxlan(dev_flow, items,
1726 subpriority = MLX5_PRIORITY_MAP_L2;
1727 item_flags |= MLX5_FLOW_LAYER_VXLAN;
1729 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
1730 flow_verbs_translate_item_vxlan_gpe(dev_flow, items,
1732 subpriority = MLX5_PRIORITY_MAP_L2;
1733 item_flags |= MLX5_FLOW_LAYER_VXLAN_GPE;
1735 case RTE_FLOW_ITEM_TYPE_GRE:
1736 flow_verbs_translate_item_gre(dev_flow, items,
1738 subpriority = MLX5_PRIORITY_MAP_L2;
1739 item_flags |= MLX5_FLOW_LAYER_GRE;
1741 case RTE_FLOW_ITEM_TYPE_MPLS:
1742 flow_verbs_translate_item_mpls(dev_flow, items,
1744 subpriority = MLX5_PRIORITY_MAP_L2;
1745 item_flags |= MLX5_FLOW_LAYER_MPLS;
1748 return rte_flow_error_set(error, ENOTSUP,
1749 RTE_FLOW_ERROR_TYPE_ITEM,
1751 "item not supported");
1754 dev_flow->handle->layers = item_flags;
1755 /* Other members of attr will be ignored. */
1756 dev_flow->verbs.attr.priority =
1757 mlx5_flow_adjust_priority(dev, priority, subpriority);
1758 dev_flow->verbs.attr.port = (uint8_t)priv->dev_port;
1763 * Remove the flow from the NIC but keeps it in memory.
1766 * Pointer to the Ethernet device structure.
1767 * @param[in, out] flow
1768 * Pointer to flow structure.
1771 flow_verbs_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
1773 struct mlx5_priv *priv = dev->data->dev_private;
1774 struct mlx5_flow_handle *handle;
1775 uint32_t handle_idx;
1779 SILIST_FOREACH(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW], flow->dev_handles,
1780 handle_idx, handle, next) {
1781 if (handle->ib_flow) {
1782 claim_zero(mlx5_glue->destroy_flow(handle->ib_flow));
1783 handle->ib_flow = NULL;
1785 /* hrxq is union, don't touch it only the flag is set. */
1786 if (handle->rix_hrxq) {
1787 if (handle->fate_action == MLX5_FLOW_FATE_DROP) {
1788 mlx5_hrxq_drop_release(dev);
1789 handle->rix_hrxq = 0;
1790 } else if (handle->fate_action ==
1791 MLX5_FLOW_FATE_QUEUE) {
1792 mlx5_hrxq_release(dev, handle->rix_hrxq);
1793 handle->rix_hrxq = 0;
1796 if (handle->vf_vlan.tag && handle->vf_vlan.created)
1797 mlx5_vlan_vmwa_release(dev, &handle->vf_vlan);
1802 * Remove the flow from the NIC and the memory.
1805 * Pointer to the Ethernet device structure.
1806 * @param[in, out] flow
1807 * Pointer to flow structure.
1810 flow_verbs_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
1812 struct mlx5_priv *priv = dev->data->dev_private;
1813 struct mlx5_flow_handle *handle;
1817 flow_verbs_remove(dev, flow);
1818 while (flow->dev_handles) {
1819 uint32_t tmp_idx = flow->dev_handles;
1821 handle = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
1825 flow->dev_handles = handle->next.next;
1826 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
1829 if (flow->counter) {
1830 flow_verbs_counter_release(dev, flow->counter);
1836 * Apply the flow to the NIC.
1839 * Pointer to the Ethernet device structure.
1840 * @param[in, out] flow
1841 * Pointer to flow structure.
1843 * Pointer to error structure.
1846 * 0 on success, a negative errno value otherwise and rte_errno is set.
1849 flow_verbs_apply(struct rte_eth_dev *dev, struct rte_flow *flow,
1850 struct rte_flow_error *error)
1852 struct mlx5_priv *priv = dev->data->dev_private;
1853 struct mlx5_flow_handle *handle;
1854 struct mlx5_flow *dev_flow;
1855 struct mlx5_hrxq *hrxq;
1856 uint32_t dev_handles;
1860 for (idx = priv->flow_idx - 1; idx >= priv->flow_nested_idx; idx--) {
1861 dev_flow = &((struct mlx5_flow *)priv->inter_flows)[idx];
1862 handle = dev_flow->handle;
1863 if (handle->fate_action == MLX5_FLOW_FATE_DROP) {
1864 hrxq = mlx5_hrxq_drop_new(dev);
1868 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1869 "cannot get drop hash queue");
1874 struct mlx5_flow_rss_desc *rss_desc =
1875 &((struct mlx5_flow_rss_desc *)priv->rss_desc)
1876 [!!priv->flow_nested_idx];
1878 MLX5_ASSERT(rss_desc->queue_num);
1879 hrxq_idx = mlx5_hrxq_get(dev, rss_desc->key,
1880 MLX5_RSS_HASH_KEY_LEN,
1881 dev_flow->hash_fields,
1883 rss_desc->queue_num);
1885 hrxq_idx = mlx5_hrxq_new(dev, rss_desc->key,
1886 MLX5_RSS_HASH_KEY_LEN,
1887 dev_flow->hash_fields,
1889 rss_desc->queue_num,
1891 MLX5_FLOW_LAYER_TUNNEL));
1892 hrxq = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_HRXQ],
1897 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1898 "cannot get hash queue");
1901 handle->rix_hrxq = hrxq_idx;
1904 handle->ib_flow = mlx5_glue->create_flow(hrxq->qp,
1905 &dev_flow->verbs.attr);
1906 if (!handle->ib_flow) {
1907 rte_flow_error_set(error, errno,
1908 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1910 "hardware refuses to create flow");
1913 if (priv->vmwa_context &&
1914 handle->vf_vlan.tag && !handle->vf_vlan.created) {
1916 * The rule contains the VLAN pattern.
1917 * For VF we are going to create VLAN
1918 * interface to make hypervisor set correct
1919 * e-Switch vport context.
1921 mlx5_vlan_vmwa_acquire(dev, &handle->vf_vlan);
1926 err = rte_errno; /* Save rte_errno before cleanup. */
1927 SILIST_FOREACH(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW], flow->dev_handles,
1928 dev_handles, handle, next) {
1929 /* hrxq is union, don't touch it only the flag is set. */
1930 if (handle->rix_hrxq) {
1931 if (handle->fate_action == MLX5_FLOW_FATE_DROP) {
1932 mlx5_hrxq_drop_release(dev);
1933 handle->rix_hrxq = 0;
1934 } else if (handle->fate_action ==
1935 MLX5_FLOW_FATE_QUEUE) {
1936 mlx5_hrxq_release(dev, handle->rix_hrxq);
1937 handle->rix_hrxq = 0;
1940 if (handle->vf_vlan.tag && handle->vf_vlan.created)
1941 mlx5_vlan_vmwa_release(dev, &handle->vf_vlan);
1943 rte_errno = err; /* Restore rte_errno. */
1950 * @see rte_flow_query()
1954 flow_verbs_query(struct rte_eth_dev *dev,
1955 struct rte_flow *flow,
1956 const struct rte_flow_action *actions,
1958 struct rte_flow_error *error)
1962 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1963 switch (actions->type) {
1964 case RTE_FLOW_ACTION_TYPE_VOID:
1966 case RTE_FLOW_ACTION_TYPE_COUNT:
1967 ret = flow_verbs_counter_query(dev, flow, data, error);
1970 return rte_flow_error_set(error, ENOTSUP,
1971 RTE_FLOW_ERROR_TYPE_ACTION,
1973 "action not supported");
1979 const struct mlx5_flow_driver_ops mlx5_flow_verbs_drv_ops = {
1980 .validate = flow_verbs_validate,
1981 .prepare = flow_verbs_prepare,
1982 .translate = flow_verbs_translate,
1983 .apply = flow_verbs_apply,
1984 .remove = flow_verbs_remove,
1985 .destroy = flow_verbs_destroy,
1986 .query = flow_verbs_query,