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>
30 #include "mlx5_defs.h"
31 #include "mlx5_flow.h"
32 #include "mlx5_glue.h"
34 #include "mlx5_rxtx.h"
36 #define VERBS_SPEC_INNER(item_flags) \
37 (!!((item_flags) & MLX5_FLOW_LAYER_TUNNEL) ? IBV_FLOW_SPEC_INNER : 0)
40 * Create Verbs flow counter with Verbs library.
43 * Pointer to the Ethernet device structure.
44 * @param[in, out] counter
45 * mlx5 flow counter object, contains the counter id,
46 * handle of created Verbs flow counter is returned
47 * in cs field (if counters are supported).
50 * 0 On success else a negative errno value is returned
51 * and rte_errno is set.
54 flow_verbs_counter_create(struct rte_eth_dev *dev,
55 struct mlx5_flow_counter *counter)
57 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42)
58 struct mlx5_priv *priv = dev->data->dev_private;
59 struct ibv_context *ctx = priv->sh->ctx;
60 struct ibv_counter_set_init_attr init = {
61 .counter_set_id = counter->id};
63 counter->cs = mlx5_glue->create_counter_set(ctx, &init);
69 #elif defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
70 struct mlx5_priv *priv = dev->data->dev_private;
71 struct ibv_context *ctx = priv->sh->ctx;
72 struct ibv_counters_init_attr init = {0};
73 struct ibv_counter_attach_attr attach;
76 memset(&attach, 0, sizeof(attach));
77 counter->cs = mlx5_glue->create_counters(ctx, &init);
82 attach.counter_desc = IBV_COUNTER_PACKETS;
84 ret = mlx5_glue->attach_counters(counter->cs, &attach, NULL);
86 attach.counter_desc = IBV_COUNTER_BYTES;
88 ret = mlx5_glue->attach_counters
89 (counter->cs, &attach, NULL);
92 claim_zero(mlx5_glue->destroy_counters(counter->cs));
107 * Get a flow counter.
110 * Pointer to the Ethernet device structure.
112 * Indicate if this counter is shared with other flows.
114 * Counter identifier.
117 * A pointer to the counter, NULL otherwise and rte_errno is set.
119 static struct mlx5_flow_counter *
120 flow_verbs_counter_new(struct rte_eth_dev *dev, uint32_t shared, uint32_t id)
122 struct mlx5_priv *priv = dev->data->dev_private;
123 struct mlx5_flow_counter *cnt;
127 TAILQ_FOREACH(cnt, &priv->sh->cmng.flow_counters, next) {
128 if (cnt->shared && cnt->id == id) {
134 cnt = rte_calloc(__func__, 1, sizeof(*cnt), 0);
140 cnt->shared = shared;
144 /* Create counter with Verbs. */
145 ret = flow_verbs_counter_create(dev, cnt);
147 TAILQ_INSERT_HEAD(&priv->sh->cmng.flow_counters, cnt, next);
150 /* Some error occurred in Verbs library. */
157 * Release a flow counter.
160 * Pointer to the Ethernet device structure.
162 * Pointer to the counter handler.
165 flow_verbs_counter_release(struct rte_eth_dev *dev,
166 struct mlx5_flow_counter *counter)
168 struct mlx5_priv *priv = dev->data->dev_private;
170 if (--counter->ref_cnt == 0) {
171 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42)
172 claim_zero(mlx5_glue->destroy_counter_set(counter->cs));
173 #elif defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
174 claim_zero(mlx5_glue->destroy_counters(counter->cs));
176 TAILQ_REMOVE(&priv->sh->cmng.flow_counters, counter, next);
182 * Query a flow counter via Verbs library call.
184 * @see rte_flow_query()
188 flow_verbs_counter_query(struct rte_eth_dev *dev __rte_unused,
189 struct rte_flow *flow, void *data,
190 struct rte_flow_error *error)
192 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42) || \
193 defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
194 if (flow->actions & MLX5_FLOW_ACTION_COUNT) {
195 struct rte_flow_query_count *qc = data;
196 uint64_t counters[2] = {0, 0};
197 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42)
198 struct ibv_query_counter_set_attr query_cs_attr = {
199 .cs = flow->counter->cs,
200 .query_flags = IBV_COUNTER_SET_FORCE_UPDATE,
202 struct ibv_counter_set_data query_out = {
204 .outlen = 2 * sizeof(uint64_t),
206 int err = mlx5_glue->query_counter_set(&query_cs_attr,
208 #elif defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
209 int err = mlx5_glue->query_counters
210 (flow->counter->cs, counters,
212 IBV_READ_COUNTERS_ATTR_PREFER_CACHED);
215 return rte_flow_error_set
217 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
219 "cannot read counter");
222 qc->hits = counters[0] - flow->counter->hits;
223 qc->bytes = counters[1] - flow->counter->bytes;
225 flow->counter->hits = counters[0];
226 flow->counter->bytes = counters[1];
230 return rte_flow_error_set(error, EINVAL,
231 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
233 "flow does not have counter");
237 return rte_flow_error_set(error, ENOTSUP,
238 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
240 "counters are not available");
245 * Add a verbs item specification into @p verbs.
248 * Pointer to verbs structure.
250 * Create specification.
252 * Size in bytes of the specification to copy.
255 flow_verbs_spec_add(struct mlx5_flow_verbs *verbs, void *src, unsigned int size)
261 assert(verbs->specs);
262 dst = (void *)(verbs->specs + verbs->size);
263 memcpy(dst, src, size);
264 ++verbs->attr->num_of_specs;
269 * Convert the @p item into a Verbs specification. This function assumes that
270 * the input is valid and that there is space to insert the requested item
273 * @param[in, out] dev_flow
274 * Pointer to dev_flow structure.
276 * Item specification.
277 * @param[in] item_flags
281 flow_verbs_translate_item_eth(struct mlx5_flow *dev_flow,
282 const struct rte_flow_item *item,
285 const struct rte_flow_item_eth *spec = item->spec;
286 const struct rte_flow_item_eth *mask = item->mask;
287 const unsigned int size = sizeof(struct ibv_flow_spec_eth);
288 struct ibv_flow_spec_eth eth = {
289 .type = IBV_FLOW_SPEC_ETH | VERBS_SPEC_INNER(item_flags),
294 mask = &rte_flow_item_eth_mask;
298 memcpy(ð.val.dst_mac, spec->dst.addr_bytes,
300 memcpy(ð.val.src_mac, spec->src.addr_bytes,
302 eth.val.ether_type = spec->type;
303 memcpy(ð.mask.dst_mac, mask->dst.addr_bytes,
305 memcpy(ð.mask.src_mac, mask->src.addr_bytes,
307 eth.mask.ether_type = mask->type;
308 /* Remove unwanted bits from values. */
309 for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i) {
310 eth.val.dst_mac[i] &= eth.mask.dst_mac[i];
311 eth.val.src_mac[i] &= eth.mask.src_mac[i];
313 eth.val.ether_type &= eth.mask.ether_type;
315 flow_verbs_spec_add(&dev_flow->verbs, ð, size);
319 * Update the VLAN tag in the Verbs Ethernet specification.
320 * This function assumes that the input is valid and there is space to add
321 * the requested item.
323 * @param[in, out] attr
324 * Pointer to Verbs attributes structure.
326 * Verbs structure containing the VLAN information to copy.
329 flow_verbs_item_vlan_update(struct ibv_flow_attr *attr,
330 struct ibv_flow_spec_eth *eth)
333 const enum ibv_flow_spec_type search = eth->type;
334 struct ibv_spec_header *hdr = (struct ibv_spec_header *)
335 ((uint8_t *)attr + sizeof(struct ibv_flow_attr));
337 for (i = 0; i != attr->num_of_specs; ++i) {
338 if (hdr->type == search) {
339 struct ibv_flow_spec_eth *e =
340 (struct ibv_flow_spec_eth *)hdr;
342 e->val.vlan_tag = eth->val.vlan_tag;
343 e->mask.vlan_tag = eth->mask.vlan_tag;
344 e->val.ether_type = eth->val.ether_type;
345 e->mask.ether_type = eth->mask.ether_type;
348 hdr = (struct ibv_spec_header *)((uint8_t *)hdr + hdr->size);
353 * Convert the @p item into a Verbs specification. This function assumes that
354 * the input is valid and that there is space to insert the requested item
357 * @param[in, out] dev_flow
358 * Pointer to dev_flow structure.
360 * Item specification.
361 * @param[in] item_flags
365 flow_verbs_translate_item_vlan(struct mlx5_flow *dev_flow,
366 const struct rte_flow_item *item,
369 const struct rte_flow_item_vlan *spec = item->spec;
370 const struct rte_flow_item_vlan *mask = item->mask;
371 unsigned int size = sizeof(struct ibv_flow_spec_eth);
372 const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
373 struct ibv_flow_spec_eth eth = {
374 .type = IBV_FLOW_SPEC_ETH | VERBS_SPEC_INNER(item_flags),
377 const uint32_t l2m = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
378 MLX5_FLOW_LAYER_OUTER_L2;
381 mask = &rte_flow_item_vlan_mask;
383 eth.val.vlan_tag = spec->tci;
384 eth.mask.vlan_tag = mask->tci;
385 eth.val.vlan_tag &= eth.mask.vlan_tag;
386 eth.val.ether_type = spec->inner_type;
387 eth.mask.ether_type = mask->inner_type;
388 eth.val.ether_type &= eth.mask.ether_type;
390 if (!(item_flags & l2m))
391 flow_verbs_spec_add(&dev_flow->verbs, ð, size);
393 flow_verbs_item_vlan_update(dev_flow->verbs.attr, ð);
395 dev_flow->verbs.vf_vlan.tag =
396 rte_be_to_cpu_16(spec->tci) & 0x0fff;
400 * Convert the @p item into a Verbs specification. This function assumes that
401 * the input is valid and that there is space to insert the requested item
404 * @param[in, out] dev_flow
405 * Pointer to dev_flow structure.
407 * Item specification.
408 * @param[in] item_flags
412 flow_verbs_translate_item_ipv4(struct mlx5_flow *dev_flow,
413 const struct rte_flow_item *item,
416 const struct rte_flow_item_ipv4 *spec = item->spec;
417 const struct rte_flow_item_ipv4 *mask = item->mask;
418 unsigned int size = sizeof(struct ibv_flow_spec_ipv4_ext);
419 struct ibv_flow_spec_ipv4_ext ipv4 = {
420 .type = IBV_FLOW_SPEC_IPV4_EXT | VERBS_SPEC_INNER(item_flags),
425 mask = &rte_flow_item_ipv4_mask;
427 ipv4.val = (struct ibv_flow_ipv4_ext_filter){
428 .src_ip = spec->hdr.src_addr,
429 .dst_ip = spec->hdr.dst_addr,
430 .proto = spec->hdr.next_proto_id,
431 .tos = spec->hdr.type_of_service,
433 ipv4.mask = (struct ibv_flow_ipv4_ext_filter){
434 .src_ip = mask->hdr.src_addr,
435 .dst_ip = mask->hdr.dst_addr,
436 .proto = mask->hdr.next_proto_id,
437 .tos = mask->hdr.type_of_service,
439 /* Remove unwanted bits from values. */
440 ipv4.val.src_ip &= ipv4.mask.src_ip;
441 ipv4.val.dst_ip &= ipv4.mask.dst_ip;
442 ipv4.val.proto &= ipv4.mask.proto;
443 ipv4.val.tos &= ipv4.mask.tos;
445 flow_verbs_spec_add(&dev_flow->verbs, &ipv4, size);
449 * Convert the @p item into a Verbs specification. This function assumes that
450 * the input is valid and that there is space to insert the requested item
453 * @param[in, out] dev_flow
454 * Pointer to dev_flow structure.
456 * Item specification.
457 * @param[in] item_flags
461 flow_verbs_translate_item_ipv6(struct mlx5_flow *dev_flow,
462 const struct rte_flow_item *item,
465 const struct rte_flow_item_ipv6 *spec = item->spec;
466 const struct rte_flow_item_ipv6 *mask = item->mask;
467 unsigned int size = sizeof(struct ibv_flow_spec_ipv6);
468 struct ibv_flow_spec_ipv6 ipv6 = {
469 .type = IBV_FLOW_SPEC_IPV6 | VERBS_SPEC_INNER(item_flags),
474 mask = &rte_flow_item_ipv6_mask;
477 uint32_t vtc_flow_val;
478 uint32_t vtc_flow_mask;
480 memcpy(&ipv6.val.src_ip, spec->hdr.src_addr,
481 RTE_DIM(ipv6.val.src_ip));
482 memcpy(&ipv6.val.dst_ip, spec->hdr.dst_addr,
483 RTE_DIM(ipv6.val.dst_ip));
484 memcpy(&ipv6.mask.src_ip, mask->hdr.src_addr,
485 RTE_DIM(ipv6.mask.src_ip));
486 memcpy(&ipv6.mask.dst_ip, mask->hdr.dst_addr,
487 RTE_DIM(ipv6.mask.dst_ip));
488 vtc_flow_val = rte_be_to_cpu_32(spec->hdr.vtc_flow);
489 vtc_flow_mask = rte_be_to_cpu_32(mask->hdr.vtc_flow);
490 ipv6.val.flow_label =
491 rte_cpu_to_be_32((vtc_flow_val & RTE_IPV6_HDR_FL_MASK) >>
492 RTE_IPV6_HDR_FL_SHIFT);
493 ipv6.val.traffic_class = (vtc_flow_val & RTE_IPV6_HDR_TC_MASK) >>
494 RTE_IPV6_HDR_TC_SHIFT;
495 ipv6.val.next_hdr = spec->hdr.proto;
496 ipv6.val.hop_limit = spec->hdr.hop_limits;
497 ipv6.mask.flow_label =
498 rte_cpu_to_be_32((vtc_flow_mask & RTE_IPV6_HDR_FL_MASK) >>
499 RTE_IPV6_HDR_FL_SHIFT);
500 ipv6.mask.traffic_class = (vtc_flow_mask & RTE_IPV6_HDR_TC_MASK) >>
501 RTE_IPV6_HDR_TC_SHIFT;
502 ipv6.mask.next_hdr = mask->hdr.proto;
503 ipv6.mask.hop_limit = mask->hdr.hop_limits;
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;
512 ipv6.val.hop_limit &= ipv6.mask.hop_limit;
514 flow_verbs_spec_add(&dev_flow->verbs, &ipv6, size);
518 * Convert the @p item into a Verbs specification. This function assumes that
519 * the input is valid and that there is space to insert the requested item
522 * @param[in, out] dev_flow
523 * Pointer to dev_flow structure.
525 * Item specification.
526 * @param[in] item_flags
530 flow_verbs_translate_item_tcp(struct mlx5_flow *dev_flow,
531 const struct rte_flow_item *item,
532 uint64_t item_flags __rte_unused)
534 const struct rte_flow_item_tcp *spec = item->spec;
535 const struct rte_flow_item_tcp *mask = item->mask;
536 unsigned int size = sizeof(struct ibv_flow_spec_tcp_udp);
537 struct ibv_flow_spec_tcp_udp tcp = {
538 .type = IBV_FLOW_SPEC_TCP | VERBS_SPEC_INNER(item_flags),
543 mask = &rte_flow_item_tcp_mask;
545 tcp.val.dst_port = spec->hdr.dst_port;
546 tcp.val.src_port = spec->hdr.src_port;
547 tcp.mask.dst_port = mask->hdr.dst_port;
548 tcp.mask.src_port = mask->hdr.src_port;
549 /* Remove unwanted bits from values. */
550 tcp.val.src_port &= tcp.mask.src_port;
551 tcp.val.dst_port &= tcp.mask.dst_port;
553 flow_verbs_spec_add(&dev_flow->verbs, &tcp, size);
557 * Convert the @p item into a Verbs specification. This function assumes that
558 * the input is valid and that there is space to insert the requested item
561 * @param[in, out] dev_flow
562 * Pointer to dev_flow structure.
564 * Item specification.
565 * @param[in] item_flags
569 flow_verbs_translate_item_udp(struct mlx5_flow *dev_flow,
570 const struct rte_flow_item *item,
571 uint64_t item_flags __rte_unused)
573 const struct rte_flow_item_udp *spec = item->spec;
574 const struct rte_flow_item_udp *mask = item->mask;
575 unsigned int size = sizeof(struct ibv_flow_spec_tcp_udp);
576 struct ibv_flow_spec_tcp_udp udp = {
577 .type = IBV_FLOW_SPEC_UDP | VERBS_SPEC_INNER(item_flags),
582 mask = &rte_flow_item_udp_mask;
584 udp.val.dst_port = spec->hdr.dst_port;
585 udp.val.src_port = spec->hdr.src_port;
586 udp.mask.dst_port = mask->hdr.dst_port;
587 udp.mask.src_port = mask->hdr.src_port;
588 /* Remove unwanted bits from values. */
589 udp.val.src_port &= udp.mask.src_port;
590 udp.val.dst_port &= udp.mask.dst_port;
592 flow_verbs_spec_add(&dev_flow->verbs, &udp, size);
596 * Convert the @p item into a Verbs specification. This function assumes that
597 * the input is valid and that there is space to insert the requested item
600 * @param[in, out] dev_flow
601 * Pointer to dev_flow structure.
603 * Item specification.
604 * @param[in] item_flags
608 flow_verbs_translate_item_vxlan(struct mlx5_flow *dev_flow,
609 const struct rte_flow_item *item,
610 uint64_t item_flags __rte_unused)
612 const struct rte_flow_item_vxlan *spec = item->spec;
613 const struct rte_flow_item_vxlan *mask = item->mask;
614 unsigned int size = sizeof(struct ibv_flow_spec_tunnel);
615 struct ibv_flow_spec_tunnel vxlan = {
616 .type = IBV_FLOW_SPEC_VXLAN_TUNNEL,
622 } id = { .vlan_id = 0, };
625 mask = &rte_flow_item_vxlan_mask;
627 memcpy(&id.vni[1], spec->vni, 3);
628 vxlan.val.tunnel_id = id.vlan_id;
629 memcpy(&id.vni[1], mask->vni, 3);
630 vxlan.mask.tunnel_id = id.vlan_id;
631 /* Remove unwanted bits from values. */
632 vxlan.val.tunnel_id &= vxlan.mask.tunnel_id;
634 flow_verbs_spec_add(&dev_flow->verbs, &vxlan, size);
638 * Convert the @p item into a Verbs specification. This function assumes that
639 * the input is valid and that there is space to insert the requested item
642 * @param[in, out] dev_flow
643 * Pointer to dev_flow structure.
645 * Item specification.
646 * @param[in] item_flags
650 flow_verbs_translate_item_vxlan_gpe(struct mlx5_flow *dev_flow,
651 const struct rte_flow_item *item,
652 uint64_t item_flags __rte_unused)
654 const struct rte_flow_item_vxlan_gpe *spec = item->spec;
655 const struct rte_flow_item_vxlan_gpe *mask = item->mask;
656 unsigned int size = sizeof(struct ibv_flow_spec_tunnel);
657 struct ibv_flow_spec_tunnel vxlan_gpe = {
658 .type = IBV_FLOW_SPEC_VXLAN_TUNNEL,
664 } id = { .vlan_id = 0, };
667 mask = &rte_flow_item_vxlan_gpe_mask;
669 memcpy(&id.vni[1], spec->vni, 3);
670 vxlan_gpe.val.tunnel_id = id.vlan_id;
671 memcpy(&id.vni[1], mask->vni, 3);
672 vxlan_gpe.mask.tunnel_id = id.vlan_id;
673 /* Remove unwanted bits from values. */
674 vxlan_gpe.val.tunnel_id &= vxlan_gpe.mask.tunnel_id;
676 flow_verbs_spec_add(&dev_flow->verbs, &vxlan_gpe, size);
680 * Update the protocol in Verbs IPv4/IPv6 spec.
682 * @param[in, out] attr
683 * Pointer to Verbs attributes structure.
685 * Specification type to search in order to update the IP protocol.
686 * @param[in] protocol
687 * Protocol value to set if none is present in the specification.
690 flow_verbs_item_gre_ip_protocol_update(struct ibv_flow_attr *attr,
691 enum ibv_flow_spec_type search,
695 struct ibv_spec_header *hdr = (struct ibv_spec_header *)
696 ((uint8_t *)attr + sizeof(struct ibv_flow_attr));
700 for (i = 0; i != attr->num_of_specs; ++i) {
701 if (hdr->type == search) {
703 struct ibv_flow_spec_ipv4_ext *ipv4;
704 struct ibv_flow_spec_ipv6 *ipv6;
708 case IBV_FLOW_SPEC_IPV4_EXT:
709 ip.ipv4 = (struct ibv_flow_spec_ipv4_ext *)hdr;
710 if (!ip.ipv4->val.proto) {
711 ip.ipv4->val.proto = protocol;
712 ip.ipv4->mask.proto = 0xff;
715 case IBV_FLOW_SPEC_IPV6:
716 ip.ipv6 = (struct ibv_flow_spec_ipv6 *)hdr;
717 if (!ip.ipv6->val.next_hdr) {
718 ip.ipv6->val.next_hdr = protocol;
719 ip.ipv6->mask.next_hdr = 0xff;
727 hdr = (struct ibv_spec_header *)((uint8_t *)hdr + hdr->size);
732 * Convert the @p item into a Verbs specification. This function assumes that
733 * the input is valid and that there is space to insert the requested item
736 * @param[in, out] dev_flow
737 * Pointer to dev_flow structure.
739 * Item specification.
740 * @param[in] item_flags
744 flow_verbs_translate_item_gre(struct mlx5_flow *dev_flow,
745 const struct rte_flow_item *item __rte_unused,
748 struct mlx5_flow_verbs *verbs = &dev_flow->verbs;
749 #ifndef HAVE_IBV_DEVICE_MPLS_SUPPORT
750 unsigned int size = sizeof(struct ibv_flow_spec_tunnel);
751 struct ibv_flow_spec_tunnel tunnel = {
752 .type = IBV_FLOW_SPEC_VXLAN_TUNNEL,
756 const struct rte_flow_item_gre *spec = item->spec;
757 const struct rte_flow_item_gre *mask = item->mask;
758 unsigned int size = sizeof(struct ibv_flow_spec_gre);
759 struct ibv_flow_spec_gre tunnel = {
760 .type = IBV_FLOW_SPEC_GRE,
765 mask = &rte_flow_item_gre_mask;
767 tunnel.val.c_ks_res0_ver = spec->c_rsvd0_ver;
768 tunnel.val.protocol = spec->protocol;
769 tunnel.mask.c_ks_res0_ver = mask->c_rsvd0_ver;
770 tunnel.mask.protocol = mask->protocol;
771 /* Remove unwanted bits from values. */
772 tunnel.val.c_ks_res0_ver &= tunnel.mask.c_ks_res0_ver;
773 tunnel.val.protocol &= tunnel.mask.protocol;
774 tunnel.val.key &= tunnel.mask.key;
777 if (item_flags & MLX5_FLOW_LAYER_OUTER_L3_IPV4)
778 flow_verbs_item_gre_ip_protocol_update(verbs->attr,
779 IBV_FLOW_SPEC_IPV4_EXT,
782 flow_verbs_item_gre_ip_protocol_update(verbs->attr,
785 flow_verbs_spec_add(verbs, &tunnel, size);
789 * Convert the @p action into a Verbs specification. This function assumes that
790 * the input is valid and that there is space to insert the requested action
791 * into the flow. This function also return the action that was added.
793 * @param[in, out] dev_flow
794 * Pointer to dev_flow structure.
796 * Item specification.
797 * @param[in] item_flags
801 flow_verbs_translate_item_mpls(struct mlx5_flow *dev_flow __rte_unused,
802 const struct rte_flow_item *item __rte_unused,
803 uint64_t item_flags __rte_unused)
805 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
806 const struct rte_flow_item_mpls *spec = item->spec;
807 const struct rte_flow_item_mpls *mask = item->mask;
808 unsigned int size = sizeof(struct ibv_flow_spec_mpls);
809 struct ibv_flow_spec_mpls mpls = {
810 .type = IBV_FLOW_SPEC_MPLS,
815 mask = &rte_flow_item_mpls_mask;
817 memcpy(&mpls.val.label, spec, sizeof(mpls.val.label));
818 memcpy(&mpls.mask.label, mask, sizeof(mpls.mask.label));
819 /* Remove unwanted bits from values. */
820 mpls.val.label &= mpls.mask.label;
822 flow_verbs_spec_add(&dev_flow->verbs, &mpls, size);
827 * Convert the @p action into a Verbs specification. This function assumes that
828 * the input is valid and that there is space to insert the requested action
831 * @param[in] dev_flow
832 * Pointer to mlx5_flow.
834 * Action configuration.
837 flow_verbs_translate_action_drop
838 (struct mlx5_flow *dev_flow,
839 const struct rte_flow_action *action __rte_unused)
841 unsigned int size = sizeof(struct ibv_flow_spec_action_drop);
842 struct ibv_flow_spec_action_drop drop = {
843 .type = IBV_FLOW_SPEC_ACTION_DROP,
847 flow_verbs_spec_add(&dev_flow->verbs, &drop, size);
851 * Convert the @p action into a Verbs specification. This function assumes that
852 * the input is valid and that there is space to insert the requested action
855 * @param[in] dev_flow
856 * Pointer to mlx5_flow.
858 * Action configuration.
861 flow_verbs_translate_action_queue(struct mlx5_flow *dev_flow,
862 const struct rte_flow_action *action)
864 const struct rte_flow_action_queue *queue = action->conf;
865 struct rte_flow *flow = dev_flow->flow;
868 (*flow->queue)[0] = queue->index;
869 flow->rss.queue_num = 1;
873 * Convert the @p action into a Verbs specification. This function assumes that
874 * the input is valid and that there is space to insert the requested action
878 * Action configuration.
879 * @param[in, out] action_flags
880 * Pointer to the detected actions.
881 * @param[in] dev_flow
882 * Pointer to mlx5_flow.
885 flow_verbs_translate_action_rss(struct mlx5_flow *dev_flow,
886 const struct rte_flow_action *action)
888 const struct rte_flow_action_rss *rss = action->conf;
889 const uint8_t *rss_key;
890 struct rte_flow *flow = dev_flow->flow;
893 memcpy((*flow->queue), rss->queue,
894 rss->queue_num * sizeof(uint16_t));
895 flow->rss.queue_num = rss->queue_num;
896 /* NULL RSS key indicates default RSS key. */
897 rss_key = !rss->key ? rss_hash_default_key : rss->key;
898 memcpy(flow->key, rss_key, MLX5_RSS_HASH_KEY_LEN);
899 /* RSS type 0 indicates default RSS type (ETH_RSS_IP). */
900 flow->rss.types = !rss->types ? ETH_RSS_IP : rss->types;
901 flow->rss.level = rss->level;
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;
1043 ret = mlx5_flow_validate_attributes(dev, attr, error);
1046 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
1047 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1050 switch (items->type) {
1051 case RTE_FLOW_ITEM_TYPE_VOID:
1053 case RTE_FLOW_ITEM_TYPE_ETH:
1054 ret = mlx5_flow_validate_item_eth(items, item_flags,
1058 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
1059 MLX5_FLOW_LAYER_OUTER_L2;
1061 case RTE_FLOW_ITEM_TYPE_VLAN:
1062 ret = mlx5_flow_validate_item_vlan(items, item_flags,
1066 last_item = tunnel ? (MLX5_FLOW_LAYER_INNER_L2 |
1067 MLX5_FLOW_LAYER_INNER_VLAN) :
1068 (MLX5_FLOW_LAYER_OUTER_L2 |
1069 MLX5_FLOW_LAYER_OUTER_VLAN);
1071 case RTE_FLOW_ITEM_TYPE_IPV4:
1072 ret = mlx5_flow_validate_item_ipv4(items, item_flags,
1076 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
1077 MLX5_FLOW_LAYER_OUTER_L3_IPV4;
1078 if (items->mask != NULL &&
1079 ((const struct rte_flow_item_ipv4 *)
1080 items->mask)->hdr.next_proto_id) {
1082 ((const struct rte_flow_item_ipv4 *)
1083 (items->spec))->hdr.next_proto_id;
1085 ((const struct rte_flow_item_ipv4 *)
1086 (items->mask))->hdr.next_proto_id;
1088 /* Reset for inner layer. */
1089 next_protocol = 0xff;
1092 case RTE_FLOW_ITEM_TYPE_IPV6:
1093 ret = mlx5_flow_validate_item_ipv6(items, item_flags,
1097 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
1098 MLX5_FLOW_LAYER_OUTER_L3_IPV6;
1099 if (items->mask != NULL &&
1100 ((const struct rte_flow_item_ipv6 *)
1101 items->mask)->hdr.proto) {
1103 ((const struct rte_flow_item_ipv6 *)
1104 items->spec)->hdr.proto;
1106 ((const struct rte_flow_item_ipv6 *)
1107 items->mask)->hdr.proto;
1109 /* Reset for inner layer. */
1110 next_protocol = 0xff;
1113 case RTE_FLOW_ITEM_TYPE_UDP:
1114 ret = mlx5_flow_validate_item_udp(items, item_flags,
1119 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
1120 MLX5_FLOW_LAYER_OUTER_L4_UDP;
1122 case RTE_FLOW_ITEM_TYPE_TCP:
1123 ret = mlx5_flow_validate_item_tcp
1126 &rte_flow_item_tcp_mask,
1130 last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
1131 MLX5_FLOW_LAYER_OUTER_L4_TCP;
1133 case RTE_FLOW_ITEM_TYPE_VXLAN:
1134 ret = mlx5_flow_validate_item_vxlan(items, item_flags,
1138 last_item = MLX5_FLOW_LAYER_VXLAN;
1140 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
1141 ret = mlx5_flow_validate_item_vxlan_gpe(items,
1146 last_item = MLX5_FLOW_LAYER_VXLAN_GPE;
1148 case RTE_FLOW_ITEM_TYPE_GRE:
1149 ret = mlx5_flow_validate_item_gre(items, item_flags,
1150 next_protocol, error);
1153 last_item = MLX5_FLOW_LAYER_GRE;
1155 case RTE_FLOW_ITEM_TYPE_MPLS:
1156 ret = mlx5_flow_validate_item_mpls(dev, items,
1161 last_item = MLX5_FLOW_LAYER_MPLS;
1164 return rte_flow_error_set(error, ENOTSUP,
1165 RTE_FLOW_ERROR_TYPE_ITEM,
1166 NULL, "item not supported");
1168 item_flags |= last_item;
1170 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1171 switch (actions->type) {
1172 case RTE_FLOW_ACTION_TYPE_VOID:
1174 case RTE_FLOW_ACTION_TYPE_FLAG:
1175 ret = mlx5_flow_validate_action_flag(action_flags,
1180 action_flags |= MLX5_FLOW_ACTION_FLAG;
1182 case RTE_FLOW_ACTION_TYPE_MARK:
1183 ret = mlx5_flow_validate_action_mark(actions,
1189 action_flags |= MLX5_FLOW_ACTION_MARK;
1191 case RTE_FLOW_ACTION_TYPE_DROP:
1192 ret = mlx5_flow_validate_action_drop(action_flags,
1197 action_flags |= MLX5_FLOW_ACTION_DROP;
1199 case RTE_FLOW_ACTION_TYPE_QUEUE:
1200 ret = mlx5_flow_validate_action_queue(actions,
1206 action_flags |= MLX5_FLOW_ACTION_QUEUE;
1208 case RTE_FLOW_ACTION_TYPE_RSS:
1209 ret = mlx5_flow_validate_action_rss(actions,
1215 action_flags |= MLX5_FLOW_ACTION_RSS;
1217 case RTE_FLOW_ACTION_TYPE_COUNT:
1218 ret = mlx5_flow_validate_action_count(dev, attr, error);
1221 action_flags |= MLX5_FLOW_ACTION_COUNT;
1224 return rte_flow_error_set(error, ENOTSUP,
1225 RTE_FLOW_ERROR_TYPE_ACTION,
1227 "action not supported");
1230 if (!(action_flags & MLX5_FLOW_FATE_ACTIONS))
1231 return rte_flow_error_set(error, EINVAL,
1232 RTE_FLOW_ERROR_TYPE_ACTION, actions,
1233 "no fate action is found");
1238 * Calculate the required bytes that are needed for the action part of the verbs
1241 * @param[in] actions
1242 * Pointer to the list of actions.
1245 * The size of the memory needed for all actions.
1248 flow_verbs_get_actions_size(const struct rte_flow_action actions[])
1252 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1253 switch (actions->type) {
1254 case RTE_FLOW_ACTION_TYPE_VOID:
1256 case RTE_FLOW_ACTION_TYPE_FLAG:
1257 size += sizeof(struct ibv_flow_spec_action_tag);
1259 case RTE_FLOW_ACTION_TYPE_MARK:
1260 size += sizeof(struct ibv_flow_spec_action_tag);
1262 case RTE_FLOW_ACTION_TYPE_DROP:
1263 size += sizeof(struct ibv_flow_spec_action_drop);
1265 case RTE_FLOW_ACTION_TYPE_QUEUE:
1267 case RTE_FLOW_ACTION_TYPE_RSS:
1269 case RTE_FLOW_ACTION_TYPE_COUNT:
1270 #if defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42) || \
1271 defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
1272 size += sizeof(struct ibv_flow_spec_counter_action);
1283 * Calculate the required bytes that are needed for the item part of the verbs
1287 * Pointer to the list of items.
1290 * The size of the memory needed for all items.
1293 flow_verbs_get_items_size(const struct rte_flow_item items[])
1297 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
1298 switch (items->type) {
1299 case RTE_FLOW_ITEM_TYPE_VOID:
1301 case RTE_FLOW_ITEM_TYPE_ETH:
1302 size += sizeof(struct ibv_flow_spec_eth);
1304 case RTE_FLOW_ITEM_TYPE_VLAN:
1305 size += sizeof(struct ibv_flow_spec_eth);
1307 case RTE_FLOW_ITEM_TYPE_IPV4:
1308 size += sizeof(struct ibv_flow_spec_ipv4_ext);
1310 case RTE_FLOW_ITEM_TYPE_IPV6:
1311 size += sizeof(struct ibv_flow_spec_ipv6);
1313 case RTE_FLOW_ITEM_TYPE_UDP:
1314 size += sizeof(struct ibv_flow_spec_tcp_udp);
1316 case RTE_FLOW_ITEM_TYPE_TCP:
1317 size += sizeof(struct ibv_flow_spec_tcp_udp);
1319 case RTE_FLOW_ITEM_TYPE_VXLAN:
1320 size += sizeof(struct ibv_flow_spec_tunnel);
1322 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
1323 size += sizeof(struct ibv_flow_spec_tunnel);
1325 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
1326 case RTE_FLOW_ITEM_TYPE_GRE:
1327 size += sizeof(struct ibv_flow_spec_gre);
1329 case RTE_FLOW_ITEM_TYPE_MPLS:
1330 size += sizeof(struct ibv_flow_spec_mpls);
1333 case RTE_FLOW_ITEM_TYPE_GRE:
1334 size += sizeof(struct ibv_flow_spec_tunnel);
1345 * Internal preparation function. Allocate mlx5_flow with the required size.
1346 * The required size is calculate based on the actions and items. This function
1347 * also returns the detected actions and items for later use.
1350 * Pointer to the flow attributes.
1352 * Pointer to the list of items.
1353 * @param[in] actions
1354 * Pointer to the list of actions.
1356 * Pointer to the error structure.
1359 * Pointer to mlx5_flow object on success, otherwise NULL and rte_errno
1362 static struct mlx5_flow *
1363 flow_verbs_prepare(const struct rte_flow_attr *attr __rte_unused,
1364 const struct rte_flow_item items[],
1365 const struct rte_flow_action actions[],
1366 struct rte_flow_error *error)
1368 uint32_t size = sizeof(struct mlx5_flow) + sizeof(struct ibv_flow_attr);
1369 struct mlx5_flow *flow;
1371 size += flow_verbs_get_actions_size(actions);
1372 size += flow_verbs_get_items_size(items);
1373 flow = rte_calloc(__func__, 1, size, 0);
1375 rte_flow_error_set(error, ENOMEM,
1376 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1377 "not enough memory to create flow");
1380 flow->verbs.attr = (void *)(flow + 1);
1382 (uint8_t *)(flow + 1) + sizeof(struct ibv_flow_attr);
1387 * Fill the flow with verb spec.
1390 * Pointer to Ethernet device.
1391 * @param[in, out] dev_flow
1392 * Pointer to the mlx5 flow.
1394 * Pointer to the flow attributes.
1396 * Pointer to the list of items.
1397 * @param[in] actions
1398 * Pointer to the list of actions.
1400 * Pointer to the error structure.
1403 * 0 on success, else a negative errno value otherwise and rte_errno is set.
1406 flow_verbs_translate(struct rte_eth_dev *dev,
1407 struct mlx5_flow *dev_flow,
1408 const struct rte_flow_attr *attr,
1409 const struct rte_flow_item items[],
1410 const struct rte_flow_action actions[],
1411 struct rte_flow_error *error)
1413 struct rte_flow *flow = dev_flow->flow;
1414 uint64_t item_flags = 0;
1415 uint64_t action_flags = 0;
1416 uint64_t priority = attr->priority;
1417 uint32_t subpriority = 0;
1418 struct mlx5_priv *priv = dev->data->dev_private;
1420 if (priority == MLX5_FLOW_PRIO_RSVD)
1421 priority = priv->config.flow_prio - 1;
1422 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1425 switch (actions->type) {
1426 case RTE_FLOW_ACTION_TYPE_VOID:
1428 case RTE_FLOW_ACTION_TYPE_FLAG:
1429 flow_verbs_translate_action_flag(dev_flow, actions);
1430 action_flags |= MLX5_FLOW_ACTION_FLAG;
1432 case RTE_FLOW_ACTION_TYPE_MARK:
1433 flow_verbs_translate_action_mark(dev_flow, actions);
1434 action_flags |= MLX5_FLOW_ACTION_MARK;
1436 case RTE_FLOW_ACTION_TYPE_DROP:
1437 flow_verbs_translate_action_drop(dev_flow, actions);
1438 action_flags |= MLX5_FLOW_ACTION_DROP;
1440 case RTE_FLOW_ACTION_TYPE_QUEUE:
1441 flow_verbs_translate_action_queue(dev_flow, actions);
1442 action_flags |= MLX5_FLOW_ACTION_QUEUE;
1444 case RTE_FLOW_ACTION_TYPE_RSS:
1445 flow_verbs_translate_action_rss(dev_flow, actions);
1446 action_flags |= MLX5_FLOW_ACTION_RSS;
1448 case RTE_FLOW_ACTION_TYPE_COUNT:
1449 ret = flow_verbs_translate_action_count(dev_flow,
1454 action_flags |= MLX5_FLOW_ACTION_COUNT;
1457 return rte_flow_error_set(error, ENOTSUP,
1458 RTE_FLOW_ERROR_TYPE_ACTION,
1460 "action not supported");
1463 flow->actions = action_flags;
1464 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
1465 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1467 switch (items->type) {
1468 case RTE_FLOW_ITEM_TYPE_VOID:
1470 case RTE_FLOW_ITEM_TYPE_ETH:
1471 flow_verbs_translate_item_eth(dev_flow, items,
1473 subpriority = MLX5_PRIORITY_MAP_L2;
1474 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
1475 MLX5_FLOW_LAYER_OUTER_L2;
1477 case RTE_FLOW_ITEM_TYPE_VLAN:
1478 flow_verbs_translate_item_vlan(dev_flow, items,
1480 subpriority = MLX5_PRIORITY_MAP_L2;
1481 item_flags |= tunnel ? (MLX5_FLOW_LAYER_INNER_L2 |
1482 MLX5_FLOW_LAYER_INNER_VLAN) :
1483 (MLX5_FLOW_LAYER_OUTER_L2 |
1484 MLX5_FLOW_LAYER_OUTER_VLAN);
1486 case RTE_FLOW_ITEM_TYPE_IPV4:
1487 flow_verbs_translate_item_ipv4(dev_flow, items,
1489 subpriority = MLX5_PRIORITY_MAP_L3;
1490 dev_flow->verbs.hash_fields |=
1491 mlx5_flow_hashfields_adjust
1493 MLX5_IPV4_LAYER_TYPES,
1494 MLX5_IPV4_IBV_RX_HASH);
1495 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
1496 MLX5_FLOW_LAYER_OUTER_L3_IPV4;
1498 case RTE_FLOW_ITEM_TYPE_IPV6:
1499 flow_verbs_translate_item_ipv6(dev_flow, items,
1501 subpriority = MLX5_PRIORITY_MAP_L3;
1502 dev_flow->verbs.hash_fields |=
1503 mlx5_flow_hashfields_adjust
1505 MLX5_IPV6_LAYER_TYPES,
1506 MLX5_IPV6_IBV_RX_HASH);
1507 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
1508 MLX5_FLOW_LAYER_OUTER_L3_IPV6;
1510 case RTE_FLOW_ITEM_TYPE_TCP:
1511 flow_verbs_translate_item_tcp(dev_flow, items,
1513 subpriority = MLX5_PRIORITY_MAP_L4;
1514 dev_flow->verbs.hash_fields |=
1515 mlx5_flow_hashfields_adjust
1516 (dev_flow, tunnel, ETH_RSS_TCP,
1517 (IBV_RX_HASH_SRC_PORT_TCP |
1518 IBV_RX_HASH_DST_PORT_TCP));
1519 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
1520 MLX5_FLOW_LAYER_OUTER_L4_TCP;
1522 case RTE_FLOW_ITEM_TYPE_UDP:
1523 flow_verbs_translate_item_udp(dev_flow, items,
1525 subpriority = MLX5_PRIORITY_MAP_L4;
1526 dev_flow->verbs.hash_fields |=
1527 mlx5_flow_hashfields_adjust
1528 (dev_flow, tunnel, ETH_RSS_UDP,
1529 (IBV_RX_HASH_SRC_PORT_UDP |
1530 IBV_RX_HASH_DST_PORT_UDP));
1531 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
1532 MLX5_FLOW_LAYER_OUTER_L4_UDP;
1534 case RTE_FLOW_ITEM_TYPE_VXLAN:
1535 flow_verbs_translate_item_vxlan(dev_flow, items,
1537 subpriority = MLX5_PRIORITY_MAP_L2;
1538 item_flags |= MLX5_FLOW_LAYER_VXLAN;
1540 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
1541 flow_verbs_translate_item_vxlan_gpe(dev_flow, items,
1543 subpriority = MLX5_PRIORITY_MAP_L2;
1544 item_flags |= MLX5_FLOW_LAYER_VXLAN_GPE;
1546 case RTE_FLOW_ITEM_TYPE_GRE:
1547 flow_verbs_translate_item_gre(dev_flow, items,
1549 subpriority = MLX5_PRIORITY_MAP_L2;
1550 item_flags |= MLX5_FLOW_LAYER_GRE;
1552 case RTE_FLOW_ITEM_TYPE_MPLS:
1553 flow_verbs_translate_item_mpls(dev_flow, items,
1555 subpriority = MLX5_PRIORITY_MAP_L2;
1556 item_flags |= MLX5_FLOW_LAYER_MPLS;
1559 return rte_flow_error_set(error, ENOTSUP,
1560 RTE_FLOW_ERROR_TYPE_ITEM,
1562 "item not supported");
1565 dev_flow->layers = item_flags;
1566 dev_flow->verbs.attr->priority =
1567 mlx5_flow_adjust_priority(dev, priority, subpriority);
1568 dev_flow->verbs.attr->port = (uint8_t)priv->ibv_port;
1573 * Remove the flow from the NIC but keeps it in memory.
1576 * Pointer to the Ethernet device structure.
1577 * @param[in, out] flow
1578 * Pointer to flow structure.
1581 flow_verbs_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
1583 struct mlx5_flow_verbs *verbs;
1584 struct mlx5_flow *dev_flow;
1588 LIST_FOREACH(dev_flow, &flow->dev_flows, next) {
1589 verbs = &dev_flow->verbs;
1591 claim_zero(mlx5_glue->destroy_flow(verbs->flow));
1595 if (flow->actions & MLX5_FLOW_ACTION_DROP)
1596 mlx5_hrxq_drop_release(dev);
1598 mlx5_hrxq_release(dev, verbs->hrxq);
1601 if (dev_flow->verbs.vf_vlan.tag &&
1602 dev_flow->verbs.vf_vlan.created) {
1603 mlx5_vlan_vmwa_release(dev, &dev_flow->verbs.vf_vlan);
1609 * Remove the flow from the NIC and the memory.
1612 * Pointer to the Ethernet device structure.
1613 * @param[in, out] flow
1614 * Pointer to flow structure.
1617 flow_verbs_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
1619 struct mlx5_flow *dev_flow;
1623 flow_verbs_remove(dev, flow);
1624 while (!LIST_EMPTY(&flow->dev_flows)) {
1625 dev_flow = LIST_FIRST(&flow->dev_flows);
1626 LIST_REMOVE(dev_flow, next);
1629 if (flow->counter) {
1630 flow_verbs_counter_release(dev, flow->counter);
1631 flow->counter = NULL;
1636 * Apply the flow to the NIC.
1639 * Pointer to the Ethernet device structure.
1640 * @param[in, out] flow
1641 * Pointer to flow structure.
1643 * Pointer to error structure.
1646 * 0 on success, a negative errno value otherwise and rte_errno is set.
1649 flow_verbs_apply(struct rte_eth_dev *dev, struct rte_flow *flow,
1650 struct rte_flow_error *error)
1652 struct mlx5_priv *priv = dev->data->dev_private;
1653 struct mlx5_flow_verbs *verbs;
1654 struct mlx5_flow *dev_flow;
1657 LIST_FOREACH(dev_flow, &flow->dev_flows, next) {
1658 verbs = &dev_flow->verbs;
1659 if (flow->actions & MLX5_FLOW_ACTION_DROP) {
1660 verbs->hrxq = mlx5_hrxq_drop_new(dev);
1664 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1665 "cannot get drop hash queue");
1669 struct mlx5_hrxq *hrxq;
1671 hrxq = mlx5_hrxq_get(dev, flow->key,
1672 MLX5_RSS_HASH_KEY_LEN,
1675 flow->rss.queue_num);
1677 hrxq = mlx5_hrxq_new(dev, flow->key,
1678 MLX5_RSS_HASH_KEY_LEN,
1681 flow->rss.queue_num,
1682 !!(dev_flow->layers &
1683 MLX5_FLOW_LAYER_TUNNEL));
1687 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1688 "cannot get hash queue");
1693 verbs->flow = mlx5_glue->create_flow(verbs->hrxq->qp,
1696 rte_flow_error_set(error, errno,
1697 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1699 "hardware refuses to create flow");
1702 if (priv->vmwa_context &&
1703 dev_flow->verbs.vf_vlan.tag &&
1704 !dev_flow->verbs.vf_vlan.created) {
1706 * The rule contains the VLAN pattern.
1707 * For VF we are going to create VLAN
1708 * interface to make hypervisor set correct
1709 * e-Switch vport context.
1711 mlx5_vlan_vmwa_acquire(dev, &dev_flow->verbs.vf_vlan);
1716 err = rte_errno; /* Save rte_errno before cleanup. */
1717 LIST_FOREACH(dev_flow, &flow->dev_flows, next) {
1718 verbs = &dev_flow->verbs;
1720 if (flow->actions & MLX5_FLOW_ACTION_DROP)
1721 mlx5_hrxq_drop_release(dev);
1723 mlx5_hrxq_release(dev, verbs->hrxq);
1726 if (dev_flow->verbs.vf_vlan.tag &&
1727 dev_flow->verbs.vf_vlan.created) {
1728 mlx5_vlan_vmwa_release(dev, &dev_flow->verbs.vf_vlan);
1731 rte_errno = err; /* Restore rte_errno. */
1738 * @see rte_flow_query()
1742 flow_verbs_query(struct rte_eth_dev *dev,
1743 struct rte_flow *flow,
1744 const struct rte_flow_action *actions,
1746 struct rte_flow_error *error)
1750 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1751 switch (actions->type) {
1752 case RTE_FLOW_ACTION_TYPE_VOID:
1754 case RTE_FLOW_ACTION_TYPE_COUNT:
1755 ret = flow_verbs_counter_query(dev, flow, data, error);
1758 return rte_flow_error_set(error, ENOTSUP,
1759 RTE_FLOW_ERROR_TYPE_ACTION,
1761 "action not supported");
1767 const struct mlx5_flow_driver_ops mlx5_flow_verbs_drv_ops = {
1768 .validate = flow_verbs_validate,
1769 .prepare = flow_verbs_prepare,
1770 .translate = flow_verbs_translate,
1771 .apply = flow_verbs_apply,
1772 .remove = flow_verbs_remove,
1773 .destroy = flow_verbs_destroy,
1774 .query = flow_verbs_query,