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 = mlx5_drop_action_create(dev);
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));
92 mlx5_drop_action_destroy(dev);
95 priority = RTE_DIM(priority_map_3);
98 priority = RTE_DIM(priority_map_5);
103 "port %u verbs maximum priority: %d expected 8/16",
104 dev->data->port_id, priority);
107 DRV_LOG(INFO, "port %u flow maximum priority: %d",
108 dev->data->port_id, priority);
113 * Adjust flow priority based on the highest layer and the request priority.
116 * Pointer to the Ethernet device structure.
117 * @param[in] priority
118 * The rule base priority.
119 * @param[in] subpriority
120 * The priority based on the items.
126 mlx5_flow_adjust_priority(struct rte_eth_dev *dev, int32_t priority,
127 uint32_t subpriority)
130 struct mlx5_priv *priv = dev->data->dev_private;
132 switch (priv->config.flow_prio) {
133 case RTE_DIM(priority_map_3):
134 res = priority_map_3[priority][subpriority];
136 case RTE_DIM(priority_map_5):
137 res = priority_map_5[priority][subpriority];
144 * Get Verbs flow counter by index.
147 * Pointer to the Ethernet device structure.
149 * mlx5 flow counter index in the container.
151 * mlx5 flow counter pool in the container,
154 * A pointer to the counter, NULL otherwise.
156 static struct mlx5_flow_counter *
157 flow_verbs_counter_get_by_idx(struct rte_eth_dev *dev,
159 struct mlx5_flow_counter_pool **ppool)
161 struct mlx5_priv *priv = dev->data->dev_private;
162 struct mlx5_flow_counter_mng *cmng = &priv->sh->cmng;
163 struct mlx5_flow_counter_pool *pool;
165 idx = (idx - 1) & (MLX5_CNT_SHARED_OFFSET - 1);
166 pool = cmng->pools[idx / MLX5_COUNTERS_PER_POOL];
170 return MLX5_POOL_GET_CNT(pool, idx % MLX5_COUNTERS_PER_POOL);
174 * Create Verbs flow counter with Verbs library.
177 * Pointer to the Ethernet device structure.
178 * @param[in, out] counter
179 * mlx5 flow counter object, contains the counter id,
180 * handle of created Verbs flow counter is returned
181 * in cs field (if counters are supported).
184 * 0 On success else a negative errno value is returned
185 * and rte_errno is set.
188 flow_verbs_counter_create(struct rte_eth_dev *dev,
189 struct mlx5_flow_counter *counter)
191 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42)
192 struct mlx5_priv *priv = dev->data->dev_private;
193 struct ibv_context *ctx = priv->sh->ctx;
194 struct ibv_counter_set_init_attr init = {
195 .counter_set_id = counter->shared_info.id};
197 counter->dcs_when_free = mlx5_glue->create_counter_set(ctx, &init);
198 if (!counter->dcs_when_free) {
203 #elif defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
204 struct mlx5_priv *priv = dev->data->dev_private;
205 struct ibv_context *ctx = priv->sh->ctx;
206 struct ibv_counters_init_attr init = {0};
207 struct ibv_counter_attach_attr attach;
210 memset(&attach, 0, sizeof(attach));
211 counter->dcs_when_free = mlx5_glue->create_counters(ctx, &init);
212 if (!counter->dcs_when_free) {
216 attach.counter_desc = IBV_COUNTER_PACKETS;
218 ret = mlx5_glue->attach_counters(counter->dcs_when_free, &attach, NULL);
220 attach.counter_desc = IBV_COUNTER_BYTES;
222 ret = mlx5_glue->attach_counters
223 (counter->dcs_when_free, &attach, NULL);
226 claim_zero(mlx5_glue->destroy_counters(counter->dcs_when_free));
227 counter->dcs_when_free = NULL;
241 * Get a flow counter.
244 * Pointer to the Ethernet device structure.
246 * Indicate if this counter is shared with other flows.
248 * Counter identifier.
251 * Index to the counter, 0 otherwise and rte_errno is set.
254 flow_verbs_counter_new(struct rte_eth_dev *dev, uint32_t shared, uint32_t id)
256 struct mlx5_priv *priv = dev->data->dev_private;
257 struct mlx5_flow_counter_mng *cmng = &priv->sh->cmng;
258 struct mlx5_flow_counter_pool *pool = NULL;
259 struct mlx5_flow_counter *cnt = NULL;
260 union mlx5_l3t_data data;
261 uint32_t n_valid = cmng->n_valid;
262 uint32_t pool_idx, cnt_idx;
266 if (shared && !mlx5_l3t_get_entry(priv->sh->cnt_id_tbl, id, &data) &&
269 for (pool_idx = 0; pool_idx < n_valid; ++pool_idx) {
270 pool = cmng->pools[pool_idx];
273 cnt = TAILQ_FIRST(&pool->counters[0]);
278 struct mlx5_flow_counter_pool **pools;
281 if (n_valid == cmng->n) {
282 /* Resize the container pool array. */
283 size = sizeof(struct mlx5_flow_counter_pool *) *
284 (n_valid + MLX5_CNT_CONTAINER_RESIZE);
285 pools = mlx5_malloc(MLX5_MEM_ZERO, size, 0,
290 memcpy(pools, cmng->pools,
291 sizeof(struct mlx5_flow_counter_pool *) *
293 mlx5_free(cmng->pools);
296 cmng->n += MLX5_CNT_CONTAINER_RESIZE;
298 /* Allocate memory for new pool*/
299 size = sizeof(*pool) + sizeof(*cnt) * MLX5_COUNTERS_PER_POOL;
300 pool = mlx5_malloc(MLX5_MEM_ZERO, size, 0, SOCKET_ID_ANY);
303 for (i = 0; i < MLX5_COUNTERS_PER_POOL; ++i) {
304 cnt = MLX5_POOL_GET_CNT(pool, i);
305 TAILQ_INSERT_HEAD(&pool->counters[0], cnt, next);
307 cnt = MLX5_POOL_GET_CNT(pool, 0);
308 cmng->pools[n_valid] = pool;
312 TAILQ_REMOVE(&pool->counters[0], cnt, next);
313 i = MLX5_CNT_ARRAY_IDX(pool, cnt);
314 cnt_idx = MLX5_MAKE_CNT_IDX(pool_idx, i);
316 data.dword = cnt_idx;
317 if (mlx5_l3t_set_entry(priv->sh->cnt_id_tbl, id, &data))
319 cnt->shared_info.id = id;
320 cnt_idx |= MLX5_CNT_SHARED_OFFSET;
322 /* Create counter with Verbs. */
323 ret = flow_verbs_counter_create(dev, cnt);
325 cnt->dcs_when_active = cnt->dcs_when_free;
330 TAILQ_INSERT_HEAD(&pool->counters[0], cnt, next);
331 /* Some error occurred in Verbs library. */
337 * Release a flow counter.
340 * Pointer to the Ethernet device structure.
342 * Index to the counter handler.
345 flow_verbs_counter_release(struct rte_eth_dev *dev, uint32_t counter)
347 struct mlx5_priv *priv = dev->data->dev_private;
348 struct mlx5_flow_counter_pool *pool;
349 struct mlx5_flow_counter *cnt;
351 cnt = flow_verbs_counter_get_by_idx(dev, counter, &pool);
352 if (IS_SHARED_CNT(counter) &&
353 mlx5_l3t_clear_entry(priv->sh->cnt_id_tbl, cnt->shared_info.id))
355 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42)
356 claim_zero(mlx5_glue->destroy_counter_set
357 ((struct ibv_counter_set *)cnt->dcs_when_active));
358 #elif defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
359 claim_zero(mlx5_glue->destroy_counters
360 ((struct ibv_counters *)cnt->dcs_when_active));
362 TAILQ_INSERT_HEAD(&pool->counters[0], cnt, next);
366 * Query a flow counter via Verbs library call.
368 * @see rte_flow_query()
372 flow_verbs_counter_query(struct rte_eth_dev *dev __rte_unused,
373 struct rte_flow *flow, void *data,
374 struct rte_flow_error *error)
376 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42) || \
377 defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
379 struct mlx5_flow_counter_pool *pool;
380 struct mlx5_flow_counter *cnt = flow_verbs_counter_get_by_idx
381 (dev, flow->counter, &pool);
382 struct rte_flow_query_count *qc = data;
383 uint64_t counters[2] = {0, 0};
384 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42)
385 struct ibv_query_counter_set_attr query_cs_attr = {
386 .dcs_when_free = (struct ibv_counter_set *)
387 cnt->dcs_when_active,
388 .query_flags = IBV_COUNTER_SET_FORCE_UPDATE,
390 struct ibv_counter_set_data query_out = {
392 .outlen = 2 * sizeof(uint64_t),
394 int err = mlx5_glue->query_counter_set(&query_cs_attr,
396 #elif defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
397 int err = mlx5_glue->query_counters
398 ((struct ibv_counters *)cnt->dcs_when_active, counters,
400 IBV_READ_COUNTERS_ATTR_PREFER_CACHED);
403 return rte_flow_error_set
405 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
407 "cannot read counter");
410 qc->hits = counters[0] - cnt->hits;
411 qc->bytes = counters[1] - cnt->bytes;
413 cnt->hits = counters[0];
414 cnt->bytes = counters[1];
418 return rte_flow_error_set(error, EINVAL,
419 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
421 "flow does not have counter");
425 return rte_flow_error_set(error, ENOTSUP,
426 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
428 "counters are not available");
433 * Add a verbs item specification into @p verbs.
436 * Pointer to verbs structure.
438 * Create specification.
440 * Size in bytes of the specification to copy.
443 flow_verbs_spec_add(struct mlx5_flow_verbs_workspace *verbs,
444 void *src, unsigned int size)
450 MLX5_ASSERT(verbs->specs);
451 dst = (void *)(verbs->specs + verbs->size);
452 memcpy(dst, src, size);
453 ++verbs->attr.num_of_specs;
458 * Convert the @p item into a Verbs specification. This function assumes that
459 * the input is valid and that there is space to insert the requested item
462 * @param[in, out] dev_flow
463 * Pointer to dev_flow structure.
465 * Item specification.
466 * @param[in] item_flags
470 flow_verbs_translate_item_eth(struct mlx5_flow *dev_flow,
471 const struct rte_flow_item *item,
474 const struct rte_flow_item_eth *spec = item->spec;
475 const struct rte_flow_item_eth *mask = item->mask;
476 const unsigned int size = sizeof(struct ibv_flow_spec_eth);
477 struct ibv_flow_spec_eth eth = {
478 .type = IBV_FLOW_SPEC_ETH | VERBS_SPEC_INNER(item_flags),
483 mask = &rte_flow_item_eth_mask;
487 memcpy(ð.val.dst_mac, spec->dst.addr_bytes,
489 memcpy(ð.val.src_mac, spec->src.addr_bytes,
491 eth.val.ether_type = spec->type;
492 memcpy(ð.mask.dst_mac, mask->dst.addr_bytes,
494 memcpy(ð.mask.src_mac, mask->src.addr_bytes,
496 eth.mask.ether_type = mask->type;
497 /* Remove unwanted bits from values. */
498 for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i) {
499 eth.val.dst_mac[i] &= eth.mask.dst_mac[i];
500 eth.val.src_mac[i] &= eth.mask.src_mac[i];
502 eth.val.ether_type &= eth.mask.ether_type;
504 flow_verbs_spec_add(&dev_flow->verbs, ð, size);
508 * Update the VLAN tag in the Verbs Ethernet specification.
509 * This function assumes that the input is valid and there is space to add
510 * the requested item.
512 * @param[in, out] attr
513 * Pointer to Verbs attributes structure.
515 * Verbs structure containing the VLAN information to copy.
518 flow_verbs_item_vlan_update(struct ibv_flow_attr *attr,
519 struct ibv_flow_spec_eth *eth)
522 const enum ibv_flow_spec_type search = eth->type;
523 struct ibv_spec_header *hdr = (struct ibv_spec_header *)
524 ((uint8_t *)attr + sizeof(struct ibv_flow_attr));
526 for (i = 0; i != attr->num_of_specs; ++i) {
527 if (hdr->type == search) {
528 struct ibv_flow_spec_eth *e =
529 (struct ibv_flow_spec_eth *)hdr;
531 e->val.vlan_tag = eth->val.vlan_tag;
532 e->mask.vlan_tag = eth->mask.vlan_tag;
533 e->val.ether_type = eth->val.ether_type;
534 e->mask.ether_type = eth->mask.ether_type;
537 hdr = (struct ibv_spec_header *)((uint8_t *)hdr + hdr->size);
542 * Convert the @p item into a Verbs specification. This function assumes that
543 * the input is valid and that there is space to insert the requested item
546 * @param[in, out] dev_flow
547 * Pointer to dev_flow structure.
549 * Item specification.
550 * @param[in] item_flags
554 flow_verbs_translate_item_vlan(struct mlx5_flow *dev_flow,
555 const struct rte_flow_item *item,
558 const struct rte_flow_item_vlan *spec = item->spec;
559 const struct rte_flow_item_vlan *mask = item->mask;
560 unsigned int size = sizeof(struct ibv_flow_spec_eth);
561 const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
562 struct ibv_flow_spec_eth eth = {
563 .type = IBV_FLOW_SPEC_ETH | VERBS_SPEC_INNER(item_flags),
566 const uint32_t l2m = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
567 MLX5_FLOW_LAYER_OUTER_L2;
570 mask = &rte_flow_item_vlan_mask;
572 eth.val.vlan_tag = spec->tci;
573 eth.mask.vlan_tag = mask->tci;
574 eth.val.vlan_tag &= eth.mask.vlan_tag;
575 eth.val.ether_type = spec->inner_type;
576 eth.mask.ether_type = mask->inner_type;
577 eth.val.ether_type &= eth.mask.ether_type;
579 if (!(item_flags & l2m))
580 flow_verbs_spec_add(&dev_flow->verbs, ð, size);
582 flow_verbs_item_vlan_update(&dev_flow->verbs.attr, ð);
584 dev_flow->handle->vf_vlan.tag =
585 rte_be_to_cpu_16(spec->tci) & 0x0fff;
589 * Convert the @p item into a Verbs specification. This function assumes that
590 * the input is valid and that there is space to insert the requested item
593 * @param[in, out] dev_flow
594 * Pointer to dev_flow structure.
596 * Item specification.
597 * @param[in] item_flags
601 flow_verbs_translate_item_ipv4(struct mlx5_flow *dev_flow,
602 const struct rte_flow_item *item,
605 const struct rte_flow_item_ipv4 *spec = item->spec;
606 const struct rte_flow_item_ipv4 *mask = item->mask;
607 unsigned int size = sizeof(struct ibv_flow_spec_ipv4_ext);
608 struct ibv_flow_spec_ipv4_ext ipv4 = {
609 .type = IBV_FLOW_SPEC_IPV4_EXT | VERBS_SPEC_INNER(item_flags),
614 mask = &rte_flow_item_ipv4_mask;
616 ipv4.val = (struct ibv_flow_ipv4_ext_filter){
617 .src_ip = spec->hdr.src_addr,
618 .dst_ip = spec->hdr.dst_addr,
619 .proto = spec->hdr.next_proto_id,
620 .tos = spec->hdr.type_of_service,
622 ipv4.mask = (struct ibv_flow_ipv4_ext_filter){
623 .src_ip = mask->hdr.src_addr,
624 .dst_ip = mask->hdr.dst_addr,
625 .proto = mask->hdr.next_proto_id,
626 .tos = mask->hdr.type_of_service,
628 /* Remove unwanted bits from values. */
629 ipv4.val.src_ip &= ipv4.mask.src_ip;
630 ipv4.val.dst_ip &= ipv4.mask.dst_ip;
631 ipv4.val.proto &= ipv4.mask.proto;
632 ipv4.val.tos &= ipv4.mask.tos;
634 flow_verbs_spec_add(&dev_flow->verbs, &ipv4, size);
638 * Convert the @p item into a Verbs specification. This function assumes that
639 * the input is valid and that there is space to insert the requested item
642 * @param[in, out] dev_flow
643 * Pointer to dev_flow structure.
645 * Item specification.
646 * @param[in] item_flags
650 flow_verbs_translate_item_ipv6(struct mlx5_flow *dev_flow,
651 const struct rte_flow_item *item,
654 const struct rte_flow_item_ipv6 *spec = item->spec;
655 const struct rte_flow_item_ipv6 *mask = item->mask;
656 unsigned int size = sizeof(struct ibv_flow_spec_ipv6);
657 struct ibv_flow_spec_ipv6 ipv6 = {
658 .type = IBV_FLOW_SPEC_IPV6 | VERBS_SPEC_INNER(item_flags),
663 mask = &rte_flow_item_ipv6_mask;
666 uint32_t vtc_flow_val;
667 uint32_t vtc_flow_mask;
669 memcpy(&ipv6.val.src_ip, spec->hdr.src_addr,
670 RTE_DIM(ipv6.val.src_ip));
671 memcpy(&ipv6.val.dst_ip, spec->hdr.dst_addr,
672 RTE_DIM(ipv6.val.dst_ip));
673 memcpy(&ipv6.mask.src_ip, mask->hdr.src_addr,
674 RTE_DIM(ipv6.mask.src_ip));
675 memcpy(&ipv6.mask.dst_ip, mask->hdr.dst_addr,
676 RTE_DIM(ipv6.mask.dst_ip));
677 vtc_flow_val = rte_be_to_cpu_32(spec->hdr.vtc_flow);
678 vtc_flow_mask = rte_be_to_cpu_32(mask->hdr.vtc_flow);
679 ipv6.val.flow_label =
680 rte_cpu_to_be_32((vtc_flow_val & RTE_IPV6_HDR_FL_MASK) >>
681 RTE_IPV6_HDR_FL_SHIFT);
682 ipv6.val.traffic_class = (vtc_flow_val & RTE_IPV6_HDR_TC_MASK) >>
683 RTE_IPV6_HDR_TC_SHIFT;
684 ipv6.val.next_hdr = spec->hdr.proto;
685 ipv6.mask.flow_label =
686 rte_cpu_to_be_32((vtc_flow_mask & RTE_IPV6_HDR_FL_MASK) >>
687 RTE_IPV6_HDR_FL_SHIFT);
688 ipv6.mask.traffic_class = (vtc_flow_mask & RTE_IPV6_HDR_TC_MASK) >>
689 RTE_IPV6_HDR_TC_SHIFT;
690 ipv6.mask.next_hdr = mask->hdr.proto;
691 /* Remove unwanted bits from values. */
692 for (i = 0; i < RTE_DIM(ipv6.val.src_ip); ++i) {
693 ipv6.val.src_ip[i] &= ipv6.mask.src_ip[i];
694 ipv6.val.dst_ip[i] &= ipv6.mask.dst_ip[i];
696 ipv6.val.flow_label &= ipv6.mask.flow_label;
697 ipv6.val.traffic_class &= ipv6.mask.traffic_class;
698 ipv6.val.next_hdr &= ipv6.mask.next_hdr;
700 flow_verbs_spec_add(&dev_flow->verbs, &ipv6, size);
704 * Convert the @p item into a Verbs specification. This function assumes that
705 * the input is valid and that there is space to insert the requested item
708 * @param[in, out] dev_flow
709 * Pointer to dev_flow structure.
711 * Item specification.
712 * @param[in] item_flags
716 flow_verbs_translate_item_tcp(struct mlx5_flow *dev_flow,
717 const struct rte_flow_item *item,
718 uint64_t item_flags __rte_unused)
720 const struct rte_flow_item_tcp *spec = item->spec;
721 const struct rte_flow_item_tcp *mask = item->mask;
722 unsigned int size = sizeof(struct ibv_flow_spec_tcp_udp);
723 struct ibv_flow_spec_tcp_udp tcp = {
724 .type = IBV_FLOW_SPEC_TCP | VERBS_SPEC_INNER(item_flags),
729 mask = &rte_flow_item_tcp_mask;
731 tcp.val.dst_port = spec->hdr.dst_port;
732 tcp.val.src_port = spec->hdr.src_port;
733 tcp.mask.dst_port = mask->hdr.dst_port;
734 tcp.mask.src_port = mask->hdr.src_port;
735 /* Remove unwanted bits from values. */
736 tcp.val.src_port &= tcp.mask.src_port;
737 tcp.val.dst_port &= tcp.mask.dst_port;
739 flow_verbs_spec_add(&dev_flow->verbs, &tcp, size);
743 * Convert the @p item into a Verbs specification. This function assumes that
744 * the input is valid and that there is space to insert the requested item
747 * @param[in, out] dev_flow
748 * Pointer to dev_flow structure.
750 * Item specification.
751 * @param[in] item_flags
755 flow_verbs_translate_item_udp(struct mlx5_flow *dev_flow,
756 const struct rte_flow_item *item,
757 uint64_t item_flags __rte_unused)
759 const struct rte_flow_item_udp *spec = item->spec;
760 const struct rte_flow_item_udp *mask = item->mask;
761 unsigned int size = sizeof(struct ibv_flow_spec_tcp_udp);
762 struct ibv_flow_spec_tcp_udp udp = {
763 .type = IBV_FLOW_SPEC_UDP | VERBS_SPEC_INNER(item_flags),
768 mask = &rte_flow_item_udp_mask;
770 udp.val.dst_port = spec->hdr.dst_port;
771 udp.val.src_port = spec->hdr.src_port;
772 udp.mask.dst_port = mask->hdr.dst_port;
773 udp.mask.src_port = mask->hdr.src_port;
774 /* Remove unwanted bits from values. */
775 udp.val.src_port &= udp.mask.src_port;
776 udp.val.dst_port &= udp.mask.dst_port;
779 while (item->type == RTE_FLOW_ITEM_TYPE_VOID)
781 if (!(udp.val.dst_port & udp.mask.dst_port)) {
782 switch ((item)->type) {
783 case RTE_FLOW_ITEM_TYPE_VXLAN:
784 udp.val.dst_port = htons(MLX5_UDP_PORT_VXLAN);
785 udp.mask.dst_port = 0xffff;
787 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
788 udp.val.dst_port = htons(MLX5_UDP_PORT_VXLAN_GPE);
789 udp.mask.dst_port = 0xffff;
791 case RTE_FLOW_ITEM_TYPE_MPLS:
792 udp.val.dst_port = htons(MLX5_UDP_PORT_MPLS);
793 udp.mask.dst_port = 0xffff;
800 flow_verbs_spec_add(&dev_flow->verbs, &udp, size);
804 * Convert the @p item into a Verbs specification. This function assumes that
805 * the input is valid and that there is space to insert the requested item
808 * @param[in, out] dev_flow
809 * Pointer to dev_flow structure.
811 * Item specification.
812 * @param[in] item_flags
816 flow_verbs_translate_item_vxlan(struct mlx5_flow *dev_flow,
817 const struct rte_flow_item *item,
818 uint64_t item_flags __rte_unused)
820 const struct rte_flow_item_vxlan *spec = item->spec;
821 const struct rte_flow_item_vxlan *mask = item->mask;
822 unsigned int size = sizeof(struct ibv_flow_spec_tunnel);
823 struct ibv_flow_spec_tunnel vxlan = {
824 .type = IBV_FLOW_SPEC_VXLAN_TUNNEL,
830 } id = { .vlan_id = 0, };
833 mask = &rte_flow_item_vxlan_mask;
835 memcpy(&id.vni[1], spec->vni, 3);
836 vxlan.val.tunnel_id = id.vlan_id;
837 memcpy(&id.vni[1], mask->vni, 3);
838 vxlan.mask.tunnel_id = id.vlan_id;
839 /* Remove unwanted bits from values. */
840 vxlan.val.tunnel_id &= vxlan.mask.tunnel_id;
842 flow_verbs_spec_add(&dev_flow->verbs, &vxlan, size);
846 * Convert the @p item into a Verbs specification. This function assumes that
847 * the input is valid and that there is space to insert the requested item
850 * @param[in, out] dev_flow
851 * Pointer to dev_flow structure.
853 * Item specification.
854 * @param[in] item_flags
858 flow_verbs_translate_item_vxlan_gpe(struct mlx5_flow *dev_flow,
859 const struct rte_flow_item *item,
860 uint64_t item_flags __rte_unused)
862 const struct rte_flow_item_vxlan_gpe *spec = item->spec;
863 const struct rte_flow_item_vxlan_gpe *mask = item->mask;
864 unsigned int size = sizeof(struct ibv_flow_spec_tunnel);
865 struct ibv_flow_spec_tunnel vxlan_gpe = {
866 .type = IBV_FLOW_SPEC_VXLAN_TUNNEL,
872 } id = { .vlan_id = 0, };
875 mask = &rte_flow_item_vxlan_gpe_mask;
877 memcpy(&id.vni[1], spec->vni, 3);
878 vxlan_gpe.val.tunnel_id = id.vlan_id;
879 memcpy(&id.vni[1], mask->vni, 3);
880 vxlan_gpe.mask.tunnel_id = id.vlan_id;
881 /* Remove unwanted bits from values. */
882 vxlan_gpe.val.tunnel_id &= vxlan_gpe.mask.tunnel_id;
884 flow_verbs_spec_add(&dev_flow->verbs, &vxlan_gpe, size);
888 * Update the protocol in Verbs IPv4/IPv6 spec.
890 * @param[in, out] attr
891 * Pointer to Verbs attributes structure.
893 * Specification type to search in order to update the IP protocol.
894 * @param[in] protocol
895 * Protocol value to set if none is present in the specification.
898 flow_verbs_item_gre_ip_protocol_update(struct ibv_flow_attr *attr,
899 enum ibv_flow_spec_type search,
903 struct ibv_spec_header *hdr = (struct ibv_spec_header *)
904 ((uint8_t *)attr + sizeof(struct ibv_flow_attr));
908 for (i = 0; i != attr->num_of_specs; ++i) {
909 if (hdr->type == search) {
911 struct ibv_flow_spec_ipv4_ext *ipv4;
912 struct ibv_flow_spec_ipv6 *ipv6;
916 case IBV_FLOW_SPEC_IPV4_EXT:
917 ip.ipv4 = (struct ibv_flow_spec_ipv4_ext *)hdr;
918 if (!ip.ipv4->val.proto) {
919 ip.ipv4->val.proto = protocol;
920 ip.ipv4->mask.proto = 0xff;
923 case IBV_FLOW_SPEC_IPV6:
924 ip.ipv6 = (struct ibv_flow_spec_ipv6 *)hdr;
925 if (!ip.ipv6->val.next_hdr) {
926 ip.ipv6->val.next_hdr = protocol;
927 ip.ipv6->mask.next_hdr = 0xff;
935 hdr = (struct ibv_spec_header *)((uint8_t *)hdr + hdr->size);
940 * Convert the @p item into a Verbs specification. This function assumes that
941 * the input is valid and that there is space to insert the requested item
944 * @param[in, out] dev_flow
945 * Pointer to dev_flow structure.
947 * Item specification.
948 * @param[in] item_flags
952 flow_verbs_translate_item_gre(struct mlx5_flow *dev_flow,
953 const struct rte_flow_item *item __rte_unused,
956 struct mlx5_flow_verbs_workspace *verbs = &dev_flow->verbs;
957 #ifndef HAVE_IBV_DEVICE_MPLS_SUPPORT
958 unsigned int size = sizeof(struct ibv_flow_spec_tunnel);
959 struct ibv_flow_spec_tunnel tunnel = {
960 .type = IBV_FLOW_SPEC_VXLAN_TUNNEL,
964 const struct rte_flow_item_gre *spec = item->spec;
965 const struct rte_flow_item_gre *mask = item->mask;
966 unsigned int size = sizeof(struct ibv_flow_spec_gre);
967 struct ibv_flow_spec_gre tunnel = {
968 .type = IBV_FLOW_SPEC_GRE,
973 mask = &rte_flow_item_gre_mask;
975 tunnel.val.c_ks_res0_ver = spec->c_rsvd0_ver;
976 tunnel.val.protocol = spec->protocol;
977 tunnel.mask.c_ks_res0_ver = mask->c_rsvd0_ver;
978 tunnel.mask.protocol = mask->protocol;
979 /* Remove unwanted bits from values. */
980 tunnel.val.c_ks_res0_ver &= tunnel.mask.c_ks_res0_ver;
981 tunnel.val.protocol &= tunnel.mask.protocol;
982 tunnel.val.key &= tunnel.mask.key;
985 if (item_flags & MLX5_FLOW_LAYER_OUTER_L3_IPV4)
986 flow_verbs_item_gre_ip_protocol_update(&verbs->attr,
987 IBV_FLOW_SPEC_IPV4_EXT,
990 flow_verbs_item_gre_ip_protocol_update(&verbs->attr,
993 flow_verbs_spec_add(verbs, &tunnel, size);
997 * Convert the @p action into a Verbs specification. This function assumes that
998 * the input is valid and that there is space to insert the requested action
999 * into the flow. This function also return the action that was added.
1001 * @param[in, out] dev_flow
1002 * Pointer to dev_flow structure.
1004 * Item specification.
1005 * @param[in] item_flags
1006 * Parsed item flags.
1009 flow_verbs_translate_item_mpls(struct mlx5_flow *dev_flow __rte_unused,
1010 const struct rte_flow_item *item __rte_unused,
1011 uint64_t item_flags __rte_unused)
1013 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
1014 const struct rte_flow_item_mpls *spec = item->spec;
1015 const struct rte_flow_item_mpls *mask = item->mask;
1016 unsigned int size = sizeof(struct ibv_flow_spec_mpls);
1017 struct ibv_flow_spec_mpls mpls = {
1018 .type = IBV_FLOW_SPEC_MPLS,
1023 mask = &rte_flow_item_mpls_mask;
1025 memcpy(&mpls.val.label, spec, sizeof(mpls.val.label));
1026 memcpy(&mpls.mask.label, mask, sizeof(mpls.mask.label));
1027 /* Remove unwanted bits from values. */
1028 mpls.val.label &= mpls.mask.label;
1030 flow_verbs_spec_add(&dev_flow->verbs, &mpls, size);
1035 * Convert the @p action into a Verbs specification. This function assumes that
1036 * the input is valid and that there is space to insert the requested action
1039 * @param[in] dev_flow
1040 * Pointer to mlx5_flow.
1042 * Action configuration.
1045 flow_verbs_translate_action_drop
1046 (struct mlx5_flow *dev_flow,
1047 const struct rte_flow_action *action __rte_unused)
1049 unsigned int size = sizeof(struct ibv_flow_spec_action_drop);
1050 struct ibv_flow_spec_action_drop drop = {
1051 .type = IBV_FLOW_SPEC_ACTION_DROP,
1055 flow_verbs_spec_add(&dev_flow->verbs, &drop, size);
1059 * Convert the @p action into a Verbs specification. This function assumes that
1060 * the input is valid and that there is space to insert the requested action
1063 * @param[in] rss_desc
1064 * Pointer to mlx5_flow_rss_desc.
1066 * Action configuration.
1069 flow_verbs_translate_action_queue(struct mlx5_flow_rss_desc *rss_desc,
1070 const struct rte_flow_action *action)
1072 const struct rte_flow_action_queue *queue = action->conf;
1074 rss_desc->queue[0] = queue->index;
1075 rss_desc->queue_num = 1;
1079 * Convert the @p action into a Verbs specification. This function assumes that
1080 * the input is valid and that there is space to insert the requested action
1083 * @param[in] rss_desc
1084 * Pointer to mlx5_flow_rss_desc.
1086 * Action configuration.
1089 flow_verbs_translate_action_rss(struct mlx5_flow_rss_desc *rss_desc,
1090 const struct rte_flow_action *action)
1092 const struct rte_flow_action_rss *rss = action->conf;
1093 const uint8_t *rss_key;
1095 memcpy(rss_desc->queue, rss->queue, rss->queue_num * sizeof(uint16_t));
1096 rss_desc->queue_num = rss->queue_num;
1097 /* NULL RSS key indicates default RSS key. */
1098 rss_key = !rss->key ? rss_hash_default_key : rss->key;
1099 memcpy(rss_desc->key, rss_key, MLX5_RSS_HASH_KEY_LEN);
1101 * rss->level and rss.types should be set in advance when expanding
1107 * Convert the @p action into a Verbs specification. This function assumes that
1108 * the input is valid and that there is space to insert the requested action
1111 * @param[in] dev_flow
1112 * Pointer to mlx5_flow.
1114 * Action configuration.
1117 flow_verbs_translate_action_flag
1118 (struct mlx5_flow *dev_flow,
1119 const struct rte_flow_action *action __rte_unused)
1121 unsigned int size = sizeof(struct ibv_flow_spec_action_tag);
1122 struct ibv_flow_spec_action_tag tag = {
1123 .type = IBV_FLOW_SPEC_ACTION_TAG,
1125 .tag_id = mlx5_flow_mark_set(MLX5_FLOW_MARK_DEFAULT),
1128 flow_verbs_spec_add(&dev_flow->verbs, &tag, size);
1132 * Convert the @p action into a Verbs specification. This function assumes that
1133 * the input is valid and that there is space to insert the requested action
1136 * @param[in] dev_flow
1137 * Pointer to mlx5_flow.
1139 * Action configuration.
1142 flow_verbs_translate_action_mark(struct mlx5_flow *dev_flow,
1143 const struct rte_flow_action *action)
1145 const struct rte_flow_action_mark *mark = action->conf;
1146 unsigned int size = sizeof(struct ibv_flow_spec_action_tag);
1147 struct ibv_flow_spec_action_tag tag = {
1148 .type = IBV_FLOW_SPEC_ACTION_TAG,
1150 .tag_id = mlx5_flow_mark_set(mark->id),
1153 flow_verbs_spec_add(&dev_flow->verbs, &tag, size);
1157 * Convert the @p action into a Verbs specification. This function assumes that
1158 * the input is valid and that there is space to insert the requested action
1162 * Pointer to the Ethernet device structure.
1164 * Action configuration.
1165 * @param[in] dev_flow
1166 * Pointer to mlx5_flow.
1168 * Pointer to error structure.
1171 * 0 On success else a negative errno value is returned and rte_errno is set.
1174 flow_verbs_translate_action_count(struct mlx5_flow *dev_flow,
1175 const struct rte_flow_action *action,
1176 struct rte_eth_dev *dev,
1177 struct rte_flow_error *error)
1179 const struct rte_flow_action_count *count = action->conf;
1180 struct rte_flow *flow = dev_flow->flow;
1181 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42) || \
1182 defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
1183 struct mlx5_flow_counter_pool *pool;
1184 struct mlx5_flow_counter *cnt = NULL;
1185 unsigned int size = sizeof(struct ibv_flow_spec_counter_action);
1186 struct ibv_flow_spec_counter_action counter = {
1187 .type = IBV_FLOW_SPEC_ACTION_COUNT,
1192 if (!flow->counter) {
1193 flow->counter = flow_verbs_counter_new(dev, count->shared,
1196 return rte_flow_error_set(error, rte_errno,
1197 RTE_FLOW_ERROR_TYPE_ACTION,
1199 "cannot get counter"
1202 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42)
1203 cnt = flow_verbs_counter_get_by_idx(dev, flow->counter, &pool);
1204 counter.counter_set_handle =
1205 ((struct ibv_counter_set *)cnt->dcs_when_active)->handle;
1206 flow_verbs_spec_add(&dev_flow->verbs, &counter, size);
1207 #elif defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
1208 cnt = flow_verbs_counter_get_by_idx(dev, flow->counter, &pool);
1209 counter.counters = (struct ibv_counters *)cnt->dcs_when_active;
1210 flow_verbs_spec_add(&dev_flow->verbs, &counter, size);
1216 * Internal validation function. For validating both actions and items.
1219 * Pointer to the Ethernet device structure.
1221 * Pointer to the flow attributes.
1223 * Pointer to the list of items.
1224 * @param[in] actions
1225 * Pointer to the list of actions.
1226 * @param[in] external
1227 * This flow rule is created by request external to PMD.
1228 * @param[in] hairpin
1229 * Number of hairpin TX actions, 0 means classic flow.
1231 * Pointer to the error structure.
1234 * 0 on success, a negative errno value otherwise and rte_errno is set.
1237 flow_verbs_validate(struct rte_eth_dev *dev,
1238 const struct rte_flow_attr *attr,
1239 const struct rte_flow_item items[],
1240 const struct rte_flow_action actions[],
1241 bool external __rte_unused,
1242 int hairpin __rte_unused,
1243 struct rte_flow_error *error)
1246 uint64_t action_flags = 0;
1247 uint64_t item_flags = 0;
1248 uint64_t last_item = 0;
1249 uint8_t next_protocol = 0xff;
1250 uint16_t ether_type = 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 ether_type = rte_be_to_cpu_16(ether_type);
1283 case RTE_FLOW_ITEM_TYPE_VLAN:
1284 ret = mlx5_flow_validate_item_vlan(items, item_flags,
1288 last_item = tunnel ? (MLX5_FLOW_LAYER_INNER_L2 |
1289 MLX5_FLOW_LAYER_INNER_VLAN) :
1290 (MLX5_FLOW_LAYER_OUTER_L2 |
1291 MLX5_FLOW_LAYER_OUTER_VLAN);
1292 if (items->mask != NULL && items->spec != NULL) {
1294 ((const struct rte_flow_item_vlan *)
1295 items->spec)->inner_type;
1297 ((const struct rte_flow_item_vlan *)
1298 items->mask)->inner_type;
1299 ether_type = rte_be_to_cpu_16(ether_type);
1304 case RTE_FLOW_ITEM_TYPE_IPV4:
1305 ret = mlx5_flow_validate_item_ipv4
1307 last_item, ether_type, NULL,
1308 MLX5_ITEM_RANGE_NOT_ACCEPTED,
1312 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
1313 MLX5_FLOW_LAYER_OUTER_L3_IPV4;
1314 if (items->mask != NULL &&
1315 ((const struct rte_flow_item_ipv4 *)
1316 items->mask)->hdr.next_proto_id) {
1318 ((const struct rte_flow_item_ipv4 *)
1319 (items->spec))->hdr.next_proto_id;
1321 ((const struct rte_flow_item_ipv4 *)
1322 (items->mask))->hdr.next_proto_id;
1324 /* Reset for inner layer. */
1325 next_protocol = 0xff;
1328 case RTE_FLOW_ITEM_TYPE_IPV6:
1329 ret = mlx5_flow_validate_item_ipv6(items, item_flags,
1335 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
1336 MLX5_FLOW_LAYER_OUTER_L3_IPV6;
1337 if (items->mask != NULL &&
1338 ((const struct rte_flow_item_ipv6 *)
1339 items->mask)->hdr.proto) {
1341 ((const struct rte_flow_item_ipv6 *)
1342 items->spec)->hdr.proto;
1344 ((const struct rte_flow_item_ipv6 *)
1345 items->mask)->hdr.proto;
1347 /* Reset for inner layer. */
1348 next_protocol = 0xff;
1351 case RTE_FLOW_ITEM_TYPE_UDP:
1352 ret = mlx5_flow_validate_item_udp(items, item_flags,
1357 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
1358 MLX5_FLOW_LAYER_OUTER_L4_UDP;
1360 case RTE_FLOW_ITEM_TYPE_TCP:
1361 ret = mlx5_flow_validate_item_tcp
1364 &rte_flow_item_tcp_mask,
1368 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
1369 MLX5_FLOW_LAYER_OUTER_L4_TCP;
1371 case RTE_FLOW_ITEM_TYPE_VXLAN:
1372 ret = mlx5_flow_validate_item_vxlan(items, item_flags,
1376 last_item = MLX5_FLOW_LAYER_VXLAN;
1378 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
1379 ret = mlx5_flow_validate_item_vxlan_gpe(items,
1384 last_item = MLX5_FLOW_LAYER_VXLAN_GPE;
1386 case RTE_FLOW_ITEM_TYPE_GRE:
1387 ret = mlx5_flow_validate_item_gre(items, item_flags,
1388 next_protocol, error);
1391 last_item = MLX5_FLOW_LAYER_GRE;
1393 case RTE_FLOW_ITEM_TYPE_MPLS:
1394 ret = mlx5_flow_validate_item_mpls(dev, items,
1399 last_item = MLX5_FLOW_LAYER_MPLS;
1402 return rte_flow_error_set(error, ENOTSUP,
1403 RTE_FLOW_ERROR_TYPE_ITEM,
1404 NULL, "item not supported");
1406 item_flags |= last_item;
1408 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1409 switch (actions->type) {
1410 case RTE_FLOW_ACTION_TYPE_VOID:
1412 case RTE_FLOW_ACTION_TYPE_FLAG:
1413 ret = mlx5_flow_validate_action_flag(action_flags,
1418 action_flags |= MLX5_FLOW_ACTION_FLAG;
1420 case RTE_FLOW_ACTION_TYPE_MARK:
1421 ret = mlx5_flow_validate_action_mark(actions,
1427 action_flags |= MLX5_FLOW_ACTION_MARK;
1429 case RTE_FLOW_ACTION_TYPE_DROP:
1430 ret = mlx5_flow_validate_action_drop(action_flags,
1435 action_flags |= MLX5_FLOW_ACTION_DROP;
1437 case RTE_FLOW_ACTION_TYPE_QUEUE:
1438 ret = mlx5_flow_validate_action_queue(actions,
1444 action_flags |= MLX5_FLOW_ACTION_QUEUE;
1446 case RTE_FLOW_ACTION_TYPE_RSS:
1447 ret = mlx5_flow_validate_action_rss(actions,
1453 action_flags |= MLX5_FLOW_ACTION_RSS;
1455 case RTE_FLOW_ACTION_TYPE_COUNT:
1456 ret = mlx5_flow_validate_action_count(dev, attr, error);
1459 action_flags |= MLX5_FLOW_ACTION_COUNT;
1462 return rte_flow_error_set(error, ENOTSUP,
1463 RTE_FLOW_ERROR_TYPE_ACTION,
1465 "action not supported");
1469 * Validate the drop action mutual exclusion with other actions.
1470 * Drop action is mutually-exclusive with any other action, except for
1473 if ((action_flags & MLX5_FLOW_ACTION_DROP) &&
1474 (action_flags & ~(MLX5_FLOW_ACTION_DROP | MLX5_FLOW_ACTION_COUNT)))
1475 return rte_flow_error_set(error, EINVAL,
1476 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1477 "Drop action is mutually-exclusive "
1478 "with any other action, except for "
1480 if (!(action_flags & MLX5_FLOW_FATE_ACTIONS))
1481 return rte_flow_error_set(error, EINVAL,
1482 RTE_FLOW_ERROR_TYPE_ACTION, actions,
1483 "no fate action is found");
1488 * Calculate the required bytes that are needed for the action part of the verbs
1491 * @param[in] actions
1492 * Pointer to the list of actions.
1495 * The size of the memory needed for all actions.
1498 flow_verbs_get_actions_size(const struct rte_flow_action actions[])
1502 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1503 switch (actions->type) {
1504 case RTE_FLOW_ACTION_TYPE_VOID:
1506 case RTE_FLOW_ACTION_TYPE_FLAG:
1507 size += sizeof(struct ibv_flow_spec_action_tag);
1509 case RTE_FLOW_ACTION_TYPE_MARK:
1510 size += sizeof(struct ibv_flow_spec_action_tag);
1512 case RTE_FLOW_ACTION_TYPE_DROP:
1513 size += sizeof(struct ibv_flow_spec_action_drop);
1515 case RTE_FLOW_ACTION_TYPE_QUEUE:
1517 case RTE_FLOW_ACTION_TYPE_RSS:
1519 case RTE_FLOW_ACTION_TYPE_COUNT:
1520 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42) || \
1521 defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
1522 size += sizeof(struct ibv_flow_spec_counter_action);
1533 * Calculate the required bytes that are needed for the item part of the verbs
1537 * Pointer to the list of items.
1540 * The size of the memory needed for all items.
1543 flow_verbs_get_items_size(const struct rte_flow_item items[])
1547 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
1548 switch (items->type) {
1549 case RTE_FLOW_ITEM_TYPE_VOID:
1551 case RTE_FLOW_ITEM_TYPE_ETH:
1552 size += sizeof(struct ibv_flow_spec_eth);
1554 case RTE_FLOW_ITEM_TYPE_VLAN:
1555 size += sizeof(struct ibv_flow_spec_eth);
1557 case RTE_FLOW_ITEM_TYPE_IPV4:
1558 size += sizeof(struct ibv_flow_spec_ipv4_ext);
1560 case RTE_FLOW_ITEM_TYPE_IPV6:
1561 size += sizeof(struct ibv_flow_spec_ipv6);
1563 case RTE_FLOW_ITEM_TYPE_UDP:
1564 size += sizeof(struct ibv_flow_spec_tcp_udp);
1566 case RTE_FLOW_ITEM_TYPE_TCP:
1567 size += sizeof(struct ibv_flow_spec_tcp_udp);
1569 case RTE_FLOW_ITEM_TYPE_VXLAN:
1570 size += sizeof(struct ibv_flow_spec_tunnel);
1572 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
1573 size += sizeof(struct ibv_flow_spec_tunnel);
1575 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
1576 case RTE_FLOW_ITEM_TYPE_GRE:
1577 size += sizeof(struct ibv_flow_spec_gre);
1579 case RTE_FLOW_ITEM_TYPE_MPLS:
1580 size += sizeof(struct ibv_flow_spec_mpls);
1583 case RTE_FLOW_ITEM_TYPE_GRE:
1584 size += sizeof(struct ibv_flow_spec_tunnel);
1595 * Internal preparation function. Allocate mlx5_flow with the required size.
1596 * The required size is calculate based on the actions and items. This function
1597 * also returns the detected actions and items for later use.
1600 * Pointer to Ethernet device.
1602 * Pointer to the flow attributes.
1604 * Pointer to the list of items.
1605 * @param[in] actions
1606 * Pointer to the list of actions.
1608 * Pointer to the error structure.
1611 * Pointer to mlx5_flow object on success, otherwise NULL and rte_errno
1614 static struct mlx5_flow *
1615 flow_verbs_prepare(struct rte_eth_dev *dev,
1616 const struct rte_flow_attr *attr __rte_unused,
1617 const struct rte_flow_item items[],
1618 const struct rte_flow_action actions[],
1619 struct rte_flow_error *error)
1622 uint32_t handle_idx = 0;
1623 struct mlx5_flow *dev_flow;
1624 struct mlx5_flow_handle *dev_handle;
1625 struct mlx5_priv *priv = dev->data->dev_private;
1627 size += flow_verbs_get_actions_size(actions);
1628 size += flow_verbs_get_items_size(items);
1629 if (size > MLX5_VERBS_MAX_SPEC_ACT_SIZE) {
1630 rte_flow_error_set(error, E2BIG,
1631 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1632 "Verbs spec/action size too large");
1635 /* In case of corrupting the memory. */
1636 if (priv->flow_idx >= MLX5_NUM_MAX_DEV_FLOWS) {
1637 rte_flow_error_set(error, ENOSPC,
1638 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1639 "not free temporary device flow");
1642 dev_handle = mlx5_ipool_zmalloc(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
1645 rte_flow_error_set(error, ENOMEM,
1646 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1647 "not enough memory to create flow handle");
1650 /* No multi-thread supporting. */
1651 dev_flow = &((struct mlx5_flow *)priv->inter_flows)[priv->flow_idx++];
1652 dev_flow->handle = dev_handle;
1653 dev_flow->handle_idx = handle_idx;
1654 /* Memcpy is used, only size needs to be cleared to 0. */
1655 dev_flow->verbs.size = 0;
1656 dev_flow->verbs.attr.num_of_specs = 0;
1657 dev_flow->ingress = attr->ingress;
1658 dev_flow->hash_fields = 0;
1659 /* Need to set transfer attribute: not supported in Verbs mode. */
1664 * Fill the flow with verb spec.
1667 * Pointer to Ethernet device.
1668 * @param[in, out] dev_flow
1669 * Pointer to the mlx5 flow.
1671 * Pointer to the flow attributes.
1673 * Pointer to the list of items.
1674 * @param[in] actions
1675 * Pointer to the list of actions.
1677 * Pointer to the error structure.
1680 * 0 on success, else a negative errno value otherwise and rte_errno is set.
1683 flow_verbs_translate(struct rte_eth_dev *dev,
1684 struct mlx5_flow *dev_flow,
1685 const struct rte_flow_attr *attr,
1686 const struct rte_flow_item items[],
1687 const struct rte_flow_action actions[],
1688 struct rte_flow_error *error)
1690 uint64_t item_flags = 0;
1691 uint64_t action_flags = 0;
1692 uint64_t priority = attr->priority;
1693 uint32_t subpriority = 0;
1694 struct mlx5_priv *priv = dev->data->dev_private;
1695 struct mlx5_flow_rss_desc *rss_desc = &((struct mlx5_flow_rss_desc *)
1697 [!!priv->flow_nested_idx];
1699 if (priority == MLX5_FLOW_PRIO_RSVD)
1700 priority = priv->config.flow_prio - 1;
1701 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1704 switch (actions->type) {
1705 case RTE_FLOW_ACTION_TYPE_VOID:
1707 case RTE_FLOW_ACTION_TYPE_FLAG:
1708 flow_verbs_translate_action_flag(dev_flow, actions);
1709 action_flags |= MLX5_FLOW_ACTION_FLAG;
1710 dev_flow->handle->mark = 1;
1712 case RTE_FLOW_ACTION_TYPE_MARK:
1713 flow_verbs_translate_action_mark(dev_flow, actions);
1714 action_flags |= MLX5_FLOW_ACTION_MARK;
1715 dev_flow->handle->mark = 1;
1717 case RTE_FLOW_ACTION_TYPE_DROP:
1718 flow_verbs_translate_action_drop(dev_flow, actions);
1719 action_flags |= MLX5_FLOW_ACTION_DROP;
1720 dev_flow->handle->fate_action = MLX5_FLOW_FATE_DROP;
1722 case RTE_FLOW_ACTION_TYPE_QUEUE:
1723 flow_verbs_translate_action_queue(rss_desc, actions);
1724 action_flags |= MLX5_FLOW_ACTION_QUEUE;
1725 dev_flow->handle->fate_action = MLX5_FLOW_FATE_QUEUE;
1727 case RTE_FLOW_ACTION_TYPE_RSS:
1728 flow_verbs_translate_action_rss(rss_desc, actions);
1729 action_flags |= MLX5_FLOW_ACTION_RSS;
1730 dev_flow->handle->fate_action = MLX5_FLOW_FATE_QUEUE;
1732 case RTE_FLOW_ACTION_TYPE_COUNT:
1733 ret = flow_verbs_translate_action_count(dev_flow,
1738 action_flags |= MLX5_FLOW_ACTION_COUNT;
1741 return rte_flow_error_set(error, ENOTSUP,
1742 RTE_FLOW_ERROR_TYPE_ACTION,
1744 "action not supported");
1747 dev_flow->act_flags = action_flags;
1748 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
1749 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1751 switch (items->type) {
1752 case RTE_FLOW_ITEM_TYPE_VOID:
1754 case RTE_FLOW_ITEM_TYPE_ETH:
1755 flow_verbs_translate_item_eth(dev_flow, items,
1757 subpriority = MLX5_PRIORITY_MAP_L2;
1758 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
1759 MLX5_FLOW_LAYER_OUTER_L2;
1761 case RTE_FLOW_ITEM_TYPE_VLAN:
1762 flow_verbs_translate_item_vlan(dev_flow, items,
1764 subpriority = MLX5_PRIORITY_MAP_L2;
1765 item_flags |= tunnel ? (MLX5_FLOW_LAYER_INNER_L2 |
1766 MLX5_FLOW_LAYER_INNER_VLAN) :
1767 (MLX5_FLOW_LAYER_OUTER_L2 |
1768 MLX5_FLOW_LAYER_OUTER_VLAN);
1770 case RTE_FLOW_ITEM_TYPE_IPV4:
1771 flow_verbs_translate_item_ipv4(dev_flow, items,
1773 subpriority = MLX5_PRIORITY_MAP_L3;
1774 dev_flow->hash_fields |=
1775 mlx5_flow_hashfields_adjust
1777 MLX5_IPV4_LAYER_TYPES,
1778 MLX5_IPV4_IBV_RX_HASH);
1779 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
1780 MLX5_FLOW_LAYER_OUTER_L3_IPV4;
1782 case RTE_FLOW_ITEM_TYPE_IPV6:
1783 flow_verbs_translate_item_ipv6(dev_flow, items,
1785 subpriority = MLX5_PRIORITY_MAP_L3;
1786 dev_flow->hash_fields |=
1787 mlx5_flow_hashfields_adjust
1789 MLX5_IPV6_LAYER_TYPES,
1790 MLX5_IPV6_IBV_RX_HASH);
1791 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
1792 MLX5_FLOW_LAYER_OUTER_L3_IPV6;
1794 case RTE_FLOW_ITEM_TYPE_TCP:
1795 flow_verbs_translate_item_tcp(dev_flow, items,
1797 subpriority = MLX5_PRIORITY_MAP_L4;
1798 dev_flow->hash_fields |=
1799 mlx5_flow_hashfields_adjust
1800 (rss_desc, tunnel, ETH_RSS_TCP,
1801 (IBV_RX_HASH_SRC_PORT_TCP |
1802 IBV_RX_HASH_DST_PORT_TCP));
1803 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
1804 MLX5_FLOW_LAYER_OUTER_L4_TCP;
1806 case RTE_FLOW_ITEM_TYPE_UDP:
1807 flow_verbs_translate_item_udp(dev_flow, items,
1809 subpriority = MLX5_PRIORITY_MAP_L4;
1810 dev_flow->hash_fields |=
1811 mlx5_flow_hashfields_adjust
1812 (rss_desc, tunnel, ETH_RSS_UDP,
1813 (IBV_RX_HASH_SRC_PORT_UDP |
1814 IBV_RX_HASH_DST_PORT_UDP));
1815 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
1816 MLX5_FLOW_LAYER_OUTER_L4_UDP;
1818 case RTE_FLOW_ITEM_TYPE_VXLAN:
1819 flow_verbs_translate_item_vxlan(dev_flow, items,
1821 subpriority = MLX5_TUNNEL_PRIO_GET(rss_desc);
1822 item_flags |= MLX5_FLOW_LAYER_VXLAN;
1824 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
1825 flow_verbs_translate_item_vxlan_gpe(dev_flow, items,
1827 subpriority = MLX5_TUNNEL_PRIO_GET(rss_desc);
1828 item_flags |= MLX5_FLOW_LAYER_VXLAN_GPE;
1830 case RTE_FLOW_ITEM_TYPE_GRE:
1831 flow_verbs_translate_item_gre(dev_flow, items,
1833 subpriority = MLX5_TUNNEL_PRIO_GET(rss_desc);
1834 item_flags |= MLX5_FLOW_LAYER_GRE;
1836 case RTE_FLOW_ITEM_TYPE_MPLS:
1837 flow_verbs_translate_item_mpls(dev_flow, items,
1839 subpriority = MLX5_TUNNEL_PRIO_GET(rss_desc);
1840 item_flags |= MLX5_FLOW_LAYER_MPLS;
1843 return rte_flow_error_set(error, ENOTSUP,
1844 RTE_FLOW_ERROR_TYPE_ITEM,
1846 "item not supported");
1849 dev_flow->handle->layers = item_flags;
1850 /* Other members of attr will be ignored. */
1851 dev_flow->verbs.attr.priority =
1852 mlx5_flow_adjust_priority(dev, priority, subpriority);
1853 dev_flow->verbs.attr.port = (uint8_t)priv->dev_port;
1858 * Remove the flow from the NIC but keeps it in memory.
1861 * Pointer to the Ethernet device structure.
1862 * @param[in, out] flow
1863 * Pointer to flow structure.
1866 flow_verbs_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
1868 struct mlx5_priv *priv = dev->data->dev_private;
1869 struct mlx5_flow_handle *handle;
1870 uint32_t handle_idx;
1874 SILIST_FOREACH(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW], flow->dev_handles,
1875 handle_idx, handle, next) {
1876 if (handle->drv_flow) {
1877 claim_zero(mlx5_glue->destroy_flow(handle->drv_flow));
1878 handle->drv_flow = NULL;
1880 /* hrxq is union, don't touch it only the flag is set. */
1881 if (handle->rix_hrxq) {
1882 if (handle->fate_action == MLX5_FLOW_FATE_DROP) {
1883 mlx5_drop_action_destroy(dev);
1884 handle->rix_hrxq = 0;
1885 } else if (handle->fate_action ==
1886 MLX5_FLOW_FATE_QUEUE) {
1887 mlx5_hrxq_release(dev, handle->rix_hrxq);
1888 handle->rix_hrxq = 0;
1891 if (handle->vf_vlan.tag && handle->vf_vlan.created)
1892 mlx5_vlan_vmwa_release(dev, &handle->vf_vlan);
1897 * Remove the flow from the NIC and the memory.
1900 * Pointer to the Ethernet device structure.
1901 * @param[in, out] flow
1902 * Pointer to flow structure.
1905 flow_verbs_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
1907 struct mlx5_priv *priv = dev->data->dev_private;
1908 struct mlx5_flow_handle *handle;
1912 flow_verbs_remove(dev, flow);
1913 while (flow->dev_handles) {
1914 uint32_t tmp_idx = flow->dev_handles;
1916 handle = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
1920 flow->dev_handles = handle->next.next;
1921 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
1924 if (flow->counter) {
1925 flow_verbs_counter_release(dev, flow->counter);
1931 * Apply the flow to the NIC.
1934 * Pointer to the Ethernet device structure.
1935 * @param[in, out] flow
1936 * Pointer to flow structure.
1938 * Pointer to error structure.
1941 * 0 on success, a negative errno value otherwise and rte_errno is set.
1944 flow_verbs_apply(struct rte_eth_dev *dev, struct rte_flow *flow,
1945 struct rte_flow_error *error)
1947 struct mlx5_priv *priv = dev->data->dev_private;
1948 struct mlx5_flow_handle *handle;
1949 struct mlx5_flow *dev_flow;
1950 struct mlx5_hrxq *hrxq;
1951 uint32_t dev_handles;
1955 for (idx = priv->flow_idx - 1; idx >= priv->flow_nested_idx; idx--) {
1956 dev_flow = &((struct mlx5_flow *)priv->inter_flows)[idx];
1957 handle = dev_flow->handle;
1958 if (handle->fate_action == MLX5_FLOW_FATE_DROP) {
1959 hrxq = mlx5_drop_action_create(dev);
1963 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1964 "cannot get drop hash queue");
1969 struct mlx5_flow_rss_desc *rss_desc =
1970 &((struct mlx5_flow_rss_desc *)priv->rss_desc)
1971 [!!priv->flow_nested_idx];
1973 MLX5_ASSERT(rss_desc->queue_num);
1974 hrxq_idx = mlx5_hrxq_get(dev, rss_desc->key,
1975 MLX5_RSS_HASH_KEY_LEN,
1976 dev_flow->hash_fields,
1978 rss_desc->queue_num);
1980 hrxq_idx = mlx5_hrxq_new
1981 (dev, rss_desc->key,
1982 MLX5_RSS_HASH_KEY_LEN,
1983 dev_flow->hash_fields,
1985 rss_desc->queue_num,
1987 MLX5_FLOW_LAYER_TUNNEL),
1989 hrxq = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_HRXQ],
1994 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1995 "cannot get hash queue");
1998 handle->rix_hrxq = hrxq_idx;
2001 handle->drv_flow = mlx5_glue->create_flow
2002 (hrxq->qp, &dev_flow->verbs.attr);
2003 if (!handle->drv_flow) {
2004 rte_flow_error_set(error, errno,
2005 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2007 "hardware refuses to create flow");
2010 if (priv->vmwa_context &&
2011 handle->vf_vlan.tag && !handle->vf_vlan.created) {
2013 * The rule contains the VLAN pattern.
2014 * For VF we are going to create VLAN
2015 * interface to make hypervisor set correct
2016 * e-Switch vport context.
2018 mlx5_vlan_vmwa_acquire(dev, &handle->vf_vlan);
2023 err = rte_errno; /* Save rte_errno before cleanup. */
2024 SILIST_FOREACH(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW], flow->dev_handles,
2025 dev_handles, handle, next) {
2026 /* hrxq is union, don't touch it only the flag is set. */
2027 if (handle->rix_hrxq) {
2028 if (handle->fate_action == MLX5_FLOW_FATE_DROP) {
2029 mlx5_drop_action_destroy(dev);
2030 handle->rix_hrxq = 0;
2031 } else if (handle->fate_action ==
2032 MLX5_FLOW_FATE_QUEUE) {
2033 mlx5_hrxq_release(dev, handle->rix_hrxq);
2034 handle->rix_hrxq = 0;
2037 if (handle->vf_vlan.tag && handle->vf_vlan.created)
2038 mlx5_vlan_vmwa_release(dev, &handle->vf_vlan);
2040 rte_errno = err; /* Restore rte_errno. */
2047 * @see rte_flow_query()
2051 flow_verbs_query(struct rte_eth_dev *dev,
2052 struct rte_flow *flow,
2053 const struct rte_flow_action *actions,
2055 struct rte_flow_error *error)
2059 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
2060 switch (actions->type) {
2061 case RTE_FLOW_ACTION_TYPE_VOID:
2063 case RTE_FLOW_ACTION_TYPE_COUNT:
2064 ret = flow_verbs_counter_query(dev, flow, data, error);
2067 return rte_flow_error_set(error, ENOTSUP,
2068 RTE_FLOW_ERROR_TYPE_ACTION,
2070 "action not supported");
2076 const struct mlx5_flow_driver_ops mlx5_flow_verbs_drv_ops = {
2077 .validate = flow_verbs_validate,
2078 .prepare = flow_verbs_prepare,
2079 .translate = flow_verbs_translate,
2080 .apply = flow_verbs_apply,
2081 .remove = flow_verbs_remove,
2082 .destroy = flow_verbs_destroy,
2083 .query = flow_verbs_query,