1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2018 Mellanox Technologies, Ltd
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_eth_ctrl.h>
24 #include <rte_ethdev_driver.h>
26 #include <rte_flow_driver.h>
27 #include <rte_malloc.h>
31 #include "mlx5_defs.h"
33 #include "mlx5_glue.h"
34 #include "mlx5_flow.h"
36 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
39 * Verify the @p attributes will be correctly understood by the NIC and store
40 * them in the @p flow if everything is correct.
43 * Pointer to dev struct.
44 * @param[in] attributes
45 * Pointer to flow attributes
47 * Pointer to error structure.
50 * 0 on success, a negative errno value otherwise and rte_errno is set.
53 flow_dv_validate_attributes(struct rte_eth_dev *dev,
54 const struct rte_flow_attr *attributes,
55 struct rte_flow_error *error)
57 struct priv *priv = dev->data->dev_private;
58 uint32_t priority_max = priv->config.flow_prio - 1;
60 if (attributes->group)
61 return rte_flow_error_set(error, ENOTSUP,
62 RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
64 "groups is not supported");
65 if (attributes->priority != MLX5_FLOW_PRIO_RSVD &&
66 attributes->priority >= priority_max)
67 return rte_flow_error_set(error, ENOTSUP,
68 RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
70 "priority out of range");
71 if (attributes->egress)
72 return rte_flow_error_set(error, ENOTSUP,
73 RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
75 "egress is not supported");
76 if (attributes->transfer)
77 return rte_flow_error_set(error, ENOTSUP,
78 RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
80 "transfer is not supported");
81 if (!attributes->ingress)
82 return rte_flow_error_set(error, EINVAL,
83 RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
85 "ingress attribute is mandatory");
90 * Internal validation function. For validating both actions and items.
93 * Pointer to the rte_eth_dev structure.
95 * Pointer to the flow attributes.
97 * Pointer to the list of items.
99 * Pointer to the list of actions.
101 * Pointer to the error structure.
104 * 0 on success, a negative errno value otherwise and rte_ernno is set.
107 flow_dv_validate(struct rte_eth_dev *dev, const struct rte_flow_attr *attr,
108 const struct rte_flow_item items[],
109 const struct rte_flow_action actions[],
110 struct rte_flow_error *error)
113 uint32_t action_flags = 0;
114 uint32_t item_flags = 0;
116 uint8_t next_protocol = 0xff;
121 ret = flow_dv_validate_attributes(dev, attr, error);
124 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
125 switch (items->type) {
126 case RTE_FLOW_ITEM_TYPE_VOID:
128 case RTE_FLOW_ITEM_TYPE_ETH:
129 ret = mlx5_flow_validate_item_eth(items, item_flags,
133 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
134 MLX5_FLOW_LAYER_OUTER_L2;
136 case RTE_FLOW_ITEM_TYPE_VLAN:
137 ret = mlx5_flow_validate_item_vlan(items, item_flags,
141 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_VLAN :
142 MLX5_FLOW_LAYER_OUTER_VLAN;
144 case RTE_FLOW_ITEM_TYPE_IPV4:
145 ret = mlx5_flow_validate_item_ipv4(items, item_flags,
149 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
150 MLX5_FLOW_LAYER_OUTER_L3_IPV4;
151 if (items->mask != NULL &&
152 ((const struct rte_flow_item_ipv4 *)
153 items->mask)->hdr.next_proto_id)
155 ((const struct rte_flow_item_ipv4 *)
156 (items->spec))->hdr.next_proto_id;
158 case RTE_FLOW_ITEM_TYPE_IPV6:
159 ret = mlx5_flow_validate_item_ipv6(items, item_flags,
163 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
164 MLX5_FLOW_LAYER_OUTER_L3_IPV6;
165 if (items->mask != NULL &&
166 ((const struct rte_flow_item_ipv6 *)
167 items->mask)->hdr.proto)
169 ((const struct rte_flow_item_ipv6 *)
170 items->spec)->hdr.proto;
172 case RTE_FLOW_ITEM_TYPE_UDP:
173 ret = mlx5_flow_validate_item_udp(items, item_flags,
178 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
179 MLX5_FLOW_LAYER_OUTER_L4_UDP;
181 case RTE_FLOW_ITEM_TYPE_TCP:
182 ret = mlx5_flow_validate_item_tcp(items, item_flags,
183 next_protocol, error);
186 item_flags |= tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
187 MLX5_FLOW_LAYER_OUTER_L4_TCP;
189 case RTE_FLOW_ITEM_TYPE_VXLAN:
190 ret = mlx5_flow_validate_item_vxlan(items, item_flags,
194 item_flags |= MLX5_FLOW_LAYER_VXLAN;
196 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
197 ret = mlx5_flow_validate_item_vxlan_gpe(items,
202 item_flags |= MLX5_FLOW_LAYER_VXLAN_GPE;
204 case RTE_FLOW_ITEM_TYPE_GRE:
205 ret = mlx5_flow_validate_item_gre(items, item_flags,
206 next_protocol, error);
209 item_flags |= MLX5_FLOW_LAYER_GRE;
211 case RTE_FLOW_ITEM_TYPE_MPLS:
212 ret = mlx5_flow_validate_item_mpls(items, item_flags,
217 item_flags |= MLX5_FLOW_LAYER_MPLS;
220 return rte_flow_error_set(error, ENOTSUP,
221 RTE_FLOW_ERROR_TYPE_ITEM,
222 NULL, "item not supported");
225 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
226 if (actions_n == MLX5_DV_MAX_NUMBER_OF_ACTIONS)
227 return rte_flow_error_set(error, ENOTSUP,
228 RTE_FLOW_ERROR_TYPE_ACTION,
229 actions, "too many actions");
230 tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
231 switch (actions->type) {
232 case RTE_FLOW_ACTION_TYPE_VOID:
234 case RTE_FLOW_ACTION_TYPE_FLAG:
235 ret = mlx5_flow_validate_action_flag(action_flags,
239 action_flags |= MLX5_FLOW_ACTION_FLAG;
242 case RTE_FLOW_ACTION_TYPE_MARK:
243 ret = mlx5_flow_validate_action_mark(actions,
248 action_flags |= MLX5_FLOW_ACTION_MARK;
251 case RTE_FLOW_ACTION_TYPE_DROP:
252 ret = mlx5_flow_validate_action_drop(action_flags,
256 action_flags |= MLX5_FLOW_ACTION_DROP;
259 case RTE_FLOW_ACTION_TYPE_QUEUE:
260 ret = mlx5_flow_validate_action_queue(actions,
265 action_flags |= MLX5_FLOW_ACTION_QUEUE;
268 case RTE_FLOW_ACTION_TYPE_RSS:
269 ret = mlx5_flow_validate_action_rss(actions,
274 action_flags |= MLX5_FLOW_ACTION_RSS;
277 case RTE_FLOW_ACTION_TYPE_COUNT:
278 ret = mlx5_flow_validate_action_count(dev, error);
281 action_flags |= MLX5_FLOW_ACTION_COUNT;
285 return rte_flow_error_set(error, ENOTSUP,
286 RTE_FLOW_ERROR_TYPE_ACTION,
288 "action not supported");
291 if (!(action_flags & MLX5_FLOW_FATE_ACTIONS))
292 return rte_flow_error_set(error, EINVAL,
293 RTE_FLOW_ERROR_TYPE_ACTION, actions,
294 "no fate action is found");
299 * Internal preparation function. Allocates the DV flow size,
300 * this size is constant.
303 * Pointer to the flow attributes.
305 * Pointer to the list of items.
307 * Pointer to the list of actions.
308 * @param[out] item_flags
309 * Pointer to bit mask of all items detected.
310 * @param[out] action_flags
311 * Pointer to bit mask of all actions detected.
313 * Pointer to the error structure.
316 * Pointer to mlx5_flow object on success,
317 * otherwise NULL and rte_ernno is set.
319 static struct mlx5_flow *
320 flow_dv_prepare(const struct rte_flow_attr *attr __rte_unused,
321 const struct rte_flow_item items[] __rte_unused,
322 const struct rte_flow_action actions[] __rte_unused,
323 uint64_t *item_flags __rte_unused,
324 uint64_t *action_flags __rte_unused,
325 struct rte_flow_error *error)
327 uint32_t size = sizeof(struct mlx5_flow);
328 struct mlx5_flow *flow;
330 flow = rte_calloc(__func__, 1, size, 0);
332 rte_flow_error_set(error, ENOMEM,
333 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
334 "not enough memory to create flow");
337 flow->dv.value.size = MLX5_ST_SZ_DB(fte_match_param);
342 * Add Ethernet item to matcher and to the value.
344 * @param[in, out] matcher
346 * @param[in, out] key
347 * Flow matcher value.
349 * Flow pattern to translate.
351 * Item is inner pattern.
354 flow_dv_translate_item_eth(void *matcher, void *key,
355 const struct rte_flow_item *item, int inner)
357 const struct rte_flow_item_eth *eth_m = item->mask;
358 const struct rte_flow_item_eth *eth_v = item->spec;
359 const struct rte_flow_item_eth nic_mask = {
360 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
361 .src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
362 .type = RTE_BE16(0xffff),
374 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
376 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
378 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
380 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
382 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m, dmac_47_16),
383 ð_m->dst, sizeof(eth_m->dst));
384 /* The value must be in the range of the mask. */
385 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, dmac_47_16);
386 for (i = 0; i < sizeof(eth_m->dst); ++i)
387 l24_v[i] = eth_m->dst.addr_bytes[i] & eth_v->dst.addr_bytes[i];
388 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m, smac_47_16),
389 ð_m->src, sizeof(eth_m->src));
390 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, smac_47_16);
391 /* The value must be in the range of the mask. */
392 for (i = 0; i < sizeof(eth_m->dst); ++i)
393 l24_v[i] = eth_m->src.addr_bytes[i] & eth_v->src.addr_bytes[i];
394 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ethertype,
395 rte_be_to_cpu_16(eth_m->type));
396 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, ethertype);
397 *(uint16_t *)(l24_v) = eth_m->type & eth_v->type;
401 * Add VLAN item to matcher and to the value.
403 * @param[in, out] matcher
405 * @param[in, out] key
406 * Flow matcher value.
408 * Flow pattern to translate.
410 * Item is inner pattern.
413 flow_dv_translate_item_vlan(void *matcher, void *key,
414 const struct rte_flow_item *item,
417 const struct rte_flow_item_vlan *vlan_m = item->mask;
418 const struct rte_flow_item_vlan *vlan_v = item->spec;
419 const struct rte_flow_item_vlan nic_mask = {
420 .tci = RTE_BE16(0x0fff),
421 .inner_type = RTE_BE16(0xffff),
433 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
435 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
437 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
439 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
441 tci_m = rte_be_to_cpu_16(vlan_m->tci);
442 tci_v = rte_be_to_cpu_16(vlan_m->tci & vlan_v->tci);
443 MLX5_SET(fte_match_set_lyr_2_4, headers_m, cvlan_tag, 1);
444 MLX5_SET(fte_match_set_lyr_2_4, headers_v, cvlan_tag, 1);
445 MLX5_SET(fte_match_set_lyr_2_4, headers_m, first_vid, tci_m);
446 MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_vid, tci_v);
447 MLX5_SET(fte_match_set_lyr_2_4, headers_m, first_cfi, tci_m >> 12);
448 MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_cfi, tci_v >> 12);
449 MLX5_SET(fte_match_set_lyr_2_4, headers_m, first_prio, tci_m >> 13);
450 MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_prio, tci_v >> 13);
454 * Add IPV4 item to matcher and to the value.
456 * @param[in, out] matcher
458 * @param[in, out] key
459 * Flow matcher value.
461 * Flow pattern to translate.
463 * Item is inner pattern.
466 flow_dv_translate_item_ipv4(void *matcher, void *key,
467 const struct rte_flow_item *item,
470 const struct rte_flow_item_ipv4 *ipv4_m = item->mask;
471 const struct rte_flow_item_ipv4 *ipv4_v = item->spec;
472 const struct rte_flow_item_ipv4 nic_mask = {
474 .src_addr = RTE_BE32(0xffffffff),
475 .dst_addr = RTE_BE32(0xffffffff),
476 .type_of_service = 0xff,
477 .next_proto_id = 0xff,
491 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
493 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
495 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
497 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
499 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version, 0xf);
500 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_version, 4);
501 l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
502 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
503 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
504 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
505 *(uint32_t *)l24_m = ipv4_m->hdr.dst_addr;
506 *(uint32_t *)l24_v = ipv4_m->hdr.dst_addr & ipv4_v->hdr.dst_addr;
507 l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
508 src_ipv4_src_ipv6.ipv4_layout.ipv4);
509 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
510 src_ipv4_src_ipv6.ipv4_layout.ipv4);
511 *(uint32_t *)l24_m = ipv4_m->hdr.src_addr;
512 *(uint32_t *)l24_v = ipv4_m->hdr.src_addr & ipv4_v->hdr.src_addr;
513 tos = ipv4_m->hdr.type_of_service & ipv4_v->hdr.type_of_service;
514 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ecn,
515 ipv4_m->hdr.type_of_service);
516 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn, tos);
517 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_dscp,
518 ipv4_m->hdr.type_of_service >> 2);
519 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp, tos >> 2);
520 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol,
521 ipv4_m->hdr.next_proto_id);
522 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
523 ipv4_v->hdr.next_proto_id & ipv4_m->hdr.next_proto_id);
527 * Add IPV6 item to matcher and to the value.
529 * @param[in, out] matcher
531 * @param[in, out] key
532 * Flow matcher value.
534 * Flow pattern to translate.
536 * Item is inner pattern.
539 flow_dv_translate_item_ipv6(void *matcher, void *key,
540 const struct rte_flow_item *item,
543 const struct rte_flow_item_ipv6 *ipv6_m = item->mask;
544 const struct rte_flow_item_ipv6 *ipv6_v = item->spec;
545 const struct rte_flow_item_ipv6 nic_mask = {
548 "\xff\xff\xff\xff\xff\xff\xff\xff"
549 "\xff\xff\xff\xff\xff\xff\xff\xff",
551 "\xff\xff\xff\xff\xff\xff\xff\xff"
552 "\xff\xff\xff\xff\xff\xff\xff\xff",
553 .vtc_flow = RTE_BE32(0xffffffff),
560 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
561 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
574 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
576 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
578 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
580 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
582 size = sizeof(ipv6_m->hdr.dst_addr);
583 l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
584 dst_ipv4_dst_ipv6.ipv6_layout.ipv6);
585 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
586 dst_ipv4_dst_ipv6.ipv6_layout.ipv6);
587 memcpy(l24_m, ipv6_m->hdr.dst_addr, size);
588 for (i = 0; i < size; ++i)
589 l24_v[i] = l24_m[i] & ipv6_v->hdr.dst_addr[i];
590 l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
591 src_ipv4_src_ipv6.ipv6_layout.ipv6);
592 l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
593 src_ipv4_src_ipv6.ipv6_layout.ipv6);
594 memcpy(l24_m, ipv6_m->hdr.src_addr, size);
595 for (i = 0; i < size; ++i)
596 l24_v[i] = l24_m[i] & ipv6_v->hdr.src_addr[i];
597 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version, 0xf);
598 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_version, 6);
600 vtc_m = rte_be_to_cpu_32(ipv6_m->hdr.vtc_flow);
601 vtc_v = rte_be_to_cpu_32(ipv6_m->hdr.vtc_flow & ipv6_v->hdr.vtc_flow);
602 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ecn, vtc_m >> 20);
603 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn, vtc_v >> 20);
604 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_dscp, vtc_m >> 22);
605 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp, vtc_v >> 22);
608 MLX5_SET(fte_match_set_misc, misc_m, inner_ipv6_flow_label,
610 MLX5_SET(fte_match_set_misc, misc_v, inner_ipv6_flow_label,
613 MLX5_SET(fte_match_set_misc, misc_m, outer_ipv6_flow_label,
615 MLX5_SET(fte_match_set_misc, misc_v, outer_ipv6_flow_label,
619 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol,
621 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
622 ipv6_v->hdr.proto & ipv6_m->hdr.proto);
626 * Add TCP item to matcher and to the value.
628 * @param[in, out] matcher
630 * @param[in, out] key
631 * Flow matcher value.
633 * Flow pattern to translate.
635 * Item is inner pattern.
638 flow_dv_translate_item_tcp(void *matcher, void *key,
639 const struct rte_flow_item *item,
642 const struct rte_flow_item_tcp *tcp_m = item->mask;
643 const struct rte_flow_item_tcp *tcp_v = item->spec;
650 tcp_m = &rte_flow_item_tcp_mask;
652 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
654 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
656 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
658 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
660 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
661 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_TCP);
662 MLX5_SET(fte_match_set_lyr_2_4, headers_m, tcp_sport,
663 rte_be_to_cpu_16(tcp_m->hdr.src_port));
664 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
665 rte_be_to_cpu_16(tcp_v->hdr.src_port & tcp_m->hdr.src_port));
666 MLX5_SET(fte_match_set_lyr_2_4, headers_m, tcp_dport,
667 rte_be_to_cpu_16(tcp_m->hdr.dst_port));
668 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
669 rte_be_to_cpu_16(tcp_v->hdr.dst_port & tcp_m->hdr.dst_port));
673 * Add UDP item to matcher and to the value.
675 * @param[in, out] matcher
677 * @param[in, out] key
678 * Flow matcher value.
680 * Flow pattern to translate.
682 * Item is inner pattern.
685 flow_dv_translate_item_udp(void *matcher, void *key,
686 const struct rte_flow_item *item,
689 const struct rte_flow_item_udp *udp_m = item->mask;
690 const struct rte_flow_item_udp *udp_v = item->spec;
697 udp_m = &rte_flow_item_udp_mask;
699 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
701 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
703 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
705 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
707 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
708 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_UDP);
709 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_sport,
710 rte_be_to_cpu_16(udp_m->hdr.src_port));
711 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
712 rte_be_to_cpu_16(udp_v->hdr.src_port & udp_m->hdr.src_port));
713 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport,
714 rte_be_to_cpu_16(udp_m->hdr.dst_port));
715 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
716 rte_be_to_cpu_16(udp_v->hdr.dst_port & udp_m->hdr.dst_port));
720 * Add GRE item to matcher and to the value.
722 * @param[in, out] matcher
724 * @param[in, out] key
725 * Flow matcher value.
727 * Flow pattern to translate.
729 * Item is inner pattern.
732 flow_dv_translate_item_gre(void *matcher, void *key,
733 const struct rte_flow_item *item,
736 const struct rte_flow_item_gre *gre_m = item->mask;
737 const struct rte_flow_item_gre *gre_v = item->spec;
740 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
741 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
746 gre_m = &rte_flow_item_gre_mask;
748 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
750 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
752 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
754 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
756 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
757 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_GRE);
758 MLX5_SET(fte_match_set_misc, misc_m, gre_protocol,
759 rte_be_to_cpu_16(gre_m->protocol));
760 MLX5_SET(fte_match_set_misc, misc_v, gre_protocol,
761 rte_be_to_cpu_16(gre_v->protocol & gre_m->protocol));
765 * Add NVGRE item to matcher and to the value.
767 * @param[in, out] matcher
769 * @param[in, out] key
770 * Flow matcher value.
772 * Flow pattern to translate.
774 * Item is inner pattern.
777 flow_dv_translate_item_nvgre(void *matcher, void *key,
778 const struct rte_flow_item *item,
781 const struct rte_flow_item_nvgre *nvgre_m = item->mask;
782 const struct rte_flow_item_nvgre *nvgre_v = item->spec;
783 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
784 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
793 nvgre_m = &rte_flow_item_nvgre_mask;
794 size = sizeof(nvgre_m->tni) + sizeof(nvgre_m->flow_id);
795 gre_key_m = MLX5_ADDR_OF(fte_match_set_misc, misc_m, gre_key_h);
796 gre_key_v = MLX5_ADDR_OF(fte_match_set_misc, misc_v, gre_key_h);
797 memcpy(gre_key_m, nvgre_m->tni, size);
798 for (i = 0; i < size; ++i)
799 gre_key_v[i] = gre_key_m[i] & ((const char *)(nvgre_v->tni))[i];
800 flow_dv_translate_item_gre(matcher, key, item, inner);
804 * Add VXLAN item to matcher and to the value.
806 * @param[in, out] matcher
808 * @param[in, out] key
809 * Flow matcher value.
811 * Flow pattern to translate.
813 * Item is inner pattern.
816 flow_dv_translate_item_vxlan(void *matcher, void *key,
817 const struct rte_flow_item *item,
820 const struct rte_flow_item_vxlan *vxlan_m = item->mask;
821 const struct rte_flow_item_vxlan *vxlan_v = item->spec;
824 void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
825 void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
835 vxlan_m = &rte_flow_item_vxlan_mask;
837 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
839 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
841 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
843 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
845 dport = item->type == RTE_FLOW_ITEM_TYPE_VXLAN ?
846 MLX5_UDP_PORT_VXLAN : MLX5_UDP_PORT_VXLAN_GPE;
847 if (!MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport)) {
848 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xFFFF);
849 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, dport);
851 size = sizeof(vxlan_m->vni);
852 vni_m = MLX5_ADDR_OF(fte_match_set_misc, misc_m, vxlan_vni);
853 vni_v = MLX5_ADDR_OF(fte_match_set_misc, misc_v, vxlan_vni);
854 memcpy(vni_m, vxlan_m->vni, size);
855 for (i = 0; i < size; ++i)
856 vni_v[i] = vni_m[i] & vxlan_v->vni[i];
860 * Update the matcher and the value based the selected item.
862 * @param[in, out] matcher
864 * @param[in, out] key
865 * Flow matcher value.
867 * Flow pattern to translate.
868 * @param[in, out] dev_flow
869 * Pointer to the mlx5_flow.
871 * Item is inner pattern.
874 flow_dv_create_item(void *matcher, void *key,
875 const struct rte_flow_item *item,
876 struct mlx5_flow *dev_flow,
879 struct mlx5_flow_dv_matcher *tmatcher = matcher;
881 switch (item->type) {
882 case RTE_FLOW_ITEM_TYPE_VOID:
883 case RTE_FLOW_ITEM_TYPE_END:
885 case RTE_FLOW_ITEM_TYPE_ETH:
886 flow_dv_translate_item_eth(tmatcher->mask.buf, key, item,
888 tmatcher->priority = MLX5_PRIORITY_MAP_L2;
890 case RTE_FLOW_ITEM_TYPE_VLAN:
891 flow_dv_translate_item_vlan(tmatcher->mask.buf, key, item,
894 case RTE_FLOW_ITEM_TYPE_IPV4:
895 flow_dv_translate_item_ipv4(tmatcher->mask.buf, key, item,
897 tmatcher->priority = MLX5_PRIORITY_MAP_L3;
898 dev_flow->dv.hash_fields |=
899 mlx5_flow_hashfields_adjust(dev_flow, inner,
900 MLX5_IPV4_LAYER_TYPES,
901 MLX5_IPV4_IBV_RX_HASH);
903 case RTE_FLOW_ITEM_TYPE_IPV6:
904 flow_dv_translate_item_ipv6(tmatcher->mask.buf, key, item,
906 tmatcher->priority = MLX5_PRIORITY_MAP_L3;
907 dev_flow->dv.hash_fields |=
908 mlx5_flow_hashfields_adjust(dev_flow, inner,
909 MLX5_IPV6_LAYER_TYPES,
910 MLX5_IPV6_IBV_RX_HASH);
912 case RTE_FLOW_ITEM_TYPE_TCP:
913 flow_dv_translate_item_tcp(tmatcher->mask.buf, key, item,
915 tmatcher->priority = MLX5_PRIORITY_MAP_L4;
916 dev_flow->dv.hash_fields |=
917 mlx5_flow_hashfields_adjust(dev_flow, inner,
919 (IBV_RX_HASH_SRC_PORT_TCP |
920 IBV_RX_HASH_DST_PORT_TCP));
922 case RTE_FLOW_ITEM_TYPE_UDP:
923 flow_dv_translate_item_udp(tmatcher->mask.buf, key, item,
925 tmatcher->priority = MLX5_PRIORITY_MAP_L4;
926 dev_flow->verbs.hash_fields |=
927 mlx5_flow_hashfields_adjust(dev_flow, inner,
929 (IBV_RX_HASH_SRC_PORT_TCP |
930 IBV_RX_HASH_DST_PORT_TCP));
932 case RTE_FLOW_ITEM_TYPE_NVGRE:
933 flow_dv_translate_item_nvgre(tmatcher->mask.buf, key, item,
936 case RTE_FLOW_ITEM_TYPE_GRE:
937 flow_dv_translate_item_gre(tmatcher->mask.buf, key, item,
940 case RTE_FLOW_ITEM_TYPE_VXLAN:
941 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
942 flow_dv_translate_item_vxlan(tmatcher->mask.buf, key, item,
951 * Store the requested actions in an array.
954 * Flow action to translate.
955 * @param[in, out] dev_flow
956 * Pointer to the mlx5_flow.
959 flow_dv_create_action(const struct rte_flow_action *action,
960 struct mlx5_flow *dev_flow)
962 const struct rte_flow_action_queue *queue;
963 const struct rte_flow_action_rss *rss;
964 int actions_n = dev_flow->dv.actions_n;
965 struct rte_flow *flow = dev_flow->flow;
967 switch (action->type) {
968 case RTE_FLOW_ACTION_TYPE_VOID:
970 case RTE_FLOW_ACTION_TYPE_FLAG:
971 dev_flow->dv.actions[actions_n].type = MLX5DV_FLOW_ACTION_TAG;
972 dev_flow->dv.actions[actions_n].tag_value =
973 MLX5_FLOW_MARK_DEFAULT;
976 case RTE_FLOW_ACTION_TYPE_MARK:
977 dev_flow->dv.actions[actions_n].type = MLX5DV_FLOW_ACTION_TAG;
978 dev_flow->dv.actions[actions_n].tag_value =
979 ((const struct rte_flow_action_mark *)
983 case RTE_FLOW_ACTION_TYPE_DROP:
984 dev_flow->dv.actions[actions_n].type = MLX5DV_FLOW_ACTION_DROP;
985 flow->actions |= MLX5_FLOW_ACTION_DROP;
987 case RTE_FLOW_ACTION_TYPE_QUEUE:
988 queue = action->conf;
989 flow->rss.queue_num = 1;
990 (*flow->queue)[0] = queue->index;
992 case RTE_FLOW_ACTION_TYPE_RSS:
995 memcpy((*flow->queue), rss->queue,
996 rss->queue_num * sizeof(uint16_t));
997 flow->rss.queue_num = rss->queue_num;
998 memcpy(flow->key, rss->key, MLX5_RSS_HASH_KEY_LEN);
999 flow->rss.types = rss->types;
1000 flow->rss.level = rss->level;
1001 /* Added to array only in apply since we need the QP */
1006 dev_flow->dv.actions_n = actions_n;
1009 static uint32_t matcher_zero[MLX5_ST_SZ_DW(fte_match_param)] = { 0 };
1011 #define HEADER_IS_ZERO(match_criteria, headers) \
1012 !(memcmp(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
1013 matcher_zero, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
1016 * Calculate flow matcher enable bitmap.
1018 * @param match_criteria
1019 * Pointer to flow matcher criteria.
1022 * Bitmap of enabled fields.
1025 flow_dv_matcher_enable(uint32_t *match_criteria)
1027 uint8_t match_criteria_enable;
1029 match_criteria_enable =
1030 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
1031 MLX5_MATCH_CRITERIA_ENABLE_OUTER_BIT;
1032 match_criteria_enable |=
1033 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
1034 MLX5_MATCH_CRITERIA_ENABLE_MISC_BIT;
1035 match_criteria_enable |=
1036 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
1037 MLX5_MATCH_CRITERIA_ENABLE_INNER_BIT;
1038 match_criteria_enable |=
1039 (!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
1040 MLX5_MATCH_CRITERIA_ENABLE_MISC2_BIT;
1042 return match_criteria_enable;
1046 * Register the flow matcher.
1048 * @param dev[in, out]
1049 * Pointer to rte_eth_dev structure.
1050 * @param[in, out] matcher
1051 * Pointer to flow matcher.
1052 * @parm[in, out] dev_flow
1053 * Pointer to the dev_flow.
1055 * pointer to error structure.
1058 * 0 on success otherwise -errno and errno is set.
1061 flow_dv_matcher_register(struct rte_eth_dev *dev,
1062 struct mlx5_flow_dv_matcher *matcher,
1063 struct mlx5_flow *dev_flow,
1064 struct rte_flow_error *error)
1066 struct priv *priv = dev->data->dev_private;
1067 struct mlx5_flow_dv_matcher *cache_matcher;
1068 struct mlx5dv_flow_matcher_attr dv_attr = {
1069 .type = IBV_FLOW_ATTR_NORMAL,
1070 .match_mask = (void *)&matcher->mask,
1073 /* Lookup from cache. */
1074 LIST_FOREACH(cache_matcher, &priv->matchers, next) {
1075 if (matcher->crc == cache_matcher->crc &&
1076 matcher->priority == cache_matcher->priority &&
1077 matcher->egress == cache_matcher->egress &&
1078 !memcmp((const void *)matcher->mask.buf,
1079 (const void *)cache_matcher->mask.buf,
1080 cache_matcher->mask.size)) {
1082 "priority %hd use %s matcher %p: refcnt %d++",
1083 cache_matcher->priority,
1084 cache_matcher->egress ? "tx" : "rx",
1085 (void *)cache_matcher,
1086 rte_atomic32_read(&cache_matcher->refcnt));
1087 rte_atomic32_inc(&cache_matcher->refcnt);
1088 dev_flow->dv.matcher = cache_matcher;
1092 /* Register new matcher. */
1093 cache_matcher = rte_calloc(__func__, 1, sizeof(*cache_matcher), 0);
1095 return rte_flow_error_set(error, ENOMEM,
1096 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1097 "cannot allocate matcher memory");
1098 *cache_matcher = *matcher;
1099 dv_attr.match_criteria_enable =
1100 flow_dv_matcher_enable(cache_matcher->mask.buf);
1101 dv_attr.priority = matcher->priority;
1102 if (matcher->egress)
1103 dv_attr.flags |= IBV_FLOW_ATTR_FLAGS_EGRESS;
1104 cache_matcher->matcher_object =
1105 mlx5_glue->dv_create_flow_matcher(priv->ctx, &dv_attr);
1106 if (!cache_matcher->matcher_object)
1107 return rte_flow_error_set(error, ENOMEM,
1108 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1109 NULL, "cannot create matcher");
1110 rte_atomic32_inc(&cache_matcher->refcnt);
1111 LIST_INSERT_HEAD(&priv->matchers, cache_matcher, next);
1112 dev_flow->dv.matcher = cache_matcher;
1113 DRV_LOG(DEBUG, "priority %hd new %s matcher %p: refcnt %d",
1114 cache_matcher->priority,
1115 cache_matcher->egress ? "tx" : "rx", (void *)cache_matcher,
1116 rte_atomic32_read(&cache_matcher->refcnt));
1122 * Fill the flow with DV spec.
1125 * Pointer to rte_eth_dev structure.
1126 * @param[in, out] dev_flow
1127 * Pointer to the sub flow.
1129 * Pointer to the flow attributes.
1131 * Pointer to the list of items.
1132 * @param[in] actions
1133 * Pointer to the list of actions.
1135 * Pointer to the error structure.
1138 * 0 on success, a negative errno value otherwise and rte_ernno is set.
1141 flow_dv_translate(struct rte_eth_dev *dev,
1142 struct mlx5_flow *dev_flow,
1143 const struct rte_flow_attr *attr,
1144 const struct rte_flow_item items[],
1145 const struct rte_flow_action actions[] __rte_unused,
1146 struct rte_flow_error *error)
1148 struct priv *priv = dev->data->dev_private;
1149 uint64_t priority = attr->priority;
1150 struct mlx5_flow_dv_matcher matcher = {
1152 .size = sizeof(matcher.mask.buf),
1155 void *match_value = dev_flow->dv.value.buf;
1158 if (priority == MLX5_FLOW_PRIO_RSVD)
1159 priority = priv->config.flow_prio - 1;
1160 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++)
1161 flow_dv_create_item(&matcher, match_value, items, dev_flow,
1163 matcher.crc = rte_raw_cksum((const void *)matcher.mask.buf,
1165 if (priority == MLX5_FLOW_PRIO_RSVD)
1166 priority = priv->config.flow_prio - 1;
1167 matcher.priority = mlx5_flow_adjust_priority(dev, priority,
1169 matcher.egress = attr->egress;
1170 if (flow_dv_matcher_register(dev, &matcher, dev_flow, error))
1172 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++)
1173 flow_dv_create_action(actions, dev_flow);
1178 * Apply the flow to the NIC.
1181 * Pointer to the Ethernet device structure.
1182 * @param[in, out] flow
1183 * Pointer to flow structure.
1185 * Pointer to error structure.
1188 * 0 on success, a negative errno value otherwise and rte_errno is set.
1191 flow_dv_apply(struct rte_eth_dev *dev, struct rte_flow *flow,
1192 struct rte_flow_error *error)
1194 struct mlx5_flow_dv *dv;
1195 struct mlx5_flow *dev_flow;
1199 LIST_FOREACH(dev_flow, &flow->dev_flows, next) {
1202 if (flow->actions & MLX5_FLOW_ACTION_DROP) {
1203 dv->hrxq = mlx5_hrxq_drop_new(dev);
1207 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1208 "cannot get drop hash queue");
1211 dv->actions[n].type = MLX5DV_FLOW_ACTION_DEST_IBV_QP;
1212 dv->actions[n].qp = dv->hrxq->qp;
1215 struct mlx5_hrxq *hrxq;
1216 hrxq = mlx5_hrxq_get(dev, flow->key,
1217 MLX5_RSS_HASH_KEY_LEN,
1220 flow->rss.queue_num);
1222 hrxq = mlx5_hrxq_new
1223 (dev, flow->key, MLX5_RSS_HASH_KEY_LEN,
1224 dv->hash_fields, (*flow->queue),
1225 flow->rss.queue_num,
1227 MLX5_FLOW_LAYER_TUNNEL));
1231 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1232 "cannot get hash queue");
1236 dv->actions[n].type = MLX5DV_FLOW_ACTION_DEST_IBV_QP;
1237 dv->actions[n].qp = hrxq->qp;
1241 mlx5_glue->dv_create_flow(dv->matcher->matcher_object,
1242 (void *)&dv->value, n,
1245 rte_flow_error_set(error, errno,
1246 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1248 "hardware refuses to create flow");
1254 err = rte_errno; /* Save rte_errno before cleanup. */
1255 LIST_FOREACH(dev_flow, &flow->dev_flows, next) {
1256 struct mlx5_flow_dv *dv = &dev_flow->dv;
1258 if (flow->actions & MLX5_FLOW_ACTION_DROP)
1259 mlx5_hrxq_drop_release(dev);
1261 mlx5_hrxq_release(dev, dv->hrxq);
1265 rte_errno = err; /* Restore rte_errno. */
1270 * Release the flow matcher.
1273 * Pointer to Ethernet device.
1275 * Pointer to mlx5_flow.
1278 * 1 while a reference on it exists, 0 when freed.
1281 flow_dv_matcher_release(struct rte_eth_dev *dev,
1282 struct mlx5_flow *flow)
1284 struct mlx5_flow_dv_matcher *matcher = flow->dv.matcher;
1286 assert(matcher->matcher_object);
1287 DRV_LOG(DEBUG, "port %u matcher %p: refcnt %d--",
1288 dev->data->port_id, (void *)matcher,
1289 rte_atomic32_read(&matcher->refcnt));
1290 if (rte_atomic32_dec_and_test(&matcher->refcnt)) {
1291 claim_zero(mlx5_glue->dv_destroy_flow_matcher
1292 (matcher->matcher_object));
1293 LIST_REMOVE(matcher, next);
1295 DRV_LOG(DEBUG, "port %u matcher %p: removed",
1296 dev->data->port_id, (void *)matcher);
1303 * Remove the flow from the NIC but keeps it in memory.
1306 * Pointer to Ethernet device.
1307 * @param[in, out] flow
1308 * Pointer to flow structure.
1311 flow_dv_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
1313 struct mlx5_flow_dv *dv;
1314 struct mlx5_flow *dev_flow;
1318 LIST_FOREACH(dev_flow, &flow->dev_flows, next) {
1321 claim_zero(mlx5_glue->destroy_flow(dv->flow));
1325 if (flow->actions & MLX5_FLOW_ACTION_DROP)
1326 mlx5_hrxq_drop_release(dev);
1328 mlx5_hrxq_release(dev, dv->hrxq);
1333 flow->counter = NULL;
1337 * Remove the flow from the NIC and the memory.
1340 * Pointer to the Ethernet device structure.
1341 * @param[in, out] flow
1342 * Pointer to flow structure.
1345 flow_dv_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
1347 struct mlx5_flow *dev_flow;
1351 flow_dv_remove(dev, flow);
1352 while (!LIST_EMPTY(&flow->dev_flows)) {
1353 dev_flow = LIST_FIRST(&flow->dev_flows);
1354 LIST_REMOVE(dev_flow, next);
1355 if (dev_flow->dv.matcher)
1356 flow_dv_matcher_release(dev, dev_flow);
1361 const struct mlx5_flow_driver_ops mlx5_flow_dv_drv_ops = {
1362 .validate = flow_dv_validate,
1363 .prepare = flow_dv_prepare,
1364 .translate = flow_dv_translate,
1365 .apply = flow_dv_apply,
1366 .remove = flow_dv_remove,
1367 .destroy = flow_dv_destroy,
1370 #endif /* HAVE_IBV_FLOW_DV_SUPPORT */