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.
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)
163 if (!(rss_hf & ~seen))
171 * Merge Ethernet pattern item into flow rule handle.
173 * Additional mlx4-specific constraints on supported fields:
175 * - No support for partial masks, except in the specific case of matching
176 * all multicast traffic (@p spec->dst and @p mask->dst equal to
177 * 01:00:00:00:00:00).
178 * - Not providing @p item->spec or providing an empty @p mask->dst is
179 * *only* supported if the rule doesn't specify additional matching
180 * criteria (i.e. rule is promiscuous-like).
182 * @param[in, out] flow
183 * Flow rule handle to update.
185 * Pattern item to merge.
187 * Associated item-processing object.
189 * Perform verbose error reporting if not NULL.
192 * 0 on success, a negative errno value otherwise and rte_errno is set.
195 mlx4_flow_merge_eth(struct rte_flow *flow,
196 const struct rte_flow_item *item,
197 const struct mlx4_flow_proc_item *proc,
198 struct rte_flow_error *error)
200 const struct rte_flow_item_eth *spec = item->spec;
201 const struct rte_flow_item_eth *mask =
202 spec ? (item->mask ? item->mask : proc->mask_default) : NULL;
203 struct ibv_flow_spec_eth *eth;
210 uint32_t sum_dst = 0;
211 uint32_t sum_src = 0;
213 for (i = 0; i != sizeof(mask->dst.addr_bytes); ++i) {
214 sum_dst += mask->dst.addr_bytes[i];
215 sum_src += mask->src.addr_bytes[i];
218 msg = "mlx4 does not support source MAC matching";
220 } else if (!sum_dst) {
222 } else if (sum_dst == 1 && mask->dst.addr_bytes[0] == 1) {
223 if (!(spec->dst.addr_bytes[0] & 1)) {
224 msg = "mlx4 does not support the explicit"
225 " exclusion of all multicast traffic";
229 } else if (sum_dst != (UINT8_C(0xff) * ETHER_ADDR_LEN)) {
230 msg = "mlx4 does not support matching partial"
238 flow->ibv_attr->type = IBV_FLOW_ATTR_ALL_DEFAULT;
241 if (flow->allmulti) {
242 flow->ibv_attr->type = IBV_FLOW_ATTR_MC_DEFAULT;
245 ++flow->ibv_attr->num_of_specs;
246 eth = (void *)((uintptr_t)flow->ibv_attr + flow->ibv_attr_size);
247 *eth = (struct ibv_flow_spec_eth) {
248 .type = IBV_FLOW_SPEC_ETH,
249 .size = sizeof(*eth),
251 memcpy(eth->val.dst_mac, spec->dst.addr_bytes, ETHER_ADDR_LEN);
252 memcpy(eth->mask.dst_mac, mask->dst.addr_bytes, ETHER_ADDR_LEN);
253 /* Remove unwanted bits from values. */
254 for (i = 0; i < ETHER_ADDR_LEN; ++i) {
255 eth->val.dst_mac[i] &= eth->mask.dst_mac[i];
259 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
264 * Merge VLAN pattern item into flow rule handle.
266 * Additional mlx4-specific constraints on supported fields:
268 * - Matching *all* VLAN traffic by omitting @p item->spec or providing an
269 * empty @p item->mask would also include non-VLAN traffic. Doing so is
270 * therefore unsupported.
271 * - No support for partial masks.
273 * @param[in, out] flow
274 * Flow rule handle to update.
276 * Pattern item to merge.
278 * Associated item-processing object.
280 * Perform verbose error reporting if not NULL.
283 * 0 on success, a negative errno value otherwise and rte_errno is set.
286 mlx4_flow_merge_vlan(struct rte_flow *flow,
287 const struct rte_flow_item *item,
288 const struct mlx4_flow_proc_item *proc,
289 struct rte_flow_error *error)
291 const struct rte_flow_item_vlan *spec = item->spec;
292 const struct rte_flow_item_vlan *mask =
293 spec ? (item->mask ? item->mask : proc->mask_default) : NULL;
294 struct ibv_flow_spec_eth *eth;
297 if (!mask || !mask->tci) {
298 msg = "mlx4 cannot match all VLAN traffic while excluding"
299 " non-VLAN traffic, TCI VID must be specified";
302 if (mask->tci != RTE_BE16(0x0fff)) {
303 msg = "mlx4 does not support partial TCI VID matching";
308 eth = (void *)((uintptr_t)flow->ibv_attr + flow->ibv_attr_size -
310 eth->val.vlan_tag = spec->tci;
311 eth->mask.vlan_tag = mask->tci;
312 eth->val.vlan_tag &= eth->mask.vlan_tag;
315 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
320 * Merge IPv4 pattern item into flow rule handle.
322 * Additional mlx4-specific constraints on supported fields:
324 * - No support for partial masks.
326 * @param[in, out] flow
327 * Flow rule handle to update.
329 * Pattern item to merge.
331 * Associated item-processing object.
333 * Perform verbose error reporting if not NULL.
336 * 0 on success, a negative errno value otherwise and rte_errno is set.
339 mlx4_flow_merge_ipv4(struct rte_flow *flow,
340 const struct rte_flow_item *item,
341 const struct mlx4_flow_proc_item *proc,
342 struct rte_flow_error *error)
344 const struct rte_flow_item_ipv4 *spec = item->spec;
345 const struct rte_flow_item_ipv4 *mask =
346 spec ? (item->mask ? item->mask : proc->mask_default) : NULL;
347 struct ibv_flow_spec_ipv4 *ipv4;
351 ((uint32_t)(mask->hdr.src_addr + 1) > UINT32_C(1) ||
352 (uint32_t)(mask->hdr.dst_addr + 1) > UINT32_C(1))) {
353 msg = "mlx4 does not support matching partial IPv4 fields";
358 ++flow->ibv_attr->num_of_specs;
359 ipv4 = (void *)((uintptr_t)flow->ibv_attr + flow->ibv_attr_size);
360 *ipv4 = (struct ibv_flow_spec_ipv4) {
361 .type = IBV_FLOW_SPEC_IPV4,
362 .size = sizeof(*ipv4),
366 ipv4->val = (struct ibv_flow_ipv4_filter) {
367 .src_ip = spec->hdr.src_addr,
368 .dst_ip = spec->hdr.dst_addr,
370 ipv4->mask = (struct ibv_flow_ipv4_filter) {
371 .src_ip = mask->hdr.src_addr,
372 .dst_ip = mask->hdr.dst_addr,
374 /* Remove unwanted bits from values. */
375 ipv4->val.src_ip &= ipv4->mask.src_ip;
376 ipv4->val.dst_ip &= ipv4->mask.dst_ip;
379 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
384 * Merge UDP pattern item into flow rule handle.
386 * Additional mlx4-specific constraints on supported fields:
388 * - No support for partial masks.
390 * @param[in, out] flow
391 * Flow rule handle to update.
393 * Pattern item to merge.
395 * Associated item-processing object.
397 * Perform verbose error reporting if not NULL.
400 * 0 on success, a negative errno value otherwise and rte_errno is set.
403 mlx4_flow_merge_udp(struct rte_flow *flow,
404 const struct rte_flow_item *item,
405 const struct mlx4_flow_proc_item *proc,
406 struct rte_flow_error *error)
408 const struct rte_flow_item_udp *spec = item->spec;
409 const struct rte_flow_item_udp *mask =
410 spec ? (item->mask ? item->mask : proc->mask_default) : NULL;
411 struct ibv_flow_spec_tcp_udp *udp;
415 ((uint16_t)(mask->hdr.src_port + 1) > UINT16_C(1) ||
416 (uint16_t)(mask->hdr.dst_port + 1) > UINT16_C(1))) {
417 msg = "mlx4 does not support matching partial UDP fields";
422 ++flow->ibv_attr->num_of_specs;
423 udp = (void *)((uintptr_t)flow->ibv_attr + flow->ibv_attr_size);
424 *udp = (struct ibv_flow_spec_tcp_udp) {
425 .type = IBV_FLOW_SPEC_UDP,
426 .size = sizeof(*udp),
430 udp->val.dst_port = spec->hdr.dst_port;
431 udp->val.src_port = spec->hdr.src_port;
432 udp->mask.dst_port = mask->hdr.dst_port;
433 udp->mask.src_port = mask->hdr.src_port;
434 /* Remove unwanted bits from values. */
435 udp->val.src_port &= udp->mask.src_port;
436 udp->val.dst_port &= udp->mask.dst_port;
439 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
444 * Merge TCP pattern item into flow rule handle.
446 * Additional mlx4-specific constraints on supported fields:
448 * - No support for partial masks.
450 * @param[in, out] flow
451 * Flow rule handle to update.
453 * Pattern item to merge.
455 * Associated item-processing object.
457 * Perform verbose error reporting if not NULL.
460 * 0 on success, a negative errno value otherwise and rte_errno is set.
463 mlx4_flow_merge_tcp(struct rte_flow *flow,
464 const struct rte_flow_item *item,
465 const struct mlx4_flow_proc_item *proc,
466 struct rte_flow_error *error)
468 const struct rte_flow_item_tcp *spec = item->spec;
469 const struct rte_flow_item_tcp *mask =
470 spec ? (item->mask ? item->mask : proc->mask_default) : NULL;
471 struct ibv_flow_spec_tcp_udp *tcp;
475 ((uint16_t)(mask->hdr.src_port + 1) > UINT16_C(1) ||
476 (uint16_t)(mask->hdr.dst_port + 1) > UINT16_C(1))) {
477 msg = "mlx4 does not support matching partial TCP fields";
482 ++flow->ibv_attr->num_of_specs;
483 tcp = (void *)((uintptr_t)flow->ibv_attr + flow->ibv_attr_size);
484 *tcp = (struct ibv_flow_spec_tcp_udp) {
485 .type = IBV_FLOW_SPEC_TCP,
486 .size = sizeof(*tcp),
490 tcp->val.dst_port = spec->hdr.dst_port;
491 tcp->val.src_port = spec->hdr.src_port;
492 tcp->mask.dst_port = mask->hdr.dst_port;
493 tcp->mask.src_port = mask->hdr.src_port;
494 /* Remove unwanted bits from values. */
495 tcp->val.src_port &= tcp->mask.src_port;
496 tcp->val.dst_port &= tcp->mask.dst_port;
499 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
504 * Perform basic sanity checks on a pattern item.
507 * Item specification.
509 * Associated item-processing object.
511 * Perform verbose error reporting if not NULL.
514 * 0 on success, a negative errno value otherwise and rte_errno is set.
517 mlx4_flow_item_check(const struct rte_flow_item *item,
518 const struct mlx4_flow_proc_item *proc,
519 struct rte_flow_error *error)
524 /* item->last and item->mask cannot exist without item->spec. */
525 if (!item->spec && (item->mask || item->last))
526 return rte_flow_error_set
527 (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, item,
528 "\"mask\" or \"last\" field provided without a"
529 " corresponding \"spec\"");
530 /* No spec, no mask, no problem. */
534 (const uint8_t *)item->mask :
535 (const uint8_t *)proc->mask_default;
538 * Single-pass check to make sure that:
539 * - Mask is supported, no bits are set outside proc->mask_support.
540 * - Both item->spec and item->last are included in mask.
542 for (i = 0; i != proc->mask_sz; ++i) {
545 if ((mask[i] | ((const uint8_t *)proc->mask_support)[i]) !=
546 ((const uint8_t *)proc->mask_support)[i])
547 return rte_flow_error_set
548 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
549 item, "unsupported field found in \"mask\"");
551 (((const uint8_t *)item->spec)[i] & mask[i]) !=
552 (((const uint8_t *)item->last)[i] & mask[i]))
553 return rte_flow_error_set
554 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
556 "range between \"spec\" and \"last\""
557 " is larger than \"mask\"");
562 /** Graph of supported items and associated actions. */
563 static const struct mlx4_flow_proc_item mlx4_flow_proc_item_list[] = {
564 [RTE_FLOW_ITEM_TYPE_END] = {
565 .next_item = NEXT_ITEM(RTE_FLOW_ITEM_TYPE_ETH),
567 [RTE_FLOW_ITEM_TYPE_ETH] = {
568 .next_item = NEXT_ITEM(RTE_FLOW_ITEM_TYPE_VLAN,
569 RTE_FLOW_ITEM_TYPE_IPV4),
570 .mask_support = &(const struct rte_flow_item_eth){
571 /* Only destination MAC can be matched. */
572 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
574 .mask_default = &rte_flow_item_eth_mask,
575 .mask_sz = sizeof(struct rte_flow_item_eth),
576 .merge = mlx4_flow_merge_eth,
577 .dst_sz = sizeof(struct ibv_flow_spec_eth),
579 [RTE_FLOW_ITEM_TYPE_VLAN] = {
580 .next_item = NEXT_ITEM(RTE_FLOW_ITEM_TYPE_IPV4),
581 .mask_support = &(const struct rte_flow_item_vlan){
582 /* Only TCI VID matching is supported. */
583 .tci = RTE_BE16(0x0fff),
585 .mask_default = &rte_flow_item_vlan_mask,
586 .mask_sz = sizeof(struct rte_flow_item_vlan),
587 .merge = mlx4_flow_merge_vlan,
590 [RTE_FLOW_ITEM_TYPE_IPV4] = {
591 .next_item = NEXT_ITEM(RTE_FLOW_ITEM_TYPE_UDP,
592 RTE_FLOW_ITEM_TYPE_TCP),
593 .mask_support = &(const struct rte_flow_item_ipv4){
595 .src_addr = RTE_BE32(0xffffffff),
596 .dst_addr = RTE_BE32(0xffffffff),
599 .mask_default = &rte_flow_item_ipv4_mask,
600 .mask_sz = sizeof(struct rte_flow_item_ipv4),
601 .merge = mlx4_flow_merge_ipv4,
602 .dst_sz = sizeof(struct ibv_flow_spec_ipv4),
604 [RTE_FLOW_ITEM_TYPE_UDP] = {
605 .mask_support = &(const struct rte_flow_item_udp){
607 .src_port = RTE_BE16(0xffff),
608 .dst_port = RTE_BE16(0xffff),
611 .mask_default = &rte_flow_item_udp_mask,
612 .mask_sz = sizeof(struct rte_flow_item_udp),
613 .merge = mlx4_flow_merge_udp,
614 .dst_sz = sizeof(struct ibv_flow_spec_tcp_udp),
616 [RTE_FLOW_ITEM_TYPE_TCP] = {
617 .mask_support = &(const struct rte_flow_item_tcp){
619 .src_port = RTE_BE16(0xffff),
620 .dst_port = RTE_BE16(0xffff),
623 .mask_default = &rte_flow_item_tcp_mask,
624 .mask_sz = sizeof(struct rte_flow_item_tcp),
625 .merge = mlx4_flow_merge_tcp,
626 .dst_sz = sizeof(struct ibv_flow_spec_tcp_udp),
631 * Make sure a flow rule is supported and initialize associated structure.
634 * Pointer to private structure.
636 * Flow rule attributes.
638 * Pattern specification (list terminated by the END pattern item).
640 * Associated actions (list terminated by the END action).
642 * Perform verbose error reporting if not NULL.
643 * @param[in, out] addr
644 * Buffer where the resulting flow rule handle pointer must be stored.
645 * If NULL, stop processing after validation stage.
648 * 0 on success, a negative errno value otherwise and rte_errno is set.
651 mlx4_flow_prepare(struct priv *priv,
652 const struct rte_flow_attr *attr,
653 const struct rte_flow_item pattern[],
654 const struct rte_flow_action actions[],
655 struct rte_flow_error *error,
656 struct rte_flow **addr)
658 const struct rte_flow_item *item;
659 const struct rte_flow_action *action;
660 const struct mlx4_flow_proc_item *proc;
661 struct rte_flow temp = { .ibv_attr_size = sizeof(*temp.ibv_attr) };
662 struct rte_flow *flow = &temp;
663 const char *msg = NULL;
666 return rte_flow_error_set
667 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
668 NULL, "groups are not supported");
669 if (attr->priority > MLX4_FLOW_PRIORITY_LAST)
670 return rte_flow_error_set
671 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
672 NULL, "maximum priority level is "
673 MLX4_STR_EXPAND(MLX4_FLOW_PRIORITY_LAST));
675 return rte_flow_error_set
676 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
677 NULL, "egress is not supported");
679 return rte_flow_error_set
680 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
681 NULL, "only ingress is supported");
683 proc = mlx4_flow_proc_item_list;
684 /* Go over pattern. */
685 for (item = pattern; item->type; ++item) {
686 const struct mlx4_flow_proc_item *next = NULL;
690 if (item->type == RTE_FLOW_ITEM_TYPE_VOID)
692 if (item->type == MLX4_FLOW_ITEM_TYPE_INTERNAL) {
696 if (flow->promisc || flow->allmulti) {
697 msg = "mlx4 does not support additional matching"
698 " criteria combined with indiscriminate"
699 " matching on Ethernet headers";
700 goto exit_item_not_supported;
702 for (i = 0; proc->next_item && proc->next_item[i]; ++i) {
703 if (proc->next_item[i] == item->type) {
704 next = &mlx4_flow_proc_item_list[item->type];
709 goto exit_item_not_supported;
712 * Perform basic sanity checks only once, while handle is
716 err = mlx4_flow_item_check(item, proc, error);
721 err = proc->merge(flow, item, proc, error);
725 flow->ibv_attr_size += proc->dst_sz;
727 /* Go over actions list. */
728 for (action = actions; action->type; ++action) {
729 switch (action->type) {
730 const struct rte_flow_action_queue *queue;
731 const struct rte_flow_action_rss *rss;
732 const struct rte_eth_rss_conf *rss_conf;
735 case RTE_FLOW_ACTION_TYPE_VOID:
737 case RTE_FLOW_ACTION_TYPE_DROP:
740 case RTE_FLOW_ACTION_TYPE_QUEUE:
743 queue = action->conf;
744 if (queue->index >= priv->dev->data->nb_rx_queues) {
745 msg = "queue target index beyond number of"
746 " configured Rx queues";
747 goto exit_action_not_supported;
749 flow->rss = mlx4_rss_get
750 (priv, 0, mlx4_rss_hash_key_default, 1,
753 msg = "not enough resources for additional"
754 " single-queue RSS context";
755 goto exit_action_not_supported;
758 case RTE_FLOW_ACTION_TYPE_RSS:
762 /* Default RSS configuration if none is provided. */
766 &(struct rte_eth_rss_conf){
767 .rss_key = mlx4_rss_hash_key_default,
768 .rss_key_len = MLX4_RSS_HASH_KEY_SIZE,
772 for (i = 0; i < rss->num; ++i)
774 priv->dev->data->nb_rx_queues)
777 msg = "queue index target beyond number of"
778 " configured Rx queues";
779 goto exit_action_not_supported;
781 if (!rte_is_power_of_2(rss->num)) {
782 msg = "for RSS, mlx4 requires the number of"
783 " queues to be a power of two";
784 goto exit_action_not_supported;
786 if (rss_conf->rss_key_len !=
787 sizeof(flow->rss->key)) {
788 msg = "mlx4 supports exactly one RSS hash key"
790 MLX4_STR_EXPAND(MLX4_RSS_HASH_KEY_SIZE);
791 goto exit_action_not_supported;
793 for (i = 1; i < rss->num; ++i)
794 if (rss->queue[i] - rss->queue[i - 1] != 1)
797 msg = "mlx4 requires RSS contexts to use"
798 " consecutive queue indices only";
799 goto exit_action_not_supported;
801 if (rss->queue[0] % rss->num) {
802 msg = "mlx4 requires the first queue of a RSS"
803 " context to be aligned on a multiple"
804 " of the context size";
805 goto exit_action_not_supported;
807 flow->rss = mlx4_rss_get
809 mlx4_conv_rss_hf(priv, rss_conf->rss_hf),
810 rss_conf->rss_key, rss->num, rss->queue);
812 msg = "either invalid parameters or not enough"
813 " resources for additional multi-queue"
815 goto exit_action_not_supported;
819 goto exit_action_not_supported;
822 if (!flow->rss && !flow->drop)
823 return rte_flow_error_set
824 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
825 NULL, "no valid action");
826 /* Validation ends here. */
829 mlx4_rss_put(flow->rss);
833 /* Allocate proper handle based on collected data. */
834 const struct mlx4_malloc_vec vec[] = {
836 .align = alignof(struct rte_flow),
837 .size = sizeof(*flow),
838 .addr = (void **)&flow,
841 .align = alignof(struct ibv_flow_attr),
842 .size = temp.ibv_attr_size,
843 .addr = (void **)&temp.ibv_attr,
847 if (!mlx4_zmallocv(__func__, vec, RTE_DIM(vec)))
848 return rte_flow_error_set
850 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
851 "flow rule handle allocation failure");
852 /* Most fields will be updated by second pass. */
853 *flow = (struct rte_flow){
854 .ibv_attr = temp.ibv_attr,
855 .ibv_attr_size = sizeof(*flow->ibv_attr),
858 *flow->ibv_attr = (struct ibv_flow_attr){
859 .type = IBV_FLOW_ATTR_NORMAL,
860 .size = sizeof(*flow->ibv_attr),
861 .priority = attr->priority,
868 exit_item_not_supported:
869 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
870 item, msg ? msg : "item not supported");
871 exit_action_not_supported:
872 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
873 action, msg ? msg : "action not supported");
877 * Validate a flow supported by the NIC.
879 * @see rte_flow_validate()
883 mlx4_flow_validate(struct rte_eth_dev *dev,
884 const struct rte_flow_attr *attr,
885 const struct rte_flow_item pattern[],
886 const struct rte_flow_action actions[],
887 struct rte_flow_error *error)
889 struct priv *priv = dev->data->dev_private;
891 return mlx4_flow_prepare(priv, attr, pattern, actions, error, NULL);
895 * Get a drop flow rule resources instance.
898 * Pointer to private structure.
901 * Pointer to drop flow resources on success, NULL otherwise and rte_errno
904 static struct mlx4_drop *
905 mlx4_drop_get(struct priv *priv)
907 struct mlx4_drop *drop = priv->drop;
910 assert(drop->refcnt);
911 assert(drop->priv == priv);
915 drop = rte_malloc(__func__, sizeof(*drop), 0);
918 *drop = (struct mlx4_drop){
922 drop->cq = ibv_create_cq(priv->ctx, 1, NULL, NULL, 0);
925 drop->qp = ibv_create_qp(priv->pd,
926 &(struct ibv_qp_init_attr){
929 .qp_type = IBV_QPT_RAW_PACKET,
937 claim_zero(ibv_destroy_qp(drop->qp));
939 claim_zero(ibv_destroy_cq(drop->cq));
947 * Give back a drop flow rule resources instance.
950 * Pointer to drop flow rule resources.
953 mlx4_drop_put(struct mlx4_drop *drop)
955 assert(drop->refcnt);
958 drop->priv->drop = NULL;
959 claim_zero(ibv_destroy_qp(drop->qp));
960 claim_zero(ibv_destroy_cq(drop->cq));
965 * Toggle a configured flow rule.
968 * Pointer to private structure.
970 * Flow rule handle to toggle.
972 * Whether associated Verbs flow must be created or removed.
974 * Perform verbose error reporting if not NULL.
977 * 0 on success, a negative errno value otherwise and rte_errno is set.
980 mlx4_flow_toggle(struct priv *priv,
981 struct rte_flow *flow,
983 struct rte_flow_error *error)
985 struct ibv_qp *qp = NULL;
992 claim_zero(ibv_destroy_flow(flow->ibv_flow));
993 flow->ibv_flow = NULL;
995 mlx4_drop_put(priv->drop);
997 mlx4_rss_detach(flow->rss);
1000 assert(flow->ibv_attr);
1001 if (!flow->internal &&
1003 flow->ibv_attr->priority == MLX4_FLOW_PRIORITY_LAST) {
1004 if (flow->ibv_flow) {
1005 claim_zero(ibv_destroy_flow(flow->ibv_flow));
1006 flow->ibv_flow = NULL;
1008 mlx4_drop_put(priv->drop);
1010 mlx4_rss_detach(flow->rss);
1013 msg = ("priority level "
1014 MLX4_STR_EXPAND(MLX4_FLOW_PRIORITY_LAST)
1015 " is reserved when not in isolated mode");
1019 struct mlx4_rss *rss = flow->rss;
1023 /* Stop at the first nonexistent target queue. */
1024 for (i = 0; i != rss->queues; ++i)
1025 if (rss->queue_id[i] >=
1026 priv->dev->data->nb_rx_queues ||
1027 !priv->dev->data->rx_queues[rss->queue_id[i]]) {
1031 if (flow->ibv_flow) {
1032 if (missing ^ !flow->drop)
1034 /* Verbs flow needs updating. */
1035 claim_zero(ibv_destroy_flow(flow->ibv_flow));
1036 flow->ibv_flow = NULL;
1038 mlx4_drop_put(priv->drop);
1040 mlx4_rss_detach(rss);
1043 err = mlx4_rss_attach(rss);
1046 msg = "cannot create indirection table or hash"
1047 " QP to associate flow rule with";
1052 /* A missing target queue drops traffic implicitly. */
1053 flow->drop = missing;
1056 mlx4_drop_get(priv);
1059 msg = "resources for drop flow rule cannot be created";
1062 qp = priv->drop->qp;
1067 flow->ibv_flow = ibv_create_flow(qp, flow->ibv_attr);
1071 mlx4_drop_put(priv->drop);
1073 mlx4_rss_detach(flow->rss);
1075 msg = "flow rule rejected by device";
1077 return rte_flow_error_set
1078 (error, err, RTE_FLOW_ERROR_TYPE_HANDLE, flow, msg);
1084 * @see rte_flow_create()
1087 static struct rte_flow *
1088 mlx4_flow_create(struct rte_eth_dev *dev,
1089 const struct rte_flow_attr *attr,
1090 const struct rte_flow_item pattern[],
1091 const struct rte_flow_action actions[],
1092 struct rte_flow_error *error)
1094 struct priv *priv = dev->data->dev_private;
1095 struct rte_flow *flow;
1098 err = mlx4_flow_prepare(priv, attr, pattern, actions, error, &flow);
1101 err = mlx4_flow_toggle(priv, flow, priv->started, error);
1103 struct rte_flow *curr = LIST_FIRST(&priv->flows);
1105 /* New rules are inserted after internal ones. */
1106 if (!curr || !curr->internal) {
1107 LIST_INSERT_HEAD(&priv->flows, flow, next);
1109 while (LIST_NEXT(curr, next) &&
1110 LIST_NEXT(curr, next)->internal)
1111 curr = LIST_NEXT(curr, next);
1112 LIST_INSERT_AFTER(curr, flow, next);
1117 mlx4_rss_put(flow->rss);
1118 rte_flow_error_set(error, -err, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1125 * Configure isolated mode.
1127 * @see rte_flow_isolate()
1131 mlx4_flow_isolate(struct rte_eth_dev *dev,
1133 struct rte_flow_error *error)
1135 struct priv *priv = dev->data->dev_private;
1137 if (!!enable == !!priv->isolated)
1139 priv->isolated = !!enable;
1140 if (mlx4_flow_sync(priv, error)) {
1141 priv->isolated = !enable;
1148 * Destroy a flow rule.
1150 * @see rte_flow_destroy()
1154 mlx4_flow_destroy(struct rte_eth_dev *dev,
1155 struct rte_flow *flow,
1156 struct rte_flow_error *error)
1158 struct priv *priv = dev->data->dev_private;
1159 int err = mlx4_flow_toggle(priv, flow, 0, error);
1163 LIST_REMOVE(flow, next);
1165 mlx4_rss_put(flow->rss);
1171 * Destroy user-configured flow rules.
1173 * This function skips internal flows rules.
1175 * @see rte_flow_flush()
1179 mlx4_flow_flush(struct rte_eth_dev *dev,
1180 struct rte_flow_error *error)
1182 struct priv *priv = dev->data->dev_private;
1183 struct rte_flow *flow = LIST_FIRST(&priv->flows);
1186 struct rte_flow *next = LIST_NEXT(flow, next);
1188 if (!flow->internal)
1189 mlx4_flow_destroy(dev, flow, error);
1196 * Helper function to determine the next configured VLAN filter.
1199 * Pointer to private structure.
1201 * VLAN ID to use as a starting point.
1204 * Next configured VLAN ID or a high value (>= 4096) if there is none.
1207 mlx4_flow_internal_next_vlan(struct priv *priv, uint16_t vlan)
1209 while (vlan < 4096) {
1210 if (priv->dev->data->vlan_filter_conf.ids[vlan / 64] &
1211 (UINT64_C(1) << (vlan % 64)))
1219 * Generate internal flow rules.
1221 * Various flow rules are created depending on the mode the device is in:
1223 * 1. Promiscuous: port MAC + catch-all (VLAN filtering is ignored).
1224 * 2. All multicast: port MAC/VLAN + catch-all multicast.
1225 * 3. Otherwise: port MAC/VLAN + broadcast MAC/VLAN.
1227 * About MAC flow rules:
1229 * - MAC flow rules are generated from @p dev->data->mac_addrs
1230 * (@p priv->mac array).
1231 * - An additional flow rule for Ethernet broadcasts is also generated.
1232 * - All these are per-VLAN if @p dev->data->dev_conf.rxmode.hw_vlan_filter
1233 * is enabled and VLAN filters are configured.
1236 * Pointer to private structure.
1238 * Perform verbose error reporting if not NULL.
1241 * 0 on success, a negative errno value otherwise and rte_errno is set.
1244 mlx4_flow_internal(struct priv *priv, struct rte_flow_error *error)
1246 struct rte_flow_attr attr = {
1247 .priority = MLX4_FLOW_PRIORITY_LAST,
1250 struct rte_flow_item_eth eth_spec;
1251 const struct rte_flow_item_eth eth_mask = {
1252 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
1254 const struct rte_flow_item_eth eth_allmulti = {
1255 .dst.addr_bytes = "\x01\x00\x00\x00\x00\x00",
1257 struct rte_flow_item_vlan vlan_spec;
1258 const struct rte_flow_item_vlan vlan_mask = {
1259 .tci = RTE_BE16(0x0fff),
1261 struct rte_flow_item pattern[] = {
1263 .type = MLX4_FLOW_ITEM_TYPE_INTERNAL,
1266 .type = RTE_FLOW_ITEM_TYPE_ETH,
1271 /* Replaced with VLAN if filtering is enabled. */
1272 .type = RTE_FLOW_ITEM_TYPE_END,
1275 .type = RTE_FLOW_ITEM_TYPE_END,
1279 * Round number of queues down to their previous power of 2 to
1280 * comply with RSS context limitations. Extra queues silently do not
1281 * get RSS by default.
1284 rte_align32pow2(priv->dev->data->nb_rx_queues + 1) >> 1;
1285 alignas(struct rte_flow_action_rss) uint8_t rss_conf_data
1286 [offsetof(struct rte_flow_action_rss, queue) +
1287 sizeof(((struct rte_flow_action_rss *)0)->queue[0]) * queues];
1288 struct rte_flow_action_rss *rss_conf = (void *)rss_conf_data;
1289 struct rte_flow_action actions[] = {
1291 .type = RTE_FLOW_ACTION_TYPE_RSS,
1295 .type = RTE_FLOW_ACTION_TYPE_END,
1298 struct ether_addr *rule_mac = ð_spec.dst;
1299 rte_be16_t *rule_vlan =
1300 priv->dev->data->dev_conf.rxmode.hw_vlan_filter &&
1301 !priv->dev->data->promiscuous ?
1305 !priv->dev->data->promiscuous &&
1306 !priv->dev->data->all_multicast;
1308 struct rte_flow *flow;
1312 /* Nothing to be done if there are no Rx queues. */
1315 /* Prepare default RSS configuration. */
1316 *rss_conf = (struct rte_flow_action_rss){
1317 .rss_conf = NULL, /* Rely on default fallback settings. */
1320 for (i = 0; i != queues; ++i)
1321 rss_conf->queue[i] = i;
1323 * Set up VLAN item if filtering is enabled and at least one VLAN
1324 * filter is configured.
1327 vlan = mlx4_flow_internal_next_vlan(priv, 0);
1329 pattern[2] = (struct rte_flow_item){
1330 .type = RTE_FLOW_ITEM_TYPE_VLAN,
1335 *rule_vlan = rte_cpu_to_be_16(vlan);
1340 for (i = 0; i != RTE_DIM(priv->mac) + broadcast; ++i) {
1341 const struct ether_addr *mac;
1343 /* Broadcasts are handled by an extra iteration. */
1344 if (i < RTE_DIM(priv->mac))
1345 mac = &priv->mac[i];
1347 mac = ð_mask.dst;
1348 if (is_zero_ether_addr(mac))
1350 /* Check if MAC flow rule is already present. */
1351 for (flow = LIST_FIRST(&priv->flows);
1352 flow && flow->internal;
1353 flow = LIST_NEXT(flow, next)) {
1354 const struct ibv_flow_spec_eth *eth =
1355 (const void *)((uintptr_t)flow->ibv_attr +
1356 sizeof(*flow->ibv_attr));
1361 assert(flow->ibv_attr->type == IBV_FLOW_ATTR_NORMAL);
1362 assert(flow->ibv_attr->num_of_specs == 1);
1363 assert(eth->type == IBV_FLOW_SPEC_ETH);
1366 (eth->val.vlan_tag != *rule_vlan ||
1367 eth->mask.vlan_tag != RTE_BE16(0x0fff)))
1369 if (!rule_vlan && eth->mask.vlan_tag)
1371 for (j = 0; j != sizeof(mac->addr_bytes); ++j)
1372 if (eth->val.dst_mac[j] != mac->addr_bytes[j] ||
1373 eth->mask.dst_mac[j] != UINT8_C(0xff) ||
1374 eth->val.src_mac[j] != UINT8_C(0x00) ||
1375 eth->mask.src_mac[j] != UINT8_C(0x00))
1377 if (j != sizeof(mac->addr_bytes))
1379 if (flow->rss->queues != queues ||
1380 memcmp(flow->rss->queue_id, rss_conf->queue,
1381 queues * sizeof(flow->rss->queue_id[0])))
1385 if (!flow || !flow->internal) {
1386 /* Not found, create a new flow rule. */
1387 memcpy(rule_mac, mac, sizeof(*mac));
1388 flow = mlx4_flow_create(priv->dev, &attr, pattern,
1399 vlan = mlx4_flow_internal_next_vlan(priv, vlan + 1);
1403 /* Take care of promiscuous and all multicast flow rules. */
1405 for (flow = LIST_FIRST(&priv->flows);
1406 flow && flow->internal;
1407 flow = LIST_NEXT(flow, next)) {
1408 if (priv->dev->data->promiscuous) {
1412 assert(priv->dev->data->all_multicast);
1417 if (flow && flow->internal) {
1419 if (flow->rss->queues != queues ||
1420 memcmp(flow->rss->queue_id, rss_conf->queue,
1421 queues * sizeof(flow->rss->queue_id[0])))
1424 if (!flow || !flow->internal) {
1425 /* Not found, create a new flow rule. */
1426 if (priv->dev->data->promiscuous) {
1427 pattern[1].spec = NULL;
1428 pattern[1].mask = NULL;
1430 assert(priv->dev->data->all_multicast);
1431 pattern[1].spec = ð_allmulti;
1432 pattern[1].mask = ð_allmulti;
1434 pattern[2] = pattern[3];
1435 flow = mlx4_flow_create(priv->dev, &attr, pattern,
1442 assert(flow->promisc || flow->allmulti);
1446 /* Clear selection and clean up stale internal flow rules. */
1447 flow = LIST_FIRST(&priv->flows);
1448 while (flow && flow->internal) {
1449 struct rte_flow *next = LIST_NEXT(flow, next);
1452 claim_zero(mlx4_flow_destroy(priv->dev, flow, error));
1461 * Synchronize flow rules.
1463 * This function synchronizes flow rules with the state of the device by
1464 * taking into account isolated mode and whether target queues are
1468 * Pointer to private structure.
1470 * Perform verbose error reporting if not NULL.
1473 * 0 on success, a negative errno value otherwise and rte_errno is set.
1476 mlx4_flow_sync(struct priv *priv, struct rte_flow_error *error)
1478 struct rte_flow *flow;
1481 /* Internal flow rules are guaranteed to come first in the list. */
1482 if (priv->isolated) {
1484 * Get rid of them in isolated mode, stop at the first
1485 * non-internal rule found.
1487 for (flow = LIST_FIRST(&priv->flows);
1488 flow && flow->internal;
1489 flow = LIST_FIRST(&priv->flows))
1490 claim_zero(mlx4_flow_destroy(priv->dev, flow, error));
1492 /* Refresh internal rules. */
1493 ret = mlx4_flow_internal(priv, error);
1497 /* Toggle the remaining flow rules . */
1498 LIST_FOREACH(flow, &priv->flows, next) {
1499 ret = mlx4_flow_toggle(priv, flow, priv->started, error);
1504 assert(!priv->drop);
1509 * Clean up all flow rules.
1511 * Unlike mlx4_flow_flush(), this function takes care of all remaining flow
1512 * rules regardless of whether they are internal or user-configured.
1515 * Pointer to private structure.
1518 mlx4_flow_clean(struct priv *priv)
1520 struct rte_flow *flow;
1522 while ((flow = LIST_FIRST(&priv->flows)))
1523 mlx4_flow_destroy(priv->dev, flow, NULL);
1524 assert(LIST_EMPTY(&priv->rss));
1527 static const struct rte_flow_ops mlx4_flow_ops = {
1528 .validate = mlx4_flow_validate,
1529 .create = mlx4_flow_create,
1530 .destroy = mlx4_flow_destroy,
1531 .flush = mlx4_flow_flush,
1532 .isolate = mlx4_flow_isolate,
1536 * Manage filter operations.
1539 * Pointer to Ethernet device structure.
1540 * @param filter_type
1543 * Operation to perform.
1545 * Pointer to operation-specific structure.
1548 * 0 on success, negative errno value otherwise and rte_errno is set.
1551 mlx4_filter_ctrl(struct rte_eth_dev *dev,
1552 enum rte_filter_type filter_type,
1553 enum rte_filter_op filter_op,
1556 switch (filter_type) {
1557 case RTE_ETH_FILTER_GENERIC:
1558 if (filter_op != RTE_ETH_FILTER_GET)
1560 *(const void **)arg = &mlx4_flow_ops;
1563 ERROR("%p: filter type (%d) not supported",
1564 (void *)dev, filter_type);
1567 rte_errno = ENOTSUP;