1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2018 Mellanox Technologies, Ltd
5 #include <netinet/in.h>
11 #include <rte_common.h>
12 #include <rte_ether.h>
13 #include <ethdev_driver.h>
15 #include <rte_flow_driver.h>
16 #include <rte_malloc.h>
19 #include <mlx5_glue.h>
21 #include <mlx5_malloc.h>
23 #include "mlx5_defs.h"
25 #include "mlx5_flow.h"
28 #define VERBS_SPEC_INNER(item_flags) \
29 (!!((item_flags) & MLX5_FLOW_LAYER_TUNNEL) ? IBV_FLOW_SPEC_INNER : 0)
31 /* Map of Verbs to Flow priority with 8 Verbs priorities. */
32 static const uint32_t priority_map_3[][MLX5_PRIORITY_MAP_MAX] = {
33 { 0, 1, 2 }, { 2, 3, 4 }, { 5, 6, 7 },
36 /* Map of Verbs to Flow priority with 16 Verbs priorities. */
37 static const uint32_t priority_map_5[][MLX5_PRIORITY_MAP_MAX] = {
38 { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 },
39 { 9, 10, 11 }, { 12, 13, 14 },
42 /* Verbs specification header. */
43 struct ibv_spec_header {
44 enum ibv_flow_spec_type type;
49 * Discover the maximum number of priority available.
52 * Pointer to the Ethernet device structure.
55 * number of supported flow priority on success, a negative errno
56 * value otherwise and rte_errno is set.
59 mlx5_flow_discover_priorities(struct rte_eth_dev *dev)
61 struct mlx5_priv *priv = dev->data->dev_private;
63 struct ibv_flow_attr attr;
64 struct ibv_flow_spec_eth eth;
65 struct ibv_flow_spec_action_drop drop;
69 .port = (uint8_t)priv->dev_port,
72 .type = IBV_FLOW_SPEC_ETH,
73 .size = sizeof(struct ibv_flow_spec_eth),
76 .size = sizeof(struct ibv_flow_spec_action_drop),
77 .type = IBV_FLOW_SPEC_ACTION_DROP,
80 struct ibv_flow *flow;
81 struct mlx5_hrxq *drop = priv->drop_queue.hrxq;
82 uint16_t vprio[] = { 8, 16 };
86 #if defined(HAVE_MLX5DV_DR_DEVX_PORT) || defined(HAVE_MLX5DV_DR_DEVX_PORT_V35)
87 /* If DevX supported, driver must support 16 verbs flow priorities. */
88 priority = RTE_DIM(priority_map_5);
95 for (i = 0; i != RTE_DIM(vprio); i++) {
96 flow_attr.attr.priority = vprio[i] - 1;
97 flow = mlx5_glue->create_flow(drop->qp, &flow_attr.attr);
100 claim_zero(mlx5_glue->destroy_flow(flow));
105 priority = RTE_DIM(priority_map_3);
108 priority = RTE_DIM(priority_map_5);
113 "port %u verbs maximum priority: %d expected 8/16",
114 dev->data->port_id, priority);
117 #if defined(HAVE_MLX5DV_DR_DEVX_PORT) || defined(HAVE_MLX5DV_DR_DEVX_PORT_V35)
120 DRV_LOG(INFO, "port %u supported flow priorities:"
121 " 0-%d for ingress or egress root table,"
122 " 0-%d for non-root table or transfer root table.",
123 dev->data->port_id, priority - 2,
124 MLX5_NON_ROOT_FLOW_MAX_PRIO - 1);
129 * Adjust flow priority based on the highest layer and the request priority.
132 * Pointer to the Ethernet device structure.
133 * @param[in] priority
134 * The rule base priority.
135 * @param[in] subpriority
136 * The priority based on the items.
142 mlx5_flow_adjust_priority(struct rte_eth_dev *dev, int32_t priority,
143 uint32_t subpriority)
146 struct mlx5_priv *priv = dev->data->dev_private;
148 switch (priv->config.flow_prio) {
149 case RTE_DIM(priority_map_3):
150 res = priority_map_3[priority][subpriority];
152 case RTE_DIM(priority_map_5):
153 res = priority_map_5[priority][subpriority];
160 * Get Verbs flow counter by index.
163 * Pointer to the Ethernet device structure.
165 * mlx5 flow counter index in the container.
167 * mlx5 flow counter pool in the container,
170 * A pointer to the counter, NULL otherwise.
172 static struct mlx5_flow_counter *
173 flow_verbs_counter_get_by_idx(struct rte_eth_dev *dev,
175 struct mlx5_flow_counter_pool **ppool)
177 struct mlx5_priv *priv = dev->data->dev_private;
178 struct mlx5_flow_counter_mng *cmng = &priv->sh->cmng;
179 struct mlx5_flow_counter_pool *pool;
181 idx = (idx - 1) & (MLX5_CNT_SHARED_OFFSET - 1);
182 pool = cmng->pools[idx / MLX5_COUNTERS_PER_POOL];
186 return MLX5_POOL_GET_CNT(pool, idx % MLX5_COUNTERS_PER_POOL);
190 * Create Verbs flow counter with Verbs library.
193 * Pointer to the Ethernet device structure.
194 * @param[in, out] counter
195 * mlx5 flow counter object, contains the counter id,
196 * handle of created Verbs flow counter is returned
197 * in cs field (if counters are supported).
200 * 0 On success else a negative errno value is returned
201 * and rte_errno is set.
204 flow_verbs_counter_create(struct rte_eth_dev *dev,
205 struct mlx5_flow_counter *counter)
207 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42)
208 struct mlx5_priv *priv = dev->data->dev_private;
209 struct ibv_context *ctx = priv->sh->cdev->ctx;
210 struct ibv_counter_set_init_attr init = {
211 .counter_set_id = counter->shared_info.id};
213 counter->dcs_when_free = mlx5_glue->create_counter_set(ctx, &init);
214 if (!counter->dcs_when_free) {
219 #elif defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
220 struct mlx5_priv *priv = dev->data->dev_private;
221 struct ibv_context *ctx = priv->sh->cdev->ctx;
222 struct ibv_counters_init_attr init = {0};
223 struct ibv_counter_attach_attr attach;
226 memset(&attach, 0, sizeof(attach));
227 counter->dcs_when_free = mlx5_glue->create_counters(ctx, &init);
228 if (!counter->dcs_when_free) {
232 attach.counter_desc = IBV_COUNTER_PACKETS;
234 ret = mlx5_glue->attach_counters(counter->dcs_when_free, &attach, NULL);
236 attach.counter_desc = IBV_COUNTER_BYTES;
238 ret = mlx5_glue->attach_counters
239 (counter->dcs_when_free, &attach, NULL);
242 claim_zero(mlx5_glue->destroy_counters(counter->dcs_when_free));
243 counter->dcs_when_free = NULL;
257 * Get a flow counter.
260 * Pointer to the Ethernet device structure.
262 * Counter identifier.
265 * Index to the counter, 0 otherwise and rte_errno is set.
268 flow_verbs_counter_new(struct rte_eth_dev *dev, uint32_t id __rte_unused)
270 struct mlx5_priv *priv = dev->data->dev_private;
271 struct mlx5_flow_counter_mng *cmng = &priv->sh->cmng;
272 struct mlx5_flow_counter_pool *pool = NULL;
273 struct mlx5_flow_counter *cnt = NULL;
274 uint32_t n_valid = cmng->n_valid;
275 uint32_t pool_idx, cnt_idx;
279 for (pool_idx = 0; pool_idx < n_valid; ++pool_idx) {
280 pool = cmng->pools[pool_idx];
283 cnt = TAILQ_FIRST(&pool->counters[0]);
288 struct mlx5_flow_counter_pool **pools;
291 if (n_valid == cmng->n) {
292 /* Resize the container pool array. */
293 size = sizeof(struct mlx5_flow_counter_pool *) *
294 (n_valid + MLX5_CNT_CONTAINER_RESIZE);
295 pools = mlx5_malloc(MLX5_MEM_ZERO, size, 0,
300 memcpy(pools, cmng->pools,
301 sizeof(struct mlx5_flow_counter_pool *) *
303 mlx5_free(cmng->pools);
306 cmng->n += MLX5_CNT_CONTAINER_RESIZE;
308 /* Allocate memory for new pool*/
309 size = sizeof(*pool) + sizeof(*cnt) * MLX5_COUNTERS_PER_POOL;
310 pool = mlx5_malloc(MLX5_MEM_ZERO, size, 0, SOCKET_ID_ANY);
313 for (i = 0; i < MLX5_COUNTERS_PER_POOL; ++i) {
314 cnt = MLX5_POOL_GET_CNT(pool, i);
315 TAILQ_INSERT_HEAD(&pool->counters[0], cnt, next);
317 cnt = MLX5_POOL_GET_CNT(pool, 0);
318 cmng->pools[n_valid] = pool;
322 TAILQ_REMOVE(&pool->counters[0], cnt, next);
323 i = MLX5_CNT_ARRAY_IDX(pool, cnt);
324 cnt_idx = MLX5_MAKE_CNT_IDX(pool_idx, i);
325 /* Create counter with Verbs. */
326 ret = flow_verbs_counter_create(dev, cnt);
328 cnt->dcs_when_active = cnt->dcs_when_free;
333 TAILQ_INSERT_HEAD(&pool->counters[0], cnt, next);
334 /* Some error occurred in Verbs library. */
340 * Release a flow counter.
343 * Pointer to the Ethernet device structure.
345 * Index to the counter handler.
348 flow_verbs_counter_release(struct rte_eth_dev *dev, uint32_t counter)
350 struct mlx5_flow_counter_pool *pool;
351 struct mlx5_flow_counter *cnt;
353 cnt = flow_verbs_counter_get_by_idx(dev, counter, &pool);
354 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42)
355 claim_zero(mlx5_glue->destroy_counter_set
356 ((struct ibv_counter_set *)cnt->dcs_when_active));
357 #elif defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
358 claim_zero(mlx5_glue->destroy_counters
359 ((struct ibv_counters *)cnt->dcs_when_active));
361 TAILQ_INSERT_HEAD(&pool->counters[0], cnt, next);
365 * Query a flow counter via Verbs library call.
367 * @see rte_flow_query()
371 flow_verbs_counter_query(struct rte_eth_dev *dev __rte_unused,
372 struct rte_flow *flow, void *data,
373 struct rte_flow_error *error)
375 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42) || \
376 defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
378 struct mlx5_flow_counter_pool *pool;
379 struct mlx5_flow_counter *cnt = flow_verbs_counter_get_by_idx
380 (dev, flow->counter, &pool);
381 struct rte_flow_query_count *qc = data;
382 uint64_t counters[2] = {0, 0};
383 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42)
384 struct ibv_query_counter_set_attr query_cs_attr = {
385 .dcs_when_free = (struct ibv_counter_set *)
386 cnt->dcs_when_active,
387 .query_flags = IBV_COUNTER_SET_FORCE_UPDATE,
389 struct ibv_counter_set_data query_out = {
391 .outlen = 2 * sizeof(uint64_t),
393 int err = mlx5_glue->query_counter_set(&query_cs_attr,
395 #elif defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
396 int err = mlx5_glue->query_counters
397 ((struct ibv_counters *)cnt->dcs_when_active, counters,
399 IBV_READ_COUNTERS_ATTR_PREFER_CACHED);
402 return rte_flow_error_set
404 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
406 "cannot read counter");
409 qc->hits = counters[0] - cnt->hits;
410 qc->bytes = counters[1] - cnt->bytes;
412 cnt->hits = counters[0];
413 cnt->bytes = counters[1];
417 return rte_flow_error_set(error, EINVAL,
418 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
420 "flow does not have counter");
424 return rte_flow_error_set(error, ENOTSUP,
425 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
427 "counters are not available");
432 * Add a verbs item specification into @p verbs.
435 * Pointer to verbs structure.
437 * Create specification.
439 * Size in bytes of the specification to copy.
442 flow_verbs_spec_add(struct mlx5_flow_verbs_workspace *verbs,
443 void *src, unsigned int size)
449 MLX5_ASSERT(verbs->specs);
450 dst = (void *)(verbs->specs + verbs->size);
451 memcpy(dst, src, size);
452 ++verbs->attr.num_of_specs;
457 * Convert the @p item into a Verbs specification. This function assumes that
458 * the input is valid and that there is space to insert the requested item
461 * @param[in, out] dev_flow
462 * Pointer to dev_flow structure.
464 * Item specification.
465 * @param[in] item_flags
469 flow_verbs_translate_item_eth(struct mlx5_flow *dev_flow,
470 const struct rte_flow_item *item,
473 const struct rte_flow_item_eth *spec = item->spec;
474 const struct rte_flow_item_eth *mask = item->mask;
475 const unsigned int size = sizeof(struct ibv_flow_spec_eth);
476 struct ibv_flow_spec_eth eth = {
477 .type = IBV_FLOW_SPEC_ETH | VERBS_SPEC_INNER(item_flags),
482 mask = &rte_flow_item_eth_mask;
486 memcpy(ð.val.dst_mac, spec->dst.addr_bytes,
488 memcpy(ð.val.src_mac, spec->src.addr_bytes,
490 eth.val.ether_type = spec->type;
491 memcpy(ð.mask.dst_mac, mask->dst.addr_bytes,
493 memcpy(ð.mask.src_mac, mask->src.addr_bytes,
495 eth.mask.ether_type = mask->type;
496 /* Remove unwanted bits from values. */
497 for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i) {
498 eth.val.dst_mac[i] &= eth.mask.dst_mac[i];
499 eth.val.src_mac[i] &= eth.mask.src_mac[i];
501 eth.val.ether_type &= eth.mask.ether_type;
503 flow_verbs_spec_add(&dev_flow->verbs, ð, size);
507 * Update the VLAN tag in the Verbs Ethernet specification.
508 * This function assumes that the input is valid and there is space to add
509 * the requested item.
511 * @param[in, out] attr
512 * Pointer to Verbs attributes structure.
514 * Verbs structure containing the VLAN information to copy.
517 flow_verbs_item_vlan_update(struct ibv_flow_attr *attr,
518 struct ibv_flow_spec_eth *eth)
521 const enum ibv_flow_spec_type search = eth->type;
522 struct ibv_spec_header *hdr = (struct ibv_spec_header *)
523 ((uint8_t *)attr + sizeof(struct ibv_flow_attr));
525 for (i = 0; i != attr->num_of_specs; ++i) {
526 if (hdr->type == search) {
527 struct ibv_flow_spec_eth *e =
528 (struct ibv_flow_spec_eth *)hdr;
530 e->val.vlan_tag = eth->val.vlan_tag;
531 e->mask.vlan_tag = eth->mask.vlan_tag;
532 e->val.ether_type = eth->val.ether_type;
533 e->mask.ether_type = eth->mask.ether_type;
536 hdr = (struct ibv_spec_header *)((uint8_t *)hdr + hdr->size);
541 * Convert the @p item into a Verbs specification. This function assumes that
542 * the input is valid and that there is space to insert the requested item
545 * @param[in, out] dev_flow
546 * Pointer to dev_flow structure.
548 * Item specification.
549 * @param[in] item_flags
553 flow_verbs_translate_item_vlan(struct mlx5_flow *dev_flow,
554 const struct rte_flow_item *item,
557 const struct rte_flow_item_vlan *spec = item->spec;
558 const struct rte_flow_item_vlan *mask = item->mask;
559 unsigned int size = sizeof(struct ibv_flow_spec_eth);
560 const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
561 struct ibv_flow_spec_eth eth = {
562 .type = IBV_FLOW_SPEC_ETH | VERBS_SPEC_INNER(item_flags),
565 const uint32_t l2m = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
566 MLX5_FLOW_LAYER_OUTER_L2;
569 mask = &rte_flow_item_vlan_mask;
571 eth.val.vlan_tag = spec->tci;
572 eth.mask.vlan_tag = mask->tci;
573 eth.val.vlan_tag &= eth.mask.vlan_tag;
574 eth.val.ether_type = spec->inner_type;
575 eth.mask.ether_type = mask->inner_type;
576 eth.val.ether_type &= eth.mask.ether_type;
578 if (!(item_flags & l2m))
579 flow_verbs_spec_add(&dev_flow->verbs, ð, size);
581 flow_verbs_item_vlan_update(&dev_flow->verbs.attr, ð);
583 dev_flow->handle->vf_vlan.tag =
584 rte_be_to_cpu_16(spec->tci) & 0x0fff;
588 * Convert the @p item into a Verbs specification. This function assumes that
589 * the input is valid and that there is space to insert the requested item
592 * @param[in, out] dev_flow
593 * Pointer to dev_flow structure.
595 * Item specification.
596 * @param[in] item_flags
600 flow_verbs_translate_item_ipv4(struct mlx5_flow *dev_flow,
601 const struct rte_flow_item *item,
604 const struct rte_flow_item_ipv4 *spec = item->spec;
605 const struct rte_flow_item_ipv4 *mask = item->mask;
606 unsigned int size = sizeof(struct ibv_flow_spec_ipv4_ext);
607 struct ibv_flow_spec_ipv4_ext ipv4 = {
608 .type = IBV_FLOW_SPEC_IPV4_EXT | VERBS_SPEC_INNER(item_flags),
613 mask = &rte_flow_item_ipv4_mask;
615 ipv4.val = (struct ibv_flow_ipv4_ext_filter){
616 .src_ip = spec->hdr.src_addr,
617 .dst_ip = spec->hdr.dst_addr,
618 .proto = spec->hdr.next_proto_id,
619 .tos = spec->hdr.type_of_service,
621 ipv4.mask = (struct ibv_flow_ipv4_ext_filter){
622 .src_ip = mask->hdr.src_addr,
623 .dst_ip = mask->hdr.dst_addr,
624 .proto = mask->hdr.next_proto_id,
625 .tos = mask->hdr.type_of_service,
627 /* Remove unwanted bits from values. */
628 ipv4.val.src_ip &= ipv4.mask.src_ip;
629 ipv4.val.dst_ip &= ipv4.mask.dst_ip;
630 ipv4.val.proto &= ipv4.mask.proto;
631 ipv4.val.tos &= ipv4.mask.tos;
633 flow_verbs_spec_add(&dev_flow->verbs, &ipv4, size);
637 * Convert the @p item into a Verbs specification. This function assumes that
638 * the input is valid and that there is space to insert the requested item
641 * @param[in, out] dev_flow
642 * Pointer to dev_flow structure.
644 * Item specification.
645 * @param[in] item_flags
649 flow_verbs_translate_item_ipv6(struct mlx5_flow *dev_flow,
650 const struct rte_flow_item *item,
653 const struct rte_flow_item_ipv6 *spec = item->spec;
654 const struct rte_flow_item_ipv6 *mask = item->mask;
655 unsigned int size = sizeof(struct ibv_flow_spec_ipv6);
656 struct ibv_flow_spec_ipv6 ipv6 = {
657 .type = IBV_FLOW_SPEC_IPV6 | VERBS_SPEC_INNER(item_flags),
662 mask = &rte_flow_item_ipv6_mask;
665 uint32_t vtc_flow_val;
666 uint32_t vtc_flow_mask;
668 memcpy(&ipv6.val.src_ip, spec->hdr.src_addr,
669 RTE_DIM(ipv6.val.src_ip));
670 memcpy(&ipv6.val.dst_ip, spec->hdr.dst_addr,
671 RTE_DIM(ipv6.val.dst_ip));
672 memcpy(&ipv6.mask.src_ip, mask->hdr.src_addr,
673 RTE_DIM(ipv6.mask.src_ip));
674 memcpy(&ipv6.mask.dst_ip, mask->hdr.dst_addr,
675 RTE_DIM(ipv6.mask.dst_ip));
676 vtc_flow_val = rte_be_to_cpu_32(spec->hdr.vtc_flow);
677 vtc_flow_mask = rte_be_to_cpu_32(mask->hdr.vtc_flow);
678 ipv6.val.flow_label =
679 rte_cpu_to_be_32((vtc_flow_val & RTE_IPV6_HDR_FL_MASK) >>
680 RTE_IPV6_HDR_FL_SHIFT);
681 ipv6.val.traffic_class = (vtc_flow_val & RTE_IPV6_HDR_TC_MASK) >>
682 RTE_IPV6_HDR_TC_SHIFT;
683 ipv6.val.next_hdr = spec->hdr.proto;
684 ipv6.mask.flow_label =
685 rte_cpu_to_be_32((vtc_flow_mask & RTE_IPV6_HDR_FL_MASK) >>
686 RTE_IPV6_HDR_FL_SHIFT);
687 ipv6.mask.traffic_class = (vtc_flow_mask & RTE_IPV6_HDR_TC_MASK) >>
688 RTE_IPV6_HDR_TC_SHIFT;
689 ipv6.mask.next_hdr = mask->hdr.proto;
690 /* Remove unwanted bits from values. */
691 for (i = 0; i < RTE_DIM(ipv6.val.src_ip); ++i) {
692 ipv6.val.src_ip[i] &= ipv6.mask.src_ip[i];
693 ipv6.val.dst_ip[i] &= ipv6.mask.dst_ip[i];
695 ipv6.val.flow_label &= ipv6.mask.flow_label;
696 ipv6.val.traffic_class &= ipv6.mask.traffic_class;
697 ipv6.val.next_hdr &= ipv6.mask.next_hdr;
699 flow_verbs_spec_add(&dev_flow->verbs, &ipv6, size);
703 * Convert the @p item into a Verbs specification. This function assumes that
704 * the input is valid and that there is space to insert the requested item
707 * @param[in, out] dev_flow
708 * Pointer to dev_flow structure.
710 * Item specification.
711 * @param[in] item_flags
715 flow_verbs_translate_item_tcp(struct mlx5_flow *dev_flow,
716 const struct rte_flow_item *item,
717 uint64_t item_flags __rte_unused)
719 const struct rte_flow_item_tcp *spec = item->spec;
720 const struct rte_flow_item_tcp *mask = item->mask;
721 unsigned int size = sizeof(struct ibv_flow_spec_tcp_udp);
722 struct ibv_flow_spec_tcp_udp tcp = {
723 .type = IBV_FLOW_SPEC_TCP | VERBS_SPEC_INNER(item_flags),
728 mask = &rte_flow_item_tcp_mask;
730 tcp.val.dst_port = spec->hdr.dst_port;
731 tcp.val.src_port = spec->hdr.src_port;
732 tcp.mask.dst_port = mask->hdr.dst_port;
733 tcp.mask.src_port = mask->hdr.src_port;
734 /* Remove unwanted bits from values. */
735 tcp.val.src_port &= tcp.mask.src_port;
736 tcp.val.dst_port &= tcp.mask.dst_port;
738 flow_verbs_spec_add(&dev_flow->verbs, &tcp, size);
742 * Convert the @p item into a Verbs specification. This function assumes that
743 * the input is valid and that there is space to insert the requested item
746 * @param[in, out] dev_flow
747 * Pointer to dev_flow structure.
749 * Item specification.
750 * @param[in] item_flags
754 flow_verbs_translate_item_udp(struct mlx5_flow *dev_flow,
755 const struct rte_flow_item *item,
756 uint64_t item_flags __rte_unused)
758 const struct rte_flow_item_udp *spec = item->spec;
759 const struct rte_flow_item_udp *mask = item->mask;
760 unsigned int size = sizeof(struct ibv_flow_spec_tcp_udp);
761 struct ibv_flow_spec_tcp_udp udp = {
762 .type = IBV_FLOW_SPEC_UDP | VERBS_SPEC_INNER(item_flags),
767 mask = &rte_flow_item_udp_mask;
769 udp.val.dst_port = spec->hdr.dst_port;
770 udp.val.src_port = spec->hdr.src_port;
771 udp.mask.dst_port = mask->hdr.dst_port;
772 udp.mask.src_port = mask->hdr.src_port;
773 /* Remove unwanted bits from values. */
774 udp.val.src_port &= udp.mask.src_port;
775 udp.val.dst_port &= udp.mask.dst_port;
778 while (item->type == RTE_FLOW_ITEM_TYPE_VOID)
780 if (!(udp.val.dst_port & udp.mask.dst_port)) {
781 switch ((item)->type) {
782 case RTE_FLOW_ITEM_TYPE_VXLAN:
783 udp.val.dst_port = htons(MLX5_UDP_PORT_VXLAN);
784 udp.mask.dst_port = 0xffff;
786 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
787 udp.val.dst_port = htons(MLX5_UDP_PORT_VXLAN_GPE);
788 udp.mask.dst_port = 0xffff;
790 case RTE_FLOW_ITEM_TYPE_MPLS:
791 udp.val.dst_port = htons(MLX5_UDP_PORT_MPLS);
792 udp.mask.dst_port = 0xffff;
799 flow_verbs_spec_add(&dev_flow->verbs, &udp, size);
803 * Convert the @p item into a Verbs specification. This function assumes that
804 * the input is valid and that there is space to insert the requested item
807 * @param[in, out] dev_flow
808 * Pointer to dev_flow structure.
810 * Item specification.
811 * @param[in] item_flags
815 flow_verbs_translate_item_vxlan(struct mlx5_flow *dev_flow,
816 const struct rte_flow_item *item,
817 uint64_t item_flags __rte_unused)
819 const struct rte_flow_item_vxlan *spec = item->spec;
820 const struct rte_flow_item_vxlan *mask = item->mask;
821 unsigned int size = sizeof(struct ibv_flow_spec_tunnel);
822 struct ibv_flow_spec_tunnel vxlan = {
823 .type = IBV_FLOW_SPEC_VXLAN_TUNNEL,
829 } id = { .vlan_id = 0, };
832 mask = &rte_flow_item_vxlan_mask;
834 memcpy(&id.vni[1], spec->vni, 3);
835 vxlan.val.tunnel_id = id.vlan_id;
836 memcpy(&id.vni[1], mask->vni, 3);
837 vxlan.mask.tunnel_id = id.vlan_id;
838 /* Remove unwanted bits from values. */
839 vxlan.val.tunnel_id &= vxlan.mask.tunnel_id;
841 flow_verbs_spec_add(&dev_flow->verbs, &vxlan, size);
845 * Convert the @p item into a Verbs specification. This function assumes that
846 * the input is valid and that there is space to insert the requested item
849 * @param[in, out] dev_flow
850 * Pointer to dev_flow structure.
852 * Item specification.
853 * @param[in] item_flags
857 flow_verbs_translate_item_vxlan_gpe(struct mlx5_flow *dev_flow,
858 const struct rte_flow_item *item,
859 uint64_t item_flags __rte_unused)
861 const struct rte_flow_item_vxlan_gpe *spec = item->spec;
862 const struct rte_flow_item_vxlan_gpe *mask = item->mask;
863 unsigned int size = sizeof(struct ibv_flow_spec_tunnel);
864 struct ibv_flow_spec_tunnel vxlan_gpe = {
865 .type = IBV_FLOW_SPEC_VXLAN_TUNNEL,
871 } id = { .vlan_id = 0, };
874 mask = &rte_flow_item_vxlan_gpe_mask;
876 memcpy(&id.vni[1], spec->vni, 3);
877 vxlan_gpe.val.tunnel_id = id.vlan_id;
878 memcpy(&id.vni[1], mask->vni, 3);
879 vxlan_gpe.mask.tunnel_id = id.vlan_id;
880 /* Remove unwanted bits from values. */
881 vxlan_gpe.val.tunnel_id &= vxlan_gpe.mask.tunnel_id;
883 flow_verbs_spec_add(&dev_flow->verbs, &vxlan_gpe, size);
887 * Update the protocol in Verbs IPv4/IPv6 spec.
889 * @param[in, out] attr
890 * Pointer to Verbs attributes structure.
892 * Specification type to search in order to update the IP protocol.
893 * @param[in] protocol
894 * Protocol value to set if none is present in the specification.
897 flow_verbs_item_gre_ip_protocol_update(struct ibv_flow_attr *attr,
898 enum ibv_flow_spec_type search,
902 struct ibv_spec_header *hdr = (struct ibv_spec_header *)
903 ((uint8_t *)attr + sizeof(struct ibv_flow_attr));
907 for (i = 0; i != attr->num_of_specs; ++i) {
908 if (hdr->type == search) {
910 struct ibv_flow_spec_ipv4_ext *ipv4;
911 struct ibv_flow_spec_ipv6 *ipv6;
915 case IBV_FLOW_SPEC_IPV4_EXT:
916 ip.ipv4 = (struct ibv_flow_spec_ipv4_ext *)hdr;
917 if (!ip.ipv4->val.proto) {
918 ip.ipv4->val.proto = protocol;
919 ip.ipv4->mask.proto = 0xff;
922 case IBV_FLOW_SPEC_IPV6:
923 ip.ipv6 = (struct ibv_flow_spec_ipv6 *)hdr;
924 if (!ip.ipv6->val.next_hdr) {
925 ip.ipv6->val.next_hdr = protocol;
926 ip.ipv6->mask.next_hdr = 0xff;
934 hdr = (struct ibv_spec_header *)((uint8_t *)hdr + hdr->size);
939 * Convert the @p item into a Verbs specification. This function assumes that
940 * the input is valid and that there is space to insert the requested item
943 * @param[in, out] dev_flow
944 * Pointer to dev_flow structure.
946 * Item specification.
947 * @param[in] item_flags
951 flow_verbs_translate_item_gre(struct mlx5_flow *dev_flow,
952 const struct rte_flow_item *item __rte_unused,
955 struct mlx5_flow_verbs_workspace *verbs = &dev_flow->verbs;
956 #ifndef HAVE_IBV_DEVICE_MPLS_SUPPORT
957 unsigned int size = sizeof(struct ibv_flow_spec_tunnel);
958 struct ibv_flow_spec_tunnel tunnel = {
959 .type = IBV_FLOW_SPEC_VXLAN_TUNNEL,
963 const struct rte_flow_item_gre *spec = item->spec;
964 const struct rte_flow_item_gre *mask = item->mask;
965 unsigned int size = sizeof(struct ibv_flow_spec_gre);
966 struct ibv_flow_spec_gre tunnel = {
967 .type = IBV_FLOW_SPEC_GRE,
972 mask = &rte_flow_item_gre_mask;
974 tunnel.val.c_ks_res0_ver = spec->c_rsvd0_ver;
975 tunnel.val.protocol = spec->protocol;
976 tunnel.mask.c_ks_res0_ver = mask->c_rsvd0_ver;
977 tunnel.mask.protocol = mask->protocol;
978 /* Remove unwanted bits from values. */
979 tunnel.val.c_ks_res0_ver &= tunnel.mask.c_ks_res0_ver;
980 tunnel.val.protocol &= tunnel.mask.protocol;
981 tunnel.val.key &= tunnel.mask.key;
984 if (item_flags & MLX5_FLOW_LAYER_OUTER_L3_IPV4)
985 flow_verbs_item_gre_ip_protocol_update(&verbs->attr,
986 IBV_FLOW_SPEC_IPV4_EXT,
989 flow_verbs_item_gre_ip_protocol_update(&verbs->attr,
992 flow_verbs_spec_add(verbs, &tunnel, size);
996 * Convert the @p action into a Verbs specification. This function assumes that
997 * the input is valid and that there is space to insert the requested action
998 * into the flow. This function also return the action that was added.
1000 * @param[in, out] dev_flow
1001 * Pointer to dev_flow structure.
1003 * Item specification.
1004 * @param[in] item_flags
1005 * Parsed item flags.
1008 flow_verbs_translate_item_mpls(struct mlx5_flow *dev_flow __rte_unused,
1009 const struct rte_flow_item *item __rte_unused,
1010 uint64_t item_flags __rte_unused)
1012 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
1013 const struct rte_flow_item_mpls *spec = item->spec;
1014 const struct rte_flow_item_mpls *mask = item->mask;
1015 unsigned int size = sizeof(struct ibv_flow_spec_mpls);
1016 struct ibv_flow_spec_mpls mpls = {
1017 .type = IBV_FLOW_SPEC_MPLS,
1022 mask = &rte_flow_item_mpls_mask;
1024 memcpy(&mpls.val.label, spec, sizeof(mpls.val.label));
1025 memcpy(&mpls.mask.label, mask, sizeof(mpls.mask.label));
1026 /* Remove unwanted bits from values. */
1027 mpls.val.label &= mpls.mask.label;
1029 flow_verbs_spec_add(&dev_flow->verbs, &mpls, size);
1034 * Convert the @p action into a Verbs specification. This function assumes that
1035 * the input is valid and that there is space to insert the requested action
1038 * @param[in] dev_flow
1039 * Pointer to mlx5_flow.
1041 * Action configuration.
1044 flow_verbs_translate_action_drop
1045 (struct mlx5_flow *dev_flow,
1046 const struct rte_flow_action *action __rte_unused)
1048 unsigned int size = sizeof(struct ibv_flow_spec_action_drop);
1049 struct ibv_flow_spec_action_drop drop = {
1050 .type = IBV_FLOW_SPEC_ACTION_DROP,
1054 flow_verbs_spec_add(&dev_flow->verbs, &drop, size);
1058 * Convert the @p action into a Verbs specification. This function assumes that
1059 * the input is valid and that there is space to insert the requested action
1062 * @param[in] rss_desc
1063 * Pointer to mlx5_flow_rss_desc.
1065 * Action configuration.
1068 flow_verbs_translate_action_queue(struct mlx5_flow_rss_desc *rss_desc,
1069 const struct rte_flow_action *action)
1071 const struct rte_flow_action_queue *queue = action->conf;
1073 rss_desc->queue[0] = queue->index;
1074 rss_desc->queue_num = 1;
1078 * Convert the @p action into a Verbs specification. This function assumes that
1079 * the input is valid and that there is space to insert the requested action
1082 * @param[in] rss_desc
1083 * Pointer to mlx5_flow_rss_desc.
1085 * Action configuration.
1088 flow_verbs_translate_action_rss(struct mlx5_flow_rss_desc *rss_desc,
1089 const struct rte_flow_action *action)
1091 const struct rte_flow_action_rss *rss = action->conf;
1092 const uint8_t *rss_key;
1094 memcpy(rss_desc->queue, rss->queue, rss->queue_num * sizeof(uint16_t));
1095 rss_desc->queue_num = rss->queue_num;
1096 /* NULL RSS key indicates default RSS key. */
1097 rss_key = !rss->key ? rss_hash_default_key : rss->key;
1098 memcpy(rss_desc->key, rss_key, MLX5_RSS_HASH_KEY_LEN);
1100 * rss->level and rss.types should be set in advance when expanding
1106 * Convert the @p action into a Verbs specification. This function assumes that
1107 * the input is valid and that there is space to insert the requested action
1110 * @param[in] dev_flow
1111 * Pointer to mlx5_flow.
1113 * Action configuration.
1116 flow_verbs_translate_action_flag
1117 (struct mlx5_flow *dev_flow,
1118 const struct rte_flow_action *action __rte_unused)
1120 unsigned int size = sizeof(struct ibv_flow_spec_action_tag);
1121 struct ibv_flow_spec_action_tag tag = {
1122 .type = IBV_FLOW_SPEC_ACTION_TAG,
1124 .tag_id = mlx5_flow_mark_set(MLX5_FLOW_MARK_DEFAULT),
1127 flow_verbs_spec_add(&dev_flow->verbs, &tag, size);
1131 * Convert the @p action into a Verbs specification. This function assumes that
1132 * the input is valid and that there is space to insert the requested action
1135 * @param[in] dev_flow
1136 * Pointer to mlx5_flow.
1138 * Action configuration.
1141 flow_verbs_translate_action_mark(struct mlx5_flow *dev_flow,
1142 const struct rte_flow_action *action)
1144 const struct rte_flow_action_mark *mark = action->conf;
1145 unsigned int size = sizeof(struct ibv_flow_spec_action_tag);
1146 struct ibv_flow_spec_action_tag tag = {
1147 .type = IBV_FLOW_SPEC_ACTION_TAG,
1149 .tag_id = mlx5_flow_mark_set(mark->id),
1152 flow_verbs_spec_add(&dev_flow->verbs, &tag, size);
1156 * Convert the @p action into a Verbs specification. This function assumes that
1157 * the input is valid and that there is space to insert the requested action
1161 * Pointer to the Ethernet device structure.
1163 * Action configuration.
1164 * @param[in] dev_flow
1165 * Pointer to mlx5_flow.
1167 * Pointer to error structure.
1170 * 0 On success else a negative errno value is returned and rte_errno is set.
1173 flow_verbs_translate_action_count(struct mlx5_flow *dev_flow,
1174 const struct rte_flow_action *action,
1175 struct rte_eth_dev *dev,
1176 struct rte_flow_error *error)
1178 const struct rte_flow_action_count *count = action->conf;
1179 struct rte_flow *flow = dev_flow->flow;
1180 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42) || \
1181 defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
1182 struct mlx5_flow_counter_pool *pool;
1183 struct mlx5_flow_counter *cnt = NULL;
1184 unsigned int size = sizeof(struct ibv_flow_spec_counter_action);
1185 struct ibv_flow_spec_counter_action counter = {
1186 .type = IBV_FLOW_SPEC_ACTION_COUNT,
1191 if (!flow->counter) {
1192 flow->counter = flow_verbs_counter_new(dev, count->id);
1194 return rte_flow_error_set(error, rte_errno,
1195 RTE_FLOW_ERROR_TYPE_ACTION,
1197 "cannot get counter"
1200 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42)
1201 cnt = flow_verbs_counter_get_by_idx(dev, flow->counter, &pool);
1202 counter.counter_set_handle =
1203 ((struct ibv_counter_set *)cnt->dcs_when_active)->handle;
1204 flow_verbs_spec_add(&dev_flow->verbs, &counter, size);
1205 #elif defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
1206 cnt = flow_verbs_counter_get_by_idx(dev, flow->counter, &pool);
1207 counter.counters = (struct ibv_counters *)cnt->dcs_when_active;
1208 flow_verbs_spec_add(&dev_flow->verbs, &counter, size);
1214 * Internal validation function. For validating both actions and items.
1217 * Pointer to the Ethernet device structure.
1219 * Pointer to the flow attributes.
1221 * Pointer to the list of items.
1222 * @param[in] actions
1223 * Pointer to the list of actions.
1224 * @param[in] external
1225 * This flow rule is created by request external to PMD.
1226 * @param[in] hairpin
1227 * Number of hairpin TX actions, 0 means classic flow.
1229 * Pointer to the error structure.
1232 * 0 on success, a negative errno value otherwise and rte_errno is set.
1235 flow_verbs_validate(struct rte_eth_dev *dev,
1236 const struct rte_flow_attr *attr,
1237 const struct rte_flow_item items[],
1238 const struct rte_flow_action actions[],
1239 bool external __rte_unused,
1240 int hairpin __rte_unused,
1241 struct rte_flow_error *error)
1244 uint64_t action_flags = 0;
1245 uint64_t item_flags = 0;
1246 uint64_t last_item = 0;
1247 uint8_t next_protocol = 0xff;
1248 uint16_t ether_type = 0;
1249 bool is_empty_vlan = false;
1250 uint16_t udp_dport = 0;
1254 ret = mlx5_flow_validate_attributes(dev, attr, error);
1257 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
1258 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1261 switch (items->type) {
1262 case RTE_FLOW_ITEM_TYPE_VOID:
1264 case RTE_FLOW_ITEM_TYPE_ETH:
1265 ret = mlx5_flow_validate_item_eth(items, item_flags,
1269 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
1270 MLX5_FLOW_LAYER_OUTER_L2;
1271 if (items->mask != NULL && items->spec != NULL) {
1273 ((const struct rte_flow_item_eth *)
1276 ((const struct rte_flow_item_eth *)
1278 if (ether_type == RTE_BE16(RTE_ETHER_TYPE_VLAN))
1279 is_empty_vlan = true;
1280 ether_type = rte_be_to_cpu_16(ether_type);
1285 case RTE_FLOW_ITEM_TYPE_VLAN:
1286 ret = mlx5_flow_validate_item_vlan(items, item_flags,
1290 last_item = tunnel ? (MLX5_FLOW_LAYER_INNER_L2 |
1291 MLX5_FLOW_LAYER_INNER_VLAN) :
1292 (MLX5_FLOW_LAYER_OUTER_L2 |
1293 MLX5_FLOW_LAYER_OUTER_VLAN);
1294 if (items->mask != NULL && items->spec != NULL) {
1296 ((const struct rte_flow_item_vlan *)
1297 items->spec)->inner_type;
1299 ((const struct rte_flow_item_vlan *)
1300 items->mask)->inner_type;
1301 ether_type = rte_be_to_cpu_16(ether_type);
1305 is_empty_vlan = false;
1307 case RTE_FLOW_ITEM_TYPE_IPV4:
1308 ret = mlx5_flow_validate_item_ipv4
1310 last_item, ether_type, NULL,
1311 MLX5_ITEM_RANGE_NOT_ACCEPTED,
1315 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
1316 MLX5_FLOW_LAYER_OUTER_L3_IPV4;
1317 if (items->mask != NULL &&
1318 ((const struct rte_flow_item_ipv4 *)
1319 items->mask)->hdr.next_proto_id) {
1321 ((const struct rte_flow_item_ipv4 *)
1322 (items->spec))->hdr.next_proto_id;
1324 ((const struct rte_flow_item_ipv4 *)
1325 (items->mask))->hdr.next_proto_id;
1327 /* Reset for inner layer. */
1328 next_protocol = 0xff;
1331 case RTE_FLOW_ITEM_TYPE_IPV6:
1332 ret = mlx5_flow_validate_item_ipv6(items, item_flags,
1338 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
1339 MLX5_FLOW_LAYER_OUTER_L3_IPV6;
1340 if (items->mask != NULL &&
1341 ((const struct rte_flow_item_ipv6 *)
1342 items->mask)->hdr.proto) {
1344 ((const struct rte_flow_item_ipv6 *)
1345 items->spec)->hdr.proto;
1347 ((const struct rte_flow_item_ipv6 *)
1348 items->mask)->hdr.proto;
1350 /* Reset for inner layer. */
1351 next_protocol = 0xff;
1354 case RTE_FLOW_ITEM_TYPE_UDP:
1355 ret = mlx5_flow_validate_item_udp(items, item_flags,
1358 const struct rte_flow_item_udp *spec = items->spec;
1359 const struct rte_flow_item_udp *mask = items->mask;
1361 mask = &rte_flow_item_udp_mask;
1363 udp_dport = rte_be_to_cpu_16
1364 (spec->hdr.dst_port &
1365 mask->hdr.dst_port);
1369 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
1370 MLX5_FLOW_LAYER_OUTER_L4_UDP;
1372 case RTE_FLOW_ITEM_TYPE_TCP:
1373 ret = mlx5_flow_validate_item_tcp
1376 &rte_flow_item_tcp_mask,
1380 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
1381 MLX5_FLOW_LAYER_OUTER_L4_TCP;
1383 case RTE_FLOW_ITEM_TYPE_VXLAN:
1384 ret = mlx5_flow_validate_item_vxlan(dev, udp_dport,
1389 last_item = MLX5_FLOW_LAYER_VXLAN;
1391 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
1392 ret = mlx5_flow_validate_item_vxlan_gpe(items,
1397 last_item = MLX5_FLOW_LAYER_VXLAN_GPE;
1399 case RTE_FLOW_ITEM_TYPE_GRE:
1400 ret = mlx5_flow_validate_item_gre(items, item_flags,
1401 next_protocol, error);
1404 last_item = MLX5_FLOW_LAYER_GRE;
1406 case RTE_FLOW_ITEM_TYPE_MPLS:
1407 ret = mlx5_flow_validate_item_mpls(dev, items,
1412 last_item = MLX5_FLOW_LAYER_MPLS;
1414 case RTE_FLOW_ITEM_TYPE_ICMP:
1415 case RTE_FLOW_ITEM_TYPE_ICMP6:
1416 return rte_flow_error_set(error, ENOTSUP,
1417 RTE_FLOW_ERROR_TYPE_ITEM,
1419 "item not supported");
1421 return rte_flow_error_set(error, ENOTSUP,
1422 RTE_FLOW_ERROR_TYPE_ITEM,
1423 NULL, "item not supported");
1425 item_flags |= last_item;
1428 return rte_flow_error_set(error, ENOTSUP,
1429 RTE_FLOW_ERROR_TYPE_ITEM, NULL,
1430 "VLAN matching without vid specification is not supported");
1431 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1432 switch (actions->type) {
1433 case RTE_FLOW_ACTION_TYPE_VOID:
1435 case RTE_FLOW_ACTION_TYPE_FLAG:
1436 ret = mlx5_flow_validate_action_flag(action_flags,
1441 action_flags |= MLX5_FLOW_ACTION_FLAG;
1443 case RTE_FLOW_ACTION_TYPE_MARK:
1444 ret = mlx5_flow_validate_action_mark(actions,
1450 action_flags |= MLX5_FLOW_ACTION_MARK;
1452 case RTE_FLOW_ACTION_TYPE_DROP:
1453 ret = mlx5_flow_validate_action_drop(action_flags,
1458 action_flags |= MLX5_FLOW_ACTION_DROP;
1460 case RTE_FLOW_ACTION_TYPE_QUEUE:
1461 ret = mlx5_flow_validate_action_queue(actions,
1467 action_flags |= MLX5_FLOW_ACTION_QUEUE;
1469 case RTE_FLOW_ACTION_TYPE_RSS:
1470 ret = mlx5_flow_validate_action_rss(actions,
1476 action_flags |= MLX5_FLOW_ACTION_RSS;
1478 case RTE_FLOW_ACTION_TYPE_COUNT:
1479 ret = mlx5_flow_validate_action_count(dev, attr, error);
1482 action_flags |= MLX5_FLOW_ACTION_COUNT;
1485 return rte_flow_error_set(error, ENOTSUP,
1486 RTE_FLOW_ERROR_TYPE_ACTION,
1488 "action not supported");
1492 * Validate the drop action mutual exclusion with other actions.
1493 * Drop action is mutually-exclusive with any other action, except for
1496 if ((action_flags & MLX5_FLOW_ACTION_DROP) &&
1497 (action_flags & ~(MLX5_FLOW_ACTION_DROP | MLX5_FLOW_ACTION_COUNT)))
1498 return rte_flow_error_set(error, EINVAL,
1499 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1500 "Drop action is mutually-exclusive "
1501 "with any other action, except for "
1503 if (!(action_flags & MLX5_FLOW_FATE_ACTIONS))
1504 return rte_flow_error_set(error, EINVAL,
1505 RTE_FLOW_ERROR_TYPE_ACTION, actions,
1506 "no fate action is found");
1511 * Calculate the required bytes that are needed for the action part of the verbs
1514 * @param[in] actions
1515 * Pointer to the list of actions.
1518 * The size of the memory needed for all actions.
1521 flow_verbs_get_actions_size(const struct rte_flow_action actions[])
1525 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1526 switch (actions->type) {
1527 case RTE_FLOW_ACTION_TYPE_VOID:
1529 case RTE_FLOW_ACTION_TYPE_FLAG:
1530 size += sizeof(struct ibv_flow_spec_action_tag);
1532 case RTE_FLOW_ACTION_TYPE_MARK:
1533 size += sizeof(struct ibv_flow_spec_action_tag);
1535 case RTE_FLOW_ACTION_TYPE_DROP:
1536 size += sizeof(struct ibv_flow_spec_action_drop);
1538 case RTE_FLOW_ACTION_TYPE_QUEUE:
1540 case RTE_FLOW_ACTION_TYPE_RSS:
1542 case RTE_FLOW_ACTION_TYPE_COUNT:
1543 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42) || \
1544 defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
1545 size += sizeof(struct ibv_flow_spec_counter_action);
1556 * Calculate the required bytes that are needed for the item part of the verbs
1560 * Pointer to the list of items.
1563 * The size of the memory needed for all items.
1566 flow_verbs_get_items_size(const struct rte_flow_item items[])
1570 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
1571 switch (items->type) {
1572 case RTE_FLOW_ITEM_TYPE_VOID:
1574 case RTE_FLOW_ITEM_TYPE_ETH:
1575 size += sizeof(struct ibv_flow_spec_eth);
1577 case RTE_FLOW_ITEM_TYPE_VLAN:
1578 size += sizeof(struct ibv_flow_spec_eth);
1580 case RTE_FLOW_ITEM_TYPE_IPV4:
1581 size += sizeof(struct ibv_flow_spec_ipv4_ext);
1583 case RTE_FLOW_ITEM_TYPE_IPV6:
1584 size += sizeof(struct ibv_flow_spec_ipv6);
1586 case RTE_FLOW_ITEM_TYPE_UDP:
1587 size += sizeof(struct ibv_flow_spec_tcp_udp);
1589 case RTE_FLOW_ITEM_TYPE_TCP:
1590 size += sizeof(struct ibv_flow_spec_tcp_udp);
1592 case RTE_FLOW_ITEM_TYPE_VXLAN:
1593 size += sizeof(struct ibv_flow_spec_tunnel);
1595 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
1596 size += sizeof(struct ibv_flow_spec_tunnel);
1598 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
1599 case RTE_FLOW_ITEM_TYPE_GRE:
1600 size += sizeof(struct ibv_flow_spec_gre);
1602 case RTE_FLOW_ITEM_TYPE_MPLS:
1603 size += sizeof(struct ibv_flow_spec_mpls);
1606 case RTE_FLOW_ITEM_TYPE_GRE:
1607 size += sizeof(struct ibv_flow_spec_tunnel);
1618 * Internal preparation function. Allocate mlx5_flow with the required size.
1619 * The required size is calculate based on the actions and items. This function
1620 * also returns the detected actions and items for later use.
1623 * Pointer to Ethernet device.
1625 * Pointer to the flow attributes.
1627 * Pointer to the list of items.
1628 * @param[in] actions
1629 * Pointer to the list of actions.
1631 * Pointer to the error structure.
1634 * Pointer to mlx5_flow object on success, otherwise NULL and rte_errno
1637 static struct mlx5_flow *
1638 flow_verbs_prepare(struct rte_eth_dev *dev,
1639 const struct rte_flow_attr *attr __rte_unused,
1640 const struct rte_flow_item items[],
1641 const struct rte_flow_action actions[],
1642 struct rte_flow_error *error)
1645 uint32_t handle_idx = 0;
1646 struct mlx5_flow *dev_flow;
1647 struct mlx5_flow_handle *dev_handle;
1648 struct mlx5_priv *priv = dev->data->dev_private;
1649 struct mlx5_flow_workspace *wks = mlx5_flow_get_thread_workspace();
1652 size += flow_verbs_get_actions_size(actions);
1653 size += flow_verbs_get_items_size(items);
1654 if (size > MLX5_VERBS_MAX_SPEC_ACT_SIZE) {
1655 rte_flow_error_set(error, E2BIG,
1656 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1657 "Verbs spec/action size too large");
1660 /* In case of corrupting the memory. */
1661 if (wks->flow_idx >= MLX5_NUM_MAX_DEV_FLOWS) {
1662 rte_flow_error_set(error, ENOSPC,
1663 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1664 "not free temporary device flow");
1667 dev_handle = mlx5_ipool_zmalloc(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
1670 rte_flow_error_set(error, ENOMEM,
1671 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1672 "not enough memory to create flow handle");
1675 MLX5_ASSERT(wks->flow_idx + 1 < RTE_DIM(wks->flows));
1676 dev_flow = &wks->flows[wks->flow_idx++];
1677 dev_flow->handle = dev_handle;
1678 dev_flow->handle_idx = handle_idx;
1679 /* Memcpy is used, only size needs to be cleared to 0. */
1680 dev_flow->verbs.size = 0;
1681 dev_flow->verbs.attr.num_of_specs = 0;
1682 dev_flow->ingress = attr->ingress;
1683 dev_flow->hash_fields = 0;
1684 /* Need to set transfer attribute: not supported in Verbs mode. */
1689 * Fill the flow with verb spec.
1692 * Pointer to Ethernet device.
1693 * @param[in, out] dev_flow
1694 * Pointer to the mlx5 flow.
1696 * Pointer to the flow attributes.
1698 * Pointer to the list of items.
1699 * @param[in] actions
1700 * Pointer to the list of actions.
1702 * Pointer to the error structure.
1705 * 0 on success, else a negative errno value otherwise and rte_errno is set.
1708 flow_verbs_translate(struct rte_eth_dev *dev,
1709 struct mlx5_flow *dev_flow,
1710 const struct rte_flow_attr *attr,
1711 const struct rte_flow_item items[],
1712 const struct rte_flow_action actions[],
1713 struct rte_flow_error *error)
1715 uint64_t item_flags = 0;
1716 uint64_t action_flags = 0;
1717 uint64_t priority = attr->priority;
1718 uint32_t subpriority = 0;
1719 struct mlx5_priv *priv = dev->data->dev_private;
1720 struct mlx5_flow_workspace *wks = mlx5_flow_get_thread_workspace();
1721 struct mlx5_flow_rss_desc *rss_desc;
1724 rss_desc = &wks->rss_desc;
1725 if (priority == MLX5_FLOW_LOWEST_PRIO_INDICATOR)
1726 priority = priv->config.flow_prio - 1;
1727 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1730 switch (actions->type) {
1731 case RTE_FLOW_ACTION_TYPE_VOID:
1733 case RTE_FLOW_ACTION_TYPE_FLAG:
1734 flow_verbs_translate_action_flag(dev_flow, actions);
1735 action_flags |= MLX5_FLOW_ACTION_FLAG;
1736 dev_flow->handle->mark = 1;
1738 case RTE_FLOW_ACTION_TYPE_MARK:
1739 flow_verbs_translate_action_mark(dev_flow, actions);
1740 action_flags |= MLX5_FLOW_ACTION_MARK;
1741 dev_flow->handle->mark = 1;
1743 case RTE_FLOW_ACTION_TYPE_DROP:
1744 flow_verbs_translate_action_drop(dev_flow, actions);
1745 action_flags |= MLX5_FLOW_ACTION_DROP;
1746 dev_flow->handle->fate_action = MLX5_FLOW_FATE_DROP;
1748 case RTE_FLOW_ACTION_TYPE_QUEUE:
1749 flow_verbs_translate_action_queue(rss_desc, actions);
1750 action_flags |= MLX5_FLOW_ACTION_QUEUE;
1751 dev_flow->handle->fate_action = MLX5_FLOW_FATE_QUEUE;
1753 case RTE_FLOW_ACTION_TYPE_RSS:
1754 flow_verbs_translate_action_rss(rss_desc, actions);
1755 action_flags |= MLX5_FLOW_ACTION_RSS;
1756 dev_flow->handle->fate_action = MLX5_FLOW_FATE_QUEUE;
1758 case RTE_FLOW_ACTION_TYPE_COUNT:
1759 ret = flow_verbs_translate_action_count(dev_flow,
1764 action_flags |= MLX5_FLOW_ACTION_COUNT;
1767 return rte_flow_error_set(error, ENOTSUP,
1768 RTE_FLOW_ERROR_TYPE_ACTION,
1770 "action not supported");
1773 dev_flow->act_flags = action_flags;
1774 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
1775 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1777 switch (items->type) {
1778 case RTE_FLOW_ITEM_TYPE_VOID:
1780 case RTE_FLOW_ITEM_TYPE_ETH:
1781 flow_verbs_translate_item_eth(dev_flow, items,
1783 subpriority = MLX5_PRIORITY_MAP_L2;
1784 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
1785 MLX5_FLOW_LAYER_OUTER_L2;
1787 case RTE_FLOW_ITEM_TYPE_VLAN:
1788 flow_verbs_translate_item_vlan(dev_flow, items,
1790 subpriority = MLX5_PRIORITY_MAP_L2;
1791 item_flags |= tunnel ? (MLX5_FLOW_LAYER_INNER_L2 |
1792 MLX5_FLOW_LAYER_INNER_VLAN) :
1793 (MLX5_FLOW_LAYER_OUTER_L2 |
1794 MLX5_FLOW_LAYER_OUTER_VLAN);
1796 case RTE_FLOW_ITEM_TYPE_IPV4:
1797 flow_verbs_translate_item_ipv4(dev_flow, items,
1799 subpriority = MLX5_PRIORITY_MAP_L3;
1800 dev_flow->hash_fields |=
1801 mlx5_flow_hashfields_adjust
1803 MLX5_IPV4_LAYER_TYPES,
1804 MLX5_IPV4_IBV_RX_HASH);
1805 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
1806 MLX5_FLOW_LAYER_OUTER_L3_IPV4;
1808 case RTE_FLOW_ITEM_TYPE_IPV6:
1809 flow_verbs_translate_item_ipv6(dev_flow, items,
1811 subpriority = MLX5_PRIORITY_MAP_L3;
1812 dev_flow->hash_fields |=
1813 mlx5_flow_hashfields_adjust
1815 MLX5_IPV6_LAYER_TYPES,
1816 MLX5_IPV6_IBV_RX_HASH);
1817 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
1818 MLX5_FLOW_LAYER_OUTER_L3_IPV6;
1820 case RTE_FLOW_ITEM_TYPE_TCP:
1821 flow_verbs_translate_item_tcp(dev_flow, items,
1823 subpriority = MLX5_PRIORITY_MAP_L4;
1824 if (dev_flow->hash_fields != 0)
1825 dev_flow->hash_fields |=
1826 mlx5_flow_hashfields_adjust
1827 (rss_desc, tunnel, ETH_RSS_TCP,
1828 (IBV_RX_HASH_SRC_PORT_TCP |
1829 IBV_RX_HASH_DST_PORT_TCP));
1830 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
1831 MLX5_FLOW_LAYER_OUTER_L4_TCP;
1833 case RTE_FLOW_ITEM_TYPE_UDP:
1834 flow_verbs_translate_item_udp(dev_flow, items,
1836 subpriority = MLX5_PRIORITY_MAP_L4;
1837 if (dev_flow->hash_fields != 0)
1838 dev_flow->hash_fields |=
1839 mlx5_flow_hashfields_adjust
1840 (rss_desc, tunnel, ETH_RSS_UDP,
1841 (IBV_RX_HASH_SRC_PORT_UDP |
1842 IBV_RX_HASH_DST_PORT_UDP));
1843 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
1844 MLX5_FLOW_LAYER_OUTER_L4_UDP;
1846 case RTE_FLOW_ITEM_TYPE_VXLAN:
1847 flow_verbs_translate_item_vxlan(dev_flow, items,
1849 subpriority = MLX5_TUNNEL_PRIO_GET(rss_desc);
1850 item_flags |= MLX5_FLOW_LAYER_VXLAN;
1852 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
1853 flow_verbs_translate_item_vxlan_gpe(dev_flow, items,
1855 subpriority = MLX5_TUNNEL_PRIO_GET(rss_desc);
1856 item_flags |= MLX5_FLOW_LAYER_VXLAN_GPE;
1858 case RTE_FLOW_ITEM_TYPE_GRE:
1859 flow_verbs_translate_item_gre(dev_flow, items,
1861 subpriority = MLX5_TUNNEL_PRIO_GET(rss_desc);
1862 item_flags |= MLX5_FLOW_LAYER_GRE;
1864 case RTE_FLOW_ITEM_TYPE_MPLS:
1865 flow_verbs_translate_item_mpls(dev_flow, items,
1867 subpriority = MLX5_TUNNEL_PRIO_GET(rss_desc);
1868 item_flags |= MLX5_FLOW_LAYER_MPLS;
1871 return rte_flow_error_set(error, ENOTSUP,
1872 RTE_FLOW_ERROR_TYPE_ITEM,
1873 NULL, "item not supported");
1876 dev_flow->handle->layers = item_flags;
1877 /* Other members of attr will be ignored. */
1878 dev_flow->verbs.attr.priority =
1879 mlx5_flow_adjust_priority(dev, priority, subpriority);
1880 dev_flow->verbs.attr.port = (uint8_t)priv->dev_port;
1885 * Remove the flow from the NIC but keeps it in memory.
1888 * Pointer to the Ethernet device structure.
1889 * @param[in, out] flow
1890 * Pointer to flow structure.
1893 flow_verbs_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
1895 struct mlx5_priv *priv = dev->data->dev_private;
1896 struct mlx5_flow_handle *handle;
1897 uint32_t handle_idx;
1901 SILIST_FOREACH(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW], flow->dev_handles,
1902 handle_idx, handle, next) {
1903 if (handle->drv_flow) {
1904 claim_zero(mlx5_glue->destroy_flow(handle->drv_flow));
1905 handle->drv_flow = NULL;
1907 /* hrxq is union, don't touch it only the flag is set. */
1908 if (handle->rix_hrxq &&
1909 handle->fate_action == MLX5_FLOW_FATE_QUEUE) {
1910 mlx5_hrxq_release(dev, handle->rix_hrxq);
1911 handle->rix_hrxq = 0;
1913 if (handle->vf_vlan.tag && handle->vf_vlan.created)
1914 mlx5_vlan_vmwa_release(dev, &handle->vf_vlan);
1919 * Remove the flow from the NIC and the memory.
1922 * Pointer to the Ethernet device structure.
1923 * @param[in, out] flow
1924 * Pointer to flow structure.
1927 flow_verbs_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
1929 struct mlx5_priv *priv = dev->data->dev_private;
1930 struct mlx5_flow_handle *handle;
1934 flow_verbs_remove(dev, flow);
1935 while (flow->dev_handles) {
1936 uint32_t tmp_idx = flow->dev_handles;
1938 handle = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
1942 flow->dev_handles = handle->next.next;
1943 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
1946 if (flow->counter) {
1947 flow_verbs_counter_release(dev, flow->counter);
1953 * Apply the flow to the NIC.
1956 * Pointer to the Ethernet device structure.
1957 * @param[in, out] flow
1958 * Pointer to flow structure.
1960 * Pointer to error structure.
1963 * 0 on success, a negative errno value otherwise and rte_errno is set.
1966 flow_verbs_apply(struct rte_eth_dev *dev, struct rte_flow *flow,
1967 struct rte_flow_error *error)
1969 struct mlx5_priv *priv = dev->data->dev_private;
1970 struct mlx5_flow_handle *handle;
1971 struct mlx5_flow *dev_flow;
1972 struct mlx5_hrxq *hrxq;
1973 uint32_t dev_handles;
1976 struct mlx5_flow_workspace *wks = mlx5_flow_get_thread_workspace();
1979 for (idx = wks->flow_idx - 1; idx >= 0; idx--) {
1980 dev_flow = &wks->flows[idx];
1981 handle = dev_flow->handle;
1982 if (handle->fate_action == MLX5_FLOW_FATE_DROP) {
1983 MLX5_ASSERT(priv->drop_queue.hrxq);
1984 hrxq = priv->drop_queue.hrxq;
1987 struct mlx5_flow_rss_desc *rss_desc = &wks->rss_desc;
1989 MLX5_ASSERT(rss_desc->queue_num);
1990 rss_desc->key_len = MLX5_RSS_HASH_KEY_LEN;
1991 rss_desc->hash_fields = dev_flow->hash_fields;
1992 rss_desc->tunnel = !!(handle->layers &
1993 MLX5_FLOW_LAYER_TUNNEL);
1994 rss_desc->shared_rss = 0;
1995 hrxq_idx = mlx5_hrxq_get(dev, rss_desc);
1996 hrxq = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_HRXQ],
2001 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2002 "cannot get hash queue");
2005 handle->rix_hrxq = hrxq_idx;
2008 handle->drv_flow = mlx5_glue->create_flow
2009 (hrxq->qp, &dev_flow->verbs.attr);
2010 if (!handle->drv_flow) {
2011 rte_flow_error_set(error, errno,
2012 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2014 "hardware refuses to create flow");
2017 if (priv->vmwa_context &&
2018 handle->vf_vlan.tag && !handle->vf_vlan.created) {
2020 * The rule contains the VLAN pattern.
2021 * For VF we are going to create VLAN
2022 * interface to make hypervisor set correct
2023 * e-Switch vport context.
2025 mlx5_vlan_vmwa_acquire(dev, &handle->vf_vlan);
2030 err = rte_errno; /* Save rte_errno before cleanup. */
2031 SILIST_FOREACH(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW], flow->dev_handles,
2032 dev_handles, handle, next) {
2033 /* hrxq is union, don't touch it only the flag is set. */
2034 if (handle->rix_hrxq &&
2035 handle->fate_action == MLX5_FLOW_FATE_QUEUE) {
2036 mlx5_hrxq_release(dev, handle->rix_hrxq);
2037 handle->rix_hrxq = 0;
2039 if (handle->vf_vlan.tag && handle->vf_vlan.created)
2040 mlx5_vlan_vmwa_release(dev, &handle->vf_vlan);
2042 rte_errno = err; /* Restore rte_errno. */
2049 * @see rte_flow_query()
2053 flow_verbs_query(struct rte_eth_dev *dev,
2054 struct rte_flow *flow,
2055 const struct rte_flow_action *actions,
2057 struct rte_flow_error *error)
2061 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
2062 switch (actions->type) {
2063 case RTE_FLOW_ACTION_TYPE_VOID:
2065 case RTE_FLOW_ACTION_TYPE_COUNT:
2066 ret = flow_verbs_counter_query(dev, flow, data, error);
2069 return rte_flow_error_set(error, ENOTSUP,
2070 RTE_FLOW_ERROR_TYPE_ACTION,
2072 "action not supported");
2079 flow_verbs_sync_domain(struct rte_eth_dev *dev, uint32_t domains,
2083 RTE_SET_USED(domains);
2084 RTE_SET_USED(flags);
2089 const struct mlx5_flow_driver_ops mlx5_flow_verbs_drv_ops = {
2090 .validate = flow_verbs_validate,
2091 .prepare = flow_verbs_prepare,
2092 .translate = flow_verbs_translate,
2093 .apply = flow_verbs_apply,
2094 .remove = flow_verbs_remove,
2095 .destroy = flow_verbs_destroy,
2096 .query = flow_verbs_query,
2097 .sync_domain = flow_verbs_sync_domain,