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_driver.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.
108 * This function returns the supported (default) set when @p rss_hf has
109 * special value (uint64_t)-1.
112 * Pointer to private structure.
114 * Hash fields in DPDK format (see struct rte_eth_rss_conf).
117 * A valid Verbs RSS hash fields mask for mlx4 on success, (uint64_t)-1
118 * otherwise and rte_errno is set.
121 mlx4_conv_rss_hf(struct priv *priv, uint64_t rss_hf)
123 enum { IPV4, IPV6, TCP, UDP, };
124 const uint64_t in[] = {
125 [IPV4] = (ETH_RSS_IPV4 |
127 ETH_RSS_NONFRAG_IPV4_TCP |
128 ETH_RSS_NONFRAG_IPV4_UDP |
129 ETH_RSS_NONFRAG_IPV4_OTHER),
130 [IPV6] = (ETH_RSS_IPV6 |
132 ETH_RSS_NONFRAG_IPV6_TCP |
133 ETH_RSS_NONFRAG_IPV6_UDP |
134 ETH_RSS_NONFRAG_IPV6_OTHER |
136 ETH_RSS_IPV6_TCP_EX |
137 ETH_RSS_IPV6_UDP_EX),
138 [TCP] = (ETH_RSS_NONFRAG_IPV4_TCP |
139 ETH_RSS_NONFRAG_IPV6_TCP |
140 ETH_RSS_IPV6_TCP_EX),
141 [UDP] = (ETH_RSS_NONFRAG_IPV4_UDP |
142 ETH_RSS_NONFRAG_IPV6_UDP |
143 ETH_RSS_IPV6_UDP_EX),
145 const uint64_t out[RTE_DIM(in)] = {
146 [IPV4] = IBV_RX_HASH_SRC_IPV4 | IBV_RX_HASH_DST_IPV4,
147 [IPV6] = IBV_RX_HASH_SRC_IPV6 | IBV_RX_HASH_DST_IPV6,
148 [TCP] = IBV_RX_HASH_SRC_PORT_TCP | IBV_RX_HASH_DST_PORT_TCP,
149 [UDP] = IBV_RX_HASH_SRC_PORT_UDP | IBV_RX_HASH_DST_PORT_UDP,
155 for (i = 0; i != RTE_DIM(in); ++i)
156 if (rss_hf & in[i]) {
157 seen |= rss_hf & in[i];
160 if ((conv & priv->hw_rss_sup) == conv) {
161 if (rss_hf == (uint64_t)-1) {
162 /* Include inner RSS by default if supported. */
163 conv |= priv->hw_rss_sup & IBV_RX_HASH_INNER;
166 if (!(rss_hf & ~seen))
174 * Merge Ethernet pattern item into flow rule handle.
176 * Additional mlx4-specific constraints on supported fields:
178 * - No support for partial masks, except in the specific case of matching
179 * all multicast traffic (@p spec->dst and @p mask->dst equal to
180 * 01:00:00:00:00:00).
181 * - Not providing @p item->spec or providing an empty @p mask->dst is
182 * *only* supported if the rule doesn't specify additional matching
183 * criteria (i.e. rule is promiscuous-like).
185 * @param[in, out] flow
186 * Flow rule handle to update.
188 * Pattern item to merge.
190 * Associated item-processing object.
192 * Perform verbose error reporting if not NULL.
195 * 0 on success, a negative errno value otherwise and rte_errno is set.
198 mlx4_flow_merge_eth(struct rte_flow *flow,
199 const struct rte_flow_item *item,
200 const struct mlx4_flow_proc_item *proc,
201 struct rte_flow_error *error)
203 const struct rte_flow_item_eth *spec = item->spec;
204 const struct rte_flow_item_eth *mask =
205 spec ? (item->mask ? item->mask : proc->mask_default) : NULL;
206 struct ibv_flow_spec_eth *eth;
213 uint32_t sum_dst = 0;
214 uint32_t sum_src = 0;
216 for (i = 0; i != sizeof(mask->dst.addr_bytes); ++i) {
217 sum_dst += mask->dst.addr_bytes[i];
218 sum_src += mask->src.addr_bytes[i];
221 msg = "mlx4 does not support source MAC matching";
223 } else if (!sum_dst) {
225 } else if (sum_dst == 1 && mask->dst.addr_bytes[0] == 1) {
226 if (!(spec->dst.addr_bytes[0] & 1)) {
227 msg = "mlx4 does not support the explicit"
228 " exclusion of all multicast traffic";
232 } else if (sum_dst != (UINT8_C(0xff) * ETHER_ADDR_LEN)) {
233 msg = "mlx4 does not support matching partial"
241 flow->ibv_attr->type = IBV_FLOW_ATTR_ALL_DEFAULT;
244 if (flow->allmulti) {
245 flow->ibv_attr->type = IBV_FLOW_ATTR_MC_DEFAULT;
248 ++flow->ibv_attr->num_of_specs;
249 eth = (void *)((uintptr_t)flow->ibv_attr + flow->ibv_attr_size);
250 *eth = (struct ibv_flow_spec_eth) {
251 .type = IBV_FLOW_SPEC_ETH,
252 .size = sizeof(*eth),
254 memcpy(eth->val.dst_mac, spec->dst.addr_bytes, ETHER_ADDR_LEN);
255 memcpy(eth->mask.dst_mac, mask->dst.addr_bytes, ETHER_ADDR_LEN);
256 /* Remove unwanted bits from values. */
257 for (i = 0; i < ETHER_ADDR_LEN; ++i) {
258 eth->val.dst_mac[i] &= eth->mask.dst_mac[i];
262 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
267 * Merge VLAN pattern item into flow rule handle.
269 * Additional mlx4-specific constraints on supported fields:
271 * - Matching *all* VLAN traffic by omitting @p item->spec or providing an
272 * empty @p item->mask would also include non-VLAN traffic. Doing so is
273 * therefore unsupported.
274 * - No support for partial masks.
276 * @param[in, out] flow
277 * Flow rule handle to update.
279 * Pattern item to merge.
281 * Associated item-processing object.
283 * Perform verbose error reporting if not NULL.
286 * 0 on success, a negative errno value otherwise and rte_errno is set.
289 mlx4_flow_merge_vlan(struct rte_flow *flow,
290 const struct rte_flow_item *item,
291 const struct mlx4_flow_proc_item *proc,
292 struct rte_flow_error *error)
294 const struct rte_flow_item_vlan *spec = item->spec;
295 const struct rte_flow_item_vlan *mask =
296 spec ? (item->mask ? item->mask : proc->mask_default) : NULL;
297 struct ibv_flow_spec_eth *eth;
300 if (!mask || !mask->tci) {
301 msg = "mlx4 cannot match all VLAN traffic while excluding"
302 " non-VLAN traffic, TCI VID must be specified";
305 if (mask->tci != RTE_BE16(0x0fff)) {
306 msg = "mlx4 does not support partial TCI VID matching";
311 eth = (void *)((uintptr_t)flow->ibv_attr + flow->ibv_attr_size -
313 eth->val.vlan_tag = spec->tci;
314 eth->mask.vlan_tag = mask->tci;
315 eth->val.vlan_tag &= eth->mask.vlan_tag;
318 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
323 * Merge IPv4 pattern item into flow rule handle.
325 * Additional mlx4-specific constraints on supported fields:
327 * - No support for partial masks.
329 * @param[in, out] flow
330 * Flow rule handle to update.
332 * Pattern item to merge.
334 * Associated item-processing object.
336 * Perform verbose error reporting if not NULL.
339 * 0 on success, a negative errno value otherwise and rte_errno is set.
342 mlx4_flow_merge_ipv4(struct rte_flow *flow,
343 const struct rte_flow_item *item,
344 const struct mlx4_flow_proc_item *proc,
345 struct rte_flow_error *error)
347 const struct rte_flow_item_ipv4 *spec = item->spec;
348 const struct rte_flow_item_ipv4 *mask =
349 spec ? (item->mask ? item->mask : proc->mask_default) : NULL;
350 struct ibv_flow_spec_ipv4 *ipv4;
354 ((uint32_t)(mask->hdr.src_addr + 1) > UINT32_C(1) ||
355 (uint32_t)(mask->hdr.dst_addr + 1) > UINT32_C(1))) {
356 msg = "mlx4 does not support matching partial IPv4 fields";
361 ++flow->ibv_attr->num_of_specs;
362 ipv4 = (void *)((uintptr_t)flow->ibv_attr + flow->ibv_attr_size);
363 *ipv4 = (struct ibv_flow_spec_ipv4) {
364 .type = IBV_FLOW_SPEC_IPV4,
365 .size = sizeof(*ipv4),
369 ipv4->val = (struct ibv_flow_ipv4_filter) {
370 .src_ip = spec->hdr.src_addr,
371 .dst_ip = spec->hdr.dst_addr,
373 ipv4->mask = (struct ibv_flow_ipv4_filter) {
374 .src_ip = mask->hdr.src_addr,
375 .dst_ip = mask->hdr.dst_addr,
377 /* Remove unwanted bits from values. */
378 ipv4->val.src_ip &= ipv4->mask.src_ip;
379 ipv4->val.dst_ip &= ipv4->mask.dst_ip;
382 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
387 * Merge UDP pattern item into flow rule handle.
389 * Additional mlx4-specific constraints on supported fields:
391 * - No support for partial masks.
393 * @param[in, out] flow
394 * Flow rule handle to update.
396 * Pattern item to merge.
398 * Associated item-processing object.
400 * Perform verbose error reporting if not NULL.
403 * 0 on success, a negative errno value otherwise and rte_errno is set.
406 mlx4_flow_merge_udp(struct rte_flow *flow,
407 const struct rte_flow_item *item,
408 const struct mlx4_flow_proc_item *proc,
409 struct rte_flow_error *error)
411 const struct rte_flow_item_udp *spec = item->spec;
412 const struct rte_flow_item_udp *mask =
413 spec ? (item->mask ? item->mask : proc->mask_default) : NULL;
414 struct ibv_flow_spec_tcp_udp *udp;
418 ((uint16_t)(mask->hdr.src_port + 1) > UINT16_C(1) ||
419 (uint16_t)(mask->hdr.dst_port + 1) > UINT16_C(1))) {
420 msg = "mlx4 does not support matching partial UDP fields";
425 ++flow->ibv_attr->num_of_specs;
426 udp = (void *)((uintptr_t)flow->ibv_attr + flow->ibv_attr_size);
427 *udp = (struct ibv_flow_spec_tcp_udp) {
428 .type = IBV_FLOW_SPEC_UDP,
429 .size = sizeof(*udp),
433 udp->val.dst_port = spec->hdr.dst_port;
434 udp->val.src_port = spec->hdr.src_port;
435 udp->mask.dst_port = mask->hdr.dst_port;
436 udp->mask.src_port = mask->hdr.src_port;
437 /* Remove unwanted bits from values. */
438 udp->val.src_port &= udp->mask.src_port;
439 udp->val.dst_port &= udp->mask.dst_port;
442 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
447 * Merge TCP pattern item into flow rule handle.
449 * Additional mlx4-specific constraints on supported fields:
451 * - No support for partial masks.
453 * @param[in, out] flow
454 * Flow rule handle to update.
456 * Pattern item to merge.
458 * Associated item-processing object.
460 * Perform verbose error reporting if not NULL.
463 * 0 on success, a negative errno value otherwise and rte_errno is set.
466 mlx4_flow_merge_tcp(struct rte_flow *flow,
467 const struct rte_flow_item *item,
468 const struct mlx4_flow_proc_item *proc,
469 struct rte_flow_error *error)
471 const struct rte_flow_item_tcp *spec = item->spec;
472 const struct rte_flow_item_tcp *mask =
473 spec ? (item->mask ? item->mask : proc->mask_default) : NULL;
474 struct ibv_flow_spec_tcp_udp *tcp;
478 ((uint16_t)(mask->hdr.src_port + 1) > UINT16_C(1) ||
479 (uint16_t)(mask->hdr.dst_port + 1) > UINT16_C(1))) {
480 msg = "mlx4 does not support matching partial TCP fields";
485 ++flow->ibv_attr->num_of_specs;
486 tcp = (void *)((uintptr_t)flow->ibv_attr + flow->ibv_attr_size);
487 *tcp = (struct ibv_flow_spec_tcp_udp) {
488 .type = IBV_FLOW_SPEC_TCP,
489 .size = sizeof(*tcp),
493 tcp->val.dst_port = spec->hdr.dst_port;
494 tcp->val.src_port = spec->hdr.src_port;
495 tcp->mask.dst_port = mask->hdr.dst_port;
496 tcp->mask.src_port = mask->hdr.src_port;
497 /* Remove unwanted bits from values. */
498 tcp->val.src_port &= tcp->mask.src_port;
499 tcp->val.dst_port &= tcp->mask.dst_port;
502 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
507 * Perform basic sanity checks on a pattern item.
510 * Item specification.
512 * Associated item-processing object.
514 * Perform verbose error reporting if not NULL.
517 * 0 on success, a negative errno value otherwise and rte_errno is set.
520 mlx4_flow_item_check(const struct rte_flow_item *item,
521 const struct mlx4_flow_proc_item *proc,
522 struct rte_flow_error *error)
527 /* item->last and item->mask cannot exist without item->spec. */
528 if (!item->spec && (item->mask || item->last))
529 return rte_flow_error_set
530 (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, item,
531 "\"mask\" or \"last\" field provided without a"
532 " corresponding \"spec\"");
533 /* No spec, no mask, no problem. */
537 (const uint8_t *)item->mask :
538 (const uint8_t *)proc->mask_default;
541 * Single-pass check to make sure that:
542 * - Mask is supported, no bits are set outside proc->mask_support.
543 * - Both item->spec and item->last are included in mask.
545 for (i = 0; i != proc->mask_sz; ++i) {
548 if ((mask[i] | ((const uint8_t *)proc->mask_support)[i]) !=
549 ((const uint8_t *)proc->mask_support)[i])
550 return rte_flow_error_set
551 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
552 item, "unsupported field found in \"mask\"");
554 (((const uint8_t *)item->spec)[i] & mask[i]) !=
555 (((const uint8_t *)item->last)[i] & mask[i]))
556 return rte_flow_error_set
557 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
559 "range between \"spec\" and \"last\""
560 " is larger than \"mask\"");
565 /** Graph of supported items and associated actions. */
566 static const struct mlx4_flow_proc_item mlx4_flow_proc_item_list[] = {
567 [RTE_FLOW_ITEM_TYPE_END] = {
568 .next_item = NEXT_ITEM(RTE_FLOW_ITEM_TYPE_ETH),
570 [RTE_FLOW_ITEM_TYPE_ETH] = {
571 .next_item = NEXT_ITEM(RTE_FLOW_ITEM_TYPE_VLAN,
572 RTE_FLOW_ITEM_TYPE_IPV4),
573 .mask_support = &(const struct rte_flow_item_eth){
574 /* Only destination MAC can be matched. */
575 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
577 .mask_default = &rte_flow_item_eth_mask,
578 .mask_sz = sizeof(struct rte_flow_item_eth),
579 .merge = mlx4_flow_merge_eth,
580 .dst_sz = sizeof(struct ibv_flow_spec_eth),
582 [RTE_FLOW_ITEM_TYPE_VLAN] = {
583 .next_item = NEXT_ITEM(RTE_FLOW_ITEM_TYPE_IPV4),
584 .mask_support = &(const struct rte_flow_item_vlan){
585 /* Only TCI VID matching is supported. */
586 .tci = RTE_BE16(0x0fff),
588 .mask_default = &rte_flow_item_vlan_mask,
589 .mask_sz = sizeof(struct rte_flow_item_vlan),
590 .merge = mlx4_flow_merge_vlan,
593 [RTE_FLOW_ITEM_TYPE_IPV4] = {
594 .next_item = NEXT_ITEM(RTE_FLOW_ITEM_TYPE_UDP,
595 RTE_FLOW_ITEM_TYPE_TCP),
596 .mask_support = &(const struct rte_flow_item_ipv4){
598 .src_addr = RTE_BE32(0xffffffff),
599 .dst_addr = RTE_BE32(0xffffffff),
602 .mask_default = &rte_flow_item_ipv4_mask,
603 .mask_sz = sizeof(struct rte_flow_item_ipv4),
604 .merge = mlx4_flow_merge_ipv4,
605 .dst_sz = sizeof(struct ibv_flow_spec_ipv4),
607 [RTE_FLOW_ITEM_TYPE_UDP] = {
608 .mask_support = &(const struct rte_flow_item_udp){
610 .src_port = RTE_BE16(0xffff),
611 .dst_port = RTE_BE16(0xffff),
614 .mask_default = &rte_flow_item_udp_mask,
615 .mask_sz = sizeof(struct rte_flow_item_udp),
616 .merge = mlx4_flow_merge_udp,
617 .dst_sz = sizeof(struct ibv_flow_spec_tcp_udp),
619 [RTE_FLOW_ITEM_TYPE_TCP] = {
620 .mask_support = &(const struct rte_flow_item_tcp){
622 .src_port = RTE_BE16(0xffff),
623 .dst_port = RTE_BE16(0xffff),
626 .mask_default = &rte_flow_item_tcp_mask,
627 .mask_sz = sizeof(struct rte_flow_item_tcp),
628 .merge = mlx4_flow_merge_tcp,
629 .dst_sz = sizeof(struct ibv_flow_spec_tcp_udp),
634 * Make sure a flow rule is supported and initialize associated structure.
637 * Pointer to private structure.
639 * Flow rule attributes.
641 * Pattern specification (list terminated by the END pattern item).
643 * Associated actions (list terminated by the END action).
645 * Perform verbose error reporting if not NULL.
646 * @param[in, out] addr
647 * Buffer where the resulting flow rule handle pointer must be stored.
648 * If NULL, stop processing after validation stage.
651 * 0 on success, a negative errno value otherwise and rte_errno is set.
654 mlx4_flow_prepare(struct priv *priv,
655 const struct rte_flow_attr *attr,
656 const struct rte_flow_item pattern[],
657 const struct rte_flow_action actions[],
658 struct rte_flow_error *error,
659 struct rte_flow **addr)
661 const struct rte_flow_item *item;
662 const struct rte_flow_action *action;
663 const struct mlx4_flow_proc_item *proc;
664 struct rte_flow temp = { .ibv_attr_size = sizeof(*temp.ibv_attr) };
665 struct rte_flow *flow = &temp;
666 const char *msg = NULL;
669 return rte_flow_error_set
670 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
671 NULL, "groups are not supported");
672 if (attr->priority > MLX4_FLOW_PRIORITY_LAST)
673 return rte_flow_error_set
674 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
675 NULL, "maximum priority level is "
676 MLX4_STR_EXPAND(MLX4_FLOW_PRIORITY_LAST));
678 return rte_flow_error_set
679 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
680 NULL, "egress is not supported");
682 return rte_flow_error_set
683 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
684 NULL, "only ingress is supported");
686 proc = mlx4_flow_proc_item_list;
687 /* Go over pattern. */
688 for (item = pattern; item->type; ++item) {
689 const struct mlx4_flow_proc_item *next = NULL;
693 if (item->type == RTE_FLOW_ITEM_TYPE_VOID)
695 if (item->type == MLX4_FLOW_ITEM_TYPE_INTERNAL) {
699 if (flow->promisc || flow->allmulti) {
700 msg = "mlx4 does not support additional matching"
701 " criteria combined with indiscriminate"
702 " matching on Ethernet headers";
703 goto exit_item_not_supported;
705 for (i = 0; proc->next_item && proc->next_item[i]; ++i) {
706 if (proc->next_item[i] == item->type) {
707 next = &mlx4_flow_proc_item_list[item->type];
712 goto exit_item_not_supported;
715 * Perform basic sanity checks only once, while handle is
719 err = mlx4_flow_item_check(item, proc, error);
724 err = proc->merge(flow, item, proc, error);
728 flow->ibv_attr_size += proc->dst_sz;
730 /* Go over actions list. */
731 for (action = actions; action->type; ++action) {
732 switch (action->type) {
733 const struct rte_flow_action_queue *queue;
734 const struct rte_flow_action_rss *rss;
735 const struct rte_eth_rss_conf *rss_conf;
738 case RTE_FLOW_ACTION_TYPE_VOID:
740 case RTE_FLOW_ACTION_TYPE_DROP:
743 case RTE_FLOW_ACTION_TYPE_QUEUE:
746 queue = action->conf;
747 if (queue->index >= priv->dev->data->nb_rx_queues) {
748 msg = "queue target index beyond number of"
749 " configured Rx queues";
750 goto exit_action_not_supported;
752 flow->rss = mlx4_rss_get
753 (priv, 0, mlx4_rss_hash_key_default, 1,
756 msg = "not enough resources for additional"
757 " single-queue RSS context";
758 goto exit_action_not_supported;
761 case RTE_FLOW_ACTION_TYPE_RSS:
765 /* Default RSS configuration if none is provided. */
769 &(struct rte_eth_rss_conf){
770 .rss_key = mlx4_rss_hash_key_default,
771 .rss_key_len = MLX4_RSS_HASH_KEY_SIZE,
775 for (i = 0; i < rss->num; ++i)
777 priv->dev->data->nb_rx_queues)
780 msg = "queue index target beyond number of"
781 " configured Rx queues";
782 goto exit_action_not_supported;
784 if (!rte_is_power_of_2(rss->num)) {
785 msg = "for RSS, mlx4 requires the number of"
786 " queues to be a power of two";
787 goto exit_action_not_supported;
789 if (rss_conf->rss_key_len !=
790 sizeof(flow->rss->key)) {
791 msg = "mlx4 supports exactly one RSS hash key"
793 MLX4_STR_EXPAND(MLX4_RSS_HASH_KEY_SIZE);
794 goto exit_action_not_supported;
796 for (i = 1; i < rss->num; ++i)
797 if (rss->queue[i] - rss->queue[i - 1] != 1)
800 msg = "mlx4 requires RSS contexts to use"
801 " consecutive queue indices only";
802 goto exit_action_not_supported;
804 if (rss->queue[0] % rss->num) {
805 msg = "mlx4 requires the first queue of a RSS"
806 " context to be aligned on a multiple"
807 " of the context size";
808 goto exit_action_not_supported;
810 flow->rss = mlx4_rss_get
812 mlx4_conv_rss_hf(priv, rss_conf->rss_hf),
813 rss_conf->rss_key, rss->num, rss->queue);
815 msg = "either invalid parameters or not enough"
816 " resources for additional multi-queue"
818 goto exit_action_not_supported;
822 goto exit_action_not_supported;
825 if (!flow->rss && !flow->drop)
826 return rte_flow_error_set
827 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
828 NULL, "no valid action");
829 /* Validation ends here. */
832 mlx4_rss_put(flow->rss);
836 /* Allocate proper handle based on collected data. */
837 const struct mlx4_malloc_vec vec[] = {
839 .align = alignof(struct rte_flow),
840 .size = sizeof(*flow),
841 .addr = (void **)&flow,
844 .align = alignof(struct ibv_flow_attr),
845 .size = temp.ibv_attr_size,
846 .addr = (void **)&temp.ibv_attr,
850 if (!mlx4_zmallocv(__func__, vec, RTE_DIM(vec)))
851 return rte_flow_error_set
853 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
854 "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 = ibv_create_cq(priv->ctx, 1, NULL, NULL, 0);
928 drop->qp = ibv_create_qp(priv->pd,
929 &(struct ibv_qp_init_attr){
932 .qp_type = IBV_QPT_RAW_PACKET,
940 claim_zero(ibv_destroy_qp(drop->qp));
942 claim_zero(ibv_destroy_cq(drop->cq));
950 * Give back a drop flow rule resources instance.
953 * Pointer to drop flow rule resources.
956 mlx4_drop_put(struct mlx4_drop *drop)
958 assert(drop->refcnt);
961 drop->priv->drop = NULL;
962 claim_zero(ibv_destroy_qp(drop->qp));
963 claim_zero(ibv_destroy_cq(drop->cq));
968 * Toggle a configured flow rule.
971 * Pointer to private structure.
973 * Flow rule handle to toggle.
975 * Whether associated Verbs flow must be created or removed.
977 * Perform verbose error reporting if not NULL.
980 * 0 on success, a negative errno value otherwise and rte_errno is set.
983 mlx4_flow_toggle(struct priv *priv,
984 struct rte_flow *flow,
986 struct rte_flow_error *error)
988 struct ibv_qp *qp = NULL;
995 claim_zero(ibv_destroy_flow(flow->ibv_flow));
996 flow->ibv_flow = NULL;
998 mlx4_drop_put(priv->drop);
1000 mlx4_rss_detach(flow->rss);
1003 assert(flow->ibv_attr);
1004 if (!flow->internal &&
1006 flow->ibv_attr->priority == MLX4_FLOW_PRIORITY_LAST) {
1007 if (flow->ibv_flow) {
1008 claim_zero(ibv_destroy_flow(flow->ibv_flow));
1009 flow->ibv_flow = NULL;
1011 mlx4_drop_put(priv->drop);
1013 mlx4_rss_detach(flow->rss);
1016 msg = ("priority level "
1017 MLX4_STR_EXPAND(MLX4_FLOW_PRIORITY_LAST)
1018 " is reserved when not in isolated mode");
1022 struct mlx4_rss *rss = flow->rss;
1026 /* Stop at the first nonexistent target queue. */
1027 for (i = 0; i != rss->queues; ++i)
1028 if (rss->queue_id[i] >=
1029 priv->dev->data->nb_rx_queues ||
1030 !priv->dev->data->rx_queues[rss->queue_id[i]]) {
1034 if (flow->ibv_flow) {
1035 if (missing ^ !flow->drop)
1037 /* Verbs flow needs updating. */
1038 claim_zero(ibv_destroy_flow(flow->ibv_flow));
1039 flow->ibv_flow = NULL;
1041 mlx4_drop_put(priv->drop);
1043 mlx4_rss_detach(rss);
1046 err = mlx4_rss_attach(rss);
1049 msg = "cannot create indirection table or hash"
1050 " QP to associate flow rule with";
1055 /* A missing target queue drops traffic implicitly. */
1056 flow->drop = missing;
1059 mlx4_drop_get(priv);
1062 msg = "resources for drop flow rule cannot be created";
1065 qp = priv->drop->qp;
1070 flow->ibv_flow = ibv_create_flow(qp, flow->ibv_attr);
1074 mlx4_drop_put(priv->drop);
1076 mlx4_rss_detach(flow->rss);
1078 msg = "flow rule rejected by device";
1080 return rte_flow_error_set
1081 (error, err, RTE_FLOW_ERROR_TYPE_HANDLE, flow, msg);
1087 * @see rte_flow_create()
1090 static struct rte_flow *
1091 mlx4_flow_create(struct rte_eth_dev *dev,
1092 const struct rte_flow_attr *attr,
1093 const struct rte_flow_item pattern[],
1094 const struct rte_flow_action actions[],
1095 struct rte_flow_error *error)
1097 struct priv *priv = dev->data->dev_private;
1098 struct rte_flow *flow;
1101 err = mlx4_flow_prepare(priv, attr, pattern, actions, error, &flow);
1104 err = mlx4_flow_toggle(priv, flow, priv->started, error);
1106 struct rte_flow *curr = LIST_FIRST(&priv->flows);
1108 /* New rules are inserted after internal ones. */
1109 if (!curr || !curr->internal) {
1110 LIST_INSERT_HEAD(&priv->flows, flow, next);
1112 while (LIST_NEXT(curr, next) &&
1113 LIST_NEXT(curr, next)->internal)
1114 curr = LIST_NEXT(curr, next);
1115 LIST_INSERT_AFTER(curr, flow, next);
1120 mlx4_rss_put(flow->rss);
1121 rte_flow_error_set(error, -err, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1128 * Configure isolated mode.
1130 * @see rte_flow_isolate()
1134 mlx4_flow_isolate(struct rte_eth_dev *dev,
1136 struct rte_flow_error *error)
1138 struct priv *priv = dev->data->dev_private;
1140 if (!!enable == !!priv->isolated)
1142 priv->isolated = !!enable;
1143 if (mlx4_flow_sync(priv, error)) {
1144 priv->isolated = !enable;
1151 * Destroy a flow rule.
1153 * @see rte_flow_destroy()
1157 mlx4_flow_destroy(struct rte_eth_dev *dev,
1158 struct rte_flow *flow,
1159 struct rte_flow_error *error)
1161 struct priv *priv = dev->data->dev_private;
1162 int err = mlx4_flow_toggle(priv, flow, 0, error);
1166 LIST_REMOVE(flow, next);
1168 mlx4_rss_put(flow->rss);
1174 * Destroy user-configured flow rules.
1176 * This function skips internal flows rules.
1178 * @see rte_flow_flush()
1182 mlx4_flow_flush(struct rte_eth_dev *dev,
1183 struct rte_flow_error *error)
1185 struct priv *priv = dev->data->dev_private;
1186 struct rte_flow *flow = LIST_FIRST(&priv->flows);
1189 struct rte_flow *next = LIST_NEXT(flow, next);
1191 if (!flow->internal)
1192 mlx4_flow_destroy(dev, flow, error);
1199 * Helper function to determine the next configured VLAN filter.
1202 * Pointer to private structure.
1204 * VLAN ID to use as a starting point.
1207 * Next configured VLAN ID or a high value (>= 4096) if there is none.
1210 mlx4_flow_internal_next_vlan(struct priv *priv, uint16_t vlan)
1212 while (vlan < 4096) {
1213 if (priv->dev->data->vlan_filter_conf.ids[vlan / 64] &
1214 (UINT64_C(1) << (vlan % 64)))
1222 * Generate internal flow rules.
1224 * Various flow rules are created depending on the mode the device is in:
1226 * 1. Promiscuous: port MAC + catch-all (VLAN filtering is ignored).
1227 * 2. All multicast: port MAC/VLAN + catch-all multicast.
1228 * 3. Otherwise: port MAC/VLAN + broadcast MAC/VLAN.
1230 * About MAC flow rules:
1232 * - MAC flow rules are generated from @p dev->data->mac_addrs
1233 * (@p priv->mac array).
1234 * - An additional flow rule for Ethernet broadcasts is also generated.
1235 * - All these are per-VLAN if @p DEV_RX_OFFLOAD_VLAN_FILTER
1236 * is enabled and VLAN filters are configured.
1239 * Pointer to private structure.
1241 * Perform verbose error reporting if not NULL.
1244 * 0 on success, a negative errno value otherwise and rte_errno is set.
1247 mlx4_flow_internal(struct priv *priv, struct rte_flow_error *error)
1249 struct rte_flow_attr attr = {
1250 .priority = MLX4_FLOW_PRIORITY_LAST,
1253 struct rte_flow_item_eth eth_spec;
1254 const struct rte_flow_item_eth eth_mask = {
1255 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
1257 const struct rte_flow_item_eth eth_allmulti = {
1258 .dst.addr_bytes = "\x01\x00\x00\x00\x00\x00",
1260 struct rte_flow_item_vlan vlan_spec;
1261 const struct rte_flow_item_vlan vlan_mask = {
1262 .tci = RTE_BE16(0x0fff),
1264 struct rte_flow_item pattern[] = {
1266 .type = MLX4_FLOW_ITEM_TYPE_INTERNAL,
1269 .type = RTE_FLOW_ITEM_TYPE_ETH,
1274 /* Replaced with VLAN if filtering is enabled. */
1275 .type = RTE_FLOW_ITEM_TYPE_END,
1278 .type = RTE_FLOW_ITEM_TYPE_END,
1282 * Round number of queues down to their previous power of 2 to
1283 * comply with RSS context limitations. Extra queues silently do not
1284 * get RSS by default.
1287 rte_align32pow2(priv->dev->data->nb_rx_queues + 1) >> 1;
1288 alignas(struct rte_flow_action_rss) uint8_t rss_conf_data
1289 [offsetof(struct rte_flow_action_rss, queue) +
1290 sizeof(((struct rte_flow_action_rss *)0)->queue[0]) * queues];
1291 struct rte_flow_action_rss *rss_conf = (void *)rss_conf_data;
1292 struct rte_flow_action actions[] = {
1294 .type = RTE_FLOW_ACTION_TYPE_RSS,
1298 .type = RTE_FLOW_ACTION_TYPE_END,
1301 struct ether_addr *rule_mac = ð_spec.dst;
1302 rte_be16_t *rule_vlan =
1303 (priv->dev->data->dev_conf.rxmode.offloads &
1304 DEV_RX_OFFLOAD_VLAN_FILTER) &&
1305 !priv->dev->data->promiscuous ?
1309 !priv->dev->data->promiscuous &&
1310 !priv->dev->data->all_multicast;
1312 struct rte_flow *flow;
1316 /* Nothing to be done if there are no Rx queues. */
1319 /* Prepare default RSS configuration. */
1320 *rss_conf = (struct rte_flow_action_rss){
1321 .rss_conf = NULL, /* Rely on default fallback settings. */
1324 for (i = 0; i != queues; ++i)
1325 rss_conf->queue[i] = i;
1327 * Set up VLAN item if filtering is enabled and at least one VLAN
1328 * filter is configured.
1331 vlan = mlx4_flow_internal_next_vlan(priv, 0);
1333 pattern[2] = (struct rte_flow_item){
1334 .type = RTE_FLOW_ITEM_TYPE_VLAN,
1339 *rule_vlan = rte_cpu_to_be_16(vlan);
1344 for (i = 0; i != RTE_DIM(priv->mac) + broadcast; ++i) {
1345 const struct ether_addr *mac;
1347 /* Broadcasts are handled by an extra iteration. */
1348 if (i < RTE_DIM(priv->mac))
1349 mac = &priv->mac[i];
1351 mac = ð_mask.dst;
1352 if (is_zero_ether_addr(mac))
1354 /* Check if MAC flow rule is already present. */
1355 for (flow = LIST_FIRST(&priv->flows);
1356 flow && flow->internal;
1357 flow = LIST_NEXT(flow, next)) {
1358 const struct ibv_flow_spec_eth *eth =
1359 (const void *)((uintptr_t)flow->ibv_attr +
1360 sizeof(*flow->ibv_attr));
1365 assert(flow->ibv_attr->type == IBV_FLOW_ATTR_NORMAL);
1366 assert(flow->ibv_attr->num_of_specs == 1);
1367 assert(eth->type == IBV_FLOW_SPEC_ETH);
1370 (eth->val.vlan_tag != *rule_vlan ||
1371 eth->mask.vlan_tag != RTE_BE16(0x0fff)))
1373 if (!rule_vlan && eth->mask.vlan_tag)
1375 for (j = 0; j != sizeof(mac->addr_bytes); ++j)
1376 if (eth->val.dst_mac[j] != mac->addr_bytes[j] ||
1377 eth->mask.dst_mac[j] != UINT8_C(0xff) ||
1378 eth->val.src_mac[j] != UINT8_C(0x00) ||
1379 eth->mask.src_mac[j] != UINT8_C(0x00))
1381 if (j != sizeof(mac->addr_bytes))
1383 if (flow->rss->queues != queues ||
1384 memcmp(flow->rss->queue_id, rss_conf->queue,
1385 queues * sizeof(flow->rss->queue_id[0])))
1389 if (!flow || !flow->internal) {
1390 /* Not found, create a new flow rule. */
1391 memcpy(rule_mac, mac, sizeof(*mac));
1392 flow = mlx4_flow_create(priv->dev, &attr, pattern,
1403 vlan = mlx4_flow_internal_next_vlan(priv, vlan + 1);
1407 /* Take care of promiscuous and all multicast flow rules. */
1409 for (flow = LIST_FIRST(&priv->flows);
1410 flow && flow->internal;
1411 flow = LIST_NEXT(flow, next)) {
1412 if (priv->dev->data->promiscuous) {
1416 assert(priv->dev->data->all_multicast);
1421 if (flow && flow->internal) {
1423 if (flow->rss->queues != queues ||
1424 memcmp(flow->rss->queue_id, rss_conf->queue,
1425 queues * sizeof(flow->rss->queue_id[0])))
1428 if (!flow || !flow->internal) {
1429 /* Not found, create a new flow rule. */
1430 if (priv->dev->data->promiscuous) {
1431 pattern[1].spec = NULL;
1432 pattern[1].mask = NULL;
1434 assert(priv->dev->data->all_multicast);
1435 pattern[1].spec = ð_allmulti;
1436 pattern[1].mask = ð_allmulti;
1438 pattern[2] = pattern[3];
1439 flow = mlx4_flow_create(priv->dev, &attr, pattern,
1446 assert(flow->promisc || flow->allmulti);
1450 /* Clear selection and clean up stale internal flow rules. */
1451 flow = LIST_FIRST(&priv->flows);
1452 while (flow && flow->internal) {
1453 struct rte_flow *next = LIST_NEXT(flow, next);
1456 claim_zero(mlx4_flow_destroy(priv->dev, flow, error));
1465 * Synchronize flow rules.
1467 * This function synchronizes flow rules with the state of the device by
1468 * taking into account isolated mode and whether target queues are
1472 * Pointer to private structure.
1474 * Perform verbose error reporting if not NULL.
1477 * 0 on success, a negative errno value otherwise and rte_errno is set.
1480 mlx4_flow_sync(struct priv *priv, struct rte_flow_error *error)
1482 struct rte_flow *flow;
1485 /* Internal flow rules are guaranteed to come first in the list. */
1486 if (priv->isolated) {
1488 * Get rid of them in isolated mode, stop at the first
1489 * non-internal rule found.
1491 for (flow = LIST_FIRST(&priv->flows);
1492 flow && flow->internal;
1493 flow = LIST_FIRST(&priv->flows))
1494 claim_zero(mlx4_flow_destroy(priv->dev, flow, error));
1496 /* Refresh internal rules. */
1497 ret = mlx4_flow_internal(priv, error);
1501 /* Toggle the remaining flow rules . */
1502 LIST_FOREACH(flow, &priv->flows, next) {
1503 ret = mlx4_flow_toggle(priv, flow, priv->started, error);
1508 assert(!priv->drop);
1513 * Clean up all flow rules.
1515 * Unlike mlx4_flow_flush(), this function takes care of all remaining flow
1516 * rules regardless of whether they are internal or user-configured.
1519 * Pointer to private structure.
1522 mlx4_flow_clean(struct priv *priv)
1524 struct rte_flow *flow;
1526 while ((flow = LIST_FIRST(&priv->flows)))
1527 mlx4_flow_destroy(priv->dev, flow, NULL);
1528 assert(LIST_EMPTY(&priv->rss));
1531 static const struct rte_flow_ops mlx4_flow_ops = {
1532 .validate = mlx4_flow_validate,
1533 .create = mlx4_flow_create,
1534 .destroy = mlx4_flow_destroy,
1535 .flush = mlx4_flow_flush,
1536 .isolate = mlx4_flow_isolate,
1540 * Manage filter operations.
1543 * Pointer to Ethernet device structure.
1544 * @param filter_type
1547 * Operation to perform.
1549 * Pointer to operation-specific structure.
1552 * 0 on success, negative errno value otherwise and rte_errno is set.
1555 mlx4_filter_ctrl(struct rte_eth_dev *dev,
1556 enum rte_filter_type filter_type,
1557 enum rte_filter_op filter_op,
1560 switch (filter_type) {
1561 case RTE_ETH_FILTER_GENERIC:
1562 if (filter_op != RTE_ETH_FILTER_GET)
1564 *(const void **)arg = &mlx4_flow_ops;
1567 ERROR("%p: filter type (%d) not supported",
1568 (void *)dev, filter_type);
1571 rte_errno = ENOTSUP;