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_resource_verbs *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_resource_verbs *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 the flow attributes.
1391 * Pointer to the list of items.
1392 * @param[in] actions
1393 * Pointer to the list of actions.
1395 * Pointer to the error structure.
1398 * Pointer to mlx5_flow object on success, otherwise NULL and rte_errno
1401 static struct mlx5_flow *
1402 flow_verbs_prepare(const struct rte_flow_attr *attr __rte_unused,
1403 const struct rte_flow_item items[],
1404 const struct rte_flow_action actions[],
1405 struct rte_flow_error *error)
1407 size_t size = sizeof(struct mlx5_flow) + sizeof(struct ibv_flow_attr);
1408 struct mlx5_flow *dev_flow;
1410 size += flow_verbs_get_actions_size(actions);
1411 size += flow_verbs_get_items_size(items);
1412 dev_flow = rte_calloc(__func__, 1, size, 0);
1414 rte_flow_error_set(error, ENOMEM,
1415 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1416 "not enough memory to create flow");
1419 dev_flow->verbs.attr = (void *)(dev_flow + 1);
1420 dev_flow->verbs.specs = (void *)(dev_flow->verbs.attr + 1);
1421 dev_flow->ingress = attr->ingress;
1422 /* Need to set transfer attribute: not supported in Verbs mode. */
1427 * Fill the flow with verb spec.
1430 * Pointer to Ethernet device.
1431 * @param[in, out] dev_flow
1432 * Pointer to the mlx5 flow.
1434 * Pointer to the flow attributes.
1436 * Pointer to the list of items.
1437 * @param[in] actions
1438 * Pointer to the list of actions.
1440 * Pointer to the error structure.
1443 * 0 on success, else a negative errno value otherwise and rte_errno is set.
1446 flow_verbs_translate(struct rte_eth_dev *dev,
1447 struct mlx5_flow *dev_flow,
1448 const struct rte_flow_attr *attr,
1449 const struct rte_flow_item items[],
1450 const struct rte_flow_action actions[],
1451 struct rte_flow_error *error)
1453 uint64_t item_flags = 0;
1454 uint64_t action_flags = 0;
1455 uint64_t priority = attr->priority;
1456 uint32_t subpriority = 0;
1457 struct mlx5_priv *priv = dev->data->dev_private;
1459 if (priority == MLX5_FLOW_PRIO_RSVD)
1460 priority = priv->config.flow_prio - 1;
1461 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1464 switch (actions->type) {
1465 case RTE_FLOW_ACTION_TYPE_VOID:
1467 case RTE_FLOW_ACTION_TYPE_FLAG:
1468 flow_verbs_translate_action_flag(dev_flow, actions);
1469 action_flags |= MLX5_FLOW_ACTION_FLAG;
1471 case RTE_FLOW_ACTION_TYPE_MARK:
1472 flow_verbs_translate_action_mark(dev_flow, actions);
1473 action_flags |= MLX5_FLOW_ACTION_MARK;
1475 case RTE_FLOW_ACTION_TYPE_DROP:
1476 flow_verbs_translate_action_drop(dev_flow, actions);
1477 action_flags |= MLX5_FLOW_ACTION_DROP;
1479 case RTE_FLOW_ACTION_TYPE_QUEUE:
1480 flow_verbs_translate_action_queue(dev_flow, actions);
1481 action_flags |= MLX5_FLOW_ACTION_QUEUE;
1483 case RTE_FLOW_ACTION_TYPE_RSS:
1484 flow_verbs_translate_action_rss(dev_flow, actions);
1485 action_flags |= MLX5_FLOW_ACTION_RSS;
1487 case RTE_FLOW_ACTION_TYPE_COUNT:
1488 ret = flow_verbs_translate_action_count(dev_flow,
1493 action_flags |= MLX5_FLOW_ACTION_COUNT;
1496 return rte_flow_error_set(error, ENOTSUP,
1497 RTE_FLOW_ERROR_TYPE_ACTION,
1499 "action not supported");
1502 dev_flow->handle.act_flags = action_flags;
1503 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
1504 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1506 switch (items->type) {
1507 case RTE_FLOW_ITEM_TYPE_VOID:
1509 case RTE_FLOW_ITEM_TYPE_ETH:
1510 flow_verbs_translate_item_eth(dev_flow, items,
1512 subpriority = MLX5_PRIORITY_MAP_L2;
1513 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
1514 MLX5_FLOW_LAYER_OUTER_L2;
1516 case RTE_FLOW_ITEM_TYPE_VLAN:
1517 flow_verbs_translate_item_vlan(dev_flow, items,
1519 subpriority = MLX5_PRIORITY_MAP_L2;
1520 item_flags |= tunnel ? (MLX5_FLOW_LAYER_INNER_L2 |
1521 MLX5_FLOW_LAYER_INNER_VLAN) :
1522 (MLX5_FLOW_LAYER_OUTER_L2 |
1523 MLX5_FLOW_LAYER_OUTER_VLAN);
1525 case RTE_FLOW_ITEM_TYPE_IPV4:
1526 flow_verbs_translate_item_ipv4(dev_flow, items,
1528 subpriority = MLX5_PRIORITY_MAP_L3;
1529 dev_flow->hash_fields |=
1530 mlx5_flow_hashfields_adjust
1532 MLX5_IPV4_LAYER_TYPES,
1533 MLX5_IPV4_IBV_RX_HASH);
1534 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
1535 MLX5_FLOW_LAYER_OUTER_L3_IPV4;
1537 case RTE_FLOW_ITEM_TYPE_IPV6:
1538 flow_verbs_translate_item_ipv6(dev_flow, items,
1540 subpriority = MLX5_PRIORITY_MAP_L3;
1541 dev_flow->hash_fields |=
1542 mlx5_flow_hashfields_adjust
1544 MLX5_IPV6_LAYER_TYPES,
1545 MLX5_IPV6_IBV_RX_HASH);
1546 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
1547 MLX5_FLOW_LAYER_OUTER_L3_IPV6;
1549 case RTE_FLOW_ITEM_TYPE_TCP:
1550 flow_verbs_translate_item_tcp(dev_flow, items,
1552 subpriority = MLX5_PRIORITY_MAP_L4;
1553 dev_flow->hash_fields |=
1554 mlx5_flow_hashfields_adjust
1555 (dev_flow, tunnel, ETH_RSS_TCP,
1556 (IBV_RX_HASH_SRC_PORT_TCP |
1557 IBV_RX_HASH_DST_PORT_TCP));
1558 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
1559 MLX5_FLOW_LAYER_OUTER_L4_TCP;
1561 case RTE_FLOW_ITEM_TYPE_UDP:
1562 flow_verbs_translate_item_udp(dev_flow, items,
1564 subpriority = MLX5_PRIORITY_MAP_L4;
1565 dev_flow->hash_fields |=
1566 mlx5_flow_hashfields_adjust
1567 (dev_flow, tunnel, ETH_RSS_UDP,
1568 (IBV_RX_HASH_SRC_PORT_UDP |
1569 IBV_RX_HASH_DST_PORT_UDP));
1570 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
1571 MLX5_FLOW_LAYER_OUTER_L4_UDP;
1573 case RTE_FLOW_ITEM_TYPE_VXLAN:
1574 flow_verbs_translate_item_vxlan(dev_flow, items,
1576 subpriority = MLX5_PRIORITY_MAP_L2;
1577 item_flags |= MLX5_FLOW_LAYER_VXLAN;
1579 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
1580 flow_verbs_translate_item_vxlan_gpe(dev_flow, items,
1582 subpriority = MLX5_PRIORITY_MAP_L2;
1583 item_flags |= MLX5_FLOW_LAYER_VXLAN_GPE;
1585 case RTE_FLOW_ITEM_TYPE_GRE:
1586 flow_verbs_translate_item_gre(dev_flow, items,
1588 subpriority = MLX5_PRIORITY_MAP_L2;
1589 item_flags |= MLX5_FLOW_LAYER_GRE;
1591 case RTE_FLOW_ITEM_TYPE_MPLS:
1592 flow_verbs_translate_item_mpls(dev_flow, items,
1594 subpriority = MLX5_PRIORITY_MAP_L2;
1595 item_flags |= MLX5_FLOW_LAYER_MPLS;
1598 return rte_flow_error_set(error, ENOTSUP,
1599 RTE_FLOW_ERROR_TYPE_ITEM,
1601 "item not supported");
1604 dev_flow->handle.layers = item_flags;
1605 dev_flow->verbs.attr->priority =
1606 mlx5_flow_adjust_priority(dev, priority, subpriority);
1607 dev_flow->verbs.attr->port = (uint8_t)priv->ibv_port;
1612 * Remove the flow from the NIC but keeps it in memory.
1615 * Pointer to the Ethernet device structure.
1616 * @param[in, out] flow
1617 * Pointer to flow structure.
1620 flow_verbs_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
1622 struct mlx5_flow_handle *dh;
1623 struct mlx5_flow *dev_flow;
1627 LIST_FOREACH(dev_flow, &flow->dev_flows, next) {
1628 dh = &dev_flow->handle;
1630 claim_zero(mlx5_glue->destroy_flow(dh->ib_flow));
1634 if (dev_flow->handle.act_flags & MLX5_FLOW_ACTION_DROP)
1635 mlx5_hrxq_drop_release(dev);
1637 mlx5_hrxq_release(dev, dh->hrxq);
1640 if (dh->vf_vlan.tag && dh->vf_vlan.created)
1641 mlx5_vlan_vmwa_release(dev, &dh->vf_vlan);
1646 * Remove the flow from the NIC and the memory.
1649 * Pointer to the Ethernet device structure.
1650 * @param[in, out] flow
1651 * Pointer to flow structure.
1654 flow_verbs_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
1656 struct mlx5_flow *dev_flow;
1660 flow_verbs_remove(dev, flow);
1661 while (!LIST_EMPTY(&flow->dev_flows)) {
1662 dev_flow = LIST_FIRST(&flow->dev_flows);
1663 LIST_REMOVE(dev_flow, next);
1666 if (flow->counter) {
1667 flow_verbs_counter_release(dev, flow->counter);
1668 flow->counter = NULL;
1673 * Apply the flow to the NIC.
1676 * Pointer to the Ethernet device structure.
1677 * @param[in, out] flow
1678 * Pointer to flow structure.
1680 * Pointer to error structure.
1683 * 0 on success, a negative errno value otherwise and rte_errno is set.
1686 flow_verbs_apply(struct rte_eth_dev *dev, struct rte_flow *flow,
1687 struct rte_flow_error *error)
1689 struct mlx5_priv *priv = dev->data->dev_private;
1690 struct mlx5_flow_handle *dh;
1691 struct mlx5_flow *dev_flow;
1694 LIST_FOREACH(dev_flow, &flow->dev_flows, next) {
1695 dh = &dev_flow->handle;
1696 if (dev_flow->handle.act_flags & MLX5_FLOW_ACTION_DROP) {
1697 dh->hrxq = mlx5_hrxq_drop_new(dev);
1701 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1702 "cannot get drop hash queue");
1706 struct mlx5_hrxq *hrxq;
1708 MLX5_ASSERT(flow->rss.queue);
1709 hrxq = mlx5_hrxq_get(dev, flow->rss.key,
1710 MLX5_RSS_HASH_KEY_LEN,
1711 dev_flow->hash_fields,
1713 flow->rss.queue_num);
1715 hrxq = mlx5_hrxq_new(dev, flow->rss.key,
1716 MLX5_RSS_HASH_KEY_LEN,
1717 dev_flow->hash_fields,
1719 flow->rss.queue_num,
1720 !!(dev_flow->handle.layers &
1721 MLX5_FLOW_LAYER_TUNNEL));
1725 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1726 "cannot get hash queue");
1731 dh->ib_flow = mlx5_glue->create_flow(dh->hrxq->qp,
1732 dev_flow->verbs.attr);
1734 rte_flow_error_set(error, errno,
1735 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1737 "hardware refuses to create flow");
1740 if (priv->vmwa_context &&
1741 dev_flow->handle.vf_vlan.tag &&
1742 !dev_flow->handle.vf_vlan.created) {
1744 * The rule contains the VLAN pattern.
1745 * For VF we are going to create VLAN
1746 * interface to make hypervisor set correct
1747 * e-Switch vport context.
1749 mlx5_vlan_vmwa_acquire(dev, &dev_flow->handle.vf_vlan);
1754 err = rte_errno; /* Save rte_errno before cleanup. */
1755 LIST_FOREACH(dev_flow, &flow->dev_flows, next) {
1756 dh = &dev_flow->handle;
1758 if (dev_flow->handle.act_flags & MLX5_FLOW_ACTION_DROP)
1759 mlx5_hrxq_drop_release(dev);
1761 mlx5_hrxq_release(dev, dh->hrxq);
1764 if (dh->vf_vlan.tag && dh->vf_vlan.created)
1765 mlx5_vlan_vmwa_release(dev, &dh->vf_vlan);
1767 rte_errno = err; /* Restore rte_errno. */
1774 * @see rte_flow_query()
1778 flow_verbs_query(struct rte_eth_dev *dev,
1779 struct rte_flow *flow,
1780 const struct rte_flow_action *actions,
1782 struct rte_flow_error *error)
1786 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1787 switch (actions->type) {
1788 case RTE_FLOW_ACTION_TYPE_VOID:
1790 case RTE_FLOW_ACTION_TYPE_COUNT:
1791 ret = flow_verbs_counter_query(dev, flow, data, error);
1794 return rte_flow_error_set(error, ENOTSUP,
1795 RTE_FLOW_ERROR_TYPE_ACTION,
1797 "action not supported");
1803 const struct mlx5_flow_driver_ops mlx5_flow_verbs_drv_ops = {
1804 .validate = flow_verbs_validate,
1805 .prepare = flow_verbs_prepare,
1806 .translate = flow_verbs_translate,
1807 .apply = flow_verbs_apply,
1808 .remove = flow_verbs_remove,
1809 .destroy = flow_verbs_destroy,
1810 .query = flow_verbs_query,