1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2018 Mellanox Technologies, Ltd
5 #include <netinet/in.h>
11 #include <rte_common.h>
12 #include <rte_ether.h>
13 #include <ethdev_driver.h>
15 #include <rte_flow_driver.h>
16 #include <rte_malloc.h>
19 #include <mlx5_glue.h>
21 #include <mlx5_malloc.h>
23 #include "mlx5_defs.h"
25 #include "mlx5_flow.h"
28 #define VERBS_SPEC_INNER(item_flags) \
29 (!!((item_flags) & MLX5_FLOW_LAYER_TUNNEL) ? IBV_FLOW_SPEC_INNER : 0)
31 /* Verbs specification header. */
32 struct ibv_spec_header {
33 enum ibv_flow_spec_type type;
38 * Discover the maximum number of priority available.
41 * Pointer to the Ethernet device structure.
43 * Expected result variants.
45 * Number of entries in @p vprio array.
47 * Number of supported flow priority on success, a negative errno
48 * value otherwise and rte_errno is set.
51 flow_verbs_discover_priorities(struct rte_eth_dev *dev,
52 const uint16_t *vprio, int vprio_n)
54 struct mlx5_priv *priv = dev->data->dev_private;
56 struct ibv_flow_attr attr;
57 struct ibv_flow_spec_eth eth;
58 struct ibv_flow_spec_action_drop drop;
62 .port = (uint8_t)priv->dev_port,
65 .type = IBV_FLOW_SPEC_ETH,
66 .size = sizeof(struct ibv_flow_spec_eth),
69 .size = sizeof(struct ibv_flow_spec_action_drop),
70 .type = IBV_FLOW_SPEC_ACTION_DROP,
73 struct ibv_flow *flow;
74 struct mlx5_hrxq *drop = priv->drop_queue.hrxq;
78 #if defined(HAVE_MLX5DV_DR_DEVX_PORT) || defined(HAVE_MLX5DV_DR_DEVX_PORT_V35)
79 /* If DevX supported, driver must support 16 verbs flow priorities. */
87 for (i = 0; i != vprio_n; i++) {
88 flow_attr.attr.priority = vprio[i] - 1;
89 flow = mlx5_glue->create_flow(drop->qp, &flow_attr.attr);
92 claim_zero(mlx5_glue->destroy_flow(flow));
95 #if defined(HAVE_MLX5DV_DR_DEVX_PORT) || defined(HAVE_MLX5DV_DR_DEVX_PORT_V35)
98 DRV_LOG(INFO, "port %u supported flow priorities:"
99 " 0-%d for ingress or egress root table,"
100 " 0-%d for non-root table or transfer root table.",
101 dev->data->port_id, priority - 2,
102 MLX5_NON_ROOT_FLOW_MAX_PRIO - 1);
107 * Get Verbs flow counter by index.
110 * Pointer to the Ethernet device structure.
112 * mlx5 flow counter index in the container.
114 * mlx5 flow counter pool in the container,
117 * A pointer to the counter, NULL otherwise.
119 static struct mlx5_flow_counter *
120 flow_verbs_counter_get_by_idx(struct rte_eth_dev *dev,
122 struct mlx5_flow_counter_pool **ppool)
124 struct mlx5_priv *priv = dev->data->dev_private;
125 struct mlx5_flow_counter_mng *cmng = &priv->sh->cmng;
126 struct mlx5_flow_counter_pool *pool;
128 idx = (idx - 1) & (MLX5_CNT_SHARED_OFFSET - 1);
129 pool = cmng->pools[idx / MLX5_COUNTERS_PER_POOL];
133 return MLX5_POOL_GET_CNT(pool, idx % MLX5_COUNTERS_PER_POOL);
137 * Create Verbs flow counter with Verbs library.
140 * Pointer to the Ethernet device structure.
141 * @param[in, out] counter
142 * mlx5 flow counter object, contains the counter id,
143 * handle of created Verbs flow counter is returned
144 * in cs field (if counters are supported).
147 * 0 On success else a negative errno value is returned
148 * and rte_errno is set.
151 flow_verbs_counter_create(struct rte_eth_dev *dev,
152 struct mlx5_flow_counter *counter)
154 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42)
155 struct mlx5_priv *priv = dev->data->dev_private;
156 struct ibv_context *ctx = priv->sh->cdev->ctx;
157 struct ibv_counter_set_init_attr init = {
158 .counter_set_id = counter->shared_info.id};
160 counter->dcs_when_free = mlx5_glue->create_counter_set(ctx, &init);
161 if (!counter->dcs_when_free) {
166 #elif defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
167 struct mlx5_priv *priv = dev->data->dev_private;
168 struct ibv_context *ctx = priv->sh->cdev->ctx;
169 struct ibv_counters_init_attr init = {0};
170 struct ibv_counter_attach_attr attach;
173 memset(&attach, 0, sizeof(attach));
174 counter->dcs_when_free = mlx5_glue->create_counters(ctx, &init);
175 if (!counter->dcs_when_free) {
179 attach.counter_desc = IBV_COUNTER_PACKETS;
181 ret = mlx5_glue->attach_counters(counter->dcs_when_free, &attach, NULL);
183 attach.counter_desc = IBV_COUNTER_BYTES;
185 ret = mlx5_glue->attach_counters
186 (counter->dcs_when_free, &attach, NULL);
189 claim_zero(mlx5_glue->destroy_counters(counter->dcs_when_free));
190 counter->dcs_when_free = NULL;
204 * Get a flow counter.
207 * Pointer to the Ethernet device structure.
209 * Counter identifier.
212 * Index to the counter, 0 otherwise and rte_errno is set.
215 flow_verbs_counter_new(struct rte_eth_dev *dev, uint32_t id __rte_unused)
217 struct mlx5_priv *priv = dev->data->dev_private;
218 struct mlx5_flow_counter_mng *cmng = &priv->sh->cmng;
219 struct mlx5_flow_counter_pool *pool = NULL;
220 struct mlx5_flow_counter *cnt = NULL;
221 uint32_t n_valid = cmng->n_valid;
222 uint32_t pool_idx, cnt_idx;
226 for (pool_idx = 0; pool_idx < n_valid; ++pool_idx) {
227 pool = cmng->pools[pool_idx];
230 cnt = TAILQ_FIRST(&pool->counters[0]);
235 struct mlx5_flow_counter_pool **pools;
238 if (n_valid == cmng->n) {
239 /* Resize the container pool array. */
240 size = sizeof(struct mlx5_flow_counter_pool *) *
241 (n_valid + MLX5_CNT_CONTAINER_RESIZE);
242 pools = mlx5_malloc(MLX5_MEM_ZERO, size, 0,
247 memcpy(pools, cmng->pools,
248 sizeof(struct mlx5_flow_counter_pool *) *
250 mlx5_free(cmng->pools);
253 cmng->n += MLX5_CNT_CONTAINER_RESIZE;
255 /* Allocate memory for new pool*/
256 size = sizeof(*pool) + sizeof(*cnt) * MLX5_COUNTERS_PER_POOL;
257 pool = mlx5_malloc(MLX5_MEM_ZERO, size, 0, SOCKET_ID_ANY);
260 for (i = 0; i < MLX5_COUNTERS_PER_POOL; ++i) {
261 cnt = MLX5_POOL_GET_CNT(pool, i);
262 TAILQ_INSERT_HEAD(&pool->counters[0], cnt, next);
264 cnt = MLX5_POOL_GET_CNT(pool, 0);
265 cmng->pools[n_valid] = pool;
269 TAILQ_REMOVE(&pool->counters[0], cnt, next);
270 i = MLX5_CNT_ARRAY_IDX(pool, cnt);
271 cnt_idx = MLX5_MAKE_CNT_IDX(pool_idx, i);
272 /* Create counter with Verbs. */
273 ret = flow_verbs_counter_create(dev, cnt);
275 cnt->dcs_when_active = cnt->dcs_when_free;
280 TAILQ_INSERT_HEAD(&pool->counters[0], cnt, next);
281 /* Some error occurred in Verbs library. */
287 * Release a flow counter.
290 * Pointer to the Ethernet device structure.
292 * Index to the counter handler.
295 flow_verbs_counter_release(struct rte_eth_dev *dev, uint32_t counter)
297 struct mlx5_flow_counter_pool *pool;
298 struct mlx5_flow_counter *cnt;
300 cnt = flow_verbs_counter_get_by_idx(dev, counter, &pool);
301 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42)
302 claim_zero(mlx5_glue->destroy_counter_set
303 ((struct ibv_counter_set *)cnt->dcs_when_active));
304 #elif defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
305 claim_zero(mlx5_glue->destroy_counters
306 ((struct ibv_counters *)cnt->dcs_when_active));
308 TAILQ_INSERT_HEAD(&pool->counters[0], cnt, next);
312 * Query a flow counter via Verbs library call.
314 * @see rte_flow_query()
318 flow_verbs_counter_query(struct rte_eth_dev *dev __rte_unused,
319 struct rte_flow *flow, void *data,
320 struct rte_flow_error *error)
322 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42) || \
323 defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
325 struct mlx5_flow_counter_pool *pool;
326 struct mlx5_flow_counter *cnt = flow_verbs_counter_get_by_idx
327 (dev, flow->counter, &pool);
328 struct rte_flow_query_count *qc = data;
329 uint64_t counters[2] = {0, 0};
330 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42)
331 struct ibv_query_counter_set_attr query_cs_attr = {
332 .dcs_when_free = (struct ibv_counter_set *)
333 cnt->dcs_when_active,
334 .query_flags = IBV_COUNTER_SET_FORCE_UPDATE,
336 struct ibv_counter_set_data query_out = {
338 .outlen = 2 * sizeof(uint64_t),
340 int err = mlx5_glue->query_counter_set(&query_cs_attr,
342 #elif defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
343 int err = mlx5_glue->query_counters
344 ((struct ibv_counters *)cnt->dcs_when_active, counters,
346 IBV_READ_COUNTERS_ATTR_PREFER_CACHED);
349 return rte_flow_error_set
351 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
353 "cannot read counter");
356 qc->hits = counters[0] - cnt->hits;
357 qc->bytes = counters[1] - cnt->bytes;
359 cnt->hits = counters[0];
360 cnt->bytes = counters[1];
364 return rte_flow_error_set(error, EINVAL,
365 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
367 "flow does not have counter");
371 return rte_flow_error_set(error, ENOTSUP,
372 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
374 "counters are not available");
379 * Add a verbs item specification into @p verbs.
382 * Pointer to verbs structure.
384 * Create specification.
386 * Size in bytes of the specification to copy.
389 flow_verbs_spec_add(struct mlx5_flow_verbs_workspace *verbs,
390 void *src, unsigned int size)
396 MLX5_ASSERT(verbs->specs);
397 dst = (void *)(verbs->specs + verbs->size);
398 memcpy(dst, src, size);
399 ++verbs->attr.num_of_specs;
404 * Convert the @p item into a Verbs specification. This function assumes that
405 * the input is valid and that there is space to insert the requested item
408 * @param[in, out] dev_flow
409 * Pointer to dev_flow structure.
411 * Item specification.
412 * @param[in] item_flags
416 flow_verbs_translate_item_eth(struct mlx5_flow *dev_flow,
417 const struct rte_flow_item *item,
420 const struct rte_flow_item_eth *spec = item->spec;
421 const struct rte_flow_item_eth *mask = item->mask;
422 const unsigned int size = sizeof(struct ibv_flow_spec_eth);
423 struct ibv_flow_spec_eth eth = {
424 .type = IBV_FLOW_SPEC_ETH | VERBS_SPEC_INNER(item_flags),
429 mask = &rte_flow_item_eth_mask;
433 memcpy(ð.val.dst_mac, spec->dst.addr_bytes,
435 memcpy(ð.val.src_mac, spec->src.addr_bytes,
437 eth.val.ether_type = spec->type;
438 memcpy(ð.mask.dst_mac, mask->dst.addr_bytes,
440 memcpy(ð.mask.src_mac, mask->src.addr_bytes,
442 eth.mask.ether_type = mask->type;
443 /* Remove unwanted bits from values. */
444 for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i) {
445 eth.val.dst_mac[i] &= eth.mask.dst_mac[i];
446 eth.val.src_mac[i] &= eth.mask.src_mac[i];
448 eth.val.ether_type &= eth.mask.ether_type;
450 flow_verbs_spec_add(&dev_flow->verbs, ð, size);
454 * Update the VLAN tag in the Verbs Ethernet specification.
455 * This function assumes that the input is valid and there is space to add
456 * the requested item.
458 * @param[in, out] attr
459 * Pointer to Verbs attributes structure.
461 * Verbs structure containing the VLAN information to copy.
464 flow_verbs_item_vlan_update(struct ibv_flow_attr *attr,
465 struct ibv_flow_spec_eth *eth)
468 const enum ibv_flow_spec_type search = eth->type;
469 struct ibv_spec_header *hdr = (struct ibv_spec_header *)
470 ((uint8_t *)attr + sizeof(struct ibv_flow_attr));
472 for (i = 0; i != attr->num_of_specs; ++i) {
473 if (hdr->type == search) {
474 struct ibv_flow_spec_eth *e =
475 (struct ibv_flow_spec_eth *)hdr;
477 e->val.vlan_tag = eth->val.vlan_tag;
478 e->mask.vlan_tag = eth->mask.vlan_tag;
479 e->val.ether_type = eth->val.ether_type;
480 e->mask.ether_type = eth->mask.ether_type;
483 hdr = (struct ibv_spec_header *)((uint8_t *)hdr + hdr->size);
488 * Convert the @p item into a Verbs specification. This function assumes that
489 * the input is valid and that there is space to insert the requested item
492 * @param[in, out] dev_flow
493 * Pointer to dev_flow structure.
495 * Item specification.
496 * @param[in] item_flags
500 flow_verbs_translate_item_vlan(struct mlx5_flow *dev_flow,
501 const struct rte_flow_item *item,
504 const struct rte_flow_item_vlan *spec = item->spec;
505 const struct rte_flow_item_vlan *mask = item->mask;
506 unsigned int size = sizeof(struct ibv_flow_spec_eth);
507 const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
508 struct ibv_flow_spec_eth eth = {
509 .type = IBV_FLOW_SPEC_ETH | VERBS_SPEC_INNER(item_flags),
512 const uint32_t l2m = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
513 MLX5_FLOW_LAYER_OUTER_L2;
516 mask = &rte_flow_item_vlan_mask;
518 eth.val.vlan_tag = spec->tci;
519 eth.mask.vlan_tag = mask->tci;
520 eth.val.vlan_tag &= eth.mask.vlan_tag;
521 eth.val.ether_type = spec->inner_type;
522 eth.mask.ether_type = mask->inner_type;
523 eth.val.ether_type &= eth.mask.ether_type;
525 if (!(item_flags & l2m))
526 flow_verbs_spec_add(&dev_flow->verbs, ð, size);
528 flow_verbs_item_vlan_update(&dev_flow->verbs.attr, ð);
530 dev_flow->handle->vf_vlan.tag =
531 rte_be_to_cpu_16(spec->tci) & 0x0fff;
535 * Convert the @p item into a Verbs specification. This function assumes that
536 * the input is valid and that there is space to insert the requested item
539 * @param[in, out] dev_flow
540 * Pointer to dev_flow structure.
542 * Item specification.
543 * @param[in] item_flags
547 flow_verbs_translate_item_ipv4(struct mlx5_flow *dev_flow,
548 const struct rte_flow_item *item,
551 const struct rte_flow_item_ipv4 *spec = item->spec;
552 const struct rte_flow_item_ipv4 *mask = item->mask;
553 unsigned int size = sizeof(struct ibv_flow_spec_ipv4_ext);
554 struct ibv_flow_spec_ipv4_ext ipv4 = {
555 .type = IBV_FLOW_SPEC_IPV4_EXT | VERBS_SPEC_INNER(item_flags),
560 mask = &rte_flow_item_ipv4_mask;
562 ipv4.val = (struct ibv_flow_ipv4_ext_filter){
563 .src_ip = spec->hdr.src_addr,
564 .dst_ip = spec->hdr.dst_addr,
565 .proto = spec->hdr.next_proto_id,
566 .tos = spec->hdr.type_of_service,
568 ipv4.mask = (struct ibv_flow_ipv4_ext_filter){
569 .src_ip = mask->hdr.src_addr,
570 .dst_ip = mask->hdr.dst_addr,
571 .proto = mask->hdr.next_proto_id,
572 .tos = mask->hdr.type_of_service,
574 /* Remove unwanted bits from values. */
575 ipv4.val.src_ip &= ipv4.mask.src_ip;
576 ipv4.val.dst_ip &= ipv4.mask.dst_ip;
577 ipv4.val.proto &= ipv4.mask.proto;
578 ipv4.val.tos &= ipv4.mask.tos;
580 flow_verbs_spec_add(&dev_flow->verbs, &ipv4, size);
584 * Convert the @p item into a Verbs specification. This function assumes that
585 * the input is valid and that there is space to insert the requested item
588 * @param[in, out] dev_flow
589 * Pointer to dev_flow structure.
591 * Item specification.
592 * @param[in] item_flags
596 flow_verbs_translate_item_ipv6(struct mlx5_flow *dev_flow,
597 const struct rte_flow_item *item,
600 const struct rte_flow_item_ipv6 *spec = item->spec;
601 const struct rte_flow_item_ipv6 *mask = item->mask;
602 unsigned int size = sizeof(struct ibv_flow_spec_ipv6);
603 struct ibv_flow_spec_ipv6 ipv6 = {
604 .type = IBV_FLOW_SPEC_IPV6 | VERBS_SPEC_INNER(item_flags),
609 mask = &rte_flow_item_ipv6_mask;
612 uint32_t vtc_flow_val;
613 uint32_t vtc_flow_mask;
615 memcpy(&ipv6.val.src_ip, spec->hdr.src_addr,
616 RTE_DIM(ipv6.val.src_ip));
617 memcpy(&ipv6.val.dst_ip, spec->hdr.dst_addr,
618 RTE_DIM(ipv6.val.dst_ip));
619 memcpy(&ipv6.mask.src_ip, mask->hdr.src_addr,
620 RTE_DIM(ipv6.mask.src_ip));
621 memcpy(&ipv6.mask.dst_ip, mask->hdr.dst_addr,
622 RTE_DIM(ipv6.mask.dst_ip));
623 vtc_flow_val = rte_be_to_cpu_32(spec->hdr.vtc_flow);
624 vtc_flow_mask = rte_be_to_cpu_32(mask->hdr.vtc_flow);
625 ipv6.val.flow_label =
626 rte_cpu_to_be_32((vtc_flow_val & RTE_IPV6_HDR_FL_MASK) >>
627 RTE_IPV6_HDR_FL_SHIFT);
628 ipv6.val.traffic_class = (vtc_flow_val & RTE_IPV6_HDR_TC_MASK) >>
629 RTE_IPV6_HDR_TC_SHIFT;
630 ipv6.val.next_hdr = spec->hdr.proto;
631 ipv6.mask.flow_label =
632 rte_cpu_to_be_32((vtc_flow_mask & RTE_IPV6_HDR_FL_MASK) >>
633 RTE_IPV6_HDR_FL_SHIFT);
634 ipv6.mask.traffic_class = (vtc_flow_mask & RTE_IPV6_HDR_TC_MASK) >>
635 RTE_IPV6_HDR_TC_SHIFT;
636 ipv6.mask.next_hdr = mask->hdr.proto;
637 /* Remove unwanted bits from values. */
638 for (i = 0; i < RTE_DIM(ipv6.val.src_ip); ++i) {
639 ipv6.val.src_ip[i] &= ipv6.mask.src_ip[i];
640 ipv6.val.dst_ip[i] &= ipv6.mask.dst_ip[i];
642 ipv6.val.flow_label &= ipv6.mask.flow_label;
643 ipv6.val.traffic_class &= ipv6.mask.traffic_class;
644 ipv6.val.next_hdr &= ipv6.mask.next_hdr;
646 flow_verbs_spec_add(&dev_flow->verbs, &ipv6, size);
650 * Convert the @p item into a Verbs specification. This function assumes that
651 * the input is valid and that there is space to insert the requested item
654 * @param[in, out] dev_flow
655 * Pointer to dev_flow structure.
657 * Item specification.
658 * @param[in] item_flags
662 flow_verbs_translate_item_tcp(struct mlx5_flow *dev_flow,
663 const struct rte_flow_item *item,
664 uint64_t item_flags __rte_unused)
666 const struct rte_flow_item_tcp *spec = item->spec;
667 const struct rte_flow_item_tcp *mask = item->mask;
668 unsigned int size = sizeof(struct ibv_flow_spec_tcp_udp);
669 struct ibv_flow_spec_tcp_udp tcp = {
670 .type = IBV_FLOW_SPEC_TCP | VERBS_SPEC_INNER(item_flags),
675 mask = &rte_flow_item_tcp_mask;
677 tcp.val.dst_port = spec->hdr.dst_port;
678 tcp.val.src_port = spec->hdr.src_port;
679 tcp.mask.dst_port = mask->hdr.dst_port;
680 tcp.mask.src_port = mask->hdr.src_port;
681 /* Remove unwanted bits from values. */
682 tcp.val.src_port &= tcp.mask.src_port;
683 tcp.val.dst_port &= tcp.mask.dst_port;
685 flow_verbs_spec_add(&dev_flow->verbs, &tcp, size);
689 * Convert the @p item into a Verbs specification. This function assumes that
690 * the input is valid and that there is space to insert the requested item
693 * @param[in, out] dev_flow
694 * Pointer to dev_flow structure.
696 * Item specification.
697 * @param[in] item_flags
701 flow_verbs_translate_item_udp(struct mlx5_flow *dev_flow,
702 const struct rte_flow_item *item,
703 uint64_t item_flags __rte_unused)
705 const struct rte_flow_item_udp *spec = item->spec;
706 const struct rte_flow_item_udp *mask = item->mask;
707 unsigned int size = sizeof(struct ibv_flow_spec_tcp_udp);
708 struct ibv_flow_spec_tcp_udp udp = {
709 .type = IBV_FLOW_SPEC_UDP | VERBS_SPEC_INNER(item_flags),
714 mask = &rte_flow_item_udp_mask;
716 udp.val.dst_port = spec->hdr.dst_port;
717 udp.val.src_port = spec->hdr.src_port;
718 udp.mask.dst_port = mask->hdr.dst_port;
719 udp.mask.src_port = mask->hdr.src_port;
720 /* Remove unwanted bits from values. */
721 udp.val.src_port &= udp.mask.src_port;
722 udp.val.dst_port &= udp.mask.dst_port;
725 while (item->type == RTE_FLOW_ITEM_TYPE_VOID)
727 if (!(udp.val.dst_port & udp.mask.dst_port)) {
728 switch ((item)->type) {
729 case RTE_FLOW_ITEM_TYPE_VXLAN:
730 udp.val.dst_port = htons(MLX5_UDP_PORT_VXLAN);
731 udp.mask.dst_port = 0xffff;
733 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
734 udp.val.dst_port = htons(MLX5_UDP_PORT_VXLAN_GPE);
735 udp.mask.dst_port = 0xffff;
737 case RTE_FLOW_ITEM_TYPE_MPLS:
738 udp.val.dst_port = htons(MLX5_UDP_PORT_MPLS);
739 udp.mask.dst_port = 0xffff;
746 flow_verbs_spec_add(&dev_flow->verbs, &udp, size);
750 * Convert the @p item into a Verbs specification. This function assumes that
751 * the input is valid and that there is space to insert the requested item
754 * @param[in, out] dev_flow
755 * Pointer to dev_flow structure.
757 * Item specification.
758 * @param[in] item_flags
762 flow_verbs_translate_item_vxlan(struct mlx5_flow *dev_flow,
763 const struct rte_flow_item *item,
764 uint64_t item_flags __rte_unused)
766 const struct rte_flow_item_vxlan *spec = item->spec;
767 const struct rte_flow_item_vxlan *mask = item->mask;
768 unsigned int size = sizeof(struct ibv_flow_spec_tunnel);
769 struct ibv_flow_spec_tunnel vxlan = {
770 .type = IBV_FLOW_SPEC_VXLAN_TUNNEL,
776 } id = { .vlan_id = 0, };
779 mask = &rte_flow_item_vxlan_mask;
781 memcpy(&id.vni[1], spec->vni, 3);
782 vxlan.val.tunnel_id = id.vlan_id;
783 memcpy(&id.vni[1], mask->vni, 3);
784 vxlan.mask.tunnel_id = id.vlan_id;
785 /* Remove unwanted bits from values. */
786 vxlan.val.tunnel_id &= vxlan.mask.tunnel_id;
788 flow_verbs_spec_add(&dev_flow->verbs, &vxlan, size);
792 * Convert the @p item into a Verbs specification. This function assumes that
793 * the input is valid and that there is space to insert the requested item
796 * @param[in, out] dev_flow
797 * Pointer to dev_flow structure.
799 * Item specification.
800 * @param[in] item_flags
804 flow_verbs_translate_item_vxlan_gpe(struct mlx5_flow *dev_flow,
805 const struct rte_flow_item *item,
806 uint64_t item_flags __rte_unused)
808 const struct rte_flow_item_vxlan_gpe *spec = item->spec;
809 const struct rte_flow_item_vxlan_gpe *mask = item->mask;
810 unsigned int size = sizeof(struct ibv_flow_spec_tunnel);
811 struct ibv_flow_spec_tunnel vxlan_gpe = {
812 .type = IBV_FLOW_SPEC_VXLAN_TUNNEL,
818 } id = { .vlan_id = 0, };
821 mask = &rte_flow_item_vxlan_gpe_mask;
823 memcpy(&id.vni[1], spec->vni, 3);
824 vxlan_gpe.val.tunnel_id = id.vlan_id;
825 memcpy(&id.vni[1], mask->vni, 3);
826 vxlan_gpe.mask.tunnel_id = id.vlan_id;
827 /* Remove unwanted bits from values. */
828 vxlan_gpe.val.tunnel_id &= vxlan_gpe.mask.tunnel_id;
830 flow_verbs_spec_add(&dev_flow->verbs, &vxlan_gpe, size);
834 * Update the protocol in Verbs IPv4/IPv6 spec.
836 * @param[in, out] attr
837 * Pointer to Verbs attributes structure.
839 * Specification type to search in order to update the IP protocol.
840 * @param[in] protocol
841 * Protocol value to set if none is present in the specification.
844 flow_verbs_item_gre_ip_protocol_update(struct ibv_flow_attr *attr,
845 enum ibv_flow_spec_type search,
849 struct ibv_spec_header *hdr = (struct ibv_spec_header *)
850 ((uint8_t *)attr + sizeof(struct ibv_flow_attr));
854 for (i = 0; i != attr->num_of_specs; ++i) {
855 if (hdr->type == search) {
857 struct ibv_flow_spec_ipv4_ext *ipv4;
858 struct ibv_flow_spec_ipv6 *ipv6;
862 case IBV_FLOW_SPEC_IPV4_EXT:
863 ip.ipv4 = (struct ibv_flow_spec_ipv4_ext *)hdr;
864 if (!ip.ipv4->val.proto) {
865 ip.ipv4->val.proto = protocol;
866 ip.ipv4->mask.proto = 0xff;
869 case IBV_FLOW_SPEC_IPV6:
870 ip.ipv6 = (struct ibv_flow_spec_ipv6 *)hdr;
871 if (!ip.ipv6->val.next_hdr) {
872 ip.ipv6->val.next_hdr = protocol;
873 ip.ipv6->mask.next_hdr = 0xff;
881 hdr = (struct ibv_spec_header *)((uint8_t *)hdr + hdr->size);
886 * Convert the @p item into a Verbs specification. This function assumes that
887 * the input is valid and that there is space to insert the requested item
890 * @param[in, out] dev_flow
891 * Pointer to dev_flow structure.
893 * Item specification.
894 * @param[in] item_flags
898 flow_verbs_translate_item_gre(struct mlx5_flow *dev_flow,
899 const struct rte_flow_item *item __rte_unused,
902 struct mlx5_flow_verbs_workspace *verbs = &dev_flow->verbs;
903 #ifndef HAVE_IBV_DEVICE_MPLS_SUPPORT
904 unsigned int size = sizeof(struct ibv_flow_spec_tunnel);
905 struct ibv_flow_spec_tunnel tunnel = {
906 .type = IBV_FLOW_SPEC_VXLAN_TUNNEL,
910 static const struct rte_flow_item_gre empty_gre = {0,};
911 const struct rte_flow_item_gre *spec = item->spec;
912 const struct rte_flow_item_gre *mask = item->mask;
913 unsigned int size = sizeof(struct ibv_flow_spec_gre);
914 struct ibv_flow_spec_gre tunnel = {
915 .type = IBV_FLOW_SPEC_GRE,
924 mask = &rte_flow_item_gre_mask;
926 tunnel.val.c_ks_res0_ver = spec->c_rsvd0_ver;
927 tunnel.val.protocol = spec->protocol;
928 tunnel.mask.c_ks_res0_ver = mask->c_rsvd0_ver;
929 tunnel.mask.protocol = mask->protocol;
930 /* Remove unwanted bits from values. */
931 tunnel.val.c_ks_res0_ver &= tunnel.mask.c_ks_res0_ver;
932 tunnel.val.key &= tunnel.mask.key;
933 if (tunnel.mask.protocol) {
934 tunnel.val.protocol &= tunnel.mask.protocol;
936 tunnel.val.protocol = mlx5_translate_tunnel_etypes(item_flags);
937 if (tunnel.val.protocol) {
938 tunnel.mask.protocol = 0xFFFF;
939 tunnel.val.protocol =
940 rte_cpu_to_be_16(tunnel.val.protocol);
944 if (item_flags & MLX5_FLOW_LAYER_OUTER_L3_IPV4)
945 flow_verbs_item_gre_ip_protocol_update(&verbs->attr,
946 IBV_FLOW_SPEC_IPV4_EXT,
949 flow_verbs_item_gre_ip_protocol_update(&verbs->attr,
952 flow_verbs_spec_add(verbs, &tunnel, size);
956 * Convert the @p action into a Verbs specification. This function assumes that
957 * the input is valid and that there is space to insert the requested action
958 * into the flow. This function also return the action that was added.
960 * @param[in, out] dev_flow
961 * Pointer to dev_flow structure.
963 * Item specification.
964 * @param[in] item_flags
968 flow_verbs_translate_item_mpls(struct mlx5_flow *dev_flow __rte_unused,
969 const struct rte_flow_item *item __rte_unused,
970 uint64_t item_flags __rte_unused)
972 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
973 const struct rte_flow_item_mpls *spec = item->spec;
974 const struct rte_flow_item_mpls *mask = item->mask;
975 unsigned int size = sizeof(struct ibv_flow_spec_mpls);
976 struct ibv_flow_spec_mpls mpls = {
977 .type = IBV_FLOW_SPEC_MPLS,
982 mask = &rte_flow_item_mpls_mask;
984 memcpy(&mpls.val.label, spec, sizeof(mpls.val.label));
985 memcpy(&mpls.mask.label, mask, sizeof(mpls.mask.label));
986 /* Remove unwanted bits from values. */
987 mpls.val.label &= mpls.mask.label;
989 flow_verbs_spec_add(&dev_flow->verbs, &mpls, size);
994 * Convert the @p action into a Verbs specification. This function assumes that
995 * the input is valid and that there is space to insert the requested action
998 * @param[in] dev_flow
999 * Pointer to mlx5_flow.
1001 * Action configuration.
1004 flow_verbs_translate_action_drop
1005 (struct mlx5_flow *dev_flow,
1006 const struct rte_flow_action *action __rte_unused)
1008 unsigned int size = sizeof(struct ibv_flow_spec_action_drop);
1009 struct ibv_flow_spec_action_drop drop = {
1010 .type = IBV_FLOW_SPEC_ACTION_DROP,
1014 flow_verbs_spec_add(&dev_flow->verbs, &drop, size);
1018 * Convert the @p action into a Verbs specification. This function assumes that
1019 * the input is valid and that there is space to insert the requested action
1022 * @param[in] rss_desc
1023 * Pointer to mlx5_flow_rss_desc.
1025 * Action configuration.
1028 flow_verbs_translate_action_queue(struct mlx5_flow_rss_desc *rss_desc,
1029 const struct rte_flow_action *action)
1031 const struct rte_flow_action_queue *queue = action->conf;
1033 rss_desc->queue[0] = queue->index;
1034 rss_desc->queue_num = 1;
1038 * Convert the @p action into a Verbs specification. This function assumes that
1039 * the input is valid and that there is space to insert the requested action
1042 * @param[in] rss_desc
1043 * Pointer to mlx5_flow_rss_desc.
1045 * Action configuration.
1048 flow_verbs_translate_action_rss(struct mlx5_flow_rss_desc *rss_desc,
1049 const struct rte_flow_action *action)
1051 const struct rte_flow_action_rss *rss = action->conf;
1052 const uint8_t *rss_key;
1054 memcpy(rss_desc->queue, rss->queue, rss->queue_num * sizeof(uint16_t));
1055 rss_desc->queue_num = rss->queue_num;
1056 /* NULL RSS key indicates default RSS key. */
1057 rss_key = !rss->key ? rss_hash_default_key : rss->key;
1058 memcpy(rss_desc->key, rss_key, MLX5_RSS_HASH_KEY_LEN);
1060 * rss->level and rss.types should be set in advance when expanding
1066 * Convert the @p action into a Verbs specification. This function assumes that
1067 * the input is valid and that there is space to insert the requested action
1070 * @param[in] dev_flow
1071 * Pointer to mlx5_flow.
1073 * Action configuration.
1076 flow_verbs_translate_action_flag
1077 (struct mlx5_flow *dev_flow,
1078 const struct rte_flow_action *action __rte_unused)
1080 unsigned int size = sizeof(struct ibv_flow_spec_action_tag);
1081 struct ibv_flow_spec_action_tag tag = {
1082 .type = IBV_FLOW_SPEC_ACTION_TAG,
1084 .tag_id = mlx5_flow_mark_set(MLX5_FLOW_MARK_DEFAULT),
1087 flow_verbs_spec_add(&dev_flow->verbs, &tag, size);
1091 * Convert the @p action into a Verbs specification. This function assumes that
1092 * the input is valid and that there is space to insert the requested action
1095 * @param[in] dev_flow
1096 * Pointer to mlx5_flow.
1098 * Action configuration.
1101 flow_verbs_translate_action_mark(struct mlx5_flow *dev_flow,
1102 const struct rte_flow_action *action)
1104 const struct rte_flow_action_mark *mark = action->conf;
1105 unsigned int size = sizeof(struct ibv_flow_spec_action_tag);
1106 struct ibv_flow_spec_action_tag tag = {
1107 .type = IBV_FLOW_SPEC_ACTION_TAG,
1109 .tag_id = mlx5_flow_mark_set(mark->id),
1112 flow_verbs_spec_add(&dev_flow->verbs, &tag, size);
1116 * Convert the @p action into a Verbs specification. This function assumes that
1117 * the input is valid and that there is space to insert the requested action
1121 * Pointer to the Ethernet device structure.
1123 * Action configuration.
1124 * @param[in] dev_flow
1125 * Pointer to mlx5_flow.
1127 * Pointer to error structure.
1130 * 0 On success else a negative errno value is returned and rte_errno is set.
1133 flow_verbs_translate_action_count(struct mlx5_flow *dev_flow,
1134 const struct rte_flow_action *action,
1135 struct rte_eth_dev *dev,
1136 struct rte_flow_error *error)
1138 const struct rte_flow_action_count *count = action->conf;
1139 struct rte_flow *flow = dev_flow->flow;
1140 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42) || \
1141 defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
1142 struct mlx5_flow_counter_pool *pool;
1143 struct mlx5_flow_counter *cnt = NULL;
1144 unsigned int size = sizeof(struct ibv_flow_spec_counter_action);
1145 struct ibv_flow_spec_counter_action counter = {
1146 .type = IBV_FLOW_SPEC_ACTION_COUNT,
1151 if (!flow->counter) {
1152 flow->counter = flow_verbs_counter_new(dev, count->id);
1154 return rte_flow_error_set(error, rte_errno,
1155 RTE_FLOW_ERROR_TYPE_ACTION,
1157 "cannot get counter"
1160 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42)
1161 cnt = flow_verbs_counter_get_by_idx(dev, flow->counter, &pool);
1162 counter.counter_set_handle =
1163 ((struct ibv_counter_set *)cnt->dcs_when_active)->handle;
1164 flow_verbs_spec_add(&dev_flow->verbs, &counter, size);
1165 #elif defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
1166 cnt = flow_verbs_counter_get_by_idx(dev, flow->counter, &pool);
1167 counter.counters = (struct ibv_counters *)cnt->dcs_when_active;
1168 flow_verbs_spec_add(&dev_flow->verbs, &counter, size);
1174 * Internal validation function. For validating both actions and items.
1177 * Pointer to the Ethernet device structure.
1179 * Pointer to the flow attributes.
1181 * Pointer to the list of items.
1182 * @param[in] actions
1183 * Pointer to the list of actions.
1184 * @param[in] external
1185 * This flow rule is created by request external to PMD.
1186 * @param[in] hairpin
1187 * Number of hairpin TX actions, 0 means classic flow.
1189 * Pointer to the error structure.
1192 * 0 on success, a negative errno value otherwise and rte_errno is set.
1195 flow_verbs_validate(struct rte_eth_dev *dev,
1196 const struct rte_flow_attr *attr,
1197 const struct rte_flow_item items[],
1198 const struct rte_flow_action actions[],
1199 bool external __rte_unused,
1200 int hairpin __rte_unused,
1201 struct rte_flow_error *error)
1204 uint64_t action_flags = 0;
1205 uint64_t item_flags = 0;
1206 uint64_t last_item = 0;
1207 uint8_t next_protocol = 0xff;
1208 uint16_t ether_type = 0;
1209 bool is_empty_vlan = false;
1210 uint16_t udp_dport = 0;
1214 ret = mlx5_flow_validate_attributes(dev, attr, error);
1217 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
1218 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1221 switch (items->type) {
1222 case RTE_FLOW_ITEM_TYPE_VOID:
1224 case RTE_FLOW_ITEM_TYPE_ETH:
1225 ret = mlx5_flow_validate_item_eth(items, item_flags,
1229 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
1230 MLX5_FLOW_LAYER_OUTER_L2;
1231 if (items->mask != NULL && items->spec != NULL) {
1233 ((const struct rte_flow_item_eth *)
1236 ((const struct rte_flow_item_eth *)
1238 if (ether_type == RTE_BE16(RTE_ETHER_TYPE_VLAN))
1239 is_empty_vlan = true;
1240 ether_type = rte_be_to_cpu_16(ether_type);
1245 case RTE_FLOW_ITEM_TYPE_VLAN:
1246 ret = mlx5_flow_validate_item_vlan(items, item_flags,
1250 last_item = tunnel ? (MLX5_FLOW_LAYER_INNER_L2 |
1251 MLX5_FLOW_LAYER_INNER_VLAN) :
1252 (MLX5_FLOW_LAYER_OUTER_L2 |
1253 MLX5_FLOW_LAYER_OUTER_VLAN);
1254 if (items->mask != NULL && items->spec != NULL) {
1256 ((const struct rte_flow_item_vlan *)
1257 items->spec)->inner_type;
1259 ((const struct rte_flow_item_vlan *)
1260 items->mask)->inner_type;
1261 ether_type = rte_be_to_cpu_16(ether_type);
1265 is_empty_vlan = false;
1267 case RTE_FLOW_ITEM_TYPE_IPV4:
1268 ret = mlx5_flow_validate_item_ipv4
1270 last_item, ether_type, NULL,
1271 MLX5_ITEM_RANGE_NOT_ACCEPTED,
1275 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
1276 MLX5_FLOW_LAYER_OUTER_L3_IPV4;
1277 if (items->mask != NULL &&
1278 ((const struct rte_flow_item_ipv4 *)
1279 items->mask)->hdr.next_proto_id) {
1281 ((const struct rte_flow_item_ipv4 *)
1282 (items->spec))->hdr.next_proto_id;
1284 ((const struct rte_flow_item_ipv4 *)
1285 (items->mask))->hdr.next_proto_id;
1287 /* Reset for inner layer. */
1288 next_protocol = 0xff;
1291 case RTE_FLOW_ITEM_TYPE_IPV6:
1292 ret = mlx5_flow_validate_item_ipv6(items, item_flags,
1298 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
1299 MLX5_FLOW_LAYER_OUTER_L3_IPV6;
1300 if (items->mask != NULL &&
1301 ((const struct rte_flow_item_ipv6 *)
1302 items->mask)->hdr.proto) {
1304 ((const struct rte_flow_item_ipv6 *)
1305 items->spec)->hdr.proto;
1307 ((const struct rte_flow_item_ipv6 *)
1308 items->mask)->hdr.proto;
1310 /* Reset for inner layer. */
1311 next_protocol = 0xff;
1314 case RTE_FLOW_ITEM_TYPE_UDP:
1315 ret = mlx5_flow_validate_item_udp(items, item_flags,
1318 const struct rte_flow_item_udp *spec = items->spec;
1319 const struct rte_flow_item_udp *mask = items->mask;
1321 mask = &rte_flow_item_udp_mask;
1323 udp_dport = rte_be_to_cpu_16
1324 (spec->hdr.dst_port &
1325 mask->hdr.dst_port);
1329 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
1330 MLX5_FLOW_LAYER_OUTER_L4_UDP;
1332 case RTE_FLOW_ITEM_TYPE_TCP:
1333 ret = mlx5_flow_validate_item_tcp
1336 &rte_flow_item_tcp_mask,
1340 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
1341 MLX5_FLOW_LAYER_OUTER_L4_TCP;
1343 case RTE_FLOW_ITEM_TYPE_VXLAN:
1344 ret = mlx5_flow_validate_item_vxlan(dev, udp_dport,
1349 last_item = MLX5_FLOW_LAYER_VXLAN;
1351 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
1352 ret = mlx5_flow_validate_item_vxlan_gpe(items,
1357 last_item = MLX5_FLOW_LAYER_VXLAN_GPE;
1359 case RTE_FLOW_ITEM_TYPE_GRE:
1360 ret = mlx5_flow_validate_item_gre(items, item_flags,
1361 next_protocol, error);
1364 last_item = MLX5_FLOW_LAYER_GRE;
1366 case RTE_FLOW_ITEM_TYPE_MPLS:
1367 ret = mlx5_flow_validate_item_mpls(dev, items,
1372 last_item = MLX5_FLOW_LAYER_MPLS;
1374 case RTE_FLOW_ITEM_TYPE_ICMP:
1375 case RTE_FLOW_ITEM_TYPE_ICMP6:
1376 return rte_flow_error_set(error, ENOTSUP,
1377 RTE_FLOW_ERROR_TYPE_ITEM,
1379 "item not supported");
1381 return rte_flow_error_set(error, ENOTSUP,
1382 RTE_FLOW_ERROR_TYPE_ITEM,
1383 NULL, "item not supported");
1385 item_flags |= last_item;
1388 return rte_flow_error_set(error, ENOTSUP,
1389 RTE_FLOW_ERROR_TYPE_ITEM, NULL,
1390 "VLAN matching without vid specification is not supported");
1391 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1392 switch (actions->type) {
1393 case RTE_FLOW_ACTION_TYPE_VOID:
1395 case RTE_FLOW_ACTION_TYPE_FLAG:
1396 ret = mlx5_flow_validate_action_flag(action_flags,
1401 action_flags |= MLX5_FLOW_ACTION_FLAG;
1403 case RTE_FLOW_ACTION_TYPE_MARK:
1404 ret = mlx5_flow_validate_action_mark(actions,
1410 action_flags |= MLX5_FLOW_ACTION_MARK;
1412 case RTE_FLOW_ACTION_TYPE_DROP:
1413 ret = mlx5_flow_validate_action_drop(action_flags,
1418 action_flags |= MLX5_FLOW_ACTION_DROP;
1420 case RTE_FLOW_ACTION_TYPE_QUEUE:
1421 ret = mlx5_flow_validate_action_queue(actions,
1427 action_flags |= MLX5_FLOW_ACTION_QUEUE;
1429 case RTE_FLOW_ACTION_TYPE_RSS:
1430 ret = mlx5_flow_validate_action_rss(actions,
1436 action_flags |= MLX5_FLOW_ACTION_RSS;
1438 case RTE_FLOW_ACTION_TYPE_COUNT:
1439 ret = mlx5_flow_validate_action_count(dev, attr, error);
1442 action_flags |= MLX5_FLOW_ACTION_COUNT;
1445 return rte_flow_error_set(error, ENOTSUP,
1446 RTE_FLOW_ERROR_TYPE_ACTION,
1448 "action not supported");
1452 * Validate the drop action mutual exclusion with other actions.
1453 * Drop action is mutually-exclusive with any other action, except for
1456 if ((action_flags & MLX5_FLOW_ACTION_DROP) &&
1457 (action_flags & ~(MLX5_FLOW_ACTION_DROP | MLX5_FLOW_ACTION_COUNT)))
1458 return rte_flow_error_set(error, EINVAL,
1459 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1460 "Drop action is mutually-exclusive "
1461 "with any other action, except for "
1463 if (!(action_flags & MLX5_FLOW_FATE_ACTIONS))
1464 return rte_flow_error_set(error, EINVAL,
1465 RTE_FLOW_ERROR_TYPE_ACTION, actions,
1466 "no fate action is found");
1471 * Calculate the required bytes that are needed for the action part of the verbs
1474 * @param[in] actions
1475 * Pointer to the list of actions.
1478 * The size of the memory needed for all actions.
1481 flow_verbs_get_actions_size(const struct rte_flow_action actions[])
1485 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1486 switch (actions->type) {
1487 case RTE_FLOW_ACTION_TYPE_VOID:
1489 case RTE_FLOW_ACTION_TYPE_FLAG:
1490 size += sizeof(struct ibv_flow_spec_action_tag);
1492 case RTE_FLOW_ACTION_TYPE_MARK:
1493 size += sizeof(struct ibv_flow_spec_action_tag);
1495 case RTE_FLOW_ACTION_TYPE_DROP:
1496 size += sizeof(struct ibv_flow_spec_action_drop);
1498 case RTE_FLOW_ACTION_TYPE_QUEUE:
1500 case RTE_FLOW_ACTION_TYPE_RSS:
1502 case RTE_FLOW_ACTION_TYPE_COUNT:
1503 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42) || \
1504 defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
1505 size += sizeof(struct ibv_flow_spec_counter_action);
1516 * Calculate the required bytes that are needed for the item part of the verbs
1520 * Pointer to the list of items.
1523 * The size of the memory needed for all items.
1526 flow_verbs_get_items_size(const struct rte_flow_item items[])
1530 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
1531 switch (items->type) {
1532 case RTE_FLOW_ITEM_TYPE_VOID:
1534 case RTE_FLOW_ITEM_TYPE_ETH:
1535 size += sizeof(struct ibv_flow_spec_eth);
1537 case RTE_FLOW_ITEM_TYPE_VLAN:
1538 size += sizeof(struct ibv_flow_spec_eth);
1540 case RTE_FLOW_ITEM_TYPE_IPV4:
1541 size += sizeof(struct ibv_flow_spec_ipv4_ext);
1543 case RTE_FLOW_ITEM_TYPE_IPV6:
1544 size += sizeof(struct ibv_flow_spec_ipv6);
1546 case RTE_FLOW_ITEM_TYPE_UDP:
1547 size += sizeof(struct ibv_flow_spec_tcp_udp);
1549 case RTE_FLOW_ITEM_TYPE_TCP:
1550 size += sizeof(struct ibv_flow_spec_tcp_udp);
1552 case RTE_FLOW_ITEM_TYPE_VXLAN:
1553 size += sizeof(struct ibv_flow_spec_tunnel);
1555 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
1556 size += sizeof(struct ibv_flow_spec_tunnel);
1558 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
1559 case RTE_FLOW_ITEM_TYPE_GRE:
1560 size += sizeof(struct ibv_flow_spec_gre);
1562 case RTE_FLOW_ITEM_TYPE_MPLS:
1563 size += sizeof(struct ibv_flow_spec_mpls);
1566 case RTE_FLOW_ITEM_TYPE_GRE:
1567 size += sizeof(struct ibv_flow_spec_tunnel);
1578 * Internal preparation function. Allocate mlx5_flow with the required size.
1579 * The required size is calculate based on the actions and items. This function
1580 * also returns the detected actions and items for later use.
1583 * Pointer to Ethernet device.
1585 * Pointer to the flow attributes.
1587 * Pointer to the list of items.
1588 * @param[in] actions
1589 * Pointer to the list of actions.
1591 * Pointer to the error structure.
1594 * Pointer to mlx5_flow object on success, otherwise NULL and rte_errno
1597 static struct mlx5_flow *
1598 flow_verbs_prepare(struct rte_eth_dev *dev,
1599 const struct rte_flow_attr *attr __rte_unused,
1600 const struct rte_flow_item items[],
1601 const struct rte_flow_action actions[],
1602 struct rte_flow_error *error)
1605 uint32_t handle_idx = 0;
1606 struct mlx5_flow *dev_flow;
1607 struct mlx5_flow_handle *dev_handle;
1608 struct mlx5_priv *priv = dev->data->dev_private;
1609 struct mlx5_flow_workspace *wks = mlx5_flow_get_thread_workspace();
1612 size += flow_verbs_get_actions_size(actions);
1613 size += flow_verbs_get_items_size(items);
1614 if (size > MLX5_VERBS_MAX_SPEC_ACT_SIZE) {
1615 rte_flow_error_set(error, E2BIG,
1616 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1617 "Verbs spec/action size too large");
1620 /* In case of corrupting the memory. */
1621 if (wks->flow_idx >= MLX5_NUM_MAX_DEV_FLOWS) {
1622 rte_flow_error_set(error, ENOSPC,
1623 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1624 "not free temporary device flow");
1627 dev_handle = mlx5_ipool_zmalloc(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
1630 rte_flow_error_set(error, ENOMEM,
1631 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1632 "not enough memory to create flow handle");
1635 MLX5_ASSERT(wks->flow_idx + 1 < RTE_DIM(wks->flows));
1636 dev_flow = &wks->flows[wks->flow_idx++];
1637 dev_flow->handle = dev_handle;
1638 dev_flow->handle_idx = handle_idx;
1639 /* Memcpy is used, only size needs to be cleared to 0. */
1640 dev_flow->verbs.size = 0;
1641 dev_flow->verbs.attr.num_of_specs = 0;
1642 dev_flow->ingress = attr->ingress;
1643 dev_flow->hash_fields = 0;
1644 /* Need to set transfer attribute: not supported in Verbs mode. */
1649 * Fill the flow with verb spec.
1652 * Pointer to Ethernet device.
1653 * @param[in, out] dev_flow
1654 * Pointer to the mlx5 flow.
1656 * Pointer to the flow attributes.
1658 * Pointer to the list of items.
1659 * @param[in] actions
1660 * Pointer to the list of actions.
1662 * Pointer to the error structure.
1665 * 0 on success, else a negative errno value otherwise and rte_errno is set.
1668 flow_verbs_translate(struct rte_eth_dev *dev,
1669 struct mlx5_flow *dev_flow,
1670 const struct rte_flow_attr *attr,
1671 const struct rte_flow_item items[],
1672 const struct rte_flow_action actions[],
1673 struct rte_flow_error *error)
1675 uint64_t item_flags = 0;
1676 uint64_t action_flags = 0;
1677 uint64_t priority = attr->priority;
1678 uint32_t subpriority = 0;
1679 struct mlx5_priv *priv = dev->data->dev_private;
1680 struct mlx5_flow_workspace *wks = mlx5_flow_get_thread_workspace();
1681 struct mlx5_flow_rss_desc *rss_desc;
1684 rss_desc = &wks->rss_desc;
1685 if (priority == MLX5_FLOW_LOWEST_PRIO_INDICATOR)
1686 priority = priv->sh->flow_max_priority - 1;
1687 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1690 switch (actions->type) {
1691 case RTE_FLOW_ACTION_TYPE_VOID:
1693 case RTE_FLOW_ACTION_TYPE_FLAG:
1694 flow_verbs_translate_action_flag(dev_flow, actions);
1695 action_flags |= MLX5_FLOW_ACTION_FLAG;
1698 case RTE_FLOW_ACTION_TYPE_MARK:
1699 flow_verbs_translate_action_mark(dev_flow, actions);
1700 action_flags |= MLX5_FLOW_ACTION_MARK;
1703 case RTE_FLOW_ACTION_TYPE_DROP:
1704 flow_verbs_translate_action_drop(dev_flow, actions);
1705 action_flags |= MLX5_FLOW_ACTION_DROP;
1706 dev_flow->handle->fate_action = MLX5_FLOW_FATE_DROP;
1708 case RTE_FLOW_ACTION_TYPE_QUEUE:
1709 flow_verbs_translate_action_queue(rss_desc, actions);
1710 action_flags |= MLX5_FLOW_ACTION_QUEUE;
1711 dev_flow->handle->fate_action = MLX5_FLOW_FATE_QUEUE;
1713 case RTE_FLOW_ACTION_TYPE_RSS:
1714 flow_verbs_translate_action_rss(rss_desc, actions);
1715 action_flags |= MLX5_FLOW_ACTION_RSS;
1716 dev_flow->handle->fate_action = MLX5_FLOW_FATE_QUEUE;
1718 case RTE_FLOW_ACTION_TYPE_COUNT:
1719 ret = flow_verbs_translate_action_count(dev_flow,
1724 action_flags |= MLX5_FLOW_ACTION_COUNT;
1727 return rte_flow_error_set(error, ENOTSUP,
1728 RTE_FLOW_ERROR_TYPE_ACTION,
1730 "action not supported");
1733 dev_flow->act_flags = action_flags;
1734 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
1735 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1737 switch (items->type) {
1738 case RTE_FLOW_ITEM_TYPE_VOID:
1740 case RTE_FLOW_ITEM_TYPE_ETH:
1741 flow_verbs_translate_item_eth(dev_flow, items,
1743 subpriority = MLX5_PRIORITY_MAP_L2;
1744 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
1745 MLX5_FLOW_LAYER_OUTER_L2;
1747 case RTE_FLOW_ITEM_TYPE_VLAN:
1748 flow_verbs_translate_item_vlan(dev_flow, items,
1750 subpriority = MLX5_PRIORITY_MAP_L2;
1751 item_flags |= tunnel ? (MLX5_FLOW_LAYER_INNER_L2 |
1752 MLX5_FLOW_LAYER_INNER_VLAN) :
1753 (MLX5_FLOW_LAYER_OUTER_L2 |
1754 MLX5_FLOW_LAYER_OUTER_VLAN);
1756 case RTE_FLOW_ITEM_TYPE_IPV4:
1757 flow_verbs_translate_item_ipv4(dev_flow, items,
1759 subpriority = MLX5_PRIORITY_MAP_L3;
1760 dev_flow->hash_fields |=
1761 mlx5_flow_hashfields_adjust
1763 MLX5_IPV4_LAYER_TYPES,
1764 MLX5_IPV4_IBV_RX_HASH);
1765 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
1766 MLX5_FLOW_LAYER_OUTER_L3_IPV4;
1768 case RTE_FLOW_ITEM_TYPE_IPV6:
1769 flow_verbs_translate_item_ipv6(dev_flow, items,
1771 subpriority = MLX5_PRIORITY_MAP_L3;
1772 dev_flow->hash_fields |=
1773 mlx5_flow_hashfields_adjust
1775 MLX5_IPV6_LAYER_TYPES,
1776 MLX5_IPV6_IBV_RX_HASH);
1777 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
1778 MLX5_FLOW_LAYER_OUTER_L3_IPV6;
1780 case RTE_FLOW_ITEM_TYPE_TCP:
1781 flow_verbs_translate_item_tcp(dev_flow, items,
1783 subpriority = MLX5_PRIORITY_MAP_L4;
1784 if (dev_flow->hash_fields != 0)
1785 dev_flow->hash_fields |=
1786 mlx5_flow_hashfields_adjust
1787 (rss_desc, tunnel, RTE_ETH_RSS_TCP,
1788 (IBV_RX_HASH_SRC_PORT_TCP |
1789 IBV_RX_HASH_DST_PORT_TCP));
1790 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
1791 MLX5_FLOW_LAYER_OUTER_L4_TCP;
1793 case RTE_FLOW_ITEM_TYPE_UDP:
1794 flow_verbs_translate_item_udp(dev_flow, items,
1796 subpriority = MLX5_PRIORITY_MAP_L4;
1797 if (dev_flow->hash_fields != 0)
1798 dev_flow->hash_fields |=
1799 mlx5_flow_hashfields_adjust
1800 (rss_desc, tunnel, RTE_ETH_RSS_UDP,
1801 (IBV_RX_HASH_SRC_PORT_UDP |
1802 IBV_RX_HASH_DST_PORT_UDP));
1803 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
1804 MLX5_FLOW_LAYER_OUTER_L4_UDP;
1806 case RTE_FLOW_ITEM_TYPE_VXLAN:
1807 flow_verbs_translate_item_vxlan(dev_flow, items,
1809 subpriority = MLX5_TUNNEL_PRIO_GET(rss_desc);
1810 item_flags |= MLX5_FLOW_LAYER_VXLAN;
1812 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
1813 flow_verbs_translate_item_vxlan_gpe(dev_flow, items,
1815 subpriority = MLX5_TUNNEL_PRIO_GET(rss_desc);
1816 item_flags |= MLX5_FLOW_LAYER_VXLAN_GPE;
1818 case RTE_FLOW_ITEM_TYPE_GRE:
1819 subpriority = MLX5_TUNNEL_PRIO_GET(rss_desc);
1820 item_flags |= MLX5_FLOW_LAYER_GRE;
1822 case RTE_FLOW_ITEM_TYPE_MPLS:
1823 flow_verbs_translate_item_mpls(dev_flow, items,
1825 subpriority = MLX5_TUNNEL_PRIO_GET(rss_desc);
1826 item_flags |= MLX5_FLOW_LAYER_MPLS;
1829 return rte_flow_error_set(error, ENOTSUP,
1830 RTE_FLOW_ERROR_TYPE_ITEM,
1831 NULL, "item not supported");
1834 if (item_flags & MLX5_FLOW_LAYER_GRE)
1835 flow_verbs_translate_item_gre(dev_flow, items, item_flags);
1836 dev_flow->handle->layers = item_flags;
1837 /* Other members of attr will be ignored. */
1838 dev_flow->verbs.attr.priority =
1839 mlx5_flow_adjust_priority(dev, priority, subpriority);
1840 dev_flow->verbs.attr.port = (uint8_t)priv->dev_port;
1845 * Remove the flow from the NIC but keeps it in memory.
1848 * Pointer to the Ethernet device structure.
1849 * @param[in, out] flow
1850 * Pointer to flow structure.
1853 flow_verbs_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
1855 struct mlx5_priv *priv = dev->data->dev_private;
1856 struct mlx5_flow_handle *handle;
1857 uint32_t handle_idx;
1861 SILIST_FOREACH(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW], flow->dev_handles,
1862 handle_idx, handle, next) {
1863 if (handle->drv_flow) {
1864 claim_zero(mlx5_glue->destroy_flow(handle->drv_flow));
1865 handle->drv_flow = NULL;
1867 /* hrxq is union, don't touch it only the flag is set. */
1868 if (handle->rix_hrxq &&
1869 handle->fate_action == MLX5_FLOW_FATE_QUEUE) {
1870 mlx5_hrxq_release(dev, handle->rix_hrxq);
1871 handle->rix_hrxq = 0;
1873 if (handle->vf_vlan.tag && handle->vf_vlan.created)
1874 mlx5_vlan_vmwa_release(dev, &handle->vf_vlan);
1879 * Remove the flow from the NIC and the memory.
1882 * Pointer to the Ethernet device structure.
1883 * @param[in, out] flow
1884 * Pointer to flow structure.
1887 flow_verbs_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
1889 struct mlx5_priv *priv = dev->data->dev_private;
1890 struct mlx5_flow_handle *handle;
1894 flow_verbs_remove(dev, flow);
1895 while (flow->dev_handles) {
1896 uint32_t tmp_idx = flow->dev_handles;
1898 handle = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
1902 flow->dev_handles = handle->next.next;
1903 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
1906 if (flow->counter) {
1907 flow_verbs_counter_release(dev, flow->counter);
1913 * Apply the flow to the NIC.
1916 * Pointer to the Ethernet device structure.
1917 * @param[in, out] flow
1918 * Pointer to flow structure.
1920 * Pointer to error structure.
1923 * 0 on success, a negative errno value otherwise and rte_errno is set.
1926 flow_verbs_apply(struct rte_eth_dev *dev, struct rte_flow *flow,
1927 struct rte_flow_error *error)
1929 struct mlx5_priv *priv = dev->data->dev_private;
1930 struct mlx5_flow_handle *handle;
1931 struct mlx5_flow *dev_flow;
1932 struct mlx5_hrxq *hrxq;
1933 uint32_t dev_handles;
1936 struct mlx5_flow_workspace *wks = mlx5_flow_get_thread_workspace();
1939 for (idx = wks->flow_idx - 1; idx >= 0; idx--) {
1940 dev_flow = &wks->flows[idx];
1941 handle = dev_flow->handle;
1942 if (handle->fate_action == MLX5_FLOW_FATE_DROP) {
1943 MLX5_ASSERT(priv->drop_queue.hrxq);
1944 hrxq = priv->drop_queue.hrxq;
1947 struct mlx5_flow_rss_desc *rss_desc = &wks->rss_desc;
1949 MLX5_ASSERT(rss_desc->queue_num);
1950 rss_desc->key_len = MLX5_RSS_HASH_KEY_LEN;
1951 rss_desc->hash_fields = dev_flow->hash_fields;
1952 rss_desc->tunnel = !!(handle->layers &
1953 MLX5_FLOW_LAYER_TUNNEL);
1954 rss_desc->shared_rss = 0;
1955 hrxq_idx = mlx5_hrxq_get(dev, rss_desc);
1956 hrxq = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_HRXQ],
1961 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1962 "cannot get hash queue");
1965 handle->rix_hrxq = hrxq_idx;
1968 handle->drv_flow = mlx5_glue->create_flow
1969 (hrxq->qp, &dev_flow->verbs.attr);
1970 if (!handle->drv_flow) {
1971 rte_flow_error_set(error, errno,
1972 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1974 "hardware refuses to create flow");
1977 if (priv->vmwa_context &&
1978 handle->vf_vlan.tag && !handle->vf_vlan.created) {
1980 * The rule contains the VLAN pattern.
1981 * For VF we are going to create VLAN
1982 * interface to make hypervisor set correct
1983 * e-Switch vport context.
1985 mlx5_vlan_vmwa_acquire(dev, &handle->vf_vlan);
1990 err = rte_errno; /* Save rte_errno before cleanup. */
1991 SILIST_FOREACH(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW], flow->dev_handles,
1992 dev_handles, handle, next) {
1993 /* hrxq is union, don't touch it only the flag is set. */
1994 if (handle->rix_hrxq &&
1995 handle->fate_action == MLX5_FLOW_FATE_QUEUE) {
1996 mlx5_hrxq_release(dev, handle->rix_hrxq);
1997 handle->rix_hrxq = 0;
1999 if (handle->vf_vlan.tag && handle->vf_vlan.created)
2000 mlx5_vlan_vmwa_release(dev, &handle->vf_vlan);
2002 rte_errno = err; /* Restore rte_errno. */
2009 * @see rte_flow_query()
2013 flow_verbs_query(struct rte_eth_dev *dev,
2014 struct rte_flow *flow,
2015 const struct rte_flow_action *actions,
2017 struct rte_flow_error *error)
2021 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
2022 switch (actions->type) {
2023 case RTE_FLOW_ACTION_TYPE_VOID:
2025 case RTE_FLOW_ACTION_TYPE_COUNT:
2026 ret = flow_verbs_counter_query(dev, flow, data, error);
2029 return rte_flow_error_set(error, ENOTSUP,
2030 RTE_FLOW_ERROR_TYPE_ACTION,
2032 "action not supported");
2039 flow_verbs_sync_domain(struct rte_eth_dev *dev, uint32_t domains,
2043 RTE_SET_USED(domains);
2044 RTE_SET_USED(flags);
2049 const struct mlx5_flow_driver_ops mlx5_flow_verbs_drv_ops = {
2050 .validate = flow_verbs_validate,
2051 .prepare = flow_verbs_prepare,
2052 .translate = flow_verbs_translate,
2053 .apply = flow_verbs_apply,
2054 .remove = flow_verbs_remove,
2055 .destroy = flow_verbs_destroy,
2056 .query = flow_verbs_query,
2057 .sync_domain = flow_verbs_sync_domain,
2058 .discover_priorities = flow_verbs_discover_priorities,