1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2018 Mellanox Technologies, Ltd
5 #include <netinet/in.h>
12 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
14 #pragma GCC diagnostic ignored "-Wpedantic"
16 #include <infiniband/verbs.h>
18 #pragma GCC diagnostic error "-Wpedantic"
21 #include <rte_common.h>
22 #include <rte_ether.h>
23 #include <rte_ethdev_driver.h>
25 #include <rte_flow_driver.h>
26 #include <rte_malloc.h>
29 #include <mlx5_glue.h>
32 #include "mlx5_defs.h"
34 #include "mlx5_flow.h"
35 #include "mlx5_rxtx.h"
37 #define VERBS_SPEC_INNER(item_flags) \
38 (!!((item_flags) & MLX5_FLOW_LAYER_TUNNEL) ? IBV_FLOW_SPEC_INNER : 0)
41 * Create Verbs flow counter with Verbs library.
44 * Pointer to the Ethernet device structure.
45 * @param[in, out] counter
46 * mlx5 flow counter object, contains the counter id,
47 * handle of created Verbs flow counter is returned
48 * in cs field (if counters are supported).
51 * 0 On success else a negative errno value is returned
52 * and rte_errno is set.
55 flow_verbs_counter_create(struct rte_eth_dev *dev,
56 struct mlx5_flow_counter *counter)
58 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42)
59 struct mlx5_priv *priv = dev->data->dev_private;
60 struct ibv_context *ctx = priv->sh->ctx;
61 struct ibv_counter_set_init_attr init = {
62 .counter_set_id = counter->id};
64 counter->cs = mlx5_glue->create_counter_set(ctx, &init);
70 #elif defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
71 struct mlx5_priv *priv = dev->data->dev_private;
72 struct ibv_context *ctx = priv->sh->ctx;
73 struct ibv_counters_init_attr init = {0};
74 struct ibv_counter_attach_attr attach;
77 memset(&attach, 0, sizeof(attach));
78 counter->cs = mlx5_glue->create_counters(ctx, &init);
83 attach.counter_desc = IBV_COUNTER_PACKETS;
85 ret = mlx5_glue->attach_counters(counter->cs, &attach, NULL);
87 attach.counter_desc = IBV_COUNTER_BYTES;
89 ret = mlx5_glue->attach_counters
90 (counter->cs, &attach, NULL);
93 claim_zero(mlx5_glue->destroy_counters(counter->cs));
108 * Get a flow counter.
111 * Pointer to the Ethernet device structure.
113 * Indicate if this counter is shared with other flows.
115 * Counter identifier.
118 * A pointer to the counter, NULL otherwise and rte_errno is set.
120 static struct mlx5_flow_counter *
121 flow_verbs_counter_new(struct rte_eth_dev *dev, uint32_t shared, uint32_t id)
123 struct mlx5_priv *priv = dev->data->dev_private;
124 struct mlx5_flow_counter *cnt;
128 TAILQ_FOREACH(cnt, &priv->sh->cmng.flow_counters, next) {
129 if (cnt->shared && cnt->id == id) {
135 cnt = rte_calloc(__func__, 1, sizeof(*cnt), 0);
141 cnt->shared = shared;
145 /* Create counter with Verbs. */
146 ret = flow_verbs_counter_create(dev, cnt);
148 TAILQ_INSERT_HEAD(&priv->sh->cmng.flow_counters, cnt, next);
151 /* Some error occurred in Verbs library. */
158 * Release a flow counter.
161 * Pointer to the Ethernet device structure.
163 * Pointer to the counter handler.
166 flow_verbs_counter_release(struct rte_eth_dev *dev,
167 struct mlx5_flow_counter *counter)
169 struct mlx5_priv *priv = dev->data->dev_private;
171 if (--counter->ref_cnt == 0) {
172 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42)
173 claim_zero(mlx5_glue->destroy_counter_set(counter->cs));
174 #elif defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
175 claim_zero(mlx5_glue->destroy_counters(counter->cs));
177 TAILQ_REMOVE(&priv->sh->cmng.flow_counters, counter, next);
183 * Query a flow counter via Verbs library call.
185 * @see rte_flow_query()
189 flow_verbs_counter_query(struct rte_eth_dev *dev __rte_unused,
190 struct rte_flow *flow, void *data,
191 struct rte_flow_error *error)
193 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42) || \
194 defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
195 if (flow->counter && flow->counter->cs) {
196 struct rte_flow_query_count *qc = data;
197 uint64_t counters[2] = {0, 0};
198 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42)
199 struct ibv_query_counter_set_attr query_cs_attr = {
200 .cs = flow->counter->cs,
201 .query_flags = IBV_COUNTER_SET_FORCE_UPDATE,
203 struct ibv_counter_set_data query_out = {
205 .outlen = 2 * sizeof(uint64_t),
207 int err = mlx5_glue->query_counter_set(&query_cs_attr,
209 #elif defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
210 int err = mlx5_glue->query_counters
211 (flow->counter->cs, counters,
213 IBV_READ_COUNTERS_ATTR_PREFER_CACHED);
216 return rte_flow_error_set
218 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
220 "cannot read counter");
223 qc->hits = counters[0] - flow->counter->hits;
224 qc->bytes = counters[1] - flow->counter->bytes;
226 flow->counter->hits = counters[0];
227 flow->counter->bytes = counters[1];
231 return rte_flow_error_set(error, EINVAL,
232 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
234 "flow does not have counter");
238 return rte_flow_error_set(error, ENOTSUP,
239 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
241 "counters are not available");
246 * Add a verbs item specification into @p verbs.
249 * Pointer to verbs structure.
251 * Create specification.
253 * Size in bytes of the specification to copy.
256 flow_verbs_spec_add(struct mlx5_flow_verbs_workspace *verbs,
257 void *src, unsigned int size)
263 MLX5_ASSERT(verbs->specs);
264 dst = (void *)(verbs->specs + verbs->size);
265 memcpy(dst, src, size);
266 ++verbs->attr.num_of_specs;
271 * Convert the @p item into a Verbs specification. This function assumes that
272 * the input is valid and that there is space to insert the requested item
275 * @param[in, out] dev_flow
276 * Pointer to dev_flow structure.
278 * Item specification.
279 * @param[in] item_flags
283 flow_verbs_translate_item_eth(struct mlx5_flow *dev_flow,
284 const struct rte_flow_item *item,
287 const struct rte_flow_item_eth *spec = item->spec;
288 const struct rte_flow_item_eth *mask = item->mask;
289 const unsigned int size = sizeof(struct ibv_flow_spec_eth);
290 struct ibv_flow_spec_eth eth = {
291 .type = IBV_FLOW_SPEC_ETH | VERBS_SPEC_INNER(item_flags),
296 mask = &rte_flow_item_eth_mask;
300 memcpy(ð.val.dst_mac, spec->dst.addr_bytes,
302 memcpy(ð.val.src_mac, spec->src.addr_bytes,
304 eth.val.ether_type = spec->type;
305 memcpy(ð.mask.dst_mac, mask->dst.addr_bytes,
307 memcpy(ð.mask.src_mac, mask->src.addr_bytes,
309 eth.mask.ether_type = mask->type;
310 /* Remove unwanted bits from values. */
311 for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i) {
312 eth.val.dst_mac[i] &= eth.mask.dst_mac[i];
313 eth.val.src_mac[i] &= eth.mask.src_mac[i];
315 eth.val.ether_type &= eth.mask.ether_type;
317 flow_verbs_spec_add(&dev_flow->verbs, ð, size);
321 * Update the VLAN tag in the Verbs Ethernet specification.
322 * This function assumes that the input is valid and there is space to add
323 * the requested item.
325 * @param[in, out] attr
326 * Pointer to Verbs attributes structure.
328 * Verbs structure containing the VLAN information to copy.
331 flow_verbs_item_vlan_update(struct ibv_flow_attr *attr,
332 struct ibv_flow_spec_eth *eth)
335 const enum ibv_flow_spec_type search = eth->type;
336 struct ibv_spec_header *hdr = (struct ibv_spec_header *)
337 ((uint8_t *)attr + sizeof(struct ibv_flow_attr));
339 for (i = 0; i != attr->num_of_specs; ++i) {
340 if (hdr->type == search) {
341 struct ibv_flow_spec_eth *e =
342 (struct ibv_flow_spec_eth *)hdr;
344 e->val.vlan_tag = eth->val.vlan_tag;
345 e->mask.vlan_tag = eth->mask.vlan_tag;
346 e->val.ether_type = eth->val.ether_type;
347 e->mask.ether_type = eth->mask.ether_type;
350 hdr = (struct ibv_spec_header *)((uint8_t *)hdr + hdr->size);
355 * Convert the @p item into a Verbs specification. This function assumes that
356 * the input is valid and that there is space to insert the requested item
359 * @param[in, out] dev_flow
360 * Pointer to dev_flow structure.
362 * Item specification.
363 * @param[in] item_flags
367 flow_verbs_translate_item_vlan(struct mlx5_flow *dev_flow,
368 const struct rte_flow_item *item,
371 const struct rte_flow_item_vlan *spec = item->spec;
372 const struct rte_flow_item_vlan *mask = item->mask;
373 unsigned int size = sizeof(struct ibv_flow_spec_eth);
374 const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
375 struct ibv_flow_spec_eth eth = {
376 .type = IBV_FLOW_SPEC_ETH | VERBS_SPEC_INNER(item_flags),
379 const uint32_t l2m = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
380 MLX5_FLOW_LAYER_OUTER_L2;
383 mask = &rte_flow_item_vlan_mask;
385 eth.val.vlan_tag = spec->tci;
386 eth.mask.vlan_tag = mask->tci;
387 eth.val.vlan_tag &= eth.mask.vlan_tag;
388 eth.val.ether_type = spec->inner_type;
389 eth.mask.ether_type = mask->inner_type;
390 eth.val.ether_type &= eth.mask.ether_type;
392 if (!(item_flags & l2m))
393 flow_verbs_spec_add(&dev_flow->verbs, ð, size);
395 flow_verbs_item_vlan_update(&dev_flow->verbs.attr, ð);
397 dev_flow->handle->vf_vlan.tag =
398 rte_be_to_cpu_16(spec->tci) & 0x0fff;
402 * Convert the @p item into a Verbs specification. This function assumes that
403 * the input is valid and that there is space to insert the requested item
406 * @param[in, out] dev_flow
407 * Pointer to dev_flow structure.
409 * Item specification.
410 * @param[in] item_flags
414 flow_verbs_translate_item_ipv4(struct mlx5_flow *dev_flow,
415 const struct rte_flow_item *item,
418 const struct rte_flow_item_ipv4 *spec = item->spec;
419 const struct rte_flow_item_ipv4 *mask = item->mask;
420 unsigned int size = sizeof(struct ibv_flow_spec_ipv4_ext);
421 struct ibv_flow_spec_ipv4_ext ipv4 = {
422 .type = IBV_FLOW_SPEC_IPV4_EXT | VERBS_SPEC_INNER(item_flags),
427 mask = &rte_flow_item_ipv4_mask;
429 ipv4.val = (struct ibv_flow_ipv4_ext_filter){
430 .src_ip = spec->hdr.src_addr,
431 .dst_ip = spec->hdr.dst_addr,
432 .proto = spec->hdr.next_proto_id,
433 .tos = spec->hdr.type_of_service,
435 ipv4.mask = (struct ibv_flow_ipv4_ext_filter){
436 .src_ip = mask->hdr.src_addr,
437 .dst_ip = mask->hdr.dst_addr,
438 .proto = mask->hdr.next_proto_id,
439 .tos = mask->hdr.type_of_service,
441 /* Remove unwanted bits from values. */
442 ipv4.val.src_ip &= ipv4.mask.src_ip;
443 ipv4.val.dst_ip &= ipv4.mask.dst_ip;
444 ipv4.val.proto &= ipv4.mask.proto;
445 ipv4.val.tos &= ipv4.mask.tos;
447 flow_verbs_spec_add(&dev_flow->verbs, &ipv4, size);
451 * Convert the @p item into a Verbs specification. This function assumes that
452 * the input is valid and that there is space to insert the requested item
455 * @param[in, out] dev_flow
456 * Pointer to dev_flow structure.
458 * Item specification.
459 * @param[in] item_flags
463 flow_verbs_translate_item_ipv6(struct mlx5_flow *dev_flow,
464 const struct rte_flow_item *item,
467 const struct rte_flow_item_ipv6 *spec = item->spec;
468 const struct rte_flow_item_ipv6 *mask = item->mask;
469 unsigned int size = sizeof(struct ibv_flow_spec_ipv6);
470 struct ibv_flow_spec_ipv6 ipv6 = {
471 .type = IBV_FLOW_SPEC_IPV6 | VERBS_SPEC_INNER(item_flags),
476 mask = &rte_flow_item_ipv6_mask;
479 uint32_t vtc_flow_val;
480 uint32_t vtc_flow_mask;
482 memcpy(&ipv6.val.src_ip, spec->hdr.src_addr,
483 RTE_DIM(ipv6.val.src_ip));
484 memcpy(&ipv6.val.dst_ip, spec->hdr.dst_addr,
485 RTE_DIM(ipv6.val.dst_ip));
486 memcpy(&ipv6.mask.src_ip, mask->hdr.src_addr,
487 RTE_DIM(ipv6.mask.src_ip));
488 memcpy(&ipv6.mask.dst_ip, mask->hdr.dst_addr,
489 RTE_DIM(ipv6.mask.dst_ip));
490 vtc_flow_val = rte_be_to_cpu_32(spec->hdr.vtc_flow);
491 vtc_flow_mask = rte_be_to_cpu_32(mask->hdr.vtc_flow);
492 ipv6.val.flow_label =
493 rte_cpu_to_be_32((vtc_flow_val & RTE_IPV6_HDR_FL_MASK) >>
494 RTE_IPV6_HDR_FL_SHIFT);
495 ipv6.val.traffic_class = (vtc_flow_val & RTE_IPV6_HDR_TC_MASK) >>
496 RTE_IPV6_HDR_TC_SHIFT;
497 ipv6.val.next_hdr = spec->hdr.proto;
498 ipv6.mask.flow_label =
499 rte_cpu_to_be_32((vtc_flow_mask & RTE_IPV6_HDR_FL_MASK) >>
500 RTE_IPV6_HDR_FL_SHIFT);
501 ipv6.mask.traffic_class = (vtc_flow_mask & RTE_IPV6_HDR_TC_MASK) >>
502 RTE_IPV6_HDR_TC_SHIFT;
503 ipv6.mask.next_hdr = mask->hdr.proto;
504 /* Remove unwanted bits from values. */
505 for (i = 0; i < RTE_DIM(ipv6.val.src_ip); ++i) {
506 ipv6.val.src_ip[i] &= ipv6.mask.src_ip[i];
507 ipv6.val.dst_ip[i] &= ipv6.mask.dst_ip[i];
509 ipv6.val.flow_label &= ipv6.mask.flow_label;
510 ipv6.val.traffic_class &= ipv6.mask.traffic_class;
511 ipv6.val.next_hdr &= ipv6.mask.next_hdr;
513 flow_verbs_spec_add(&dev_flow->verbs, &ipv6, size);
517 * Convert the @p item into a Verbs specification. This function assumes that
518 * the input is valid and that there is space to insert the requested item
521 * @param[in, out] dev_flow
522 * Pointer to dev_flow structure.
524 * Item specification.
525 * @param[in] item_flags
529 flow_verbs_translate_item_tcp(struct mlx5_flow *dev_flow,
530 const struct rte_flow_item *item,
531 uint64_t item_flags __rte_unused)
533 const struct rte_flow_item_tcp *spec = item->spec;
534 const struct rte_flow_item_tcp *mask = item->mask;
535 unsigned int size = sizeof(struct ibv_flow_spec_tcp_udp);
536 struct ibv_flow_spec_tcp_udp tcp = {
537 .type = IBV_FLOW_SPEC_TCP | VERBS_SPEC_INNER(item_flags),
542 mask = &rte_flow_item_tcp_mask;
544 tcp.val.dst_port = spec->hdr.dst_port;
545 tcp.val.src_port = spec->hdr.src_port;
546 tcp.mask.dst_port = mask->hdr.dst_port;
547 tcp.mask.src_port = mask->hdr.src_port;
548 /* Remove unwanted bits from values. */
549 tcp.val.src_port &= tcp.mask.src_port;
550 tcp.val.dst_port &= tcp.mask.dst_port;
552 flow_verbs_spec_add(&dev_flow->verbs, &tcp, size);
556 * Convert the @p item into a Verbs specification. This function assumes that
557 * the input is valid and that there is space to insert the requested item
560 * @param[in, out] dev_flow
561 * Pointer to dev_flow structure.
563 * Item specification.
564 * @param[in] item_flags
568 flow_verbs_translate_item_udp(struct mlx5_flow *dev_flow,
569 const struct rte_flow_item *item,
570 uint64_t item_flags __rte_unused)
572 const struct rte_flow_item_udp *spec = item->spec;
573 const struct rte_flow_item_udp *mask = item->mask;
574 unsigned int size = sizeof(struct ibv_flow_spec_tcp_udp);
575 struct ibv_flow_spec_tcp_udp udp = {
576 .type = IBV_FLOW_SPEC_UDP | VERBS_SPEC_INNER(item_flags),
581 mask = &rte_flow_item_udp_mask;
583 udp.val.dst_port = spec->hdr.dst_port;
584 udp.val.src_port = spec->hdr.src_port;
585 udp.mask.dst_port = mask->hdr.dst_port;
586 udp.mask.src_port = mask->hdr.src_port;
587 /* Remove unwanted bits from values. */
588 udp.val.src_port &= udp.mask.src_port;
589 udp.val.dst_port &= udp.mask.dst_port;
591 flow_verbs_spec_add(&dev_flow->verbs, &udp, size);
595 * Convert the @p item into a Verbs specification. This function assumes that
596 * the input is valid and that there is space to insert the requested item
599 * @param[in, out] dev_flow
600 * Pointer to dev_flow structure.
602 * Item specification.
603 * @param[in] item_flags
607 flow_verbs_translate_item_vxlan(struct mlx5_flow *dev_flow,
608 const struct rte_flow_item *item,
609 uint64_t item_flags __rte_unused)
611 const struct rte_flow_item_vxlan *spec = item->spec;
612 const struct rte_flow_item_vxlan *mask = item->mask;
613 unsigned int size = sizeof(struct ibv_flow_spec_tunnel);
614 struct ibv_flow_spec_tunnel vxlan = {
615 .type = IBV_FLOW_SPEC_VXLAN_TUNNEL,
621 } id = { .vlan_id = 0, };
624 mask = &rte_flow_item_vxlan_mask;
626 memcpy(&id.vni[1], spec->vni, 3);
627 vxlan.val.tunnel_id = id.vlan_id;
628 memcpy(&id.vni[1], mask->vni, 3);
629 vxlan.mask.tunnel_id = id.vlan_id;
630 /* Remove unwanted bits from values. */
631 vxlan.val.tunnel_id &= vxlan.mask.tunnel_id;
633 flow_verbs_spec_add(&dev_flow->verbs, &vxlan, size);
637 * Convert the @p item into a Verbs specification. This function assumes that
638 * the input is valid and that there is space to insert the requested item
641 * @param[in, out] dev_flow
642 * Pointer to dev_flow structure.
644 * Item specification.
645 * @param[in] item_flags
649 flow_verbs_translate_item_vxlan_gpe(struct mlx5_flow *dev_flow,
650 const struct rte_flow_item *item,
651 uint64_t item_flags __rte_unused)
653 const struct rte_flow_item_vxlan_gpe *spec = item->spec;
654 const struct rte_flow_item_vxlan_gpe *mask = item->mask;
655 unsigned int size = sizeof(struct ibv_flow_spec_tunnel);
656 struct ibv_flow_spec_tunnel vxlan_gpe = {
657 .type = IBV_FLOW_SPEC_VXLAN_TUNNEL,
663 } id = { .vlan_id = 0, };
666 mask = &rte_flow_item_vxlan_gpe_mask;
668 memcpy(&id.vni[1], spec->vni, 3);
669 vxlan_gpe.val.tunnel_id = id.vlan_id;
670 memcpy(&id.vni[1], mask->vni, 3);
671 vxlan_gpe.mask.tunnel_id = id.vlan_id;
672 /* Remove unwanted bits from values. */
673 vxlan_gpe.val.tunnel_id &= vxlan_gpe.mask.tunnel_id;
675 flow_verbs_spec_add(&dev_flow->verbs, &vxlan_gpe, size);
679 * Update the protocol in Verbs IPv4/IPv6 spec.
681 * @param[in, out] attr
682 * Pointer to Verbs attributes structure.
684 * Specification type to search in order to update the IP protocol.
685 * @param[in] protocol
686 * Protocol value to set if none is present in the specification.
689 flow_verbs_item_gre_ip_protocol_update(struct ibv_flow_attr *attr,
690 enum ibv_flow_spec_type search,
694 struct ibv_spec_header *hdr = (struct ibv_spec_header *)
695 ((uint8_t *)attr + sizeof(struct ibv_flow_attr));
699 for (i = 0; i != attr->num_of_specs; ++i) {
700 if (hdr->type == search) {
702 struct ibv_flow_spec_ipv4_ext *ipv4;
703 struct ibv_flow_spec_ipv6 *ipv6;
707 case IBV_FLOW_SPEC_IPV4_EXT:
708 ip.ipv4 = (struct ibv_flow_spec_ipv4_ext *)hdr;
709 if (!ip.ipv4->val.proto) {
710 ip.ipv4->val.proto = protocol;
711 ip.ipv4->mask.proto = 0xff;
714 case IBV_FLOW_SPEC_IPV6:
715 ip.ipv6 = (struct ibv_flow_spec_ipv6 *)hdr;
716 if (!ip.ipv6->val.next_hdr) {
717 ip.ipv6->val.next_hdr = protocol;
718 ip.ipv6->mask.next_hdr = 0xff;
726 hdr = (struct ibv_spec_header *)((uint8_t *)hdr + hdr->size);
731 * Convert the @p item into a Verbs specification. This function assumes that
732 * the input is valid and that there is space to insert the requested item
735 * @param[in, out] dev_flow
736 * Pointer to dev_flow structure.
738 * Item specification.
739 * @param[in] item_flags
743 flow_verbs_translate_item_gre(struct mlx5_flow *dev_flow,
744 const struct rte_flow_item *item __rte_unused,
747 struct mlx5_flow_verbs_workspace *verbs = &dev_flow->verbs;
748 #ifndef HAVE_IBV_DEVICE_MPLS_SUPPORT
749 unsigned int size = sizeof(struct ibv_flow_spec_tunnel);
750 struct ibv_flow_spec_tunnel tunnel = {
751 .type = IBV_FLOW_SPEC_VXLAN_TUNNEL,
755 const struct rte_flow_item_gre *spec = item->spec;
756 const struct rte_flow_item_gre *mask = item->mask;
757 unsigned int size = sizeof(struct ibv_flow_spec_gre);
758 struct ibv_flow_spec_gre tunnel = {
759 .type = IBV_FLOW_SPEC_GRE,
764 mask = &rte_flow_item_gre_mask;
766 tunnel.val.c_ks_res0_ver = spec->c_rsvd0_ver;
767 tunnel.val.protocol = spec->protocol;
768 tunnel.mask.c_ks_res0_ver = mask->c_rsvd0_ver;
769 tunnel.mask.protocol = mask->protocol;
770 /* Remove unwanted bits from values. */
771 tunnel.val.c_ks_res0_ver &= tunnel.mask.c_ks_res0_ver;
772 tunnel.val.protocol &= tunnel.mask.protocol;
773 tunnel.val.key &= tunnel.mask.key;
776 if (item_flags & MLX5_FLOW_LAYER_OUTER_L3_IPV4)
777 flow_verbs_item_gre_ip_protocol_update(&verbs->attr,
778 IBV_FLOW_SPEC_IPV4_EXT,
781 flow_verbs_item_gre_ip_protocol_update(&verbs->attr,
784 flow_verbs_spec_add(verbs, &tunnel, size);
788 * Convert the @p action into a Verbs specification. This function assumes that
789 * the input is valid and that there is space to insert the requested action
790 * into the flow. This function also return the action that was added.
792 * @param[in, out] dev_flow
793 * Pointer to dev_flow structure.
795 * Item specification.
796 * @param[in] item_flags
800 flow_verbs_translate_item_mpls(struct mlx5_flow *dev_flow __rte_unused,
801 const struct rte_flow_item *item __rte_unused,
802 uint64_t item_flags __rte_unused)
804 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
805 const struct rte_flow_item_mpls *spec = item->spec;
806 const struct rte_flow_item_mpls *mask = item->mask;
807 unsigned int size = sizeof(struct ibv_flow_spec_mpls);
808 struct ibv_flow_spec_mpls mpls = {
809 .type = IBV_FLOW_SPEC_MPLS,
814 mask = &rte_flow_item_mpls_mask;
816 memcpy(&mpls.val.label, spec, sizeof(mpls.val.label));
817 memcpy(&mpls.mask.label, mask, sizeof(mpls.mask.label));
818 /* Remove unwanted bits from values. */
819 mpls.val.label &= mpls.mask.label;
821 flow_verbs_spec_add(&dev_flow->verbs, &mpls, size);
826 * Convert the @p action into a Verbs specification. This function assumes that
827 * the input is valid and that there is space to insert the requested action
830 * @param[in] dev_flow
831 * Pointer to mlx5_flow.
833 * Action configuration.
836 flow_verbs_translate_action_drop
837 (struct mlx5_flow *dev_flow,
838 const struct rte_flow_action *action __rte_unused)
840 unsigned int size = sizeof(struct ibv_flow_spec_action_drop);
841 struct ibv_flow_spec_action_drop drop = {
842 .type = IBV_FLOW_SPEC_ACTION_DROP,
846 flow_verbs_spec_add(&dev_flow->verbs, &drop, size);
850 * Convert the @p action into a Verbs specification. This function assumes that
851 * the input is valid and that there is space to insert the requested action
854 * @param[in] dev_flow
855 * Pointer to mlx5_flow.
857 * Action configuration.
860 flow_verbs_translate_action_queue(struct mlx5_flow *dev_flow,
861 const struct rte_flow_action *action)
863 const struct rte_flow_action_queue *queue = action->conf;
864 struct rte_flow *flow = dev_flow->flow;
867 (*flow->rss.queue)[0] = queue->index;
868 flow->rss.queue_num = 1;
872 * Convert the @p action into a Verbs specification. This function assumes that
873 * the input is valid and that there is space to insert the requested action
877 * Action configuration.
878 * @param[in, out] action_flags
879 * Pointer to the detected actions.
880 * @param[in] dev_flow
881 * Pointer to mlx5_flow.
884 flow_verbs_translate_action_rss(struct mlx5_flow *dev_flow,
885 const struct rte_flow_action *action)
887 const struct rte_flow_action_rss *rss = action->conf;
888 const uint8_t *rss_key;
889 struct rte_flow *flow = dev_flow->flow;
892 memcpy((*flow->rss.queue), rss->queue,
893 rss->queue_num * sizeof(uint16_t));
894 flow->rss.queue_num = rss->queue_num;
895 /* NULL RSS key indicates default RSS key. */
896 rss_key = !rss->key ? rss_hash_default_key : rss->key;
897 memcpy(flow->rss.key, rss_key, MLX5_RSS_HASH_KEY_LEN);
899 * rss->level and rss.types should be set in advance when expanding
905 * Convert the @p action into a Verbs specification. This function assumes that
906 * the input is valid and that there is space to insert the requested action
909 * @param[in] dev_flow
910 * Pointer to mlx5_flow.
912 * Action configuration.
915 flow_verbs_translate_action_flag
916 (struct mlx5_flow *dev_flow,
917 const struct rte_flow_action *action __rte_unused)
919 unsigned int size = sizeof(struct ibv_flow_spec_action_tag);
920 struct ibv_flow_spec_action_tag tag = {
921 .type = IBV_FLOW_SPEC_ACTION_TAG,
923 .tag_id = mlx5_flow_mark_set(MLX5_FLOW_MARK_DEFAULT),
926 flow_verbs_spec_add(&dev_flow->verbs, &tag, size);
930 * Convert the @p action into a Verbs specification. This function assumes that
931 * the input is valid and that there is space to insert the requested action
934 * @param[in] dev_flow
935 * Pointer to mlx5_flow.
937 * Action configuration.
940 flow_verbs_translate_action_mark(struct mlx5_flow *dev_flow,
941 const struct rte_flow_action *action)
943 const struct rte_flow_action_mark *mark = action->conf;
944 unsigned int size = sizeof(struct ibv_flow_spec_action_tag);
945 struct ibv_flow_spec_action_tag tag = {
946 .type = IBV_FLOW_SPEC_ACTION_TAG,
948 .tag_id = mlx5_flow_mark_set(mark->id),
951 flow_verbs_spec_add(&dev_flow->verbs, &tag, size);
955 * Convert the @p action into a Verbs specification. This function assumes that
956 * the input is valid and that there is space to insert the requested action
960 * Pointer to the Ethernet device structure.
962 * Action configuration.
963 * @param[in] dev_flow
964 * Pointer to mlx5_flow.
966 * Pointer to error structure.
969 * 0 On success else a negative errno value is returned and rte_errno is set.
972 flow_verbs_translate_action_count(struct mlx5_flow *dev_flow,
973 const struct rte_flow_action *action,
974 struct rte_eth_dev *dev,
975 struct rte_flow_error *error)
977 const struct rte_flow_action_count *count = action->conf;
978 struct rte_flow *flow = dev_flow->flow;
979 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42) || \
980 defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
981 unsigned int size = sizeof(struct ibv_flow_spec_counter_action);
982 struct ibv_flow_spec_counter_action counter = {
983 .type = IBV_FLOW_SPEC_ACTION_COUNT,
988 if (!flow->counter) {
989 flow->counter = flow_verbs_counter_new(dev, count->shared,
992 return rte_flow_error_set(error, rte_errno,
993 RTE_FLOW_ERROR_TYPE_ACTION,
998 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42)
999 counter.counter_set_handle = flow->counter->cs->handle;
1000 flow_verbs_spec_add(&dev_flow->verbs, &counter, size);
1001 #elif defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
1002 counter.counters = flow->counter->cs;
1003 flow_verbs_spec_add(&dev_flow->verbs, &counter, size);
1009 * Internal validation function. For validating both actions and items.
1012 * Pointer to the Ethernet device structure.
1014 * Pointer to the flow attributes.
1016 * Pointer to the list of items.
1017 * @param[in] actions
1018 * Pointer to the list of actions.
1019 * @param[in] external
1020 * This flow rule is created by request external to PMD.
1022 * Pointer to the error structure.
1025 * 0 on success, a negative errno value otherwise and rte_errno is set.
1028 flow_verbs_validate(struct rte_eth_dev *dev,
1029 const struct rte_flow_attr *attr,
1030 const struct rte_flow_item items[],
1031 const struct rte_flow_action actions[],
1032 bool external __rte_unused,
1033 struct rte_flow_error *error)
1036 uint64_t action_flags = 0;
1037 uint64_t item_flags = 0;
1038 uint64_t last_item = 0;
1039 uint8_t next_protocol = 0xff;
1040 uint16_t ether_type = 0;
1044 ret = mlx5_flow_validate_attributes(dev, attr, error);
1047 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
1048 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1051 switch (items->type) {
1052 case RTE_FLOW_ITEM_TYPE_VOID:
1054 case RTE_FLOW_ITEM_TYPE_ETH:
1055 ret = mlx5_flow_validate_item_eth(items, item_flags,
1059 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
1060 MLX5_FLOW_LAYER_OUTER_L2;
1061 if (items->mask != NULL && items->spec != NULL) {
1063 ((const struct rte_flow_item_eth *)
1066 ((const struct rte_flow_item_eth *)
1068 ether_type = rte_be_to_cpu_16(ether_type);
1073 case RTE_FLOW_ITEM_TYPE_VLAN:
1074 ret = mlx5_flow_validate_item_vlan(items, item_flags,
1078 last_item = tunnel ? (MLX5_FLOW_LAYER_INNER_L2 |
1079 MLX5_FLOW_LAYER_INNER_VLAN) :
1080 (MLX5_FLOW_LAYER_OUTER_L2 |
1081 MLX5_FLOW_LAYER_OUTER_VLAN);
1082 if (items->mask != NULL && items->spec != NULL) {
1084 ((const struct rte_flow_item_vlan *)
1085 items->spec)->inner_type;
1087 ((const struct rte_flow_item_vlan *)
1088 items->mask)->inner_type;
1089 ether_type = rte_be_to_cpu_16(ether_type);
1094 case RTE_FLOW_ITEM_TYPE_IPV4:
1095 ret = mlx5_flow_validate_item_ipv4(items, item_flags,
1101 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
1102 MLX5_FLOW_LAYER_OUTER_L3_IPV4;
1103 if (items->mask != NULL &&
1104 ((const struct rte_flow_item_ipv4 *)
1105 items->mask)->hdr.next_proto_id) {
1107 ((const struct rte_flow_item_ipv4 *)
1108 (items->spec))->hdr.next_proto_id;
1110 ((const struct rte_flow_item_ipv4 *)
1111 (items->mask))->hdr.next_proto_id;
1113 /* Reset for inner layer. */
1114 next_protocol = 0xff;
1117 case RTE_FLOW_ITEM_TYPE_IPV6:
1118 ret = mlx5_flow_validate_item_ipv6(items, item_flags,
1124 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
1125 MLX5_FLOW_LAYER_OUTER_L3_IPV6;
1126 if (items->mask != NULL &&
1127 ((const struct rte_flow_item_ipv6 *)
1128 items->mask)->hdr.proto) {
1130 ((const struct rte_flow_item_ipv6 *)
1131 items->spec)->hdr.proto;
1133 ((const struct rte_flow_item_ipv6 *)
1134 items->mask)->hdr.proto;
1136 /* Reset for inner layer. */
1137 next_protocol = 0xff;
1140 case RTE_FLOW_ITEM_TYPE_UDP:
1141 ret = mlx5_flow_validate_item_udp(items, item_flags,
1146 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
1147 MLX5_FLOW_LAYER_OUTER_L4_UDP;
1149 case RTE_FLOW_ITEM_TYPE_TCP:
1150 ret = mlx5_flow_validate_item_tcp
1153 &rte_flow_item_tcp_mask,
1157 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
1158 MLX5_FLOW_LAYER_OUTER_L4_TCP;
1160 case RTE_FLOW_ITEM_TYPE_VXLAN:
1161 ret = mlx5_flow_validate_item_vxlan(items, item_flags,
1165 last_item = MLX5_FLOW_LAYER_VXLAN;
1167 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
1168 ret = mlx5_flow_validate_item_vxlan_gpe(items,
1173 last_item = MLX5_FLOW_LAYER_VXLAN_GPE;
1175 case RTE_FLOW_ITEM_TYPE_GRE:
1176 ret = mlx5_flow_validate_item_gre(items, item_flags,
1177 next_protocol, error);
1180 last_item = MLX5_FLOW_LAYER_GRE;
1182 case RTE_FLOW_ITEM_TYPE_MPLS:
1183 ret = mlx5_flow_validate_item_mpls(dev, items,
1188 last_item = MLX5_FLOW_LAYER_MPLS;
1191 return rte_flow_error_set(error, ENOTSUP,
1192 RTE_FLOW_ERROR_TYPE_ITEM,
1193 NULL, "item not supported");
1195 item_flags |= last_item;
1197 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1198 switch (actions->type) {
1199 case RTE_FLOW_ACTION_TYPE_VOID:
1201 case RTE_FLOW_ACTION_TYPE_FLAG:
1202 ret = mlx5_flow_validate_action_flag(action_flags,
1207 action_flags |= MLX5_FLOW_ACTION_FLAG;
1209 case RTE_FLOW_ACTION_TYPE_MARK:
1210 ret = mlx5_flow_validate_action_mark(actions,
1216 action_flags |= MLX5_FLOW_ACTION_MARK;
1218 case RTE_FLOW_ACTION_TYPE_DROP:
1219 ret = mlx5_flow_validate_action_drop(action_flags,
1224 action_flags |= MLX5_FLOW_ACTION_DROP;
1226 case RTE_FLOW_ACTION_TYPE_QUEUE:
1227 ret = mlx5_flow_validate_action_queue(actions,
1233 action_flags |= MLX5_FLOW_ACTION_QUEUE;
1235 case RTE_FLOW_ACTION_TYPE_RSS:
1236 ret = mlx5_flow_validate_action_rss(actions,
1242 action_flags |= MLX5_FLOW_ACTION_RSS;
1244 case RTE_FLOW_ACTION_TYPE_COUNT:
1245 ret = mlx5_flow_validate_action_count(dev, attr, error);
1248 action_flags |= MLX5_FLOW_ACTION_COUNT;
1251 return rte_flow_error_set(error, ENOTSUP,
1252 RTE_FLOW_ERROR_TYPE_ACTION,
1254 "action not supported");
1258 * Validate the drop action mutual exclusion with other actions.
1259 * Drop action is mutually-exclusive with any other action, except for
1262 if ((action_flags & MLX5_FLOW_ACTION_DROP) &&
1263 (action_flags & ~(MLX5_FLOW_ACTION_DROP | MLX5_FLOW_ACTION_COUNT)))
1264 return rte_flow_error_set(error, EINVAL,
1265 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1266 "Drop action is mutually-exclusive "
1267 "with any other action, except for "
1269 if (!(action_flags & MLX5_FLOW_FATE_ACTIONS))
1270 return rte_flow_error_set(error, EINVAL,
1271 RTE_FLOW_ERROR_TYPE_ACTION, actions,
1272 "no fate action is found");
1277 * Calculate the required bytes that are needed for the action part of the verbs
1280 * @param[in] actions
1281 * Pointer to the list of actions.
1284 * The size of the memory needed for all actions.
1287 flow_verbs_get_actions_size(const struct rte_flow_action actions[])
1291 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1292 switch (actions->type) {
1293 case RTE_FLOW_ACTION_TYPE_VOID:
1295 case RTE_FLOW_ACTION_TYPE_FLAG:
1296 size += sizeof(struct ibv_flow_spec_action_tag);
1298 case RTE_FLOW_ACTION_TYPE_MARK:
1299 size += sizeof(struct ibv_flow_spec_action_tag);
1301 case RTE_FLOW_ACTION_TYPE_DROP:
1302 size += sizeof(struct ibv_flow_spec_action_drop);
1304 case RTE_FLOW_ACTION_TYPE_QUEUE:
1306 case RTE_FLOW_ACTION_TYPE_RSS:
1308 case RTE_FLOW_ACTION_TYPE_COUNT:
1309 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42) || \
1310 defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
1311 size += sizeof(struct ibv_flow_spec_counter_action);
1322 * Calculate the required bytes that are needed for the item part of the verbs
1326 * Pointer to the list of items.
1329 * The size of the memory needed for all items.
1332 flow_verbs_get_items_size(const struct rte_flow_item items[])
1336 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
1337 switch (items->type) {
1338 case RTE_FLOW_ITEM_TYPE_VOID:
1340 case RTE_FLOW_ITEM_TYPE_ETH:
1341 size += sizeof(struct ibv_flow_spec_eth);
1343 case RTE_FLOW_ITEM_TYPE_VLAN:
1344 size += sizeof(struct ibv_flow_spec_eth);
1346 case RTE_FLOW_ITEM_TYPE_IPV4:
1347 size += sizeof(struct ibv_flow_spec_ipv4_ext);
1349 case RTE_FLOW_ITEM_TYPE_IPV6:
1350 size += sizeof(struct ibv_flow_spec_ipv6);
1352 case RTE_FLOW_ITEM_TYPE_UDP:
1353 size += sizeof(struct ibv_flow_spec_tcp_udp);
1355 case RTE_FLOW_ITEM_TYPE_TCP:
1356 size += sizeof(struct ibv_flow_spec_tcp_udp);
1358 case RTE_FLOW_ITEM_TYPE_VXLAN:
1359 size += sizeof(struct ibv_flow_spec_tunnel);
1361 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
1362 size += sizeof(struct ibv_flow_spec_tunnel);
1364 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
1365 case RTE_FLOW_ITEM_TYPE_GRE:
1366 size += sizeof(struct ibv_flow_spec_gre);
1368 case RTE_FLOW_ITEM_TYPE_MPLS:
1369 size += sizeof(struct ibv_flow_spec_mpls);
1372 case RTE_FLOW_ITEM_TYPE_GRE:
1373 size += sizeof(struct ibv_flow_spec_tunnel);
1384 * Internal preparation function. Allocate mlx5_flow with the required size.
1385 * The required size is calculate based on the actions and items. This function
1386 * also returns the detected actions and items for later use.
1389 * Pointer to Ethernet device.
1391 * Pointer to the flow attributes.
1393 * Pointer to the list of items.
1394 * @param[in] actions
1395 * Pointer to the list of actions.
1397 * Pointer to the error structure.
1400 * Pointer to mlx5_flow object on success, otherwise NULL and rte_errno
1403 static struct mlx5_flow *
1404 flow_verbs_prepare(struct rte_eth_dev *dev,
1405 const struct rte_flow_attr *attr __rte_unused,
1406 const struct rte_flow_item items[],
1407 const struct rte_flow_action actions[],
1408 struct rte_flow_error *error)
1411 struct mlx5_flow *dev_flow;
1412 struct mlx5_flow_handle *dev_handle;
1413 struct mlx5_priv *priv = dev->data->dev_private;
1415 size += flow_verbs_get_actions_size(actions);
1416 size += flow_verbs_get_items_size(items);
1417 if (size > MLX5_VERBS_MAX_SPEC_ACT_SIZE) {
1418 rte_flow_error_set(error, E2BIG,
1419 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1420 "Verbs spec/action size too large");
1423 /* In case of corrupting the memory. */
1424 if (priv->flow_idx >= MLX5_NUM_MAX_DEV_FLOWS) {
1425 rte_flow_error_set(error, ENOSPC,
1426 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1427 "not free temporary device flow");
1430 dev_handle = rte_calloc(__func__, 1, MLX5_FLOW_HANDLE_VERBS_SIZE, 0);
1432 rte_flow_error_set(error, ENOMEM,
1433 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1434 "not enough memory to create flow handle");
1437 /* No multi-thread supporting. */
1438 dev_flow = &((struct mlx5_flow *)priv->inter_flows)[priv->flow_idx++];
1439 dev_flow->handle = dev_handle;
1440 /* Memcpy is used, only size needs to be cleared to 0. */
1441 dev_flow->verbs.size = 0;
1442 dev_flow->verbs.attr.num_of_specs = 0;
1443 dev_flow->ingress = attr->ingress;
1444 /* Need to set transfer attribute: not supported in Verbs mode. */
1449 * Fill the flow with verb spec.
1452 * Pointer to Ethernet device.
1453 * @param[in, out] dev_flow
1454 * Pointer to the mlx5 flow.
1456 * Pointer to the flow attributes.
1458 * Pointer to the list of items.
1459 * @param[in] actions
1460 * Pointer to the list of actions.
1462 * Pointer to the error structure.
1465 * 0 on success, else a negative errno value otherwise and rte_errno is set.
1468 flow_verbs_translate(struct rte_eth_dev *dev,
1469 struct mlx5_flow *dev_flow,
1470 const struct rte_flow_attr *attr,
1471 const struct rte_flow_item items[],
1472 const struct rte_flow_action actions[],
1473 struct rte_flow_error *error)
1475 uint64_t item_flags = 0;
1476 uint64_t action_flags = 0;
1477 uint64_t priority = attr->priority;
1478 uint32_t subpriority = 0;
1479 struct mlx5_priv *priv = dev->data->dev_private;
1481 if (priority == MLX5_FLOW_PRIO_RSVD)
1482 priority = priv->config.flow_prio - 1;
1483 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 flow_verbs_translate_action_flag(dev_flow, actions);
1491 action_flags |= MLX5_FLOW_ACTION_FLAG;
1493 case RTE_FLOW_ACTION_TYPE_MARK:
1494 flow_verbs_translate_action_mark(dev_flow, actions);
1495 action_flags |= MLX5_FLOW_ACTION_MARK;
1497 case RTE_FLOW_ACTION_TYPE_DROP:
1498 flow_verbs_translate_action_drop(dev_flow, actions);
1499 action_flags |= MLX5_FLOW_ACTION_DROP;
1501 case RTE_FLOW_ACTION_TYPE_QUEUE:
1502 flow_verbs_translate_action_queue(dev_flow, actions);
1503 action_flags |= MLX5_FLOW_ACTION_QUEUE;
1505 case RTE_FLOW_ACTION_TYPE_RSS:
1506 flow_verbs_translate_action_rss(dev_flow, actions);
1507 action_flags |= MLX5_FLOW_ACTION_RSS;
1509 case RTE_FLOW_ACTION_TYPE_COUNT:
1510 ret = flow_verbs_translate_action_count(dev_flow,
1515 action_flags |= MLX5_FLOW_ACTION_COUNT;
1518 return rte_flow_error_set(error, ENOTSUP,
1519 RTE_FLOW_ERROR_TYPE_ACTION,
1521 "action not supported");
1524 dev_flow->handle->act_flags = action_flags;
1525 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
1526 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1528 switch (items->type) {
1529 case RTE_FLOW_ITEM_TYPE_VOID:
1531 case RTE_FLOW_ITEM_TYPE_ETH:
1532 flow_verbs_translate_item_eth(dev_flow, items,
1534 subpriority = MLX5_PRIORITY_MAP_L2;
1535 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
1536 MLX5_FLOW_LAYER_OUTER_L2;
1538 case RTE_FLOW_ITEM_TYPE_VLAN:
1539 flow_verbs_translate_item_vlan(dev_flow, items,
1541 subpriority = MLX5_PRIORITY_MAP_L2;
1542 item_flags |= tunnel ? (MLX5_FLOW_LAYER_INNER_L2 |
1543 MLX5_FLOW_LAYER_INNER_VLAN) :
1544 (MLX5_FLOW_LAYER_OUTER_L2 |
1545 MLX5_FLOW_LAYER_OUTER_VLAN);
1547 case RTE_FLOW_ITEM_TYPE_IPV4:
1548 flow_verbs_translate_item_ipv4(dev_flow, items,
1550 subpriority = MLX5_PRIORITY_MAP_L3;
1551 dev_flow->hash_fields |=
1552 mlx5_flow_hashfields_adjust
1554 MLX5_IPV4_LAYER_TYPES,
1555 MLX5_IPV4_IBV_RX_HASH);
1556 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
1557 MLX5_FLOW_LAYER_OUTER_L3_IPV4;
1559 case RTE_FLOW_ITEM_TYPE_IPV6:
1560 flow_verbs_translate_item_ipv6(dev_flow, items,
1562 subpriority = MLX5_PRIORITY_MAP_L3;
1563 dev_flow->hash_fields |=
1564 mlx5_flow_hashfields_adjust
1566 MLX5_IPV6_LAYER_TYPES,
1567 MLX5_IPV6_IBV_RX_HASH);
1568 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
1569 MLX5_FLOW_LAYER_OUTER_L3_IPV6;
1571 case RTE_FLOW_ITEM_TYPE_TCP:
1572 flow_verbs_translate_item_tcp(dev_flow, items,
1574 subpriority = MLX5_PRIORITY_MAP_L4;
1575 dev_flow->hash_fields |=
1576 mlx5_flow_hashfields_adjust
1577 (dev_flow, tunnel, ETH_RSS_TCP,
1578 (IBV_RX_HASH_SRC_PORT_TCP |
1579 IBV_RX_HASH_DST_PORT_TCP));
1580 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
1581 MLX5_FLOW_LAYER_OUTER_L4_TCP;
1583 case RTE_FLOW_ITEM_TYPE_UDP:
1584 flow_verbs_translate_item_udp(dev_flow, items,
1586 subpriority = MLX5_PRIORITY_MAP_L4;
1587 dev_flow->hash_fields |=
1588 mlx5_flow_hashfields_adjust
1589 (dev_flow, tunnel, ETH_RSS_UDP,
1590 (IBV_RX_HASH_SRC_PORT_UDP |
1591 IBV_RX_HASH_DST_PORT_UDP));
1592 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
1593 MLX5_FLOW_LAYER_OUTER_L4_UDP;
1595 case RTE_FLOW_ITEM_TYPE_VXLAN:
1596 flow_verbs_translate_item_vxlan(dev_flow, items,
1598 subpriority = MLX5_PRIORITY_MAP_L2;
1599 item_flags |= MLX5_FLOW_LAYER_VXLAN;
1601 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
1602 flow_verbs_translate_item_vxlan_gpe(dev_flow, items,
1604 subpriority = MLX5_PRIORITY_MAP_L2;
1605 item_flags |= MLX5_FLOW_LAYER_VXLAN_GPE;
1607 case RTE_FLOW_ITEM_TYPE_GRE:
1608 flow_verbs_translate_item_gre(dev_flow, items,
1610 subpriority = MLX5_PRIORITY_MAP_L2;
1611 item_flags |= MLX5_FLOW_LAYER_GRE;
1613 case RTE_FLOW_ITEM_TYPE_MPLS:
1614 flow_verbs_translate_item_mpls(dev_flow, items,
1616 subpriority = MLX5_PRIORITY_MAP_L2;
1617 item_flags |= MLX5_FLOW_LAYER_MPLS;
1620 return rte_flow_error_set(error, ENOTSUP,
1621 RTE_FLOW_ERROR_TYPE_ITEM,
1623 "item not supported");
1626 dev_flow->handle->layers = item_flags;
1627 /* Other members of attr will be ignored. */
1628 dev_flow->verbs.attr.priority =
1629 mlx5_flow_adjust_priority(dev, priority, subpriority);
1630 dev_flow->verbs.attr.port = (uint8_t)priv->ibv_port;
1635 * Remove the flow from the NIC but keeps it in memory.
1638 * Pointer to the Ethernet device structure.
1639 * @param[in, out] flow
1640 * Pointer to flow structure.
1643 flow_verbs_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
1645 struct mlx5_flow_handle *handle;
1649 LIST_FOREACH(handle, &flow->dev_handles, next) {
1650 if (handle->ib_flow) {
1651 claim_zero(mlx5_glue->destroy_flow(handle->ib_flow));
1652 handle->ib_flow = NULL;
1655 if (handle->act_flags & MLX5_FLOW_ACTION_DROP)
1656 mlx5_hrxq_drop_release(dev);
1658 mlx5_hrxq_release(dev, handle->hrxq);
1659 handle->hrxq = NULL;
1661 if (handle->vf_vlan.tag && handle->vf_vlan.created)
1662 mlx5_vlan_vmwa_release(dev, &handle->vf_vlan);
1667 * Remove the flow from the NIC and the memory.
1670 * Pointer to the Ethernet device structure.
1671 * @param[in, out] flow
1672 * Pointer to flow structure.
1675 flow_verbs_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
1677 struct mlx5_flow_handle *handle;
1681 flow_verbs_remove(dev, flow);
1682 while (!LIST_EMPTY(&flow->dev_handles)) {
1683 handle = LIST_FIRST(&flow->dev_handles);
1684 LIST_REMOVE(handle, next);
1687 if (flow->counter) {
1688 flow_verbs_counter_release(dev, flow->counter);
1689 flow->counter = NULL;
1694 * Apply the flow to the NIC.
1697 * Pointer to the Ethernet device structure.
1698 * @param[in, out] flow
1699 * Pointer to flow structure.
1701 * Pointer to error structure.
1704 * 0 on success, a negative errno value otherwise and rte_errno is set.
1707 flow_verbs_apply(struct rte_eth_dev *dev, struct rte_flow *flow,
1708 struct rte_flow_error *error)
1710 struct mlx5_priv *priv = dev->data->dev_private;
1711 struct mlx5_flow_handle *handle;
1712 struct mlx5_flow *dev_flow;
1716 for (idx = priv->flow_idx - 1; idx >= 0; idx--) {
1717 dev_flow = &((struct mlx5_flow *)priv->inter_flows)[idx];
1718 handle = dev_flow->handle;
1719 if (handle->act_flags & MLX5_FLOW_ACTION_DROP) {
1720 handle->hrxq = mlx5_hrxq_drop_new(dev);
1721 if (!handle->hrxq) {
1724 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1725 "cannot get drop hash queue");
1729 struct mlx5_hrxq *hrxq;
1731 MLX5_ASSERT(flow->rss.queue);
1732 hrxq = mlx5_hrxq_get(dev, flow->rss.key,
1733 MLX5_RSS_HASH_KEY_LEN,
1734 dev_flow->hash_fields,
1736 flow->rss.queue_num);
1738 hrxq = mlx5_hrxq_new(dev, flow->rss.key,
1739 MLX5_RSS_HASH_KEY_LEN,
1740 dev_flow->hash_fields,
1742 flow->rss.queue_num,
1744 MLX5_FLOW_LAYER_TUNNEL));
1748 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1749 "cannot get hash queue");
1752 handle->hrxq = hrxq;
1754 handle->ib_flow = mlx5_glue->create_flow(handle->hrxq->qp,
1755 &dev_flow->verbs.attr);
1756 if (!handle->ib_flow) {
1757 rte_flow_error_set(error, errno,
1758 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1760 "hardware refuses to create flow");
1763 if (priv->vmwa_context &&
1764 handle->vf_vlan.tag && !handle->vf_vlan.created) {
1766 * The rule contains the VLAN pattern.
1767 * For VF we are going to create VLAN
1768 * interface to make hypervisor set correct
1769 * e-Switch vport context.
1771 mlx5_vlan_vmwa_acquire(dev, &handle->vf_vlan);
1776 err = rte_errno; /* Save rte_errno before cleanup. */
1777 LIST_FOREACH(handle, &flow->dev_handles, next) {
1779 if (handle->act_flags & MLX5_FLOW_ACTION_DROP)
1780 mlx5_hrxq_drop_release(dev);
1782 mlx5_hrxq_release(dev, handle->hrxq);
1783 handle->hrxq = NULL;
1785 if (handle->vf_vlan.tag && handle->vf_vlan.created)
1786 mlx5_vlan_vmwa_release(dev, &handle->vf_vlan);
1788 rte_errno = err; /* Restore rte_errno. */
1795 * @see rte_flow_query()
1799 flow_verbs_query(struct rte_eth_dev *dev,
1800 struct rte_flow *flow,
1801 const struct rte_flow_action *actions,
1803 struct rte_flow_error *error)
1807 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1808 switch (actions->type) {
1809 case RTE_FLOW_ACTION_TYPE_VOID:
1811 case RTE_FLOW_ACTION_TYPE_COUNT:
1812 ret = flow_verbs_counter_query(dev, flow, data, error);
1815 return rte_flow_error_set(error, ENOTSUP,
1816 RTE_FLOW_ERROR_TYPE_ACTION,
1818 "action not supported");
1824 const struct mlx5_flow_driver_ops mlx5_flow_verbs_drv_ops = {
1825 .validate = flow_verbs_validate,
1826 .prepare = flow_verbs_prepare,
1827 .translate = flow_verbs_translate,
1828 .apply = flow_verbs_apply,
1829 .remove = flow_verbs_remove,
1830 .destroy = flow_verbs_destroy,
1831 .query = flow_verbs_query,