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 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 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.
383 * @param[in, out] flow
384 * Flow rule handle to update.
386 * Pattern item to merge.
388 * Associated item-processing object.
390 * Perform verbose error reporting if not NULL.
393 * 0 on success, a negative errno value otherwise and rte_errno is set.
396 mlx4_flow_merge_udp(struct rte_flow *flow,
397 const struct rte_flow_item *item,
398 const struct mlx4_flow_proc_item *proc,
399 struct rte_flow_error *error)
401 const struct rte_flow_item_udp *spec = item->spec;
402 const struct rte_flow_item_udp *mask =
403 spec ? (item->mask ? item->mask : proc->mask_default) : NULL;
404 struct ibv_flow_spec_tcp_udp *udp;
408 ((uint16_t)(mask->hdr.src_port + 1) > UINT16_C(1) ||
409 (uint16_t)(mask->hdr.dst_port + 1) > UINT16_C(1))) {
410 msg = "mlx4 does not support matching partial UDP fields";
415 ++flow->ibv_attr->num_of_specs;
416 udp = (void *)((uintptr_t)flow->ibv_attr + flow->ibv_attr_size);
417 *udp = (struct ibv_flow_spec_tcp_udp) {
418 .type = IBV_FLOW_SPEC_UDP,
419 .size = sizeof(*udp),
423 udp->val.dst_port = spec->hdr.dst_port;
424 udp->val.src_port = spec->hdr.src_port;
425 udp->mask.dst_port = mask->hdr.dst_port;
426 udp->mask.src_port = mask->hdr.src_port;
427 /* Remove unwanted bits from values. */
428 udp->val.src_port &= udp->mask.src_port;
429 udp->val.dst_port &= udp->mask.dst_port;
432 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
437 * Merge TCP pattern item into flow rule handle.
439 * Additional mlx4-specific constraints on supported fields:
441 * - No support for partial masks.
443 * @param[in, out] flow
444 * Flow rule handle to update.
446 * Pattern item to merge.
448 * Associated item-processing object.
450 * Perform verbose error reporting if not NULL.
453 * 0 on success, a negative errno value otherwise and rte_errno is set.
456 mlx4_flow_merge_tcp(struct rte_flow *flow,
457 const struct rte_flow_item *item,
458 const struct mlx4_flow_proc_item *proc,
459 struct rte_flow_error *error)
461 const struct rte_flow_item_tcp *spec = item->spec;
462 const struct rte_flow_item_tcp *mask =
463 spec ? (item->mask ? item->mask : proc->mask_default) : NULL;
464 struct ibv_flow_spec_tcp_udp *tcp;
468 ((uint16_t)(mask->hdr.src_port + 1) > UINT16_C(1) ||
469 (uint16_t)(mask->hdr.dst_port + 1) > UINT16_C(1))) {
470 msg = "mlx4 does not support matching partial TCP fields";
475 ++flow->ibv_attr->num_of_specs;
476 tcp = (void *)((uintptr_t)flow->ibv_attr + flow->ibv_attr_size);
477 *tcp = (struct ibv_flow_spec_tcp_udp) {
478 .type = IBV_FLOW_SPEC_TCP,
479 .size = sizeof(*tcp),
483 tcp->val.dst_port = spec->hdr.dst_port;
484 tcp->val.src_port = spec->hdr.src_port;
485 tcp->mask.dst_port = mask->hdr.dst_port;
486 tcp->mask.src_port = mask->hdr.src_port;
487 /* Remove unwanted bits from values. */
488 tcp->val.src_port &= tcp->mask.src_port;
489 tcp->val.dst_port &= tcp->mask.dst_port;
492 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
497 * Perform basic sanity checks on a pattern item.
500 * Item specification.
502 * Associated item-processing object.
504 * Perform verbose error reporting if not NULL.
507 * 0 on success, a negative errno value otherwise and rte_errno is set.
510 mlx4_flow_item_check(const struct rte_flow_item *item,
511 const struct mlx4_flow_proc_item *proc,
512 struct rte_flow_error *error)
517 /* item->last and item->mask cannot exist without item->spec. */
518 if (!item->spec && (item->mask || item->last))
519 return rte_flow_error_set
520 (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, item,
521 "\"mask\" or \"last\" field provided without a"
522 " corresponding \"spec\"");
523 /* No spec, no mask, no problem. */
527 (const uint8_t *)item->mask :
528 (const uint8_t *)proc->mask_default;
531 * Single-pass check to make sure that:
532 * - Mask is supported, no bits are set outside proc->mask_support.
533 * - Both item->spec and item->last are included in mask.
535 for (i = 0; i != proc->mask_sz; ++i) {
538 if ((mask[i] | ((const uint8_t *)proc->mask_support)[i]) !=
539 ((const uint8_t *)proc->mask_support)[i])
540 return rte_flow_error_set
541 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
542 item, "unsupported field found in \"mask\"");
544 (((const uint8_t *)item->spec)[i] & mask[i]) !=
545 (((const uint8_t *)item->last)[i] & mask[i]))
546 return rte_flow_error_set
547 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
549 "range between \"spec\" and \"last\""
550 " is larger than \"mask\"");
555 /** Graph of supported items and associated actions. */
556 static const struct mlx4_flow_proc_item mlx4_flow_proc_item_list[] = {
557 [RTE_FLOW_ITEM_TYPE_END] = {
558 .next_item = NEXT_ITEM(RTE_FLOW_ITEM_TYPE_ETH),
560 [RTE_FLOW_ITEM_TYPE_ETH] = {
561 .next_item = NEXT_ITEM(RTE_FLOW_ITEM_TYPE_VLAN,
562 RTE_FLOW_ITEM_TYPE_IPV4),
563 .mask_support = &(const struct rte_flow_item_eth){
564 /* Only destination MAC can be matched. */
565 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
567 .mask_default = &rte_flow_item_eth_mask,
568 .mask_sz = sizeof(struct rte_flow_item_eth),
569 .merge = mlx4_flow_merge_eth,
570 .dst_sz = sizeof(struct ibv_flow_spec_eth),
572 [RTE_FLOW_ITEM_TYPE_VLAN] = {
573 .next_item = NEXT_ITEM(RTE_FLOW_ITEM_TYPE_IPV4),
574 .mask_support = &(const struct rte_flow_item_vlan){
575 /* Only TCI VID matching is supported. */
576 .tci = RTE_BE16(0x0fff),
578 .mask_default = &rte_flow_item_vlan_mask,
579 .mask_sz = sizeof(struct rte_flow_item_vlan),
580 .merge = mlx4_flow_merge_vlan,
583 [RTE_FLOW_ITEM_TYPE_IPV4] = {
584 .next_item = NEXT_ITEM(RTE_FLOW_ITEM_TYPE_UDP,
585 RTE_FLOW_ITEM_TYPE_TCP),
586 .mask_support = &(const struct rte_flow_item_ipv4){
588 .src_addr = RTE_BE32(0xffffffff),
589 .dst_addr = RTE_BE32(0xffffffff),
592 .mask_default = &rte_flow_item_ipv4_mask,
593 .mask_sz = sizeof(struct rte_flow_item_ipv4),
594 .merge = mlx4_flow_merge_ipv4,
595 .dst_sz = sizeof(struct ibv_flow_spec_ipv4),
597 [RTE_FLOW_ITEM_TYPE_UDP] = {
598 .mask_support = &(const struct rte_flow_item_udp){
600 .src_port = RTE_BE16(0xffff),
601 .dst_port = RTE_BE16(0xffff),
604 .mask_default = &rte_flow_item_udp_mask,
605 .mask_sz = sizeof(struct rte_flow_item_udp),
606 .merge = mlx4_flow_merge_udp,
607 .dst_sz = sizeof(struct ibv_flow_spec_tcp_udp),
609 [RTE_FLOW_ITEM_TYPE_TCP] = {
610 .mask_support = &(const struct rte_flow_item_tcp){
612 .src_port = RTE_BE16(0xffff),
613 .dst_port = RTE_BE16(0xffff),
616 .mask_default = &rte_flow_item_tcp_mask,
617 .mask_sz = sizeof(struct rte_flow_item_tcp),
618 .merge = mlx4_flow_merge_tcp,
619 .dst_sz = sizeof(struct ibv_flow_spec_tcp_udp),
624 * Make sure a flow rule is supported and initialize associated structure.
627 * Pointer to private structure.
629 * Flow rule attributes.
631 * Pattern specification (list terminated by the END pattern item).
633 * Associated actions (list terminated by the END action).
635 * Perform verbose error reporting if not NULL.
636 * @param[in, out] addr
637 * Buffer where the resulting flow rule handle pointer must be stored.
638 * If NULL, stop processing after validation stage.
641 * 0 on success, a negative errno value otherwise and rte_errno is set.
644 mlx4_flow_prepare(struct priv *priv,
645 const struct rte_flow_attr *attr,
646 const struct rte_flow_item pattern[],
647 const struct rte_flow_action actions[],
648 struct rte_flow_error *error,
649 struct rte_flow **addr)
651 const struct rte_flow_item *item;
652 const struct rte_flow_action *action;
653 const struct mlx4_flow_proc_item *proc;
654 struct rte_flow temp = { .ibv_attr_size = sizeof(*temp.ibv_attr) };
655 struct rte_flow *flow = &temp;
656 const char *msg = NULL;
659 return rte_flow_error_set
660 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
661 NULL, "groups are not supported");
662 if (attr->priority > MLX4_FLOW_PRIORITY_LAST)
663 return rte_flow_error_set
664 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
665 NULL, "maximum priority level is "
666 MLX4_STR_EXPAND(MLX4_FLOW_PRIORITY_LAST));
668 return rte_flow_error_set
669 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
670 NULL, "egress is not supported");
672 return rte_flow_error_set
673 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
674 NULL, "only ingress is supported");
676 proc = mlx4_flow_proc_item_list;
677 /* Go over pattern. */
678 for (item = pattern; item->type; ++item) {
679 const struct mlx4_flow_proc_item *next = NULL;
683 if (item->type == RTE_FLOW_ITEM_TYPE_VOID)
685 if (item->type == MLX4_FLOW_ITEM_TYPE_INTERNAL) {
689 if (flow->promisc || flow->allmulti) {
690 msg = "mlx4 does not support additional matching"
691 " criteria combined with indiscriminate"
692 " matching on Ethernet headers";
693 goto exit_item_not_supported;
695 for (i = 0; proc->next_item && proc->next_item[i]; ++i) {
696 if (proc->next_item[i] == item->type) {
697 next = &mlx4_flow_proc_item_list[item->type];
702 goto exit_item_not_supported;
705 * Perform basic sanity checks only once, while handle is
709 err = mlx4_flow_item_check(item, proc, error);
714 err = proc->merge(flow, item, proc, error);
718 flow->ibv_attr_size += proc->dst_sz;
720 /* Go over actions list. */
721 for (action = actions; action->type; ++action) {
722 switch (action->type) {
723 const struct rte_flow_action_queue *queue;
724 const struct rte_flow_action_rss *rss;
725 const struct rte_eth_rss_conf *rss_conf;
728 case RTE_FLOW_ACTION_TYPE_VOID:
730 case RTE_FLOW_ACTION_TYPE_DROP:
733 case RTE_FLOW_ACTION_TYPE_QUEUE:
736 queue = action->conf;
737 flow->rss = mlx4_rss_get
738 (priv, 0, mlx4_rss_hash_key_default, 1,
741 msg = "not enough resources for additional"
742 " single-queue RSS context";
743 goto exit_action_not_supported;
746 case RTE_FLOW_ACTION_TYPE_RSS:
750 /* Default RSS configuration if none is provided. */
754 &(struct rte_eth_rss_conf){
755 .rss_key = mlx4_rss_hash_key_default,
756 .rss_key_len = MLX4_RSS_HASH_KEY_SIZE,
757 .rss_hf = (ETH_RSS_IPV4 |
758 ETH_RSS_NONFRAG_IPV4_TCP |
760 ETH_RSS_NONFRAG_IPV6_TCP),
763 if (!rte_is_power_of_2(rss->num)) {
764 msg = "for RSS, mlx4 requires the number of"
765 " queues to be a power of two";
766 goto exit_action_not_supported;
768 if (rss_conf->rss_key_len !=
769 sizeof(flow->rss->key)) {
770 msg = "mlx4 supports exactly one RSS hash key"
772 MLX4_STR_EXPAND(MLX4_RSS_HASH_KEY_SIZE);
773 goto exit_action_not_supported;
775 for (i = 1; i < rss->num; ++i)
776 if (rss->queue[i] - rss->queue[i - 1] != 1)
779 msg = "mlx4 requires RSS contexts to use"
780 " consecutive queue indices only";
781 goto exit_action_not_supported;
783 if (rss->queue[0] % rss->num) {
784 msg = "mlx4 requires the first queue of a RSS"
785 " context to be aligned on a multiple"
786 " of the context size";
787 goto exit_action_not_supported;
789 flow->rss = mlx4_rss_get
790 (priv, mlx4_conv_rss_hf(rss_conf->rss_hf),
791 rss_conf->rss_key, rss->num, rss->queue);
793 msg = "either invalid parameters or not enough"
794 " resources for additional multi-queue"
796 goto exit_action_not_supported;
800 goto exit_action_not_supported;
803 if (!flow->rss && !flow->drop)
804 return rte_flow_error_set
805 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
806 NULL, "no valid action");
807 /* Validation ends here. */
810 mlx4_rss_put(flow->rss);
814 /* Allocate proper handle based on collected data. */
815 const struct mlx4_malloc_vec vec[] = {
817 .align = alignof(struct rte_flow),
818 .size = sizeof(*flow),
819 .addr = (void **)&flow,
822 .align = alignof(struct ibv_flow_attr),
823 .size = temp.ibv_attr_size,
824 .addr = (void **)&temp.ibv_attr,
828 if (!mlx4_zmallocv(__func__, vec, RTE_DIM(vec)))
829 return rte_flow_error_set
831 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
832 "flow rule handle allocation failure");
833 /* Most fields will be updated by second pass. */
834 *flow = (struct rte_flow){
835 .ibv_attr = temp.ibv_attr,
836 .ibv_attr_size = sizeof(*flow->ibv_attr),
839 *flow->ibv_attr = (struct ibv_flow_attr){
840 .type = IBV_FLOW_ATTR_NORMAL,
841 .size = sizeof(*flow->ibv_attr),
842 .priority = attr->priority,
849 exit_item_not_supported:
850 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
851 item, msg ? msg : "item not supported");
852 exit_action_not_supported:
853 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
854 action, msg ? msg : "action not supported");
858 * Validate a flow supported by the NIC.
860 * @see rte_flow_validate()
864 mlx4_flow_validate(struct rte_eth_dev *dev,
865 const struct rte_flow_attr *attr,
866 const struct rte_flow_item pattern[],
867 const struct rte_flow_action actions[],
868 struct rte_flow_error *error)
870 struct priv *priv = dev->data->dev_private;
872 return mlx4_flow_prepare(priv, attr, pattern, actions, error, NULL);
876 * Get a drop flow rule resources instance.
879 * Pointer to private structure.
882 * Pointer to drop flow resources on success, NULL otherwise and rte_errno
885 static struct mlx4_drop *
886 mlx4_drop_get(struct priv *priv)
888 struct mlx4_drop *drop = priv->drop;
891 assert(drop->refcnt);
892 assert(drop->priv == priv);
896 drop = rte_malloc(__func__, sizeof(*drop), 0);
899 *drop = (struct mlx4_drop){
903 drop->cq = ibv_create_cq(priv->ctx, 1, NULL, NULL, 0);
906 drop->qp = ibv_create_qp(priv->pd,
907 &(struct ibv_qp_init_attr){
910 .qp_type = IBV_QPT_RAW_PACKET,
918 claim_zero(ibv_destroy_qp(drop->qp));
920 claim_zero(ibv_destroy_cq(drop->cq));
928 * Give back a drop flow rule resources instance.
931 * Pointer to drop flow rule resources.
934 mlx4_drop_put(struct mlx4_drop *drop)
936 assert(drop->refcnt);
939 drop->priv->drop = NULL;
940 claim_zero(ibv_destroy_qp(drop->qp));
941 claim_zero(ibv_destroy_cq(drop->cq));
946 * Toggle a configured flow rule.
949 * Pointer to private structure.
951 * Flow rule handle to toggle.
953 * Whether associated Verbs flow must be created or removed.
955 * Perform verbose error reporting if not NULL.
958 * 0 on success, a negative errno value otherwise and rte_errno is set.
961 mlx4_flow_toggle(struct priv *priv,
962 struct rte_flow *flow,
964 struct rte_flow_error *error)
966 struct ibv_qp *qp = NULL;
973 claim_zero(ibv_destroy_flow(flow->ibv_flow));
974 flow->ibv_flow = NULL;
976 mlx4_drop_put(priv->drop);
978 mlx4_rss_detach(flow->rss);
981 assert(flow->ibv_attr);
982 if (!flow->internal &&
984 flow->ibv_attr->priority == MLX4_FLOW_PRIORITY_LAST) {
985 if (flow->ibv_flow) {
986 claim_zero(ibv_destroy_flow(flow->ibv_flow));
987 flow->ibv_flow = NULL;
989 mlx4_drop_put(priv->drop);
991 mlx4_rss_detach(flow->rss);
994 msg = ("priority level "
995 MLX4_STR_EXPAND(MLX4_FLOW_PRIORITY_LAST)
996 " is reserved when not in isolated mode");
1000 struct mlx4_rss *rss = flow->rss;
1004 /* Stop at the first nonexistent target queue. */
1005 for (i = 0; i != rss->queues; ++i)
1006 if (rss->queue_id[i] >=
1007 priv->dev->data->nb_rx_queues ||
1008 !priv->dev->data->rx_queues[rss->queue_id[i]]) {
1012 if (flow->ibv_flow) {
1013 if (missing ^ !flow->drop)
1015 /* Verbs flow needs updating. */
1016 claim_zero(ibv_destroy_flow(flow->ibv_flow));
1017 flow->ibv_flow = NULL;
1019 mlx4_drop_put(priv->drop);
1021 mlx4_rss_detach(rss);
1024 err = mlx4_rss_attach(rss);
1027 msg = "cannot create indirection table or hash"
1028 " QP to associate flow rule with";
1033 /* A missing target queue drops traffic implicitly. */
1034 flow->drop = missing;
1037 mlx4_drop_get(priv);
1040 msg = "resources for drop flow rule cannot be created";
1043 qp = priv->drop->qp;
1048 flow->ibv_flow = ibv_create_flow(qp, flow->ibv_attr);
1052 mlx4_drop_put(priv->drop);
1054 mlx4_rss_detach(flow->rss);
1056 msg = "flow rule rejected by device";
1058 return rte_flow_error_set
1059 (error, err, RTE_FLOW_ERROR_TYPE_HANDLE, flow, msg);
1065 * @see rte_flow_create()
1068 static struct rte_flow *
1069 mlx4_flow_create(struct rte_eth_dev *dev,
1070 const struct rte_flow_attr *attr,
1071 const struct rte_flow_item pattern[],
1072 const struct rte_flow_action actions[],
1073 struct rte_flow_error *error)
1075 struct priv *priv = dev->data->dev_private;
1076 struct rte_flow *flow;
1079 err = mlx4_flow_prepare(priv, attr, pattern, actions, error, &flow);
1082 err = mlx4_flow_toggle(priv, flow, priv->started, error);
1084 struct rte_flow *curr = LIST_FIRST(&priv->flows);
1086 /* New rules are inserted after internal ones. */
1087 if (!curr || !curr->internal) {
1088 LIST_INSERT_HEAD(&priv->flows, flow, next);
1090 while (LIST_NEXT(curr, next) &&
1091 LIST_NEXT(curr, next)->internal)
1092 curr = LIST_NEXT(curr, next);
1093 LIST_INSERT_AFTER(curr, flow, next);
1098 mlx4_rss_put(flow->rss);
1099 rte_flow_error_set(error, -err, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1106 * Configure isolated mode.
1108 * @see rte_flow_isolate()
1112 mlx4_flow_isolate(struct rte_eth_dev *dev,
1114 struct rte_flow_error *error)
1116 struct priv *priv = dev->data->dev_private;
1118 if (!!enable == !!priv->isolated)
1120 priv->isolated = !!enable;
1121 if (mlx4_flow_sync(priv, error)) {
1122 priv->isolated = !enable;
1129 * Destroy a flow rule.
1131 * @see rte_flow_destroy()
1135 mlx4_flow_destroy(struct rte_eth_dev *dev,
1136 struct rte_flow *flow,
1137 struct rte_flow_error *error)
1139 struct priv *priv = dev->data->dev_private;
1140 int err = mlx4_flow_toggle(priv, flow, 0, error);
1144 LIST_REMOVE(flow, next);
1146 mlx4_rss_put(flow->rss);
1152 * Destroy user-configured flow rules.
1154 * This function skips internal flows rules.
1156 * @see rte_flow_flush()
1160 mlx4_flow_flush(struct rte_eth_dev *dev,
1161 struct rte_flow_error *error)
1163 struct priv *priv = dev->data->dev_private;
1164 struct rte_flow *flow = LIST_FIRST(&priv->flows);
1167 struct rte_flow *next = LIST_NEXT(flow, next);
1169 if (!flow->internal)
1170 mlx4_flow_destroy(dev, flow, error);
1177 * Helper function to determine the next configured VLAN filter.
1180 * Pointer to private structure.
1182 * VLAN ID to use as a starting point.
1185 * Next configured VLAN ID or a high value (>= 4096) if there is none.
1188 mlx4_flow_internal_next_vlan(struct priv *priv, uint16_t vlan)
1190 while (vlan < 4096) {
1191 if (priv->dev->data->vlan_filter_conf.ids[vlan / 64] &
1192 (UINT64_C(1) << (vlan % 64)))
1200 * Generate internal flow rules.
1202 * Various flow rules are created depending on the mode the device is in:
1204 * 1. Promiscuous: port MAC + catch-all (VLAN filtering is ignored).
1205 * 2. All multicast: port MAC/VLAN + catch-all multicast.
1206 * 3. Otherwise: port MAC/VLAN + broadcast MAC/VLAN.
1208 * About MAC flow rules:
1210 * - MAC flow rules are generated from @p dev->data->mac_addrs
1211 * (@p priv->mac array).
1212 * - An additional flow rule for Ethernet broadcasts is also generated.
1213 * - All these are per-VLAN if @p dev->data->dev_conf.rxmode.hw_vlan_filter
1214 * is enabled and VLAN filters are configured.
1217 * Pointer to private structure.
1219 * Perform verbose error reporting if not NULL.
1222 * 0 on success, a negative errno value otherwise and rte_errno is set.
1225 mlx4_flow_internal(struct priv *priv, struct rte_flow_error *error)
1227 struct rte_flow_attr attr = {
1228 .priority = MLX4_FLOW_PRIORITY_LAST,
1231 struct rte_flow_item_eth eth_spec;
1232 const struct rte_flow_item_eth eth_mask = {
1233 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
1235 const struct rte_flow_item_eth eth_allmulti = {
1236 .dst.addr_bytes = "\x01\x00\x00\x00\x00\x00",
1238 struct rte_flow_item_vlan vlan_spec;
1239 const struct rte_flow_item_vlan vlan_mask = {
1240 .tci = RTE_BE16(0x0fff),
1242 struct rte_flow_item pattern[] = {
1244 .type = MLX4_FLOW_ITEM_TYPE_INTERNAL,
1247 .type = RTE_FLOW_ITEM_TYPE_ETH,
1252 /* Replaced with VLAN if filtering is enabled. */
1253 .type = RTE_FLOW_ITEM_TYPE_END,
1256 .type = RTE_FLOW_ITEM_TYPE_END,
1260 * Round number of queues down to their previous power of 2 to
1261 * comply with RSS context limitations. Extra queues silently do not
1262 * get RSS by default.
1265 rte_align32pow2(priv->dev->data->nb_rx_queues + 1) >> 1;
1266 alignas(struct rte_flow_action_rss) uint8_t rss_conf_data
1267 [offsetof(struct rte_flow_action_rss, queue) +
1268 sizeof(((struct rte_flow_action_rss *)0)->queue[0]) * queues];
1269 struct rte_flow_action_rss *rss_conf = (void *)rss_conf_data;
1270 struct rte_flow_action actions[] = {
1272 .type = RTE_FLOW_ACTION_TYPE_RSS,
1276 .type = RTE_FLOW_ACTION_TYPE_END,
1279 struct ether_addr *rule_mac = ð_spec.dst;
1280 rte_be16_t *rule_vlan =
1281 priv->dev->data->dev_conf.rxmode.hw_vlan_filter &&
1282 !priv->dev->data->promiscuous ?
1286 !priv->dev->data->promiscuous &&
1287 !priv->dev->data->all_multicast;
1289 struct rte_flow *flow;
1293 /* Prepare default RSS configuration. */
1294 *rss_conf = (struct rte_flow_action_rss){
1295 .rss_conf = NULL, /* Rely on default fallback settings. */
1298 for (i = 0; i != queues; ++i)
1299 rss_conf->queue[i] = i;
1301 * Set up VLAN item if filtering is enabled and at least one VLAN
1302 * filter is configured.
1305 vlan = mlx4_flow_internal_next_vlan(priv, 0);
1307 pattern[2] = (struct rte_flow_item){
1308 .type = RTE_FLOW_ITEM_TYPE_VLAN,
1313 *rule_vlan = rte_cpu_to_be_16(vlan);
1318 for (i = 0; i != RTE_DIM(priv->mac) + broadcast; ++i) {
1319 const struct ether_addr *mac;
1321 /* Broadcasts are handled by an extra iteration. */
1322 if (i < RTE_DIM(priv->mac))
1323 mac = &priv->mac[i];
1325 mac = ð_mask.dst;
1326 if (is_zero_ether_addr(mac))
1328 /* Check if MAC flow rule is already present. */
1329 for (flow = LIST_FIRST(&priv->flows);
1330 flow && flow->internal;
1331 flow = LIST_NEXT(flow, next)) {
1332 const struct ibv_flow_spec_eth *eth =
1333 (const void *)((uintptr_t)flow->ibv_attr +
1334 sizeof(*flow->ibv_attr));
1339 assert(flow->ibv_attr->type == IBV_FLOW_ATTR_NORMAL);
1340 assert(flow->ibv_attr->num_of_specs == 1);
1341 assert(eth->type == IBV_FLOW_SPEC_ETH);
1343 (eth->val.vlan_tag != *rule_vlan ||
1344 eth->mask.vlan_tag != RTE_BE16(0x0fff)))
1346 if (!rule_vlan && eth->mask.vlan_tag)
1348 for (j = 0; j != sizeof(mac->addr_bytes); ++j)
1349 if (eth->val.dst_mac[j] != mac->addr_bytes[j] ||
1350 eth->mask.dst_mac[j] != UINT8_C(0xff) ||
1351 eth->val.src_mac[j] != UINT8_C(0x00) ||
1352 eth->mask.src_mac[j] != UINT8_C(0x00))
1354 if (j == sizeof(mac->addr_bytes))
1357 if (!flow || !flow->internal) {
1358 /* Not found, create a new flow rule. */
1359 memcpy(rule_mac, mac, sizeof(*mac));
1360 flow = mlx4_flow_create(priv->dev, &attr, pattern,
1371 vlan = mlx4_flow_internal_next_vlan(priv, vlan + 1);
1375 /* Take care of promiscuous and all multicast flow rules. */
1377 for (flow = LIST_FIRST(&priv->flows);
1378 flow && flow->internal;
1379 flow = LIST_NEXT(flow, next)) {
1380 if (priv->dev->data->promiscuous) {
1384 assert(priv->dev->data->all_multicast);
1389 if (!flow || !flow->internal) {
1390 /* Not found, create a new flow rule. */
1391 if (priv->dev->data->promiscuous) {
1392 pattern[1].spec = NULL;
1393 pattern[1].mask = NULL;
1395 assert(priv->dev->data->all_multicast);
1396 pattern[1].spec = ð_allmulti;
1397 pattern[1].mask = ð_allmulti;
1399 pattern[2] = pattern[3];
1400 flow = mlx4_flow_create(priv->dev, &attr, pattern,
1407 assert(flow->promisc || flow->allmulti);
1411 /* Clear selection and clean up stale internal flow rules. */
1412 flow = LIST_FIRST(&priv->flows);
1413 while (flow && flow->internal) {
1414 struct rte_flow *next = LIST_NEXT(flow, next);
1417 claim_zero(mlx4_flow_destroy(priv->dev, flow, error));
1426 * Synchronize flow rules.
1428 * This function synchronizes flow rules with the state of the device by
1429 * taking into account isolated mode and whether target queues are
1433 * Pointer to private structure.
1435 * Perform verbose error reporting if not NULL.
1438 * 0 on success, a negative errno value otherwise and rte_errno is set.
1441 mlx4_flow_sync(struct priv *priv, struct rte_flow_error *error)
1443 struct rte_flow *flow;
1446 /* Internal flow rules are guaranteed to come first in the list. */
1447 if (priv->isolated) {
1449 * Get rid of them in isolated mode, stop at the first
1450 * non-internal rule found.
1452 for (flow = LIST_FIRST(&priv->flows);
1453 flow && flow->internal;
1454 flow = LIST_FIRST(&priv->flows))
1455 claim_zero(mlx4_flow_destroy(priv->dev, flow, error));
1457 /* Refresh internal rules. */
1458 ret = mlx4_flow_internal(priv, error);
1462 /* Toggle the remaining flow rules . */
1463 for (flow = LIST_FIRST(&priv->flows);
1465 flow = LIST_NEXT(flow, next)) {
1466 ret = mlx4_flow_toggle(priv, flow, priv->started, error);
1471 assert(!priv->drop);
1476 * Clean up all flow rules.
1478 * Unlike mlx4_flow_flush(), this function takes care of all remaining flow
1479 * rules regardless of whether they are internal or user-configured.
1482 * Pointer to private structure.
1485 mlx4_flow_clean(struct priv *priv)
1487 struct rte_flow *flow;
1489 while ((flow = LIST_FIRST(&priv->flows)))
1490 mlx4_flow_destroy(priv->dev, flow, NULL);
1491 assert(LIST_EMPTY(&priv->rss));
1494 static const struct rte_flow_ops mlx4_flow_ops = {
1495 .validate = mlx4_flow_validate,
1496 .create = mlx4_flow_create,
1497 .destroy = mlx4_flow_destroy,
1498 .flush = mlx4_flow_flush,
1499 .isolate = mlx4_flow_isolate,
1503 * Manage filter operations.
1506 * Pointer to Ethernet device structure.
1507 * @param filter_type
1510 * Operation to perform.
1512 * Pointer to operation-specific structure.
1515 * 0 on success, negative errno value otherwise and rte_errno is set.
1518 mlx4_filter_ctrl(struct rte_eth_dev *dev,
1519 enum rte_filter_type filter_type,
1520 enum rte_filter_op filter_op,
1523 switch (filter_type) {
1524 case RTE_ETH_FILTER_GENERIC:
1525 if (filter_op != RTE_ETH_FILTER_GET)
1527 *(const void **)arg = &mlx4_flow_ops;
1530 ERROR("%p: filter type (%d) not supported",
1531 (void *)dev, filter_type);
1534 rte_errno = ENOTSUP;