1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2017 6WIND S.A.
3 * Copyright 2017 Mellanox Technologies, Ltd
8 * Flow API operations for mlx4 driver.
11 #include <arpa/inet.h>
18 #include <sys/queue.h>
20 /* Verbs headers do not support -pedantic. */
22 #pragma GCC diagnostic ignored "-Wpedantic"
24 #include <infiniband/verbs.h>
26 #pragma GCC diagnostic error "-Wpedantic"
29 #include <rte_byteorder.h>
30 #include <rte_errno.h>
31 #include <rte_eth_ctrl.h>
32 #include <rte_ethdev_driver.h>
33 #include <rte_ether.h>
35 #include <rte_flow_driver.h>
36 #include <rte_malloc.h>
40 #include "mlx4_glue.h"
41 #include "mlx4_flow.h"
42 #include "mlx4_rxtx.h"
43 #include "mlx4_utils.h"
45 /** Static initializer for a list of subsequent item types. */
46 #define NEXT_ITEM(...) \
47 (const enum rte_flow_item_type []){ \
48 __VA_ARGS__, RTE_FLOW_ITEM_TYPE_END, \
51 /** Processor structure associated with a flow item. */
52 struct mlx4_flow_proc_item {
53 /** Bit-mask for fields supported by this PMD. */
54 const void *mask_support;
55 /** Bit-mask to use when @p item->mask is not provided. */
56 const void *mask_default;
57 /** Size in bytes for @p mask_support and @p mask_default. */
58 const unsigned int mask_sz;
59 /** Merge a pattern item into a flow rule handle. */
60 int (*merge)(struct rte_flow *flow,
61 const struct rte_flow_item *item,
62 const struct mlx4_flow_proc_item *proc,
63 struct rte_flow_error *error);
64 /** Size in bytes of the destination structure. */
65 const unsigned int dst_sz;
66 /** List of possible subsequent items. */
67 const enum rte_flow_item_type *const next_item;
70 /** Shared resources for drop flow rules. */
72 struct ibv_qp *qp; /**< QP target. */
73 struct ibv_cq *cq; /**< CQ associated with above QP. */
74 struct priv *priv; /**< Back pointer to private data. */
75 uint32_t refcnt; /**< Reference count. */
79 * Convert DPDK RSS hash types to their Verbs equivalent.
81 * This function returns the supported (default) set when @p types has
82 * special value (uint64_t)-1.
85 * Pointer to private structure.
87 * Hash types in DPDK format (see struct rte_eth_rss_conf).
90 * A valid Verbs RSS hash fields mask for mlx4 on success, (uint64_t)-1
91 * otherwise and rte_errno is set.
94 mlx4_conv_rss_types(struct priv *priv, uint64_t types)
96 enum { IPV4, IPV6, TCP, UDP, };
97 const uint64_t in[] = {
98 [IPV4] = (ETH_RSS_IPV4 |
100 ETH_RSS_NONFRAG_IPV4_TCP |
101 ETH_RSS_NONFRAG_IPV4_UDP |
102 ETH_RSS_NONFRAG_IPV4_OTHER),
103 [IPV6] = (ETH_RSS_IPV6 |
105 ETH_RSS_NONFRAG_IPV6_TCP |
106 ETH_RSS_NONFRAG_IPV6_UDP |
107 ETH_RSS_NONFRAG_IPV6_OTHER |
109 ETH_RSS_IPV6_TCP_EX |
110 ETH_RSS_IPV6_UDP_EX),
111 [TCP] = (ETH_RSS_NONFRAG_IPV4_TCP |
112 ETH_RSS_NONFRAG_IPV6_TCP |
113 ETH_RSS_IPV6_TCP_EX),
114 [UDP] = (ETH_RSS_NONFRAG_IPV4_UDP |
115 ETH_RSS_NONFRAG_IPV6_UDP |
116 ETH_RSS_IPV6_UDP_EX),
118 const uint64_t out[RTE_DIM(in)] = {
119 [IPV4] = IBV_RX_HASH_SRC_IPV4 | IBV_RX_HASH_DST_IPV4,
120 [IPV6] = IBV_RX_HASH_SRC_IPV6 | IBV_RX_HASH_DST_IPV6,
121 [TCP] = IBV_RX_HASH_SRC_PORT_TCP | IBV_RX_HASH_DST_PORT_TCP,
122 [UDP] = IBV_RX_HASH_SRC_PORT_UDP | IBV_RX_HASH_DST_PORT_UDP,
128 if (types == (uint64_t)-1)
129 return priv->hw_rss_sup;
130 for (i = 0; i != RTE_DIM(in); ++i)
132 seen |= types & in[i];
135 if ((conv & priv->hw_rss_sup) == conv && !(types & ~seen))
142 * Merge Ethernet pattern item into flow rule handle.
144 * Additional mlx4-specific constraints on supported fields:
146 * - No support for partial masks, except in the specific case of matching
147 * all multicast traffic (@p spec->dst and @p mask->dst equal to
148 * 01:00:00:00:00:00).
149 * - Not providing @p item->spec or providing an empty @p mask->dst is
150 * *only* supported if the rule doesn't specify additional matching
151 * criteria (i.e. rule is promiscuous-like).
153 * @param[in, out] flow
154 * Flow rule handle to update.
156 * Pattern item to merge.
158 * Associated item-processing object.
160 * Perform verbose error reporting if not NULL.
163 * 0 on success, a negative errno value otherwise and rte_errno is set.
166 mlx4_flow_merge_eth(struct rte_flow *flow,
167 const struct rte_flow_item *item,
168 const struct mlx4_flow_proc_item *proc,
169 struct rte_flow_error *error)
171 const struct rte_flow_item_eth *spec = item->spec;
172 const struct rte_flow_item_eth *mask =
173 spec ? (item->mask ? item->mask : proc->mask_default) : NULL;
174 struct ibv_flow_spec_eth *eth;
181 uint32_t sum_dst = 0;
182 uint32_t sum_src = 0;
184 for (i = 0; i != sizeof(mask->dst.addr_bytes); ++i) {
185 sum_dst += mask->dst.addr_bytes[i];
186 sum_src += mask->src.addr_bytes[i];
189 msg = "mlx4 does not support source MAC matching";
191 } else if (!sum_dst) {
193 } else if (sum_dst == 1 && mask->dst.addr_bytes[0] == 1) {
194 if (!(spec->dst.addr_bytes[0] & 1)) {
195 msg = "mlx4 does not support the explicit"
196 " exclusion of all multicast traffic";
200 } else if (sum_dst != (UINT8_C(0xff) * ETHER_ADDR_LEN)) {
201 msg = "mlx4 does not support matching partial"
209 flow->ibv_attr->type = IBV_FLOW_ATTR_ALL_DEFAULT;
212 if (flow->allmulti) {
213 flow->ibv_attr->type = IBV_FLOW_ATTR_MC_DEFAULT;
216 ++flow->ibv_attr->num_of_specs;
217 eth = (void *)((uintptr_t)flow->ibv_attr + flow->ibv_attr_size);
218 *eth = (struct ibv_flow_spec_eth) {
219 .type = IBV_FLOW_SPEC_ETH,
220 .size = sizeof(*eth),
222 memcpy(eth->val.dst_mac, spec->dst.addr_bytes, ETHER_ADDR_LEN);
223 memcpy(eth->mask.dst_mac, mask->dst.addr_bytes, ETHER_ADDR_LEN);
224 /* Remove unwanted bits from values. */
225 for (i = 0; i < ETHER_ADDR_LEN; ++i) {
226 eth->val.dst_mac[i] &= eth->mask.dst_mac[i];
230 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
235 * Merge VLAN pattern item into flow rule handle.
237 * Additional mlx4-specific constraints on supported fields:
239 * - Matching *all* VLAN traffic by omitting @p item->spec or providing an
240 * empty @p item->mask would also include non-VLAN traffic. Doing so is
241 * therefore unsupported.
242 * - No support for partial masks.
244 * @param[in, out] flow
245 * Flow rule handle to update.
247 * Pattern item to merge.
249 * Associated item-processing object.
251 * Perform verbose error reporting if not NULL.
254 * 0 on success, a negative errno value otherwise and rte_errno is set.
257 mlx4_flow_merge_vlan(struct rte_flow *flow,
258 const struct rte_flow_item *item,
259 const struct mlx4_flow_proc_item *proc,
260 struct rte_flow_error *error)
262 const struct rte_flow_item_vlan *spec = item->spec;
263 const struct rte_flow_item_vlan *mask =
264 spec ? (item->mask ? item->mask : proc->mask_default) : NULL;
265 struct ibv_flow_spec_eth *eth;
268 if (!mask || !mask->tci) {
269 msg = "mlx4 cannot match all VLAN traffic while excluding"
270 " non-VLAN traffic, TCI VID must be specified";
273 if (mask->tci != RTE_BE16(0x0fff)) {
274 msg = "mlx4 does not support partial TCI VID matching";
279 eth = (void *)((uintptr_t)flow->ibv_attr + flow->ibv_attr_size -
281 eth->val.vlan_tag = spec->tci;
282 eth->mask.vlan_tag = mask->tci;
283 eth->val.vlan_tag &= eth->mask.vlan_tag;
286 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
291 * Merge IPv4 pattern item into flow rule handle.
293 * Additional mlx4-specific constraints on supported fields:
295 * - No support for partial masks.
297 * @param[in, out] flow
298 * Flow rule handle to update.
300 * Pattern item to merge.
302 * Associated item-processing object.
304 * Perform verbose error reporting if not NULL.
307 * 0 on success, a negative errno value otherwise and rte_errno is set.
310 mlx4_flow_merge_ipv4(struct rte_flow *flow,
311 const struct rte_flow_item *item,
312 const struct mlx4_flow_proc_item *proc,
313 struct rte_flow_error *error)
315 const struct rte_flow_item_ipv4 *spec = item->spec;
316 const struct rte_flow_item_ipv4 *mask =
317 spec ? (item->mask ? item->mask : proc->mask_default) : NULL;
318 struct ibv_flow_spec_ipv4 *ipv4;
322 ((uint32_t)(mask->hdr.src_addr + 1) > UINT32_C(1) ||
323 (uint32_t)(mask->hdr.dst_addr + 1) > UINT32_C(1))) {
324 msg = "mlx4 does not support matching partial IPv4 fields";
329 ++flow->ibv_attr->num_of_specs;
330 ipv4 = (void *)((uintptr_t)flow->ibv_attr + flow->ibv_attr_size);
331 *ipv4 = (struct ibv_flow_spec_ipv4) {
332 .type = IBV_FLOW_SPEC_IPV4,
333 .size = sizeof(*ipv4),
337 ipv4->val = (struct ibv_flow_ipv4_filter) {
338 .src_ip = spec->hdr.src_addr,
339 .dst_ip = spec->hdr.dst_addr,
341 ipv4->mask = (struct ibv_flow_ipv4_filter) {
342 .src_ip = mask->hdr.src_addr,
343 .dst_ip = mask->hdr.dst_addr,
345 /* Remove unwanted bits from values. */
346 ipv4->val.src_ip &= ipv4->mask.src_ip;
347 ipv4->val.dst_ip &= ipv4->mask.dst_ip;
350 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
355 * Merge UDP pattern item into flow rule handle.
357 * Additional mlx4-specific constraints on supported fields:
359 * - No support for partial masks.
361 * @param[in, out] flow
362 * Flow rule handle to update.
364 * Pattern item to merge.
366 * Associated item-processing object.
368 * Perform verbose error reporting if not NULL.
371 * 0 on success, a negative errno value otherwise and rte_errno is set.
374 mlx4_flow_merge_udp(struct rte_flow *flow,
375 const struct rte_flow_item *item,
376 const struct mlx4_flow_proc_item *proc,
377 struct rte_flow_error *error)
379 const struct rte_flow_item_udp *spec = item->spec;
380 const struct rte_flow_item_udp *mask =
381 spec ? (item->mask ? item->mask : proc->mask_default) : NULL;
382 struct ibv_flow_spec_tcp_udp *udp;
386 ((uint16_t)(mask->hdr.src_port + 1) > UINT16_C(1) ||
387 (uint16_t)(mask->hdr.dst_port + 1) > UINT16_C(1))) {
388 msg = "mlx4 does not support matching partial UDP fields";
393 ++flow->ibv_attr->num_of_specs;
394 udp = (void *)((uintptr_t)flow->ibv_attr + flow->ibv_attr_size);
395 *udp = (struct ibv_flow_spec_tcp_udp) {
396 .type = IBV_FLOW_SPEC_UDP,
397 .size = sizeof(*udp),
401 udp->val.dst_port = spec->hdr.dst_port;
402 udp->val.src_port = spec->hdr.src_port;
403 udp->mask.dst_port = mask->hdr.dst_port;
404 udp->mask.src_port = mask->hdr.src_port;
405 /* Remove unwanted bits from values. */
406 udp->val.src_port &= udp->mask.src_port;
407 udp->val.dst_port &= udp->mask.dst_port;
410 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
415 * Merge TCP pattern item into flow rule handle.
417 * Additional mlx4-specific constraints on supported fields:
419 * - No support for partial masks.
421 * @param[in, out] flow
422 * Flow rule handle to update.
424 * Pattern item to merge.
426 * Associated item-processing object.
428 * Perform verbose error reporting if not NULL.
431 * 0 on success, a negative errno value otherwise and rte_errno is set.
434 mlx4_flow_merge_tcp(struct rte_flow *flow,
435 const struct rte_flow_item *item,
436 const struct mlx4_flow_proc_item *proc,
437 struct rte_flow_error *error)
439 const struct rte_flow_item_tcp *spec = item->spec;
440 const struct rte_flow_item_tcp *mask =
441 spec ? (item->mask ? item->mask : proc->mask_default) : NULL;
442 struct ibv_flow_spec_tcp_udp *tcp;
446 ((uint16_t)(mask->hdr.src_port + 1) > UINT16_C(1) ||
447 (uint16_t)(mask->hdr.dst_port + 1) > UINT16_C(1))) {
448 msg = "mlx4 does not support matching partial TCP fields";
453 ++flow->ibv_attr->num_of_specs;
454 tcp = (void *)((uintptr_t)flow->ibv_attr + flow->ibv_attr_size);
455 *tcp = (struct ibv_flow_spec_tcp_udp) {
456 .type = IBV_FLOW_SPEC_TCP,
457 .size = sizeof(*tcp),
461 tcp->val.dst_port = spec->hdr.dst_port;
462 tcp->val.src_port = spec->hdr.src_port;
463 tcp->mask.dst_port = mask->hdr.dst_port;
464 tcp->mask.src_port = mask->hdr.src_port;
465 /* Remove unwanted bits from values. */
466 tcp->val.src_port &= tcp->mask.src_port;
467 tcp->val.dst_port &= tcp->mask.dst_port;
470 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
475 * Perform basic sanity checks on a pattern item.
478 * Item specification.
480 * Associated item-processing object.
482 * Perform verbose error reporting if not NULL.
485 * 0 on success, a negative errno value otherwise and rte_errno is set.
488 mlx4_flow_item_check(const struct rte_flow_item *item,
489 const struct mlx4_flow_proc_item *proc,
490 struct rte_flow_error *error)
495 /* item->last and item->mask cannot exist without item->spec. */
496 if (!item->spec && (item->mask || item->last))
497 return rte_flow_error_set
498 (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, item,
499 "\"mask\" or \"last\" field provided without a"
500 " corresponding \"spec\"");
501 /* No spec, no mask, no problem. */
505 (const uint8_t *)item->mask :
506 (const uint8_t *)proc->mask_default;
509 * Single-pass check to make sure that:
510 * - Mask is supported, no bits are set outside proc->mask_support.
511 * - Both item->spec and item->last are included in mask.
513 for (i = 0; i != proc->mask_sz; ++i) {
516 if ((mask[i] | ((const uint8_t *)proc->mask_support)[i]) !=
517 ((const uint8_t *)proc->mask_support)[i])
518 return rte_flow_error_set
519 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
520 item, "unsupported field found in \"mask\"");
522 (((const uint8_t *)item->spec)[i] & mask[i]) !=
523 (((const uint8_t *)item->last)[i] & mask[i]))
524 return rte_flow_error_set
525 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
527 "range between \"spec\" and \"last\""
528 " is larger than \"mask\"");
533 /** Graph of supported items and associated actions. */
534 static const struct mlx4_flow_proc_item mlx4_flow_proc_item_list[] = {
535 [RTE_FLOW_ITEM_TYPE_END] = {
536 .next_item = NEXT_ITEM(RTE_FLOW_ITEM_TYPE_ETH),
538 [RTE_FLOW_ITEM_TYPE_ETH] = {
539 .next_item = NEXT_ITEM(RTE_FLOW_ITEM_TYPE_VLAN,
540 RTE_FLOW_ITEM_TYPE_IPV4),
541 .mask_support = &(const struct rte_flow_item_eth){
542 /* Only destination MAC can be matched. */
543 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
545 .mask_default = &rte_flow_item_eth_mask,
546 .mask_sz = sizeof(struct rte_flow_item_eth),
547 .merge = mlx4_flow_merge_eth,
548 .dst_sz = sizeof(struct ibv_flow_spec_eth),
550 [RTE_FLOW_ITEM_TYPE_VLAN] = {
551 .next_item = NEXT_ITEM(RTE_FLOW_ITEM_TYPE_IPV4),
552 .mask_support = &(const struct rte_flow_item_vlan){
553 /* Only TCI VID matching is supported. */
554 .tci = RTE_BE16(0x0fff),
556 .mask_default = &rte_flow_item_vlan_mask,
557 .mask_sz = sizeof(struct rte_flow_item_vlan),
558 .merge = mlx4_flow_merge_vlan,
561 [RTE_FLOW_ITEM_TYPE_IPV4] = {
562 .next_item = NEXT_ITEM(RTE_FLOW_ITEM_TYPE_UDP,
563 RTE_FLOW_ITEM_TYPE_TCP),
564 .mask_support = &(const struct rte_flow_item_ipv4){
566 .src_addr = RTE_BE32(0xffffffff),
567 .dst_addr = RTE_BE32(0xffffffff),
570 .mask_default = &rte_flow_item_ipv4_mask,
571 .mask_sz = sizeof(struct rte_flow_item_ipv4),
572 .merge = mlx4_flow_merge_ipv4,
573 .dst_sz = sizeof(struct ibv_flow_spec_ipv4),
575 [RTE_FLOW_ITEM_TYPE_UDP] = {
576 .mask_support = &(const struct rte_flow_item_udp){
578 .src_port = RTE_BE16(0xffff),
579 .dst_port = RTE_BE16(0xffff),
582 .mask_default = &rte_flow_item_udp_mask,
583 .mask_sz = sizeof(struct rte_flow_item_udp),
584 .merge = mlx4_flow_merge_udp,
585 .dst_sz = sizeof(struct ibv_flow_spec_tcp_udp),
587 [RTE_FLOW_ITEM_TYPE_TCP] = {
588 .mask_support = &(const struct rte_flow_item_tcp){
590 .src_port = RTE_BE16(0xffff),
591 .dst_port = RTE_BE16(0xffff),
594 .mask_default = &rte_flow_item_tcp_mask,
595 .mask_sz = sizeof(struct rte_flow_item_tcp),
596 .merge = mlx4_flow_merge_tcp,
597 .dst_sz = sizeof(struct ibv_flow_spec_tcp_udp),
602 * Make sure a flow rule is supported and initialize associated structure.
605 * Pointer to private structure.
607 * Flow rule attributes.
609 * Pattern specification (list terminated by the END pattern item).
611 * Associated actions (list terminated by the END action).
613 * Perform verbose error reporting if not NULL.
614 * @param[in, out] addr
615 * Buffer where the resulting flow rule handle pointer must be stored.
616 * If NULL, stop processing after validation stage.
619 * 0 on success, a negative errno value otherwise and rte_errno is set.
622 mlx4_flow_prepare(struct priv *priv,
623 const struct rte_flow_attr *attr,
624 const struct rte_flow_item pattern[],
625 const struct rte_flow_action actions[],
626 struct rte_flow_error *error,
627 struct rte_flow **addr)
629 const struct rte_flow_item *item;
630 const struct rte_flow_action *action;
631 const struct mlx4_flow_proc_item *proc;
632 struct rte_flow temp = { .ibv_attr_size = sizeof(*temp.ibv_attr) };
633 struct rte_flow *flow = &temp;
634 const char *msg = NULL;
638 return rte_flow_error_set
639 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
640 NULL, "groups are not supported");
641 if (attr->priority > MLX4_FLOW_PRIORITY_LAST)
642 return rte_flow_error_set
643 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
644 NULL, "maximum priority level is "
645 MLX4_STR_EXPAND(MLX4_FLOW_PRIORITY_LAST));
647 return rte_flow_error_set
648 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
649 NULL, "egress is not supported");
651 return rte_flow_error_set
652 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
653 NULL, "transfer is not supported");
655 return rte_flow_error_set
656 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
657 NULL, "only ingress is supported");
660 proc = mlx4_flow_proc_item_list;
661 /* Go over pattern. */
662 for (item = pattern; item->type; ++item) {
663 const struct mlx4_flow_proc_item *next = NULL;
667 if (item->type == RTE_FLOW_ITEM_TYPE_VOID)
669 if (item->type == MLX4_FLOW_ITEM_TYPE_INTERNAL) {
673 if (flow->promisc || flow->allmulti) {
674 msg = "mlx4 does not support additional matching"
675 " criteria combined with indiscriminate"
676 " matching on Ethernet headers";
677 goto exit_item_not_supported;
679 for (i = 0; proc->next_item && proc->next_item[i]; ++i) {
680 if (proc->next_item[i] == item->type) {
681 next = &mlx4_flow_proc_item_list[item->type];
686 goto exit_item_not_supported;
689 * Perform basic sanity checks only once, while handle is
693 err = mlx4_flow_item_check(item, proc, error);
698 err = proc->merge(flow, item, proc, error);
702 flow->ibv_attr_size += proc->dst_sz;
704 /* Go over actions list. */
705 for (action = actions; action->type; ++action) {
706 /* This one may appear anywhere multiple times. */
707 if (action->type == RTE_FLOW_ACTION_TYPE_VOID)
709 /* Fate-deciding actions may appear exactly once. */
711 msg = "cannot combine several fate-deciding actions,"
712 " choose between DROP, QUEUE or RSS";
713 goto exit_action_not_supported;
716 switch (action->type) {
717 const struct rte_flow_action_queue *queue;
718 const struct rte_flow_action_rss *rss;
719 const uint8_t *rss_key;
720 uint32_t rss_key_len;
724 case RTE_FLOW_ACTION_TYPE_DROP:
727 case RTE_FLOW_ACTION_TYPE_QUEUE:
730 queue = action->conf;
731 if (queue->index >= priv->dev->data->nb_rx_queues) {
732 msg = "queue target index beyond number of"
733 " configured Rx queues";
734 goto exit_action_not_supported;
736 flow->rss = mlx4_rss_get
737 (priv, 0, mlx4_rss_hash_key_default, 1,
740 msg = "not enough resources for additional"
741 " single-queue RSS context";
742 goto exit_action_not_supported;
745 case RTE_FLOW_ACTION_TYPE_RSS:
749 /* Default RSS configuration if none is provided. */
752 rss_key_len = rss->key_len;
754 rss_key = mlx4_rss_hash_key_default;
755 rss_key_len = MLX4_RSS_HASH_KEY_SIZE;
758 for (i = 0; i < rss->queue_num; ++i)
760 priv->dev->data->nb_rx_queues)
762 if (i != rss->queue_num) {
763 msg = "queue index target beyond number of"
764 " configured Rx queues";
765 goto exit_action_not_supported;
767 if (!rte_is_power_of_2(rss->queue_num)) {
768 msg = "for RSS, mlx4 requires the number of"
769 " queues to be a power of two";
770 goto exit_action_not_supported;
772 if (rss_key_len != sizeof(flow->rss->key)) {
773 msg = "mlx4 supports exactly one RSS hash key"
775 MLX4_STR_EXPAND(MLX4_RSS_HASH_KEY_SIZE);
776 goto exit_action_not_supported;
778 for (i = 1; i < rss->queue_num; ++i)
779 if (rss->queue[i] - rss->queue[i - 1] != 1)
781 if (i != rss->queue_num) {
782 msg = "mlx4 requires RSS contexts to use"
783 " consecutive queue indices only";
784 goto exit_action_not_supported;
786 if (rss->queue[0] % rss->queue_num) {
787 msg = "mlx4 requires the first queue of a RSS"
788 " context to be aligned on a multiple"
789 " of the context size";
790 goto exit_action_not_supported;
793 rss->func != RTE_ETH_HASH_FUNCTION_TOEPLITZ) {
794 msg = "the only supported RSS hash function"
796 goto exit_action_not_supported;
799 msg = "a nonzero RSS encapsulation level is"
801 goto exit_action_not_supported;
804 fields = mlx4_conv_rss_types(priv, rss->types);
805 if (fields == (uint64_t)-1 && rte_errno) {
806 msg = "unsupported RSS hash type requested";
807 goto exit_action_not_supported;
809 flow->rss = mlx4_rss_get
810 (priv, fields, rss_key, rss->queue_num,
813 msg = "either invalid parameters or not enough"
814 " resources for additional multi-queue"
816 goto exit_action_not_supported;
820 goto exit_action_not_supported;
823 /* When fate is unknown, drop traffic. */
826 /* Validation ends here. */
829 mlx4_rss_put(flow->rss);
833 /* Allocate proper handle based on collected data. */
834 const struct mlx4_malloc_vec vec[] = {
836 .align = alignof(struct rte_flow),
837 .size = sizeof(*flow),
838 .addr = (void **)&flow,
841 .align = alignof(struct ibv_flow_attr),
842 .size = temp.ibv_attr_size,
843 .addr = (void **)&temp.ibv_attr,
847 if (!mlx4_zmallocv(__func__, vec, RTE_DIM(vec))) {
849 mlx4_rss_put(temp.rss);
850 return rte_flow_error_set
852 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
853 "flow rule handle allocation failure");
855 /* Most fields will be updated by second pass. */
856 *flow = (struct rte_flow){
857 .ibv_attr = temp.ibv_attr,
858 .ibv_attr_size = sizeof(*flow->ibv_attr),
861 *flow->ibv_attr = (struct ibv_flow_attr){
862 .type = IBV_FLOW_ATTR_NORMAL,
863 .size = sizeof(*flow->ibv_attr),
864 .priority = attr->priority,
871 exit_item_not_supported:
872 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
873 item, msg ? msg : "item not supported");
874 exit_action_not_supported:
875 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
876 action, msg ? msg : "action not supported");
880 * Validate a flow supported by the NIC.
882 * @see rte_flow_validate()
886 mlx4_flow_validate(struct rte_eth_dev *dev,
887 const struct rte_flow_attr *attr,
888 const struct rte_flow_item pattern[],
889 const struct rte_flow_action actions[],
890 struct rte_flow_error *error)
892 struct priv *priv = dev->data->dev_private;
894 return mlx4_flow_prepare(priv, attr, pattern, actions, error, NULL);
898 * Get a drop flow rule resources instance.
901 * Pointer to private structure.
904 * Pointer to drop flow resources on success, NULL otherwise and rte_errno
907 static struct mlx4_drop *
908 mlx4_drop_get(struct priv *priv)
910 struct mlx4_drop *drop = priv->drop;
913 assert(drop->refcnt);
914 assert(drop->priv == priv);
918 drop = rte_malloc(__func__, sizeof(*drop), 0);
921 *drop = (struct mlx4_drop){
925 drop->cq = mlx4_glue->create_cq(priv->ctx, 1, NULL, NULL, 0);
928 drop->qp = mlx4_glue->create_qp
930 &(struct ibv_qp_init_attr){
933 .qp_type = IBV_QPT_RAW_PACKET,
941 claim_zero(mlx4_glue->destroy_qp(drop->qp));
943 claim_zero(mlx4_glue->destroy_cq(drop->cq));
951 * Give back a drop flow rule resources instance.
954 * Pointer to drop flow rule resources.
957 mlx4_drop_put(struct mlx4_drop *drop)
959 assert(drop->refcnt);
962 drop->priv->drop = NULL;
963 claim_zero(mlx4_glue->destroy_qp(drop->qp));
964 claim_zero(mlx4_glue->destroy_cq(drop->cq));
969 * Toggle a configured flow rule.
972 * Pointer to private structure.
974 * Flow rule handle to toggle.
976 * Whether associated Verbs flow must be created or removed.
978 * Perform verbose error reporting if not NULL.
981 * 0 on success, a negative errno value otherwise and rte_errno is set.
984 mlx4_flow_toggle(struct priv *priv,
985 struct rte_flow *flow,
987 struct rte_flow_error *error)
989 struct ibv_qp *qp = NULL;
996 claim_zero(mlx4_glue->destroy_flow(flow->ibv_flow));
997 flow->ibv_flow = NULL;
999 mlx4_drop_put(priv->drop);
1001 mlx4_rss_detach(flow->rss);
1004 assert(flow->ibv_attr);
1005 if (!flow->internal &&
1007 flow->ibv_attr->priority == MLX4_FLOW_PRIORITY_LAST) {
1008 if (flow->ibv_flow) {
1009 claim_zero(mlx4_glue->destroy_flow(flow->ibv_flow));
1010 flow->ibv_flow = NULL;
1012 mlx4_drop_put(priv->drop);
1014 mlx4_rss_detach(flow->rss);
1017 msg = ("priority level "
1018 MLX4_STR_EXPAND(MLX4_FLOW_PRIORITY_LAST)
1019 " is reserved when not in isolated mode");
1023 struct mlx4_rss *rss = flow->rss;
1027 /* Stop at the first nonexistent target queue. */
1028 for (i = 0; i != rss->queues; ++i)
1029 if (rss->queue_id[i] >=
1030 priv->dev->data->nb_rx_queues ||
1031 !priv->dev->data->rx_queues[rss->queue_id[i]]) {
1035 if (flow->ibv_flow) {
1036 if (missing ^ !flow->drop)
1038 /* Verbs flow needs updating. */
1039 claim_zero(mlx4_glue->destroy_flow(flow->ibv_flow));
1040 flow->ibv_flow = NULL;
1042 mlx4_drop_put(priv->drop);
1044 mlx4_rss_detach(rss);
1047 err = mlx4_rss_attach(rss);
1050 msg = "cannot create indirection table or hash"
1051 " QP to associate flow rule with";
1056 /* A missing target queue drops traffic implicitly. */
1057 flow->drop = missing;
1062 mlx4_drop_get(priv);
1065 msg = "resources for drop flow rule cannot be created";
1068 qp = priv->drop->qp;
1073 flow->ibv_flow = mlx4_glue->create_flow(qp, flow->ibv_attr);
1077 mlx4_drop_put(priv->drop);
1079 mlx4_rss_detach(flow->rss);
1081 msg = "flow rule rejected by device";
1083 return rte_flow_error_set
1084 (error, err, RTE_FLOW_ERROR_TYPE_HANDLE, flow, msg);
1090 * @see rte_flow_create()
1093 static struct rte_flow *
1094 mlx4_flow_create(struct rte_eth_dev *dev,
1095 const struct rte_flow_attr *attr,
1096 const struct rte_flow_item pattern[],
1097 const struct rte_flow_action actions[],
1098 struct rte_flow_error *error)
1100 struct priv *priv = dev->data->dev_private;
1101 struct rte_flow *flow;
1104 err = mlx4_flow_prepare(priv, attr, pattern, actions, error, &flow);
1107 err = mlx4_flow_toggle(priv, flow, priv->started, error);
1109 struct rte_flow *curr = LIST_FIRST(&priv->flows);
1111 /* New rules are inserted after internal ones. */
1112 if (!curr || !curr->internal) {
1113 LIST_INSERT_HEAD(&priv->flows, flow, next);
1115 while (LIST_NEXT(curr, next) &&
1116 LIST_NEXT(curr, next)->internal)
1117 curr = LIST_NEXT(curr, next);
1118 LIST_INSERT_AFTER(curr, flow, next);
1123 mlx4_rss_put(flow->rss);
1124 rte_flow_error_set(error, -err, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1131 * Configure isolated mode.
1133 * @see rte_flow_isolate()
1137 mlx4_flow_isolate(struct rte_eth_dev *dev,
1139 struct rte_flow_error *error)
1141 struct priv *priv = dev->data->dev_private;
1143 if (!!enable == !!priv->isolated)
1145 priv->isolated = !!enable;
1146 if (mlx4_flow_sync(priv, error)) {
1147 priv->isolated = !enable;
1154 * Destroy a flow rule.
1156 * @see rte_flow_destroy()
1160 mlx4_flow_destroy(struct rte_eth_dev *dev,
1161 struct rte_flow *flow,
1162 struct rte_flow_error *error)
1164 struct priv *priv = dev->data->dev_private;
1165 int err = mlx4_flow_toggle(priv, flow, 0, error);
1169 LIST_REMOVE(flow, next);
1171 mlx4_rss_put(flow->rss);
1177 * Destroy user-configured flow rules.
1179 * This function skips internal flows rules.
1181 * @see rte_flow_flush()
1185 mlx4_flow_flush(struct rte_eth_dev *dev,
1186 struct rte_flow_error *error)
1188 struct priv *priv = dev->data->dev_private;
1189 struct rte_flow *flow = LIST_FIRST(&priv->flows);
1192 struct rte_flow *next = LIST_NEXT(flow, next);
1194 if (!flow->internal)
1195 mlx4_flow_destroy(dev, flow, error);
1202 * Helper function to determine the next configured VLAN filter.
1205 * Pointer to private structure.
1207 * VLAN ID to use as a starting point.
1210 * Next configured VLAN ID or a high value (>= 4096) if there is none.
1213 mlx4_flow_internal_next_vlan(struct priv *priv, uint16_t vlan)
1215 while (vlan < 4096) {
1216 if (priv->dev->data->vlan_filter_conf.ids[vlan / 64] &
1217 (UINT64_C(1) << (vlan % 64)))
1225 * Generate internal flow rules.
1227 * Various flow rules are created depending on the mode the device is in:
1230 * port MAC + broadcast + catch-all (VLAN filtering is ignored).
1232 * port MAC/VLAN + broadcast + catch-all multicast.
1234 * port MAC/VLAN + broadcast MAC/VLAN.
1236 * About MAC flow rules:
1238 * - MAC flow rules are generated from @p dev->data->mac_addrs
1239 * (@p priv->mac array).
1240 * - An additional flow rule for Ethernet broadcasts is also generated.
1241 * - All these are per-VLAN if @p DEV_RX_OFFLOAD_VLAN_FILTER
1242 * is enabled and VLAN filters are configured.
1245 * Pointer to private structure.
1247 * Perform verbose error reporting if not NULL.
1250 * 0 on success, a negative errno value otherwise and rte_errno is set.
1253 mlx4_flow_internal(struct priv *priv, struct rte_flow_error *error)
1255 struct rte_flow_attr attr = {
1256 .priority = MLX4_FLOW_PRIORITY_LAST,
1259 struct rte_flow_item_eth eth_spec;
1260 const struct rte_flow_item_eth eth_mask = {
1261 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
1263 const struct rte_flow_item_eth eth_allmulti = {
1264 .dst.addr_bytes = "\x01\x00\x00\x00\x00\x00",
1266 struct rte_flow_item_vlan vlan_spec;
1267 const struct rte_flow_item_vlan vlan_mask = {
1268 .tci = RTE_BE16(0x0fff),
1270 struct rte_flow_item pattern[] = {
1272 .type = MLX4_FLOW_ITEM_TYPE_INTERNAL,
1275 .type = RTE_FLOW_ITEM_TYPE_ETH,
1280 /* Replaced with VLAN if filtering is enabled. */
1281 .type = RTE_FLOW_ITEM_TYPE_END,
1284 .type = RTE_FLOW_ITEM_TYPE_END,
1288 * Round number of queues down to their previous power of 2 to
1289 * comply with RSS context limitations. Extra queues silently do not
1290 * get RSS by default.
1293 rte_align32pow2(priv->dev->data->nb_rx_queues + 1) >> 1;
1294 uint16_t queue[queues];
1295 struct rte_flow_action_rss action_rss = {
1296 .func = RTE_ETH_HASH_FUNCTION_DEFAULT,
1299 .key_len = MLX4_RSS_HASH_KEY_SIZE,
1300 .queue_num = queues,
1301 .key = mlx4_rss_hash_key_default,
1304 struct rte_flow_action actions[] = {
1306 .type = RTE_FLOW_ACTION_TYPE_RSS,
1307 .conf = &action_rss,
1310 .type = RTE_FLOW_ACTION_TYPE_END,
1313 struct ether_addr *rule_mac = ð_spec.dst;
1314 rte_be16_t *rule_vlan =
1315 (priv->dev->data->dev_conf.rxmode.offloads &
1316 DEV_RX_OFFLOAD_VLAN_FILTER) &&
1317 !priv->dev->data->promiscuous ?
1321 struct rte_flow *flow;
1325 /* Nothing to be done if there are no Rx queues. */
1328 /* Prepare default RSS configuration. */
1329 for (i = 0; i != queues; ++i)
1332 * Set up VLAN item if filtering is enabled and at least one VLAN
1333 * filter is configured.
1336 vlan = mlx4_flow_internal_next_vlan(priv, 0);
1338 pattern[2] = (struct rte_flow_item){
1339 .type = RTE_FLOW_ITEM_TYPE_VLAN,
1344 *rule_vlan = rte_cpu_to_be_16(vlan);
1349 for (i = 0; i != RTE_DIM(priv->mac) + 1; ++i) {
1350 const struct ether_addr *mac;
1352 /* Broadcasts are handled by an extra iteration. */
1353 if (i < RTE_DIM(priv->mac))
1354 mac = &priv->mac[i];
1356 mac = ð_mask.dst;
1357 if (is_zero_ether_addr(mac))
1359 /* Check if MAC flow rule is already present. */
1360 for (flow = LIST_FIRST(&priv->flows);
1361 flow && flow->internal;
1362 flow = LIST_NEXT(flow, next)) {
1363 const struct ibv_flow_spec_eth *eth =
1364 (const void *)((uintptr_t)flow->ibv_attr +
1365 sizeof(*flow->ibv_attr));
1370 assert(flow->ibv_attr->type == IBV_FLOW_ATTR_NORMAL);
1371 assert(flow->ibv_attr->num_of_specs == 1);
1372 assert(eth->type == IBV_FLOW_SPEC_ETH);
1375 (eth->val.vlan_tag != *rule_vlan ||
1376 eth->mask.vlan_tag != RTE_BE16(0x0fff)))
1378 if (!rule_vlan && eth->mask.vlan_tag)
1380 for (j = 0; j != sizeof(mac->addr_bytes); ++j)
1381 if (eth->val.dst_mac[j] != mac->addr_bytes[j] ||
1382 eth->mask.dst_mac[j] != UINT8_C(0xff) ||
1383 eth->val.src_mac[j] != UINT8_C(0x00) ||
1384 eth->mask.src_mac[j] != UINT8_C(0x00))
1386 if (j != sizeof(mac->addr_bytes))
1388 if (flow->rss->queues != queues ||
1389 memcmp(flow->rss->queue_id, action_rss.queue,
1390 queues * sizeof(flow->rss->queue_id[0])))
1394 if (!flow || !flow->internal) {
1395 /* Not found, create a new flow rule. */
1396 memcpy(rule_mac, mac, sizeof(*mac));
1397 flow = mlx4_flow_create(priv->dev, &attr, pattern,
1408 vlan = mlx4_flow_internal_next_vlan(priv, vlan + 1);
1412 /* Take care of promiscuous and all multicast flow rules. */
1413 if (priv->dev->data->promiscuous || priv->dev->data->all_multicast) {
1414 for (flow = LIST_FIRST(&priv->flows);
1415 flow && flow->internal;
1416 flow = LIST_NEXT(flow, next)) {
1417 if (priv->dev->data->promiscuous) {
1421 assert(priv->dev->data->all_multicast);
1426 if (flow && flow->internal) {
1428 if (flow->rss->queues != queues ||
1429 memcmp(flow->rss->queue_id, action_rss.queue,
1430 queues * sizeof(flow->rss->queue_id[0])))
1433 if (!flow || !flow->internal) {
1434 /* Not found, create a new flow rule. */
1435 if (priv->dev->data->promiscuous) {
1436 pattern[1].spec = NULL;
1437 pattern[1].mask = NULL;
1439 assert(priv->dev->data->all_multicast);
1440 pattern[1].spec = ð_allmulti;
1441 pattern[1].mask = ð_allmulti;
1443 pattern[2] = pattern[3];
1444 flow = mlx4_flow_create(priv->dev, &attr, pattern,
1451 assert(flow->promisc || flow->allmulti);
1455 /* Clear selection and clean up stale internal flow rules. */
1456 flow = LIST_FIRST(&priv->flows);
1457 while (flow && flow->internal) {
1458 struct rte_flow *next = LIST_NEXT(flow, next);
1461 claim_zero(mlx4_flow_destroy(priv->dev, flow, error));
1470 * Synchronize flow rules.
1472 * This function synchronizes flow rules with the state of the device by
1473 * taking into account isolated mode and whether target queues are
1477 * Pointer to private structure.
1479 * Perform verbose error reporting if not NULL.
1482 * 0 on success, a negative errno value otherwise and rte_errno is set.
1485 mlx4_flow_sync(struct priv *priv, struct rte_flow_error *error)
1487 struct rte_flow *flow;
1490 /* Internal flow rules are guaranteed to come first in the list. */
1491 if (priv->isolated) {
1493 * Get rid of them in isolated mode, stop at the first
1494 * non-internal rule found.
1496 for (flow = LIST_FIRST(&priv->flows);
1497 flow && flow->internal;
1498 flow = LIST_FIRST(&priv->flows))
1499 claim_zero(mlx4_flow_destroy(priv->dev, flow, error));
1501 /* Refresh internal rules. */
1502 ret = mlx4_flow_internal(priv, error);
1506 /* Toggle the remaining flow rules . */
1507 LIST_FOREACH(flow, &priv->flows, next) {
1508 ret = mlx4_flow_toggle(priv, flow, priv->started, error);
1513 assert(!priv->drop);
1518 * Clean up all flow rules.
1520 * Unlike mlx4_flow_flush(), this function takes care of all remaining flow
1521 * rules regardless of whether they are internal or user-configured.
1524 * Pointer to private structure.
1527 mlx4_flow_clean(struct priv *priv)
1529 struct rte_flow *flow;
1531 while ((flow = LIST_FIRST(&priv->flows)))
1532 mlx4_flow_destroy(priv->dev, flow, NULL);
1533 assert(LIST_EMPTY(&priv->rss));
1536 static const struct rte_flow_ops mlx4_flow_ops = {
1537 .validate = mlx4_flow_validate,
1538 .create = mlx4_flow_create,
1539 .destroy = mlx4_flow_destroy,
1540 .flush = mlx4_flow_flush,
1541 .isolate = mlx4_flow_isolate,
1545 * Manage filter operations.
1548 * Pointer to Ethernet device structure.
1549 * @param filter_type
1552 * Operation to perform.
1554 * Pointer to operation-specific structure.
1557 * 0 on success, negative errno value otherwise and rte_errno is set.
1560 mlx4_filter_ctrl(struct rte_eth_dev *dev,
1561 enum rte_filter_type filter_type,
1562 enum rte_filter_op filter_op,
1565 switch (filter_type) {
1566 case RTE_ETH_FILTER_GENERIC:
1567 if (filter_op != RTE_ETH_FILTER_GET)
1569 *(const void **)arg = &mlx4_flow_ops;
1572 ERROR("%p: filter type (%d) not supported",
1573 (void *)dev, filter_type);
1576 rte_errno = ENOTSUP;