4 * Copyright 2017 6WIND S.A.
5 * Copyright 2017 Mellanox
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of 6WIND S.A. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * Flow API operations for mlx4 driver.
39 #include <arpa/inet.h>
46 #include <sys/queue.h>
48 /* Verbs headers do not support -pedantic. */
50 #pragma GCC diagnostic ignored "-Wpedantic"
52 #include <infiniband/verbs.h>
54 #pragma GCC diagnostic error "-Wpedantic"
57 #include <rte_byteorder.h>
58 #include <rte_errno.h>
59 #include <rte_eth_ctrl.h>
60 #include <rte_ethdev.h>
61 #include <rte_ether.h>
63 #include <rte_flow_driver.h>
64 #include <rte_malloc.h>
68 #include "mlx4_flow.h"
69 #include "mlx4_rxtx.h"
70 #include "mlx4_utils.h"
72 /** Static initializer for a list of subsequent item types. */
73 #define NEXT_ITEM(...) \
74 (const enum rte_flow_item_type []){ \
75 __VA_ARGS__, RTE_FLOW_ITEM_TYPE_END, \
78 /** Processor structure associated with a flow item. */
79 struct mlx4_flow_proc_item {
80 /** Bit-mask for fields supported by this PMD. */
81 const void *mask_support;
82 /** Bit-mask to use when @p item->mask is not provided. */
83 const void *mask_default;
84 /** Size in bytes for @p mask_support and @p mask_default. */
85 const unsigned int mask_sz;
86 /** Merge a pattern item into a flow rule handle. */
87 int (*merge)(struct rte_flow *flow,
88 const struct rte_flow_item *item,
89 const struct mlx4_flow_proc_item *proc,
90 struct rte_flow_error *error);
91 /** Size in bytes of the destination structure. */
92 const unsigned int dst_sz;
93 /** List of possible subsequent items. */
94 const enum rte_flow_item_type *const next_item;
97 /** Shared resources for drop flow rules. */
99 struct ibv_qp *qp; /**< QP target. */
100 struct ibv_cq *cq; /**< CQ associated with above QP. */
101 struct priv *priv; /**< Back pointer to private data. */
102 uint32_t refcnt; /**< Reference count. */
106 * Convert DPDK RSS hash fields to their Verbs equivalent.
109 * Hash fields in DPDK format (see struct rte_eth_rss_conf).
112 * A valid Verbs RSS hash fields mask for mlx4 on success, (uint64_t)-1
113 * otherwise and rte_errno is set.
116 mlx4_conv_rss_hf(uint64_t rss_hf)
118 enum { IPV4, IPV6, TCP, UDP, };
119 static const uint64_t in[] = {
120 [IPV4] = (ETH_RSS_IPV4 |
122 ETH_RSS_NONFRAG_IPV4_TCP |
123 ETH_RSS_NONFRAG_IPV4_UDP |
124 ETH_RSS_NONFRAG_IPV4_OTHER),
125 [IPV6] = (ETH_RSS_IPV6 |
127 ETH_RSS_NONFRAG_IPV6_TCP |
128 ETH_RSS_NONFRAG_IPV6_UDP |
129 ETH_RSS_NONFRAG_IPV6_OTHER |
131 ETH_RSS_IPV6_TCP_EX |
132 ETH_RSS_IPV6_UDP_EX),
133 [TCP] = (ETH_RSS_NONFRAG_IPV4_TCP |
134 ETH_RSS_NONFRAG_IPV6_TCP |
135 ETH_RSS_IPV6_TCP_EX),
137 * UDP support is temporarily disabled due to an
138 * implementation issue in the kernel.
142 static const uint64_t out[RTE_DIM(in)] = {
143 [IPV4] = IBV_RX_HASH_SRC_IPV4 | IBV_RX_HASH_DST_IPV4,
144 [IPV6] = IBV_RX_HASH_SRC_IPV6 | IBV_RX_HASH_DST_IPV6,
145 [TCP] = IBV_RX_HASH_SRC_PORT_TCP | IBV_RX_HASH_DST_PORT_TCP,
146 [UDP] = IBV_RX_HASH_SRC_PORT_UDP | IBV_RX_HASH_DST_PORT_UDP,
152 for (i = 0; i != RTE_DIM(in); ++i)
153 if (rss_hf & in[i]) {
154 seen |= rss_hf & in[i];
157 if (!(rss_hf & ~seen))
164 * Merge Ethernet pattern item into flow rule handle.
166 * Additional mlx4-specific constraints on supported fields:
168 * - No support for partial masks, except in the specific case of matching
169 * all multicast traffic (@p spec->dst and @p mask->dst equal to
170 * 01:00:00:00:00:00).
171 * - Not providing @p item->spec or providing an empty @p mask->dst is
172 * *only* supported if the rule doesn't specify additional matching
173 * criteria (i.e. rule is promiscuous-like).
175 * @param[in, out] flow
176 * Flow rule handle to update.
178 * Pattern item to merge.
180 * Associated item-processing object.
182 * Perform verbose error reporting if not NULL.
185 * 0 on success, a negative errno value otherwise and rte_errno is set.
188 mlx4_flow_merge_eth(struct rte_flow *flow,
189 const struct rte_flow_item *item,
190 const struct mlx4_flow_proc_item *proc,
191 struct rte_flow_error *error)
193 const struct rte_flow_item_eth *spec = item->spec;
194 const struct rte_flow_item_eth *mask =
195 spec ? (item->mask ? item->mask : proc->mask_default) : NULL;
196 struct ibv_flow_spec_eth *eth;
203 uint32_t sum_dst = 0;
204 uint32_t sum_src = 0;
206 for (i = 0; i != sizeof(mask->dst.addr_bytes); ++i) {
207 sum_dst += mask->dst.addr_bytes[i];
208 sum_src += mask->src.addr_bytes[i];
211 msg = "mlx4 does not support source MAC matching";
213 } else if (!sum_dst) {
215 } else if (sum_dst == 1 && mask->dst.addr_bytes[0] == 1) {
216 if (!(spec->dst.addr_bytes[0] & 1)) {
217 msg = "mlx4 does not support the explicit"
218 " exclusion of all multicast traffic";
222 } else if (sum_dst != (UINT8_C(0xff) * ETHER_ADDR_LEN)) {
223 msg = "mlx4 does not support matching partial"
231 flow->ibv_attr->type = IBV_FLOW_ATTR_ALL_DEFAULT;
234 if (flow->allmulti) {
235 flow->ibv_attr->type = IBV_FLOW_ATTR_MC_DEFAULT;
238 ++flow->ibv_attr->num_of_specs;
239 eth = (void *)((uintptr_t)flow->ibv_attr + flow->ibv_attr_size);
240 *eth = (struct ibv_flow_spec_eth) {
241 .type = IBV_FLOW_SPEC_ETH,
242 .size = sizeof(*eth),
244 memcpy(eth->val.dst_mac, spec->dst.addr_bytes, ETHER_ADDR_LEN);
245 memcpy(eth->mask.dst_mac, mask->dst.addr_bytes, ETHER_ADDR_LEN);
246 /* Remove unwanted bits from values. */
247 for (i = 0; i < ETHER_ADDR_LEN; ++i) {
248 eth->val.dst_mac[i] &= eth->mask.dst_mac[i];
252 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
257 * Merge VLAN pattern item into flow rule handle.
259 * Additional mlx4-specific constraints on supported fields:
261 * - Matching *all* VLAN traffic by omitting @p item->spec or providing an
262 * empty @p item->mask would also include non-VLAN traffic. Doing so is
263 * therefore unsupported.
264 * - No support for partial masks.
266 * @param[in, out] flow
267 * Flow rule handle to update.
269 * Pattern item to merge.
271 * Associated item-processing object.
273 * Perform verbose error reporting if not NULL.
276 * 0 on success, a negative errno value otherwise and rte_errno is set.
279 mlx4_flow_merge_vlan(struct rte_flow *flow,
280 const struct rte_flow_item *item,
281 const struct mlx4_flow_proc_item *proc,
282 struct rte_flow_error *error)
284 const struct rte_flow_item_vlan *spec = item->spec;
285 const struct rte_flow_item_vlan *mask =
286 spec ? (item->mask ? item->mask : proc->mask_default) : NULL;
287 struct ibv_flow_spec_eth *eth;
290 if (!mask || !mask->tci) {
291 msg = "mlx4 cannot match all VLAN traffic while excluding"
292 " non-VLAN traffic, TCI VID must be specified";
295 if (mask->tci != RTE_BE16(0x0fff)) {
296 msg = "mlx4 does not support partial TCI VID matching";
301 eth = (void *)((uintptr_t)flow->ibv_attr + flow->ibv_attr_size -
303 eth->val.vlan_tag = spec->tci;
304 eth->mask.vlan_tag = mask->tci;
305 eth->val.vlan_tag &= eth->mask.vlan_tag;
308 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
313 * Merge IPv4 pattern item into flow rule handle.
315 * Additional mlx4-specific constraints on supported fields:
317 * - No support for partial masks.
319 * @param[in, out] flow
320 * Flow rule handle to update.
322 * Pattern item to merge.
324 * Associated item-processing object.
326 * Perform verbose error reporting if not NULL.
329 * 0 on success, a negative errno value otherwise and rte_errno is set.
332 mlx4_flow_merge_ipv4(struct rte_flow *flow,
333 const struct rte_flow_item *item,
334 const struct mlx4_flow_proc_item *proc,
335 struct rte_flow_error *error)
337 const struct rte_flow_item_ipv4 *spec = item->spec;
338 const struct rte_flow_item_ipv4 *mask =
339 spec ? (item->mask ? item->mask : proc->mask_default) : NULL;
340 struct ibv_flow_spec_ipv4 *ipv4;
344 ((uint32_t)(mask->hdr.src_addr + 1) > UINT32_C(1) ||
345 (uint32_t)(mask->hdr.dst_addr + 1) > UINT32_C(1))) {
346 msg = "mlx4 does not support matching partial IPv4 fields";
351 ++flow->ibv_attr->num_of_specs;
352 ipv4 = (void *)((uintptr_t)flow->ibv_attr + flow->ibv_attr_size);
353 *ipv4 = (struct ibv_flow_spec_ipv4) {
354 .type = IBV_FLOW_SPEC_IPV4,
355 .size = sizeof(*ipv4),
359 ipv4->val = (struct ibv_flow_ipv4_filter) {
360 .src_ip = spec->hdr.src_addr,
361 .dst_ip = spec->hdr.dst_addr,
363 ipv4->mask = (struct ibv_flow_ipv4_filter) {
364 .src_ip = mask->hdr.src_addr,
365 .dst_ip = mask->hdr.dst_addr,
367 /* Remove unwanted bits from values. */
368 ipv4->val.src_ip &= ipv4->mask.src_ip;
369 ipv4->val.dst_ip &= ipv4->mask.dst_ip;
372 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
377 * Merge UDP pattern item into flow rule handle.
379 * Additional mlx4-specific constraints on supported fields:
381 * - No support for partial masks.
382 * - Due to HW/FW limitation, flow rule priority is not taken into account
383 * when matching UDP destination ports, doing is therefore only supported
384 * at the highest priority level (0).
386 * @param[in, out] flow
387 * Flow rule handle to update.
389 * Pattern item to merge.
391 * Associated item-processing object.
393 * Perform verbose error reporting if not NULL.
396 * 0 on success, a negative errno value otherwise and rte_errno is set.
399 mlx4_flow_merge_udp(struct rte_flow *flow,
400 const struct rte_flow_item *item,
401 const struct mlx4_flow_proc_item *proc,
402 struct rte_flow_error *error)
404 const struct rte_flow_item_udp *spec = item->spec;
405 const struct rte_flow_item_udp *mask =
406 spec ? (item->mask ? item->mask : proc->mask_default) : NULL;
407 struct ibv_flow_spec_tcp_udp *udp;
411 ((uint16_t)(mask->hdr.src_port + 1) > UINT16_C(1) ||
412 (uint16_t)(mask->hdr.dst_port + 1) > UINT16_C(1))) {
413 msg = "mlx4 does not support matching partial UDP fields";
416 if (mask && mask->hdr.dst_port && flow->priority) {
417 msg = "combining UDP destination port matching with a nonzero"
418 " priority level is not supported";
423 ++flow->ibv_attr->num_of_specs;
424 udp = (void *)((uintptr_t)flow->ibv_attr + flow->ibv_attr_size);
425 *udp = (struct ibv_flow_spec_tcp_udp) {
426 .type = IBV_FLOW_SPEC_UDP,
427 .size = sizeof(*udp),
431 udp->val.dst_port = spec->hdr.dst_port;
432 udp->val.src_port = spec->hdr.src_port;
433 udp->mask.dst_port = mask->hdr.dst_port;
434 udp->mask.src_port = mask->hdr.src_port;
435 /* Remove unwanted bits from values. */
436 udp->val.src_port &= udp->mask.src_port;
437 udp->val.dst_port &= udp->mask.dst_port;
440 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
445 * Merge TCP pattern item into flow rule handle.
447 * Additional mlx4-specific constraints on supported fields:
449 * - No support for partial masks.
451 * @param[in, out] flow
452 * Flow rule handle to update.
454 * Pattern item to merge.
456 * Associated item-processing object.
458 * Perform verbose error reporting if not NULL.
461 * 0 on success, a negative errno value otherwise and rte_errno is set.
464 mlx4_flow_merge_tcp(struct rte_flow *flow,
465 const struct rte_flow_item *item,
466 const struct mlx4_flow_proc_item *proc,
467 struct rte_flow_error *error)
469 const struct rte_flow_item_tcp *spec = item->spec;
470 const struct rte_flow_item_tcp *mask =
471 spec ? (item->mask ? item->mask : proc->mask_default) : NULL;
472 struct ibv_flow_spec_tcp_udp *tcp;
476 ((uint16_t)(mask->hdr.src_port + 1) > UINT16_C(1) ||
477 (uint16_t)(mask->hdr.dst_port + 1) > UINT16_C(1))) {
478 msg = "mlx4 does not support matching partial TCP fields";
483 ++flow->ibv_attr->num_of_specs;
484 tcp = (void *)((uintptr_t)flow->ibv_attr + flow->ibv_attr_size);
485 *tcp = (struct ibv_flow_spec_tcp_udp) {
486 .type = IBV_FLOW_SPEC_TCP,
487 .size = sizeof(*tcp),
491 tcp->val.dst_port = spec->hdr.dst_port;
492 tcp->val.src_port = spec->hdr.src_port;
493 tcp->mask.dst_port = mask->hdr.dst_port;
494 tcp->mask.src_port = mask->hdr.src_port;
495 /* Remove unwanted bits from values. */
496 tcp->val.src_port &= tcp->mask.src_port;
497 tcp->val.dst_port &= tcp->mask.dst_port;
500 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
505 * Perform basic sanity checks on a pattern item.
508 * Item specification.
510 * Associated item-processing object.
512 * Perform verbose error reporting if not NULL.
515 * 0 on success, a negative errno value otherwise and rte_errno is set.
518 mlx4_flow_item_check(const struct rte_flow_item *item,
519 const struct mlx4_flow_proc_item *proc,
520 struct rte_flow_error *error)
525 /* item->last and item->mask cannot exist without item->spec. */
526 if (!item->spec && (item->mask || item->last))
527 return rte_flow_error_set
528 (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, item,
529 "\"mask\" or \"last\" field provided without a"
530 " corresponding \"spec\"");
531 /* No spec, no mask, no problem. */
535 (const uint8_t *)item->mask :
536 (const uint8_t *)proc->mask_default;
539 * Single-pass check to make sure that:
540 * - Mask is supported, no bits are set outside proc->mask_support.
541 * - Both item->spec and item->last are included in mask.
543 for (i = 0; i != proc->mask_sz; ++i) {
546 if ((mask[i] | ((const uint8_t *)proc->mask_support)[i]) !=
547 ((const uint8_t *)proc->mask_support)[i])
548 return rte_flow_error_set
549 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
550 item, "unsupported field found in \"mask\"");
552 (((const uint8_t *)item->spec)[i] & mask[i]) !=
553 (((const uint8_t *)item->last)[i] & mask[i]))
554 return rte_flow_error_set
555 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
557 "range between \"spec\" and \"last\""
558 " is larger than \"mask\"");
563 /** Graph of supported items and associated actions. */
564 static const struct mlx4_flow_proc_item mlx4_flow_proc_item_list[] = {
565 [RTE_FLOW_ITEM_TYPE_END] = {
566 .next_item = NEXT_ITEM(RTE_FLOW_ITEM_TYPE_ETH),
568 [RTE_FLOW_ITEM_TYPE_ETH] = {
569 .next_item = NEXT_ITEM(RTE_FLOW_ITEM_TYPE_VLAN,
570 RTE_FLOW_ITEM_TYPE_IPV4),
571 .mask_support = &(const struct rte_flow_item_eth){
572 /* Only destination MAC can be matched. */
573 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
575 .mask_default = &rte_flow_item_eth_mask,
576 .mask_sz = sizeof(struct rte_flow_item_eth),
577 .merge = mlx4_flow_merge_eth,
578 .dst_sz = sizeof(struct ibv_flow_spec_eth),
580 [RTE_FLOW_ITEM_TYPE_VLAN] = {
581 .next_item = NEXT_ITEM(RTE_FLOW_ITEM_TYPE_IPV4),
582 .mask_support = &(const struct rte_flow_item_vlan){
583 /* Only TCI VID matching is supported. */
584 .tci = RTE_BE16(0x0fff),
586 .mask_default = &rte_flow_item_vlan_mask,
587 .mask_sz = sizeof(struct rte_flow_item_vlan),
588 .merge = mlx4_flow_merge_vlan,
591 [RTE_FLOW_ITEM_TYPE_IPV4] = {
592 .next_item = NEXT_ITEM(RTE_FLOW_ITEM_TYPE_UDP,
593 RTE_FLOW_ITEM_TYPE_TCP),
594 .mask_support = &(const struct rte_flow_item_ipv4){
596 .src_addr = RTE_BE32(0xffffffff),
597 .dst_addr = RTE_BE32(0xffffffff),
600 .mask_default = &rte_flow_item_ipv4_mask,
601 .mask_sz = sizeof(struct rte_flow_item_ipv4),
602 .merge = mlx4_flow_merge_ipv4,
603 .dst_sz = sizeof(struct ibv_flow_spec_ipv4),
605 [RTE_FLOW_ITEM_TYPE_UDP] = {
606 .mask_support = &(const struct rte_flow_item_udp){
608 .src_port = RTE_BE16(0xffff),
609 .dst_port = RTE_BE16(0xffff),
612 .mask_default = &rte_flow_item_udp_mask,
613 .mask_sz = sizeof(struct rte_flow_item_udp),
614 .merge = mlx4_flow_merge_udp,
615 .dst_sz = sizeof(struct ibv_flow_spec_tcp_udp),
617 [RTE_FLOW_ITEM_TYPE_TCP] = {
618 .mask_support = &(const struct rte_flow_item_tcp){
620 .src_port = RTE_BE16(0xffff),
621 .dst_port = RTE_BE16(0xffff),
624 .mask_default = &rte_flow_item_tcp_mask,
625 .mask_sz = sizeof(struct rte_flow_item_tcp),
626 .merge = mlx4_flow_merge_tcp,
627 .dst_sz = sizeof(struct ibv_flow_spec_tcp_udp),
632 * Make sure a flow rule is supported and initialize associated structure.
635 * Pointer to private structure.
637 * Flow rule attributes.
639 * Pattern specification (list terminated by the END pattern item).
641 * Associated actions (list terminated by the END action).
643 * Perform verbose error reporting if not NULL.
644 * @param[in, out] addr
645 * Buffer where the resulting flow rule handle pointer must be stored.
646 * If NULL, stop processing after validation stage.
649 * 0 on success, a negative errno value otherwise and rte_errno is set.
652 mlx4_flow_prepare(struct priv *priv,
653 const struct rte_flow_attr *attr,
654 const struct rte_flow_item pattern[],
655 const struct rte_flow_action actions[],
656 struct rte_flow_error *error,
657 struct rte_flow **addr)
659 const struct rte_flow_item *item;
660 const struct rte_flow_action *action;
661 const struct mlx4_flow_proc_item *proc;
662 struct rte_flow temp = { .ibv_attr_size = sizeof(*temp.ibv_attr) };
663 struct rte_flow *flow = &temp;
664 const char *msg = NULL;
667 return rte_flow_error_set
668 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
669 NULL, "groups are not supported");
670 if (attr->priority > MLX4_FLOW_PRIORITY_LAST)
671 return rte_flow_error_set
672 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
673 NULL, "maximum priority level is "
674 MLX4_STR_EXPAND(MLX4_FLOW_PRIORITY_LAST));
676 return rte_flow_error_set
677 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
678 NULL, "egress is not supported");
680 return rte_flow_error_set
681 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
682 NULL, "only ingress is supported");
684 proc = mlx4_flow_proc_item_list;
685 flow->priority = attr->priority;
686 /* Go over pattern. */
687 for (item = pattern; item->type; ++item) {
688 const struct mlx4_flow_proc_item *next = NULL;
692 if (item->type == RTE_FLOW_ITEM_TYPE_VOID)
694 if (item->type == MLX4_FLOW_ITEM_TYPE_INTERNAL) {
698 if (flow->promisc || flow->allmulti) {
699 msg = "mlx4 does not support additional matching"
700 " criteria combined with indiscriminate"
701 " matching on Ethernet headers";
702 goto exit_item_not_supported;
704 for (i = 0; proc->next_item && proc->next_item[i]; ++i) {
705 if (proc->next_item[i] == item->type) {
706 next = &mlx4_flow_proc_item_list[item->type];
711 goto exit_item_not_supported;
714 * Perform basic sanity checks only once, while handle is
718 err = mlx4_flow_item_check(item, proc, error);
723 err = proc->merge(flow, item, proc, error);
727 flow->ibv_attr_size += proc->dst_sz;
729 /* Go over actions list. */
730 for (action = actions; action->type; ++action) {
731 switch (action->type) {
732 const struct rte_flow_action_queue *queue;
733 const struct rte_flow_action_rss *rss;
734 const struct rte_eth_rss_conf *rss_conf;
737 case RTE_FLOW_ACTION_TYPE_VOID:
739 case RTE_FLOW_ACTION_TYPE_DROP:
742 case RTE_FLOW_ACTION_TYPE_QUEUE:
745 queue = action->conf;
746 if (queue->index >= priv->dev->data->nb_rx_queues) {
747 msg = "queue target index beyond number of"
748 " configured Rx queues";
749 goto exit_action_not_supported;
751 flow->rss = mlx4_rss_get
752 (priv, 0, mlx4_rss_hash_key_default, 1,
755 msg = "not enough resources for additional"
756 " single-queue RSS context";
757 goto exit_action_not_supported;
760 case RTE_FLOW_ACTION_TYPE_RSS:
764 /* Default RSS configuration if none is provided. */
768 &(struct rte_eth_rss_conf){
769 .rss_key = mlx4_rss_hash_key_default,
770 .rss_key_len = MLX4_RSS_HASH_KEY_SIZE,
771 .rss_hf = (ETH_RSS_IPV4 |
772 ETH_RSS_NONFRAG_IPV4_TCP |
774 ETH_RSS_NONFRAG_IPV6_TCP),
777 for (i = 0; i < rss->num; ++i)
779 priv->dev->data->nb_rx_queues)
782 msg = "queue index target beyond number of"
783 " configured Rx queues";
784 goto exit_action_not_supported;
786 if (!rte_is_power_of_2(rss->num)) {
787 msg = "for RSS, mlx4 requires the number of"
788 " queues to be a power of two";
789 goto exit_action_not_supported;
791 if (rss_conf->rss_key_len !=
792 sizeof(flow->rss->key)) {
793 msg = "mlx4 supports exactly one RSS hash key"
795 MLX4_STR_EXPAND(MLX4_RSS_HASH_KEY_SIZE);
796 goto exit_action_not_supported;
798 for (i = 1; i < rss->num; ++i)
799 if (rss->queue[i] - rss->queue[i - 1] != 1)
802 msg = "mlx4 requires RSS contexts to use"
803 " consecutive queue indices only";
804 goto exit_action_not_supported;
806 if (rss->queue[0] % rss->num) {
807 msg = "mlx4 requires the first queue of a RSS"
808 " context to be aligned on a multiple"
809 " of the context size";
810 goto exit_action_not_supported;
812 flow->rss = mlx4_rss_get
813 (priv, mlx4_conv_rss_hf(rss_conf->rss_hf),
814 rss_conf->rss_key, rss->num, rss->queue);
816 msg = "either invalid parameters or not enough"
817 " resources for additional multi-queue"
819 goto exit_action_not_supported;
823 goto exit_action_not_supported;
826 if (!flow->rss && !flow->drop)
827 return rte_flow_error_set
828 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
829 NULL, "no valid action");
830 /* Validation ends here. */
833 mlx4_rss_put(flow->rss);
837 /* Allocate proper handle based on collected data. */
838 const struct mlx4_malloc_vec vec[] = {
840 .align = alignof(struct rte_flow),
841 .size = sizeof(*flow),
842 .addr = (void **)&flow,
845 .align = alignof(struct ibv_flow_attr),
846 .size = temp.ibv_attr_size,
847 .addr = (void **)&temp.ibv_attr,
851 if (!mlx4_zmallocv(__func__, vec, RTE_DIM(vec))) {
853 mlx4_rss_put(temp.rss);
854 return rte_flow_error_set
856 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
857 "flow rule handle allocation failure");
859 /* Most fields will be updated by second pass. */
860 *flow = (struct rte_flow){
861 .ibv_attr = temp.ibv_attr,
862 .ibv_attr_size = sizeof(*flow->ibv_attr),
865 *flow->ibv_attr = (struct ibv_flow_attr){
866 .type = IBV_FLOW_ATTR_NORMAL,
867 .size = sizeof(*flow->ibv_attr),
868 .priority = attr->priority,
875 exit_item_not_supported:
876 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
877 item, msg ? msg : "item not supported");
878 exit_action_not_supported:
879 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
880 action, msg ? msg : "action not supported");
884 * Validate a flow supported by the NIC.
886 * @see rte_flow_validate()
890 mlx4_flow_validate(struct rte_eth_dev *dev,
891 const struct rte_flow_attr *attr,
892 const struct rte_flow_item pattern[],
893 const struct rte_flow_action actions[],
894 struct rte_flow_error *error)
896 struct priv *priv = dev->data->dev_private;
898 return mlx4_flow_prepare(priv, attr, pattern, actions, error, NULL);
902 * Get a drop flow rule resources instance.
905 * Pointer to private structure.
908 * Pointer to drop flow resources on success, NULL otherwise and rte_errno
911 static struct mlx4_drop *
912 mlx4_drop_get(struct priv *priv)
914 struct mlx4_drop *drop = priv->drop;
917 assert(drop->refcnt);
918 assert(drop->priv == priv);
922 drop = rte_malloc(__func__, sizeof(*drop), 0);
925 *drop = (struct mlx4_drop){
929 drop->cq = ibv_create_cq(priv->ctx, 1, NULL, NULL, 0);
932 drop->qp = ibv_create_qp(priv->pd,
933 &(struct ibv_qp_init_attr){
936 .qp_type = IBV_QPT_RAW_PACKET,
944 claim_zero(ibv_destroy_qp(drop->qp));
946 claim_zero(ibv_destroy_cq(drop->cq));
954 * Give back a drop flow rule resources instance.
957 * Pointer to drop flow rule resources.
960 mlx4_drop_put(struct mlx4_drop *drop)
962 assert(drop->refcnt);
965 drop->priv->drop = NULL;
966 claim_zero(ibv_destroy_qp(drop->qp));
967 claim_zero(ibv_destroy_cq(drop->cq));
972 * Toggle a configured flow rule.
975 * Pointer to private structure.
977 * Flow rule handle to toggle.
979 * Whether associated Verbs flow must be created or removed.
981 * Perform verbose error reporting if not NULL.
984 * 0 on success, a negative errno value otherwise and rte_errno is set.
987 mlx4_flow_toggle(struct priv *priv,
988 struct rte_flow *flow,
990 struct rte_flow_error *error)
992 struct ibv_qp *qp = NULL;
999 claim_zero(ibv_destroy_flow(flow->ibv_flow));
1000 flow->ibv_flow = NULL;
1002 mlx4_drop_put(priv->drop);
1004 mlx4_rss_detach(flow->rss);
1007 assert(flow->ibv_attr);
1008 if (!flow->internal &&
1010 flow->ibv_attr->priority == MLX4_FLOW_PRIORITY_LAST) {
1011 if (flow->ibv_flow) {
1012 claim_zero(ibv_destroy_flow(flow->ibv_flow));
1013 flow->ibv_flow = NULL;
1015 mlx4_drop_put(priv->drop);
1017 mlx4_rss_detach(flow->rss);
1020 msg = ("priority level "
1021 MLX4_STR_EXPAND(MLX4_FLOW_PRIORITY_LAST)
1022 " is reserved when not in isolated mode");
1026 struct mlx4_rss *rss = flow->rss;
1030 /* Stop at the first nonexistent target queue. */
1031 for (i = 0; i != rss->queues; ++i)
1032 if (rss->queue_id[i] >=
1033 priv->dev->data->nb_rx_queues ||
1034 !priv->dev->data->rx_queues[rss->queue_id[i]]) {
1038 if (flow->ibv_flow) {
1039 if (missing ^ !flow->drop)
1041 /* Verbs flow needs updating. */
1042 claim_zero(ibv_destroy_flow(flow->ibv_flow));
1043 flow->ibv_flow = NULL;
1045 mlx4_drop_put(priv->drop);
1047 mlx4_rss_detach(rss);
1050 err = mlx4_rss_attach(rss);
1053 msg = "cannot create indirection table or hash"
1054 " QP to associate flow rule with";
1059 /* A missing target queue drops traffic implicitly. */
1060 flow->drop = missing;
1065 mlx4_drop_get(priv);
1068 msg = "resources for drop flow rule cannot be created";
1071 qp = priv->drop->qp;
1076 flow->ibv_flow = ibv_create_flow(qp, flow->ibv_attr);
1080 mlx4_drop_put(priv->drop);
1082 mlx4_rss_detach(flow->rss);
1084 msg = "flow rule rejected by device";
1086 return rte_flow_error_set
1087 (error, err, RTE_FLOW_ERROR_TYPE_HANDLE, flow, msg);
1093 * @see rte_flow_create()
1096 static struct rte_flow *
1097 mlx4_flow_create(struct rte_eth_dev *dev,
1098 const struct rte_flow_attr *attr,
1099 const struct rte_flow_item pattern[],
1100 const struct rte_flow_action actions[],
1101 struct rte_flow_error *error)
1103 struct priv *priv = dev->data->dev_private;
1104 struct rte_flow *flow;
1107 err = mlx4_flow_prepare(priv, attr, pattern, actions, error, &flow);
1110 err = mlx4_flow_toggle(priv, flow, priv->started, error);
1112 struct rte_flow *curr = LIST_FIRST(&priv->flows);
1114 /* New rules are inserted after internal ones. */
1115 if (!curr || !curr->internal) {
1116 LIST_INSERT_HEAD(&priv->flows, flow, next);
1118 while (LIST_NEXT(curr, next) &&
1119 LIST_NEXT(curr, next)->internal)
1120 curr = LIST_NEXT(curr, next);
1121 LIST_INSERT_AFTER(curr, flow, next);
1126 mlx4_rss_put(flow->rss);
1127 rte_flow_error_set(error, -err, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1134 * Configure isolated mode.
1136 * @see rte_flow_isolate()
1140 mlx4_flow_isolate(struct rte_eth_dev *dev,
1142 struct rte_flow_error *error)
1144 struct priv *priv = dev->data->dev_private;
1146 if (!!enable == !!priv->isolated)
1148 priv->isolated = !!enable;
1149 if (mlx4_flow_sync(priv, error)) {
1150 priv->isolated = !enable;
1157 * Destroy a flow rule.
1159 * @see rte_flow_destroy()
1163 mlx4_flow_destroy(struct rte_eth_dev *dev,
1164 struct rte_flow *flow,
1165 struct rte_flow_error *error)
1167 struct priv *priv = dev->data->dev_private;
1168 int err = mlx4_flow_toggle(priv, flow, 0, error);
1172 LIST_REMOVE(flow, next);
1174 mlx4_rss_put(flow->rss);
1180 * Destroy user-configured flow rules.
1182 * This function skips internal flows rules.
1184 * @see rte_flow_flush()
1188 mlx4_flow_flush(struct rte_eth_dev *dev,
1189 struct rte_flow_error *error)
1191 struct priv *priv = dev->data->dev_private;
1192 struct rte_flow *flow = LIST_FIRST(&priv->flows);
1195 struct rte_flow *next = LIST_NEXT(flow, next);
1197 if (!flow->internal)
1198 mlx4_flow_destroy(dev, flow, error);
1205 * Helper function to determine the next configured VLAN filter.
1208 * Pointer to private structure.
1210 * VLAN ID to use as a starting point.
1213 * Next configured VLAN ID or a high value (>= 4096) if there is none.
1216 mlx4_flow_internal_next_vlan(struct priv *priv, uint16_t vlan)
1218 while (vlan < 4096) {
1219 if (priv->dev->data->vlan_filter_conf.ids[vlan / 64] &
1220 (UINT64_C(1) << (vlan % 64)))
1228 * Generate internal flow rules.
1230 * Various flow rules are created depending on the mode the device is in:
1233 * port MAC + broadcast + catch-all (VLAN filtering is ignored).
1235 * port MAC/VLAN + broadcast + catch-all multicast.
1237 * port MAC/VLAN + broadcast MAC/VLAN.
1239 * About MAC flow rules:
1241 * - MAC flow rules are generated from @p dev->data->mac_addrs
1242 * (@p priv->mac array).
1243 * - An additional flow rule for Ethernet broadcasts is also generated.
1244 * - All these are per-VLAN if @p dev->data->dev_conf.rxmode.hw_vlan_filter
1245 * is enabled and VLAN filters are configured.
1248 * Pointer to private structure.
1250 * Perform verbose error reporting if not NULL.
1253 * 0 on success, a negative errno value otherwise and rte_errno is set.
1256 mlx4_flow_internal(struct priv *priv, struct rte_flow_error *error)
1258 struct rte_flow_attr attr = {
1259 .priority = MLX4_FLOW_PRIORITY_LAST,
1262 struct rte_flow_item_eth eth_spec;
1263 const struct rte_flow_item_eth eth_mask = {
1264 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
1266 const struct rte_flow_item_eth eth_allmulti = {
1267 .dst.addr_bytes = "\x01\x00\x00\x00\x00\x00",
1269 struct rte_flow_item_vlan vlan_spec;
1270 const struct rte_flow_item_vlan vlan_mask = {
1271 .tci = RTE_BE16(0x0fff),
1273 struct rte_flow_item pattern[] = {
1275 .type = MLX4_FLOW_ITEM_TYPE_INTERNAL,
1278 .type = RTE_FLOW_ITEM_TYPE_ETH,
1283 /* Replaced with VLAN if filtering is enabled. */
1284 .type = RTE_FLOW_ITEM_TYPE_END,
1287 .type = RTE_FLOW_ITEM_TYPE_END,
1291 * Round number of queues down to their previous power of 2 to
1292 * comply with RSS context limitations. Extra queues silently do not
1293 * get RSS by default.
1296 rte_align32pow2(priv->dev->data->nb_rx_queues + 1) >> 1;
1297 alignas(struct rte_flow_action_rss) uint8_t rss_conf_data
1298 [offsetof(struct rte_flow_action_rss, queue) +
1299 sizeof(((struct rte_flow_action_rss *)0)->queue[0]) * queues];
1300 struct rte_flow_action_rss *rss_conf = (void *)rss_conf_data;
1301 struct rte_flow_action actions[] = {
1303 .type = RTE_FLOW_ACTION_TYPE_RSS,
1307 .type = RTE_FLOW_ACTION_TYPE_END,
1310 struct ether_addr *rule_mac = ð_spec.dst;
1311 rte_be16_t *rule_vlan =
1312 priv->dev->data->dev_conf.rxmode.hw_vlan_filter &&
1313 !priv->dev->data->promiscuous ?
1317 struct rte_flow *flow;
1321 /* Nothing to be done if there are no Rx queues. */
1324 /* Prepare default RSS configuration. */
1325 *rss_conf = (struct rte_flow_action_rss){
1326 .rss_conf = NULL, /* Rely on default fallback settings. */
1329 for (i = 0; i != queues; ++i)
1330 rss_conf->queue[i] = 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, rss_conf->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, rss_conf->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;