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 <rte_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"
26 #include "mlx5_rxtx.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 },
43 * Discover the maximum number of priority available.
46 * Pointer to the Ethernet device structure.
49 * number of supported flow priority on success, a negative errno
50 * value otherwise and rte_errno is set.
53 mlx5_flow_discover_priorities(struct rte_eth_dev *dev)
55 struct mlx5_priv *priv = dev->data->dev_private;
57 struct ibv_flow_attr attr;
58 struct ibv_flow_spec_eth eth;
59 struct ibv_flow_spec_action_drop drop;
63 .port = (uint8_t)priv->dev_port,
66 .type = IBV_FLOW_SPEC_ETH,
67 .size = sizeof(struct ibv_flow_spec_eth),
70 .size = sizeof(struct ibv_flow_spec_action_drop),
71 .type = IBV_FLOW_SPEC_ACTION_DROP,
74 struct ibv_flow *flow;
75 struct mlx5_hrxq *drop = priv->drop_queue.hrxq;
76 uint16_t vprio[] = { 8, 16 };
84 for (i = 0; i != RTE_DIM(vprio); i++) {
85 flow_attr.attr.priority = vprio[i] - 1;
86 flow = mlx5_glue->create_flow(drop->qp, &flow_attr.attr);
89 claim_zero(mlx5_glue->destroy_flow(flow));
94 priority = RTE_DIM(priority_map_3);
97 priority = RTE_DIM(priority_map_5);
102 "port %u verbs maximum priority: %d expected 8/16",
103 dev->data->port_id, priority);
106 DRV_LOG(INFO, "port %u flow maximum priority: %d",
107 dev->data->port_id, priority);
112 * Adjust flow priority based on the highest layer and the request priority.
115 * Pointer to the Ethernet device structure.
116 * @param[in] priority
117 * The rule base priority.
118 * @param[in] subpriority
119 * The priority based on the items.
125 mlx5_flow_adjust_priority(struct rte_eth_dev *dev, int32_t priority,
126 uint32_t subpriority)
129 struct mlx5_priv *priv = dev->data->dev_private;
131 switch (priv->config.flow_prio) {
132 case RTE_DIM(priority_map_3):
133 res = priority_map_3[priority][subpriority];
135 case RTE_DIM(priority_map_5):
136 res = priority_map_5[priority][subpriority];
143 * Get Verbs flow counter by index.
146 * Pointer to the Ethernet device structure.
148 * mlx5 flow counter index in the container.
150 * mlx5 flow counter pool in the container,
153 * A pointer to the counter, NULL otherwise.
155 static struct mlx5_flow_counter *
156 flow_verbs_counter_get_by_idx(struct rte_eth_dev *dev,
158 struct mlx5_flow_counter_pool **ppool)
160 struct mlx5_priv *priv = dev->data->dev_private;
161 struct mlx5_flow_counter_mng *cmng = &priv->sh->cmng;
162 struct mlx5_flow_counter_pool *pool;
164 idx = (idx - 1) & (MLX5_CNT_SHARED_OFFSET - 1);
165 pool = cmng->pools[idx / MLX5_COUNTERS_PER_POOL];
169 return MLX5_POOL_GET_CNT(pool, idx % MLX5_COUNTERS_PER_POOL);
173 * Create Verbs flow counter with Verbs library.
176 * Pointer to the Ethernet device structure.
177 * @param[in, out] counter
178 * mlx5 flow counter object, contains the counter id,
179 * handle of created Verbs flow counter is returned
180 * in cs field (if counters are supported).
183 * 0 On success else a negative errno value is returned
184 * and rte_errno is set.
187 flow_verbs_counter_create(struct rte_eth_dev *dev,
188 struct mlx5_flow_counter *counter)
190 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42)
191 struct mlx5_priv *priv = dev->data->dev_private;
192 struct ibv_context *ctx = priv->sh->ctx;
193 struct ibv_counter_set_init_attr init = {
194 .counter_set_id = counter->shared_info.id};
196 counter->dcs_when_free = mlx5_glue->create_counter_set(ctx, &init);
197 if (!counter->dcs_when_free) {
202 #elif defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
203 struct mlx5_priv *priv = dev->data->dev_private;
204 struct ibv_context *ctx = priv->sh->ctx;
205 struct ibv_counters_init_attr init = {0};
206 struct ibv_counter_attach_attr attach;
209 memset(&attach, 0, sizeof(attach));
210 counter->dcs_when_free = mlx5_glue->create_counters(ctx, &init);
211 if (!counter->dcs_when_free) {
215 attach.counter_desc = IBV_COUNTER_PACKETS;
217 ret = mlx5_glue->attach_counters(counter->dcs_when_free, &attach, NULL);
219 attach.counter_desc = IBV_COUNTER_BYTES;
221 ret = mlx5_glue->attach_counters
222 (counter->dcs_when_free, &attach, NULL);
225 claim_zero(mlx5_glue->destroy_counters(counter->dcs_when_free));
226 counter->dcs_when_free = NULL;
240 * Get a flow counter.
243 * Pointer to the Ethernet device structure.
245 * Indicate if this counter is shared with other flows.
247 * Counter identifier.
250 * Index to the counter, 0 otherwise and rte_errno is set.
253 flow_verbs_counter_new(struct rte_eth_dev *dev, uint32_t shared, uint32_t id)
255 struct mlx5_priv *priv = dev->data->dev_private;
256 struct mlx5_flow_counter_mng *cmng = &priv->sh->cmng;
257 struct mlx5_flow_counter_pool *pool = NULL;
258 struct mlx5_flow_counter *cnt = NULL;
259 union mlx5_l3t_data data;
260 uint32_t n_valid = cmng->n_valid;
261 uint32_t pool_idx, cnt_idx;
265 if (shared && !mlx5_l3t_get_entry(priv->sh->cnt_id_tbl, id, &data) &&
268 for (pool_idx = 0; pool_idx < n_valid; ++pool_idx) {
269 pool = cmng->pools[pool_idx];
272 cnt = TAILQ_FIRST(&pool->counters[0]);
277 struct mlx5_flow_counter_pool **pools;
280 if (n_valid == cmng->n) {
281 /* Resize the container pool array. */
282 size = sizeof(struct mlx5_flow_counter_pool *) *
283 (n_valid + MLX5_CNT_CONTAINER_RESIZE);
284 pools = mlx5_malloc(MLX5_MEM_ZERO, size, 0,
289 memcpy(pools, cmng->pools,
290 sizeof(struct mlx5_flow_counter_pool *) *
292 mlx5_free(cmng->pools);
295 cmng->n += MLX5_CNT_CONTAINER_RESIZE;
297 /* Allocate memory for new pool*/
298 size = sizeof(*pool) + sizeof(*cnt) * MLX5_COUNTERS_PER_POOL;
299 pool = mlx5_malloc(MLX5_MEM_ZERO, size, 0, SOCKET_ID_ANY);
302 for (i = 0; i < MLX5_COUNTERS_PER_POOL; ++i) {
303 cnt = MLX5_POOL_GET_CNT(pool, i);
304 TAILQ_INSERT_HEAD(&pool->counters[0], cnt, next);
306 cnt = MLX5_POOL_GET_CNT(pool, 0);
307 cmng->pools[n_valid] = pool;
311 TAILQ_REMOVE(&pool->counters[0], cnt, next);
312 i = MLX5_CNT_ARRAY_IDX(pool, cnt);
313 cnt_idx = MLX5_MAKE_CNT_IDX(pool_idx, i);
315 data.dword = cnt_idx;
316 if (mlx5_l3t_set_entry(priv->sh->cnt_id_tbl, id, &data))
318 cnt->shared_info.id = id;
319 cnt_idx |= MLX5_CNT_SHARED_OFFSET;
321 /* Create counter with Verbs. */
322 ret = flow_verbs_counter_create(dev, cnt);
324 cnt->dcs_when_active = cnt->dcs_when_free;
329 TAILQ_INSERT_HEAD(&pool->counters[0], cnt, next);
330 /* Some error occurred in Verbs library. */
336 * Release a flow counter.
339 * Pointer to the Ethernet device structure.
341 * Index to the counter handler.
344 flow_verbs_counter_release(struct rte_eth_dev *dev, uint32_t counter)
346 struct mlx5_priv *priv = dev->data->dev_private;
347 struct mlx5_flow_counter_pool *pool;
348 struct mlx5_flow_counter *cnt;
350 cnt = flow_verbs_counter_get_by_idx(dev, counter, &pool);
351 if (IS_SHARED_CNT(counter) &&
352 mlx5_l3t_clear_entry(priv->sh->cnt_id_tbl, cnt->shared_info.id))
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->shared,
1195 return rte_flow_error_set(error, rte_errno,
1196 RTE_FLOW_ERROR_TYPE_ACTION,
1198 "cannot get counter"
1201 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42)
1202 cnt = flow_verbs_counter_get_by_idx(dev, flow->counter, &pool);
1203 counter.counter_set_handle =
1204 ((struct ibv_counter_set *)cnt->dcs_when_active)->handle;
1205 flow_verbs_spec_add(&dev_flow->verbs, &counter, size);
1206 #elif defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
1207 cnt = flow_verbs_counter_get_by_idx(dev, flow->counter, &pool);
1208 counter.counters = (struct ibv_counters *)cnt->dcs_when_active;
1209 flow_verbs_spec_add(&dev_flow->verbs, &counter, size);
1215 * Internal validation function. For validating both actions and items.
1218 * Pointer to the Ethernet device structure.
1220 * Pointer to the flow attributes.
1222 * Pointer to the list of items.
1223 * @param[in] actions
1224 * Pointer to the list of actions.
1225 * @param[in] external
1226 * This flow rule is created by request external to PMD.
1227 * @param[in] hairpin
1228 * Number of hairpin TX actions, 0 means classic flow.
1230 * Pointer to the error structure.
1233 * 0 on success, a negative errno value otherwise and rte_errno is set.
1236 flow_verbs_validate(struct rte_eth_dev *dev,
1237 const struct rte_flow_attr *attr,
1238 const struct rte_flow_item items[],
1239 const struct rte_flow_action actions[],
1240 bool external __rte_unused,
1241 int hairpin __rte_unused,
1242 struct rte_flow_error *error)
1245 uint64_t action_flags = 0;
1246 uint64_t item_flags = 0;
1247 uint64_t last_item = 0;
1248 uint8_t next_protocol = 0xff;
1249 uint16_t ether_type = 0;
1253 ret = mlx5_flow_validate_attributes(dev, attr, error);
1256 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
1257 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1260 switch (items->type) {
1261 case RTE_FLOW_ITEM_TYPE_VOID:
1263 case RTE_FLOW_ITEM_TYPE_ETH:
1264 ret = mlx5_flow_validate_item_eth(items, item_flags,
1268 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
1269 MLX5_FLOW_LAYER_OUTER_L2;
1270 if (items->mask != NULL && items->spec != NULL) {
1272 ((const struct rte_flow_item_eth *)
1275 ((const struct rte_flow_item_eth *)
1277 ether_type = rte_be_to_cpu_16(ether_type);
1282 case RTE_FLOW_ITEM_TYPE_VLAN:
1283 ret = mlx5_flow_validate_item_vlan(items, item_flags,
1287 last_item = tunnel ? (MLX5_FLOW_LAYER_INNER_L2 |
1288 MLX5_FLOW_LAYER_INNER_VLAN) :
1289 (MLX5_FLOW_LAYER_OUTER_L2 |
1290 MLX5_FLOW_LAYER_OUTER_VLAN);
1291 if (items->mask != NULL && items->spec != NULL) {
1293 ((const struct rte_flow_item_vlan *)
1294 items->spec)->inner_type;
1296 ((const struct rte_flow_item_vlan *)
1297 items->mask)->inner_type;
1298 ether_type = rte_be_to_cpu_16(ether_type);
1303 case RTE_FLOW_ITEM_TYPE_IPV4:
1304 ret = mlx5_flow_validate_item_ipv4
1306 last_item, ether_type, NULL,
1307 MLX5_ITEM_RANGE_NOT_ACCEPTED,
1311 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
1312 MLX5_FLOW_LAYER_OUTER_L3_IPV4;
1313 if (items->mask != NULL &&
1314 ((const struct rte_flow_item_ipv4 *)
1315 items->mask)->hdr.next_proto_id) {
1317 ((const struct rte_flow_item_ipv4 *)
1318 (items->spec))->hdr.next_proto_id;
1320 ((const struct rte_flow_item_ipv4 *)
1321 (items->mask))->hdr.next_proto_id;
1323 /* Reset for inner layer. */
1324 next_protocol = 0xff;
1327 case RTE_FLOW_ITEM_TYPE_IPV6:
1328 ret = mlx5_flow_validate_item_ipv6(items, item_flags,
1334 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
1335 MLX5_FLOW_LAYER_OUTER_L3_IPV6;
1336 if (items->mask != NULL &&
1337 ((const struct rte_flow_item_ipv6 *)
1338 items->mask)->hdr.proto) {
1340 ((const struct rte_flow_item_ipv6 *)
1341 items->spec)->hdr.proto;
1343 ((const struct rte_flow_item_ipv6 *)
1344 items->mask)->hdr.proto;
1346 /* Reset for inner layer. */
1347 next_protocol = 0xff;
1350 case RTE_FLOW_ITEM_TYPE_UDP:
1351 ret = mlx5_flow_validate_item_udp(items, item_flags,
1356 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
1357 MLX5_FLOW_LAYER_OUTER_L4_UDP;
1359 case RTE_FLOW_ITEM_TYPE_TCP:
1360 ret = mlx5_flow_validate_item_tcp
1363 &rte_flow_item_tcp_mask,
1367 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
1368 MLX5_FLOW_LAYER_OUTER_L4_TCP;
1370 case RTE_FLOW_ITEM_TYPE_VXLAN:
1371 ret = mlx5_flow_validate_item_vxlan(items, item_flags,
1375 last_item = MLX5_FLOW_LAYER_VXLAN;
1377 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
1378 ret = mlx5_flow_validate_item_vxlan_gpe(items,
1383 last_item = MLX5_FLOW_LAYER_VXLAN_GPE;
1385 case RTE_FLOW_ITEM_TYPE_GRE:
1386 ret = mlx5_flow_validate_item_gre(items, item_flags,
1387 next_protocol, error);
1390 last_item = MLX5_FLOW_LAYER_GRE;
1392 case RTE_FLOW_ITEM_TYPE_MPLS:
1393 ret = mlx5_flow_validate_item_mpls(dev, items,
1398 last_item = MLX5_FLOW_LAYER_MPLS;
1400 case RTE_FLOW_ITEM_TYPE_ICMP:
1401 case RTE_FLOW_ITEM_TYPE_ICMP6:
1402 return rte_flow_error_set(error, ENOTSUP,
1403 RTE_FLOW_ERROR_TYPE_ITEM,
1405 "item not supported");
1407 return rte_flow_error_set(error, ENOTSUP,
1408 RTE_FLOW_ERROR_TYPE_ITEM,
1409 NULL, "item not supported");
1411 item_flags |= last_item;
1413 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1414 switch (actions->type) {
1415 case RTE_FLOW_ACTION_TYPE_VOID:
1417 case RTE_FLOW_ACTION_TYPE_FLAG:
1418 ret = mlx5_flow_validate_action_flag(action_flags,
1423 action_flags |= MLX5_FLOW_ACTION_FLAG;
1425 case RTE_FLOW_ACTION_TYPE_MARK:
1426 ret = mlx5_flow_validate_action_mark(actions,
1432 action_flags |= MLX5_FLOW_ACTION_MARK;
1434 case RTE_FLOW_ACTION_TYPE_DROP:
1435 ret = mlx5_flow_validate_action_drop(action_flags,
1440 action_flags |= MLX5_FLOW_ACTION_DROP;
1442 case RTE_FLOW_ACTION_TYPE_QUEUE:
1443 ret = mlx5_flow_validate_action_queue(actions,
1449 action_flags |= MLX5_FLOW_ACTION_QUEUE;
1451 case RTE_FLOW_ACTION_TYPE_RSS:
1452 ret = mlx5_flow_validate_action_rss(actions,
1458 action_flags |= MLX5_FLOW_ACTION_RSS;
1460 case RTE_FLOW_ACTION_TYPE_COUNT:
1461 ret = mlx5_flow_validate_action_count(dev, attr, error);
1464 action_flags |= MLX5_FLOW_ACTION_COUNT;
1467 return rte_flow_error_set(error, ENOTSUP,
1468 RTE_FLOW_ERROR_TYPE_ACTION,
1470 "action not supported");
1474 * Validate the drop action mutual exclusion with other actions.
1475 * Drop action is mutually-exclusive with any other action, except for
1478 if ((action_flags & MLX5_FLOW_ACTION_DROP) &&
1479 (action_flags & ~(MLX5_FLOW_ACTION_DROP | MLX5_FLOW_ACTION_COUNT)))
1480 return rte_flow_error_set(error, EINVAL,
1481 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1482 "Drop action is mutually-exclusive "
1483 "with any other action, except for "
1485 if (!(action_flags & MLX5_FLOW_FATE_ACTIONS))
1486 return rte_flow_error_set(error, EINVAL,
1487 RTE_FLOW_ERROR_TYPE_ACTION, actions,
1488 "no fate action is found");
1493 * Calculate the required bytes that are needed for the action part of the verbs
1496 * @param[in] actions
1497 * Pointer to the list of actions.
1500 * The size of the memory needed for all actions.
1503 flow_verbs_get_actions_size(const struct rte_flow_action actions[])
1507 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1508 switch (actions->type) {
1509 case RTE_FLOW_ACTION_TYPE_VOID:
1511 case RTE_FLOW_ACTION_TYPE_FLAG:
1512 size += sizeof(struct ibv_flow_spec_action_tag);
1514 case RTE_FLOW_ACTION_TYPE_MARK:
1515 size += sizeof(struct ibv_flow_spec_action_tag);
1517 case RTE_FLOW_ACTION_TYPE_DROP:
1518 size += sizeof(struct ibv_flow_spec_action_drop);
1520 case RTE_FLOW_ACTION_TYPE_QUEUE:
1522 case RTE_FLOW_ACTION_TYPE_RSS:
1524 case RTE_FLOW_ACTION_TYPE_COUNT:
1525 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42) || \
1526 defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
1527 size += sizeof(struct ibv_flow_spec_counter_action);
1538 * Calculate the required bytes that are needed for the item part of the verbs
1542 * Pointer to the list of items.
1545 * The size of the memory needed for all items.
1548 flow_verbs_get_items_size(const struct rte_flow_item items[])
1552 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
1553 switch (items->type) {
1554 case RTE_FLOW_ITEM_TYPE_VOID:
1556 case RTE_FLOW_ITEM_TYPE_ETH:
1557 size += sizeof(struct ibv_flow_spec_eth);
1559 case RTE_FLOW_ITEM_TYPE_VLAN:
1560 size += sizeof(struct ibv_flow_spec_eth);
1562 case RTE_FLOW_ITEM_TYPE_IPV4:
1563 size += sizeof(struct ibv_flow_spec_ipv4_ext);
1565 case RTE_FLOW_ITEM_TYPE_IPV6:
1566 size += sizeof(struct ibv_flow_spec_ipv6);
1568 case RTE_FLOW_ITEM_TYPE_UDP:
1569 size += sizeof(struct ibv_flow_spec_tcp_udp);
1571 case RTE_FLOW_ITEM_TYPE_TCP:
1572 size += sizeof(struct ibv_flow_spec_tcp_udp);
1574 case RTE_FLOW_ITEM_TYPE_VXLAN:
1575 size += sizeof(struct ibv_flow_spec_tunnel);
1577 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
1578 size += sizeof(struct ibv_flow_spec_tunnel);
1580 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
1581 case RTE_FLOW_ITEM_TYPE_GRE:
1582 size += sizeof(struct ibv_flow_spec_gre);
1584 case RTE_FLOW_ITEM_TYPE_MPLS:
1585 size += sizeof(struct ibv_flow_spec_mpls);
1588 case RTE_FLOW_ITEM_TYPE_GRE:
1589 size += sizeof(struct ibv_flow_spec_tunnel);
1600 * Internal preparation function. Allocate mlx5_flow with the required size.
1601 * The required size is calculate based on the actions and items. This function
1602 * also returns the detected actions and items for later use.
1605 * Pointer to Ethernet device.
1607 * Pointer to the flow attributes.
1609 * Pointer to the list of items.
1610 * @param[in] actions
1611 * Pointer to the list of actions.
1613 * Pointer to the error structure.
1616 * Pointer to mlx5_flow object on success, otherwise NULL and rte_errno
1619 static struct mlx5_flow *
1620 flow_verbs_prepare(struct rte_eth_dev *dev,
1621 const struct rte_flow_attr *attr __rte_unused,
1622 const struct rte_flow_item items[],
1623 const struct rte_flow_action actions[],
1624 struct rte_flow_error *error)
1627 uint32_t handle_idx = 0;
1628 struct mlx5_flow *dev_flow;
1629 struct mlx5_flow_handle *dev_handle;
1630 struct mlx5_priv *priv = dev->data->dev_private;
1631 struct mlx5_flow_workspace *wks = mlx5_flow_get_thread_workspace();
1634 size += flow_verbs_get_actions_size(actions);
1635 size += flow_verbs_get_items_size(items);
1636 if (size > MLX5_VERBS_MAX_SPEC_ACT_SIZE) {
1637 rte_flow_error_set(error, E2BIG,
1638 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1639 "Verbs spec/action size too large");
1642 /* In case of corrupting the memory. */
1643 if (wks->flow_idx >= MLX5_NUM_MAX_DEV_FLOWS) {
1644 rte_flow_error_set(error, ENOSPC,
1645 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1646 "not free temporary device flow");
1649 dev_handle = mlx5_ipool_zmalloc(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
1652 rte_flow_error_set(error, ENOMEM,
1653 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1654 "not enough memory to create flow handle");
1657 MLX5_ASSERT(wks->flow_idx + 1 < RTE_DIM(wks->flows));
1658 dev_flow = &wks->flows[wks->flow_idx++];
1659 dev_flow->handle = dev_handle;
1660 dev_flow->handle_idx = handle_idx;
1661 /* Memcpy is used, only size needs to be cleared to 0. */
1662 dev_flow->verbs.size = 0;
1663 dev_flow->verbs.attr.num_of_specs = 0;
1664 dev_flow->ingress = attr->ingress;
1665 dev_flow->hash_fields = 0;
1666 /* Need to set transfer attribute: not supported in Verbs mode. */
1671 * Fill the flow with verb spec.
1674 * Pointer to Ethernet device.
1675 * @param[in, out] dev_flow
1676 * Pointer to the mlx5 flow.
1678 * Pointer to the flow attributes.
1680 * Pointer to the list of items.
1681 * @param[in] actions
1682 * Pointer to the list of actions.
1684 * Pointer to the error structure.
1687 * 0 on success, else a negative errno value otherwise and rte_errno is set.
1690 flow_verbs_translate(struct rte_eth_dev *dev,
1691 struct mlx5_flow *dev_flow,
1692 const struct rte_flow_attr *attr,
1693 const struct rte_flow_item items[],
1694 const struct rte_flow_action actions[],
1695 struct rte_flow_error *error)
1697 uint64_t item_flags = 0;
1698 uint64_t action_flags = 0;
1699 uint64_t priority = attr->priority;
1700 uint32_t subpriority = 0;
1701 struct mlx5_priv *priv = dev->data->dev_private;
1702 struct mlx5_flow_workspace *wks = mlx5_flow_get_thread_workspace();
1703 struct mlx5_flow_rss_desc *rss_desc;
1706 rss_desc = &wks->rss_desc;
1707 if (priority == MLX5_FLOW_PRIO_RSVD)
1708 priority = priv->config.flow_prio - 1;
1709 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1712 switch (actions->type) {
1713 case RTE_FLOW_ACTION_TYPE_VOID:
1715 case RTE_FLOW_ACTION_TYPE_FLAG:
1716 flow_verbs_translate_action_flag(dev_flow, actions);
1717 action_flags |= MLX5_FLOW_ACTION_FLAG;
1718 dev_flow->handle->mark = 1;
1720 case RTE_FLOW_ACTION_TYPE_MARK:
1721 flow_verbs_translate_action_mark(dev_flow, actions);
1722 action_flags |= MLX5_FLOW_ACTION_MARK;
1723 dev_flow->handle->mark = 1;
1725 case RTE_FLOW_ACTION_TYPE_DROP:
1726 flow_verbs_translate_action_drop(dev_flow, actions);
1727 action_flags |= MLX5_FLOW_ACTION_DROP;
1728 dev_flow->handle->fate_action = MLX5_FLOW_FATE_DROP;
1730 case RTE_FLOW_ACTION_TYPE_QUEUE:
1731 flow_verbs_translate_action_queue(rss_desc, actions);
1732 action_flags |= MLX5_FLOW_ACTION_QUEUE;
1733 dev_flow->handle->fate_action = MLX5_FLOW_FATE_QUEUE;
1735 case RTE_FLOW_ACTION_TYPE_RSS:
1736 flow_verbs_translate_action_rss(rss_desc, actions);
1737 action_flags |= MLX5_FLOW_ACTION_RSS;
1738 dev_flow->handle->fate_action = MLX5_FLOW_FATE_QUEUE;
1740 case RTE_FLOW_ACTION_TYPE_COUNT:
1741 ret = flow_verbs_translate_action_count(dev_flow,
1746 action_flags |= MLX5_FLOW_ACTION_COUNT;
1749 return rte_flow_error_set(error, ENOTSUP,
1750 RTE_FLOW_ERROR_TYPE_ACTION,
1752 "action not supported");
1755 dev_flow->act_flags = action_flags;
1756 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
1757 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1759 switch (items->type) {
1760 case RTE_FLOW_ITEM_TYPE_VOID:
1762 case RTE_FLOW_ITEM_TYPE_ETH:
1763 flow_verbs_translate_item_eth(dev_flow, items,
1765 subpriority = MLX5_PRIORITY_MAP_L2;
1766 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
1767 MLX5_FLOW_LAYER_OUTER_L2;
1769 case RTE_FLOW_ITEM_TYPE_VLAN:
1770 flow_verbs_translate_item_vlan(dev_flow, items,
1772 subpriority = MLX5_PRIORITY_MAP_L2;
1773 item_flags |= tunnel ? (MLX5_FLOW_LAYER_INNER_L2 |
1774 MLX5_FLOW_LAYER_INNER_VLAN) :
1775 (MLX5_FLOW_LAYER_OUTER_L2 |
1776 MLX5_FLOW_LAYER_OUTER_VLAN);
1778 case RTE_FLOW_ITEM_TYPE_IPV4:
1779 flow_verbs_translate_item_ipv4(dev_flow, items,
1781 subpriority = MLX5_PRIORITY_MAP_L3;
1782 dev_flow->hash_fields |=
1783 mlx5_flow_hashfields_adjust
1785 MLX5_IPV4_LAYER_TYPES,
1786 MLX5_IPV4_IBV_RX_HASH);
1787 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
1788 MLX5_FLOW_LAYER_OUTER_L3_IPV4;
1790 case RTE_FLOW_ITEM_TYPE_IPV6:
1791 flow_verbs_translate_item_ipv6(dev_flow, items,
1793 subpriority = MLX5_PRIORITY_MAP_L3;
1794 dev_flow->hash_fields |=
1795 mlx5_flow_hashfields_adjust
1797 MLX5_IPV6_LAYER_TYPES,
1798 MLX5_IPV6_IBV_RX_HASH);
1799 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
1800 MLX5_FLOW_LAYER_OUTER_L3_IPV6;
1802 case RTE_FLOW_ITEM_TYPE_TCP:
1803 flow_verbs_translate_item_tcp(dev_flow, items,
1805 subpriority = MLX5_PRIORITY_MAP_L4;
1806 dev_flow->hash_fields |=
1807 mlx5_flow_hashfields_adjust
1808 (rss_desc, tunnel, ETH_RSS_TCP,
1809 (IBV_RX_HASH_SRC_PORT_TCP |
1810 IBV_RX_HASH_DST_PORT_TCP));
1811 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
1812 MLX5_FLOW_LAYER_OUTER_L4_TCP;
1814 case RTE_FLOW_ITEM_TYPE_UDP:
1815 flow_verbs_translate_item_udp(dev_flow, items,
1817 subpriority = MLX5_PRIORITY_MAP_L4;
1818 dev_flow->hash_fields |=
1819 mlx5_flow_hashfields_adjust
1820 (rss_desc, tunnel, ETH_RSS_UDP,
1821 (IBV_RX_HASH_SRC_PORT_UDP |
1822 IBV_RX_HASH_DST_PORT_UDP));
1823 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
1824 MLX5_FLOW_LAYER_OUTER_L4_UDP;
1826 case RTE_FLOW_ITEM_TYPE_VXLAN:
1827 flow_verbs_translate_item_vxlan(dev_flow, items,
1829 subpriority = MLX5_TUNNEL_PRIO_GET(rss_desc);
1830 item_flags |= MLX5_FLOW_LAYER_VXLAN;
1832 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
1833 flow_verbs_translate_item_vxlan_gpe(dev_flow, items,
1835 subpriority = MLX5_TUNNEL_PRIO_GET(rss_desc);
1836 item_flags |= MLX5_FLOW_LAYER_VXLAN_GPE;
1838 case RTE_FLOW_ITEM_TYPE_GRE:
1839 flow_verbs_translate_item_gre(dev_flow, items,
1841 subpriority = MLX5_TUNNEL_PRIO_GET(rss_desc);
1842 item_flags |= MLX5_FLOW_LAYER_GRE;
1844 case RTE_FLOW_ITEM_TYPE_MPLS:
1845 flow_verbs_translate_item_mpls(dev_flow, items,
1847 subpriority = MLX5_TUNNEL_PRIO_GET(rss_desc);
1848 item_flags |= MLX5_FLOW_LAYER_MPLS;
1851 return rte_flow_error_set(error, ENOTSUP,
1852 RTE_FLOW_ERROR_TYPE_ITEM,
1853 NULL, "item not supported");
1856 dev_flow->handle->layers = item_flags;
1857 /* Other members of attr will be ignored. */
1858 dev_flow->verbs.attr.priority =
1859 mlx5_flow_adjust_priority(dev, priority, subpriority);
1860 dev_flow->verbs.attr.port = (uint8_t)priv->dev_port;
1865 * Remove the flow from the NIC but keeps it in memory.
1868 * Pointer to the Ethernet device structure.
1869 * @param[in, out] flow
1870 * Pointer to flow structure.
1873 flow_verbs_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
1875 struct mlx5_priv *priv = dev->data->dev_private;
1876 struct mlx5_flow_handle *handle;
1877 uint32_t handle_idx;
1881 SILIST_FOREACH(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW], flow->dev_handles,
1882 handle_idx, handle, next) {
1883 if (handle->drv_flow) {
1884 claim_zero(mlx5_glue->destroy_flow(handle->drv_flow));
1885 handle->drv_flow = NULL;
1887 /* hrxq is union, don't touch it only the flag is set. */
1888 if (handle->rix_hrxq &&
1889 handle->fate_action == MLX5_FLOW_FATE_QUEUE) {
1890 mlx5_hrxq_release(dev, handle->rix_hrxq);
1891 handle->rix_hrxq = 0;
1893 if (handle->vf_vlan.tag && handle->vf_vlan.created)
1894 mlx5_vlan_vmwa_release(dev, &handle->vf_vlan);
1899 * Remove the flow from the NIC and the memory.
1902 * Pointer to the Ethernet device structure.
1903 * @param[in, out] flow
1904 * Pointer to flow structure.
1907 flow_verbs_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
1909 struct mlx5_priv *priv = dev->data->dev_private;
1910 struct mlx5_flow_handle *handle;
1914 flow_verbs_remove(dev, flow);
1915 while (flow->dev_handles) {
1916 uint32_t tmp_idx = flow->dev_handles;
1918 handle = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
1922 flow->dev_handles = handle->next.next;
1923 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
1926 if (flow->counter) {
1927 flow_verbs_counter_release(dev, flow->counter);
1933 * Apply the flow to the NIC.
1936 * Pointer to the Ethernet device structure.
1937 * @param[in, out] flow
1938 * Pointer to flow structure.
1940 * Pointer to error structure.
1943 * 0 on success, a negative errno value otherwise and rte_errno is set.
1946 flow_verbs_apply(struct rte_eth_dev *dev, struct rte_flow *flow,
1947 struct rte_flow_error *error)
1949 struct mlx5_priv *priv = dev->data->dev_private;
1950 struct mlx5_flow_handle *handle;
1951 struct mlx5_flow *dev_flow;
1952 struct mlx5_hrxq *hrxq;
1953 uint32_t dev_handles;
1956 struct mlx5_flow_workspace *wks = mlx5_flow_get_thread_workspace();
1959 for (idx = wks->flow_idx - 1; idx >= 0; idx--) {
1960 dev_flow = &wks->flows[idx];
1961 handle = dev_flow->handle;
1962 if (handle->fate_action == MLX5_FLOW_FATE_DROP) {
1963 MLX5_ASSERT(priv->drop_queue.hrxq);
1964 hrxq = priv->drop_queue.hrxq;
1967 struct mlx5_flow_rss_desc *rss_desc = &wks->rss_desc;
1969 MLX5_ASSERT(rss_desc->queue_num);
1970 rss_desc->key_len = MLX5_RSS_HASH_KEY_LEN;
1971 rss_desc->hash_fields = dev_flow->hash_fields;
1972 rss_desc->tunnel = !!(handle->layers &
1973 MLX5_FLOW_LAYER_TUNNEL);
1974 rss_desc->shared_rss = 0;
1975 hrxq_idx = mlx5_hrxq_get(dev, rss_desc);
1976 hrxq = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_HRXQ],
1981 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1982 "cannot get hash queue");
1985 handle->rix_hrxq = hrxq_idx;
1988 handle->drv_flow = mlx5_glue->create_flow
1989 (hrxq->qp, &dev_flow->verbs.attr);
1990 if (!handle->drv_flow) {
1991 rte_flow_error_set(error, errno,
1992 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1994 "hardware refuses to create flow");
1997 if (priv->vmwa_context &&
1998 handle->vf_vlan.tag && !handle->vf_vlan.created) {
2000 * The rule contains the VLAN pattern.
2001 * For VF we are going to create VLAN
2002 * interface to make hypervisor set correct
2003 * e-Switch vport context.
2005 mlx5_vlan_vmwa_acquire(dev, &handle->vf_vlan);
2010 err = rte_errno; /* Save rte_errno before cleanup. */
2011 SILIST_FOREACH(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW], flow->dev_handles,
2012 dev_handles, handle, next) {
2013 /* hrxq is union, don't touch it only the flag is set. */
2014 if (handle->rix_hrxq &&
2015 handle->fate_action == MLX5_FLOW_FATE_QUEUE) {
2016 mlx5_hrxq_release(dev, handle->rix_hrxq);
2017 handle->rix_hrxq = 0;
2019 if (handle->vf_vlan.tag && handle->vf_vlan.created)
2020 mlx5_vlan_vmwa_release(dev, &handle->vf_vlan);
2022 rte_errno = err; /* Restore rte_errno. */
2029 * @see rte_flow_query()
2033 flow_verbs_query(struct rte_eth_dev *dev,
2034 struct rte_flow *flow,
2035 const struct rte_flow_action *actions,
2037 struct rte_flow_error *error)
2041 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
2042 switch (actions->type) {
2043 case RTE_FLOW_ACTION_TYPE_VOID:
2045 case RTE_FLOW_ACTION_TYPE_COUNT:
2046 ret = flow_verbs_counter_query(dev, flow, data, error);
2049 return rte_flow_error_set(error, ENOTSUP,
2050 RTE_FLOW_ERROR_TYPE_ACTION,
2052 "action not supported");
2059 flow_verbs_sync_domain(struct rte_eth_dev *dev, uint32_t domains,
2063 RTE_SET_USED(domains);
2064 RTE_SET_USED(flags);
2069 const struct mlx5_flow_driver_ops mlx5_flow_verbs_drv_ops = {
2070 .validate = flow_verbs_validate,
2071 .prepare = flow_verbs_prepare,
2072 .translate = flow_verbs_translate,
2073 .apply = flow_verbs_apply,
2074 .remove = flow_verbs_remove,
2075 .destroy = flow_verbs_destroy,
2076 .query = flow_verbs_query,
2077 .sync_domain = flow_verbs_sync_domain,