1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2017 6WIND S.A.
3 * Copyright 2017 Mellanox Technologies, Ltd
8 * Flow API operations for mlx4 driver.
11 #include <arpa/inet.h>
18 #include <sys/queue.h>
20 /* Verbs headers do not support -pedantic. */
22 #pragma GCC diagnostic ignored "-Wpedantic"
24 #include <infiniband/verbs.h>
26 #pragma GCC diagnostic error "-Wpedantic"
29 #include <rte_byteorder.h>
30 #include <rte_errno.h>
31 #include <rte_ethdev_driver.h>
32 #include <rte_ether.h>
34 #include <rte_flow_driver.h>
35 #include <rte_malloc.h>
39 #include "mlx4_glue.h"
40 #include "mlx4_flow.h"
41 #include "mlx4_rxtx.h"
42 #include "mlx4_utils.h"
44 /** Static initializer for a list of subsequent item types. */
45 #define NEXT_ITEM(...) \
46 (const enum rte_flow_item_type []){ \
47 __VA_ARGS__, RTE_FLOW_ITEM_TYPE_END, \
50 /** Processor structure associated with a flow item. */
51 struct mlx4_flow_proc_item {
52 /** Bit-mask for fields supported by this PMD. */
53 const void *mask_support;
54 /** Bit-mask to use when @p item->mask is not provided. */
55 const void *mask_default;
56 /** Size in bytes for @p mask_support and @p mask_default. */
57 const unsigned int mask_sz;
58 /** Merge a pattern item into a flow rule handle. */
59 int (*merge)(struct rte_flow *flow,
60 const struct rte_flow_item *item,
61 const struct mlx4_flow_proc_item *proc,
62 struct rte_flow_error *error);
63 /** Size in bytes of the destination structure. */
64 const unsigned int dst_sz;
65 /** List of possible subsequent items. */
66 const enum rte_flow_item_type *const next_item;
69 /** Shared resources for drop flow rules. */
71 struct ibv_qp *qp; /**< QP target. */
72 struct ibv_cq *cq; /**< CQ associated with above QP. */
73 struct mlx4_priv *priv; /**< Back pointer to private data. */
74 uint32_t refcnt; /**< Reference count. */
78 * Convert supported RSS hash field types between DPDK and Verbs formats.
80 * This function returns the supported (default) set when @p types has
84 * Pointer to private structure.
86 * Depending on @p verbs_to_dpdk, hash types in either DPDK (see struct
87 * rte_eth_rss_conf) or Verbs format.
88 * @param verbs_to_dpdk
89 * A zero value converts @p types from DPDK to Verbs, a nonzero value
90 * performs the reverse operation.
93 * Converted RSS hash fields on success, (uint64_t)-1 otherwise and
97 mlx4_conv_rss_types(struct mlx4_priv *priv, uint64_t types, int verbs_to_dpdk)
101 IPV4, IPV4_1, IPV4_2, IPV6, IPV6_1, IPV6_2, IPV6_3,
103 IPV4_TCP, IPV4_UDP, IPV6_TCP, IPV6_TCP_1, IPV6_UDP, IPV6_UDP_1,
106 VERBS_IPV4 = IBV_RX_HASH_SRC_IPV4 | IBV_RX_HASH_DST_IPV4,
107 VERBS_IPV6 = IBV_RX_HASH_SRC_IPV6 | IBV_RX_HASH_DST_IPV6,
108 VERBS_TCP = IBV_RX_HASH_SRC_PORT_TCP | IBV_RX_HASH_DST_PORT_TCP,
109 VERBS_UDP = IBV_RX_HASH_SRC_PORT_UDP | IBV_RX_HASH_DST_PORT_UDP,
111 static const uint64_t dpdk[] = {
113 [IPV4] = ETH_RSS_IPV4,
114 [IPV4_1] = ETH_RSS_FRAG_IPV4,
115 [IPV4_2] = ETH_RSS_NONFRAG_IPV4_OTHER,
116 [IPV6] = ETH_RSS_IPV6,
117 [IPV6_1] = ETH_RSS_FRAG_IPV6,
118 [IPV6_2] = ETH_RSS_NONFRAG_IPV6_OTHER,
119 [IPV6_3] = ETH_RSS_IPV6_EX,
122 [IPV4_TCP] = ETH_RSS_NONFRAG_IPV4_TCP,
123 [IPV4_UDP] = ETH_RSS_NONFRAG_IPV4_UDP,
124 [IPV6_TCP] = ETH_RSS_NONFRAG_IPV6_TCP,
125 [IPV6_TCP_1] = ETH_RSS_IPV6_TCP_EX,
126 [IPV6_UDP] = ETH_RSS_NONFRAG_IPV6_UDP,
127 [IPV6_UDP_1] = ETH_RSS_IPV6_UDP_EX,
129 static const uint64_t verbs[RTE_DIM(dpdk)] = {
130 [INNER] = IBV_RX_HASH_INNER,
132 [IPV4_1] = VERBS_IPV4,
133 [IPV4_2] = VERBS_IPV4,
135 [IPV6_1] = VERBS_IPV6,
136 [IPV6_2] = VERBS_IPV6,
137 [IPV6_3] = VERBS_IPV6,
140 [IPV4_TCP] = VERBS_IPV4 | VERBS_TCP,
141 [IPV4_UDP] = VERBS_IPV4 | VERBS_UDP,
142 [IPV6_TCP] = VERBS_IPV6 | VERBS_TCP,
143 [IPV6_TCP_1] = VERBS_IPV6 | VERBS_TCP,
144 [IPV6_UDP] = VERBS_IPV6 | VERBS_UDP,
145 [IPV6_UDP_1] = VERBS_IPV6 | VERBS_UDP,
147 const uint64_t *in = verbs_to_dpdk ? verbs : dpdk;
148 const uint64_t *out = verbs_to_dpdk ? dpdk : verbs;
155 return priv->hw_rss_sup;
156 types = priv->hw_rss_sup;
158 for (i = 0; i != RTE_DIM(dpdk); ++i)
159 if (in[i] && (types & in[i]) == in[i]) {
160 seen |= types & in[i];
163 if ((verbs_to_dpdk || (conv & priv->hw_rss_sup) == conv) &&
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;
208 uint32_t sum_dst = 0;
209 uint32_t sum_src = 0;
211 for (i = 0; i != sizeof(mask->dst.addr_bytes); ++i) {
212 sum_dst += mask->dst.addr_bytes[i];
213 sum_src += mask->src.addr_bytes[i];
216 msg = "mlx4 does not support source MAC matching";
218 } else if (!sum_dst) {
220 } else if (sum_dst == 1 && mask->dst.addr_bytes[0] == 1) {
221 if (!(spec->dst.addr_bytes[0] & 1)) {
222 msg = "mlx4 does not support the explicit"
223 " exclusion of all multicast traffic";
227 } else if (sum_dst != (UINT8_C(0xff) * RTE_ETHER_ADDR_LEN)) {
228 msg = "mlx4 does not support matching partial"
236 flow->ibv_attr->type = IBV_FLOW_ATTR_ALL_DEFAULT;
239 if (flow->allmulti) {
240 flow->ibv_attr->type = IBV_FLOW_ATTR_MC_DEFAULT;
243 ++flow->ibv_attr->num_of_specs;
244 eth = (void *)((uintptr_t)flow->ibv_attr + flow->ibv_attr_size);
245 *eth = (struct ibv_flow_spec_eth) {
246 .type = IBV_FLOW_SPEC_ETH,
247 .size = sizeof(*eth),
250 eth->val.dst_mac[0] = 0xff;
251 flow->ibv_attr->type = IBV_FLOW_ATTR_ALL_DEFAULT;
255 memcpy(eth->val.dst_mac, spec->dst.addr_bytes, RTE_ETHER_ADDR_LEN);
256 memcpy(eth->mask.dst_mac, mask->dst.addr_bytes, RTE_ETHER_ADDR_LEN);
257 /* Remove unwanted bits from values. */
258 for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i)
259 eth->val.dst_mac[i] &= eth->mask.dst_mac[i];
263 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
268 * Merge VLAN pattern item into flow rule handle.
270 * Additional mlx4-specific constraints on supported fields:
272 * - Matching *all* VLAN traffic by omitting @p item->spec or providing an
273 * empty @p item->mask would also include non-VLAN traffic. Doing so is
274 * therefore unsupported.
275 * - No support for partial masks.
277 * @param[in, out] flow
278 * Flow rule handle to update.
280 * Pattern item to merge.
282 * Associated item-processing object.
284 * Perform verbose error reporting if not NULL.
287 * 0 on success, a negative errno value otherwise and rte_errno is set.
290 mlx4_flow_merge_vlan(struct rte_flow *flow,
291 const struct rte_flow_item *item,
292 const struct mlx4_flow_proc_item *proc,
293 struct rte_flow_error *error)
295 const struct rte_flow_item_vlan *spec = item->spec;
296 const struct rte_flow_item_vlan *mask =
297 spec ? (item->mask ? item->mask : proc->mask_default) : NULL;
298 struct ibv_flow_spec_eth *eth;
301 if (!mask || !mask->tci) {
302 msg = "mlx4 cannot match all VLAN traffic while excluding"
303 " non-VLAN traffic, TCI VID must be specified";
306 if (mask->tci != RTE_BE16(0x0fff)) {
307 msg = "mlx4 does not support partial TCI VID matching";
312 eth = (void *)((uintptr_t)flow->ibv_attr + flow->ibv_attr_size -
314 eth->val.vlan_tag = spec->tci;
315 eth->mask.vlan_tag = mask->tci;
316 eth->val.vlan_tag &= eth->mask.vlan_tag;
317 if (flow->ibv_attr->type == IBV_FLOW_ATTR_ALL_DEFAULT)
318 flow->ibv_attr->type = IBV_FLOW_ATTR_NORMAL;
321 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
326 * Merge IPv4 pattern item into flow rule handle.
328 * Additional mlx4-specific constraints on supported fields:
330 * - No support for partial masks.
332 * @param[in, out] flow
333 * Flow rule handle to update.
335 * Pattern item to merge.
337 * Associated item-processing object.
339 * Perform verbose error reporting if not NULL.
342 * 0 on success, a negative errno value otherwise and rte_errno is set.
345 mlx4_flow_merge_ipv4(struct rte_flow *flow,
346 const struct rte_flow_item *item,
347 const struct mlx4_flow_proc_item *proc,
348 struct rte_flow_error *error)
350 const struct rte_flow_item_ipv4 *spec = item->spec;
351 const struct rte_flow_item_ipv4 *mask =
352 spec ? (item->mask ? item->mask : proc->mask_default) : NULL;
353 struct ibv_flow_spec_ipv4 *ipv4;
357 ((uint32_t)(mask->hdr.src_addr + 1) > UINT32_C(1) ||
358 (uint32_t)(mask->hdr.dst_addr + 1) > UINT32_C(1))) {
359 msg = "mlx4 does not support matching partial IPv4 fields";
364 ++flow->ibv_attr->num_of_specs;
365 ipv4 = (void *)((uintptr_t)flow->ibv_attr + flow->ibv_attr_size);
366 *ipv4 = (struct ibv_flow_spec_ipv4) {
367 .type = IBV_FLOW_SPEC_IPV4,
368 .size = sizeof(*ipv4),
372 ipv4->val = (struct ibv_flow_ipv4_filter) {
373 .src_ip = spec->hdr.src_addr,
374 .dst_ip = spec->hdr.dst_addr,
376 ipv4->mask = (struct ibv_flow_ipv4_filter) {
377 .src_ip = mask->hdr.src_addr,
378 .dst_ip = mask->hdr.dst_addr,
380 /* Remove unwanted bits from values. */
381 ipv4->val.src_ip &= ipv4->mask.src_ip;
382 ipv4->val.dst_ip &= ipv4->mask.dst_ip;
385 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
390 * Merge UDP pattern item into flow rule handle.
392 * Additional mlx4-specific constraints on supported fields:
394 * - No support for partial masks.
395 * - Due to HW/FW limitation, flow rule priority is not taken into account
396 * when matching UDP destination ports, doing is therefore only supported
397 * at the highest priority level (0).
399 * @param[in, out] flow
400 * Flow rule handle to update.
402 * Pattern item to merge.
404 * Associated item-processing object.
406 * Perform verbose error reporting if not NULL.
409 * 0 on success, a negative errno value otherwise and rte_errno is set.
412 mlx4_flow_merge_udp(struct rte_flow *flow,
413 const struct rte_flow_item *item,
414 const struct mlx4_flow_proc_item *proc,
415 struct rte_flow_error *error)
417 const struct rte_flow_item_udp *spec = item->spec;
418 const struct rte_flow_item_udp *mask =
419 spec ? (item->mask ? item->mask : proc->mask_default) : NULL;
420 struct ibv_flow_spec_tcp_udp *udp;
424 ((uint16_t)(mask->hdr.src_port + 1) > UINT16_C(1) ||
425 (uint16_t)(mask->hdr.dst_port + 1) > UINT16_C(1))) {
426 msg = "mlx4 does not support matching partial UDP fields";
429 if (mask && mask->hdr.dst_port && flow->priority) {
430 msg = "combining UDP destination port matching with a nonzero"
431 " priority level is not supported";
436 ++flow->ibv_attr->num_of_specs;
437 udp = (void *)((uintptr_t)flow->ibv_attr + flow->ibv_attr_size);
438 *udp = (struct ibv_flow_spec_tcp_udp) {
439 .type = IBV_FLOW_SPEC_UDP,
440 .size = sizeof(*udp),
444 udp->val.dst_port = spec->hdr.dst_port;
445 udp->val.src_port = spec->hdr.src_port;
446 udp->mask.dst_port = mask->hdr.dst_port;
447 udp->mask.src_port = mask->hdr.src_port;
448 /* Remove unwanted bits from values. */
449 udp->val.src_port &= udp->mask.src_port;
450 udp->val.dst_port &= udp->mask.dst_port;
453 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
458 * Merge TCP pattern item into flow rule handle.
460 * Additional mlx4-specific constraints on supported fields:
462 * - No support for partial masks.
464 * @param[in, out] flow
465 * Flow rule handle to update.
467 * Pattern item to merge.
469 * Associated item-processing object.
471 * Perform verbose error reporting if not NULL.
474 * 0 on success, a negative errno value otherwise and rte_errno is set.
477 mlx4_flow_merge_tcp(struct rte_flow *flow,
478 const struct rte_flow_item *item,
479 const struct mlx4_flow_proc_item *proc,
480 struct rte_flow_error *error)
482 const struct rte_flow_item_tcp *spec = item->spec;
483 const struct rte_flow_item_tcp *mask =
484 spec ? (item->mask ? item->mask : proc->mask_default) : NULL;
485 struct ibv_flow_spec_tcp_udp *tcp;
489 ((uint16_t)(mask->hdr.src_port + 1) > UINT16_C(1) ||
490 (uint16_t)(mask->hdr.dst_port + 1) > UINT16_C(1))) {
491 msg = "mlx4 does not support matching partial TCP fields";
496 ++flow->ibv_attr->num_of_specs;
497 tcp = (void *)((uintptr_t)flow->ibv_attr + flow->ibv_attr_size);
498 *tcp = (struct ibv_flow_spec_tcp_udp) {
499 .type = IBV_FLOW_SPEC_TCP,
500 .size = sizeof(*tcp),
504 tcp->val.dst_port = spec->hdr.dst_port;
505 tcp->val.src_port = spec->hdr.src_port;
506 tcp->mask.dst_port = mask->hdr.dst_port;
507 tcp->mask.src_port = mask->hdr.src_port;
508 /* Remove unwanted bits from values. */
509 tcp->val.src_port &= tcp->mask.src_port;
510 tcp->val.dst_port &= tcp->mask.dst_port;
513 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
518 * Perform basic sanity checks on a pattern item.
521 * Item specification.
523 * Associated item-processing object.
525 * Perform verbose error reporting if not NULL.
528 * 0 on success, a negative errno value otherwise and rte_errno is set.
531 mlx4_flow_item_check(const struct rte_flow_item *item,
532 const struct mlx4_flow_proc_item *proc,
533 struct rte_flow_error *error)
538 /* item->last and item->mask cannot exist without item->spec. */
539 if (!item->spec && (item->mask || item->last))
540 return rte_flow_error_set
541 (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, item,
542 "\"mask\" or \"last\" field provided without a"
543 " corresponding \"spec\"");
544 /* No spec, no mask, no problem. */
548 (const uint8_t *)item->mask :
549 (const uint8_t *)proc->mask_default;
552 * Single-pass check to make sure that:
553 * - Mask is supported, no bits are set outside proc->mask_support.
554 * - Both item->spec and item->last are included in mask.
556 for (i = 0; i != proc->mask_sz; ++i) {
559 if ((mask[i] | ((const uint8_t *)proc->mask_support)[i]) !=
560 ((const uint8_t *)proc->mask_support)[i])
561 return rte_flow_error_set
562 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
563 item, "unsupported field found in \"mask\"");
565 (((const uint8_t *)item->spec)[i] & mask[i]) !=
566 (((const uint8_t *)item->last)[i] & mask[i]))
567 return rte_flow_error_set
568 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
570 "range between \"spec\" and \"last\""
571 " is larger than \"mask\"");
576 /** Graph of supported items and associated actions. */
577 static const struct mlx4_flow_proc_item mlx4_flow_proc_item_list[] = {
578 [RTE_FLOW_ITEM_TYPE_END] = {
579 .next_item = NEXT_ITEM(RTE_FLOW_ITEM_TYPE_ETH),
581 [RTE_FLOW_ITEM_TYPE_ETH] = {
582 .next_item = NEXT_ITEM(RTE_FLOW_ITEM_TYPE_VLAN,
583 RTE_FLOW_ITEM_TYPE_IPV4),
584 .mask_support = &(const struct rte_flow_item_eth){
585 /* Only destination MAC can be matched. */
586 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
588 .mask_default = &rte_flow_item_eth_mask,
589 .mask_sz = sizeof(struct rte_flow_item_eth),
590 .merge = mlx4_flow_merge_eth,
591 .dst_sz = sizeof(struct ibv_flow_spec_eth),
593 [RTE_FLOW_ITEM_TYPE_VLAN] = {
594 .next_item = NEXT_ITEM(RTE_FLOW_ITEM_TYPE_IPV4),
595 .mask_support = &(const struct rte_flow_item_vlan){
596 /* Only TCI VID matching is supported. */
597 .tci = RTE_BE16(0x0fff),
599 .mask_default = &rte_flow_item_vlan_mask,
600 .mask_sz = sizeof(struct rte_flow_item_vlan),
601 .merge = mlx4_flow_merge_vlan,
604 [RTE_FLOW_ITEM_TYPE_IPV4] = {
605 .next_item = NEXT_ITEM(RTE_FLOW_ITEM_TYPE_UDP,
606 RTE_FLOW_ITEM_TYPE_TCP),
607 .mask_support = &(const struct rte_flow_item_ipv4){
609 .src_addr = RTE_BE32(0xffffffff),
610 .dst_addr = RTE_BE32(0xffffffff),
613 .mask_default = &rte_flow_item_ipv4_mask,
614 .mask_sz = sizeof(struct rte_flow_item_ipv4),
615 .merge = mlx4_flow_merge_ipv4,
616 .dst_sz = sizeof(struct ibv_flow_spec_ipv4),
618 [RTE_FLOW_ITEM_TYPE_UDP] = {
619 .mask_support = &(const struct rte_flow_item_udp){
621 .src_port = RTE_BE16(0xffff),
622 .dst_port = RTE_BE16(0xffff),
625 .mask_default = &rte_flow_item_udp_mask,
626 .mask_sz = sizeof(struct rte_flow_item_udp),
627 .merge = mlx4_flow_merge_udp,
628 .dst_sz = sizeof(struct ibv_flow_spec_tcp_udp),
630 [RTE_FLOW_ITEM_TYPE_TCP] = {
631 .mask_support = &(const struct rte_flow_item_tcp){
633 .src_port = RTE_BE16(0xffff),
634 .dst_port = RTE_BE16(0xffff),
637 .mask_default = &rte_flow_item_tcp_mask,
638 .mask_sz = sizeof(struct rte_flow_item_tcp),
639 .merge = mlx4_flow_merge_tcp,
640 .dst_sz = sizeof(struct ibv_flow_spec_tcp_udp),
645 * Make sure a flow rule is supported and initialize associated structure.
648 * Pointer to private structure.
650 * Flow rule attributes.
652 * Pattern specification (list terminated by the END pattern item).
654 * Associated actions (list terminated by the END action).
656 * Perform verbose error reporting if not NULL.
657 * @param[in, out] addr
658 * Buffer where the resulting flow rule handle pointer must be stored.
659 * If NULL, stop processing after validation stage.
662 * 0 on success, a negative errno value otherwise and rte_errno is set.
665 mlx4_flow_prepare(struct mlx4_priv *priv,
666 const struct rte_flow_attr *attr,
667 const struct rte_flow_item pattern[],
668 const struct rte_flow_action actions[],
669 struct rte_flow_error *error,
670 struct rte_flow **addr)
672 const struct rte_flow_item *item;
673 const struct rte_flow_action *action;
674 const struct mlx4_flow_proc_item *proc;
675 struct rte_flow temp = { .ibv_attr_size = sizeof(*temp.ibv_attr) };
676 struct rte_flow *flow = &temp;
677 const char *msg = NULL;
681 return rte_flow_error_set
682 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
683 NULL, "groups are not supported");
684 if (attr->priority > MLX4_FLOW_PRIORITY_LAST)
685 return rte_flow_error_set
686 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
687 NULL, "maximum priority level is "
688 MLX4_STR_EXPAND(MLX4_FLOW_PRIORITY_LAST));
690 return rte_flow_error_set
691 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
692 NULL, "egress is not supported");
694 return rte_flow_error_set
695 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
696 NULL, "transfer is not supported");
698 return rte_flow_error_set
699 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
700 NULL, "only ingress is supported");
703 proc = mlx4_flow_proc_item_list;
704 flow->priority = attr->priority;
705 /* Go over pattern. */
706 for (item = pattern; item->type; ++item) {
707 const struct mlx4_flow_proc_item *next = NULL;
711 if (item->type == RTE_FLOW_ITEM_TYPE_VOID)
713 if (item->type == MLX4_FLOW_ITEM_TYPE_INTERNAL) {
717 if (flow->promisc || flow->allmulti) {
718 msg = "mlx4 does not support additional matching"
719 " criteria combined with indiscriminate"
720 " matching on Ethernet headers";
721 goto exit_item_not_supported;
723 for (i = 0; proc->next_item && proc->next_item[i]; ++i) {
724 if (proc->next_item[i] == item->type) {
725 next = &mlx4_flow_proc_item_list[item->type];
730 goto exit_item_not_supported;
733 * Perform basic sanity checks only once, while handle is
737 err = mlx4_flow_item_check(item, proc, error);
742 err = proc->merge(flow, item, proc, error);
746 flow->ibv_attr_size += proc->dst_sz;
748 /* Go over actions list. */
749 for (action = actions; action->type; ++action) {
750 /* This one may appear anywhere multiple times. */
751 if (action->type == RTE_FLOW_ACTION_TYPE_VOID)
753 /* Fate-deciding actions may appear exactly once. */
755 msg = "cannot combine several fate-deciding actions,"
756 " choose between DROP, QUEUE or RSS";
757 goto exit_action_not_supported;
760 switch (action->type) {
761 const struct rte_flow_action_queue *queue;
762 const struct rte_flow_action_rss *rss;
763 const uint8_t *rss_key;
764 uint32_t rss_key_len;
768 case RTE_FLOW_ACTION_TYPE_DROP:
771 case RTE_FLOW_ACTION_TYPE_QUEUE:
774 queue = action->conf;
775 if (queue->index >= ETH_DEV(priv)->data->nb_rx_queues) {
776 msg = "queue target index beyond number of"
777 " configured Rx queues";
778 goto exit_action_not_supported;
780 flow->rss = mlx4_rss_get
781 (priv, 0, mlx4_rss_hash_key_default, 1,
784 msg = "not enough resources for additional"
785 " single-queue RSS context";
786 goto exit_action_not_supported;
789 case RTE_FLOW_ACTION_TYPE_RSS:
793 /* Default RSS configuration if none is provided. */
796 rss_key_len = rss->key_len;
798 rss_key = mlx4_rss_hash_key_default;
799 rss_key_len = MLX4_RSS_HASH_KEY_SIZE;
802 for (i = 0; i < rss->queue_num; ++i)
804 ETH_DEV(priv)->data->nb_rx_queues)
806 if (i != rss->queue_num) {
807 msg = "queue index target beyond number of"
808 " configured Rx queues";
809 goto exit_action_not_supported;
811 if (!rte_is_power_of_2(rss->queue_num)) {
812 msg = "for RSS, mlx4 requires the number of"
813 " queues to be a power of two";
814 goto exit_action_not_supported;
816 if (rss_key_len != sizeof(flow->rss->key)) {
817 msg = "mlx4 supports exactly one RSS hash key"
819 MLX4_STR_EXPAND(MLX4_RSS_HASH_KEY_SIZE);
820 goto exit_action_not_supported;
822 for (i = 1; i < rss->queue_num; ++i)
823 if (rss->queue[i] - rss->queue[i - 1] != 1)
825 if (i != rss->queue_num) {
826 msg = "mlx4 requires RSS contexts to use"
827 " consecutive queue indices only";
828 goto exit_action_not_supported;
830 if (rss->queue[0] % rss->queue_num) {
831 msg = "mlx4 requires the first queue of a RSS"
832 " context to be aligned on a multiple"
833 " of the context size";
834 goto exit_action_not_supported;
837 rss->func != RTE_ETH_HASH_FUNCTION_TOEPLITZ) {
838 msg = "the only supported RSS hash function"
840 goto exit_action_not_supported;
843 msg = "a nonzero RSS encapsulation level is"
845 goto exit_action_not_supported;
848 fields = mlx4_conv_rss_types(priv, rss->types, 0);
849 if (fields == (uint64_t)-1 && rte_errno) {
850 msg = "unsupported RSS hash type requested";
851 goto exit_action_not_supported;
853 flow->rss = mlx4_rss_get
854 (priv, fields, rss_key, rss->queue_num,
857 msg = "either invalid parameters or not enough"
858 " resources for additional multi-queue"
860 goto exit_action_not_supported;
864 goto exit_action_not_supported;
867 /* When fate is unknown, drop traffic. */
870 /* Validation ends here. */
873 mlx4_rss_put(flow->rss);
877 /* Allocate proper handle based on collected data. */
878 const struct mlx4_malloc_vec vec[] = {
880 .align = alignof(struct rte_flow),
881 .size = sizeof(*flow),
882 .addr = (void **)&flow,
885 .align = alignof(struct ibv_flow_attr),
886 .size = temp.ibv_attr_size,
887 .addr = (void **)&temp.ibv_attr,
891 if (!mlx4_zmallocv(__func__, vec, RTE_DIM(vec))) {
893 mlx4_rss_put(temp.rss);
894 return rte_flow_error_set
896 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
897 "flow rule handle allocation failure");
899 /* Most fields will be updated by second pass. */
900 *flow = (struct rte_flow){
901 .ibv_attr = temp.ibv_attr,
902 .ibv_attr_size = sizeof(*flow->ibv_attr),
905 *flow->ibv_attr = (struct ibv_flow_attr){
906 .type = IBV_FLOW_ATTR_NORMAL,
907 .size = sizeof(*flow->ibv_attr),
908 .priority = attr->priority,
915 exit_item_not_supported:
916 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
917 item, msg ? msg : "item not supported");
918 exit_action_not_supported:
919 return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
920 action, msg ? msg : "action not supported");
924 * Validate a flow supported by the NIC.
926 * @see rte_flow_validate()
930 mlx4_flow_validate(struct rte_eth_dev *dev,
931 const struct rte_flow_attr *attr,
932 const struct rte_flow_item pattern[],
933 const struct rte_flow_action actions[],
934 struct rte_flow_error *error)
936 struct mlx4_priv *priv = dev->data->dev_private;
938 return mlx4_flow_prepare(priv, attr, pattern, actions, error, NULL);
942 * Get a drop flow rule resources instance.
945 * Pointer to private structure.
948 * Pointer to drop flow resources on success, NULL otherwise and rte_errno
951 static struct mlx4_drop *
952 mlx4_drop_get(struct mlx4_priv *priv)
954 struct mlx4_drop *drop = priv->drop;
957 assert(drop->refcnt);
958 assert(drop->priv == priv);
962 drop = rte_malloc(__func__, sizeof(*drop), 0);
965 *drop = (struct mlx4_drop){
969 drop->cq = mlx4_glue->create_cq(priv->ctx, 1, NULL, NULL, 0);
972 drop->qp = mlx4_glue->create_qp
974 &(struct ibv_qp_init_attr){
977 .qp_type = IBV_QPT_RAW_PACKET,
985 claim_zero(mlx4_glue->destroy_qp(drop->qp));
987 claim_zero(mlx4_glue->destroy_cq(drop->cq));
995 * Give back a drop flow rule resources instance.
998 * Pointer to drop flow rule resources.
1001 mlx4_drop_put(struct mlx4_drop *drop)
1003 assert(drop->refcnt);
1006 drop->priv->drop = NULL;
1007 claim_zero(mlx4_glue->destroy_qp(drop->qp));
1008 claim_zero(mlx4_glue->destroy_cq(drop->cq));
1013 * Toggle a configured flow rule.
1016 * Pointer to private structure.
1018 * Flow rule handle to toggle.
1020 * Whether associated Verbs flow must be created or removed.
1022 * Perform verbose error reporting if not NULL.
1025 * 0 on success, a negative errno value otherwise and rte_errno is set.
1028 mlx4_flow_toggle(struct mlx4_priv *priv,
1029 struct rte_flow *flow,
1031 struct rte_flow_error *error)
1033 struct ibv_qp *qp = NULL;
1038 if (!flow->ibv_flow)
1040 claim_zero(mlx4_glue->destroy_flow(flow->ibv_flow));
1041 flow->ibv_flow = NULL;
1043 mlx4_drop_put(priv->drop);
1045 mlx4_rss_detach(flow->rss);
1048 assert(flow->ibv_attr);
1049 if (!flow->internal &&
1051 flow->ibv_attr->priority == MLX4_FLOW_PRIORITY_LAST) {
1052 if (flow->ibv_flow) {
1053 claim_zero(mlx4_glue->destroy_flow(flow->ibv_flow));
1054 flow->ibv_flow = NULL;
1056 mlx4_drop_put(priv->drop);
1058 mlx4_rss_detach(flow->rss);
1061 msg = ("priority level "
1062 MLX4_STR_EXPAND(MLX4_FLOW_PRIORITY_LAST)
1063 " is reserved when not in isolated mode");
1067 struct mlx4_rss *rss = flow->rss;
1071 /* Stop at the first nonexistent target queue. */
1072 for (i = 0; i != rss->queues; ++i)
1073 if (rss->queue_id[i] >=
1074 ETH_DEV(priv)->data->nb_rx_queues ||
1075 !ETH_DEV(priv)->data->rx_queues[rss->queue_id[i]]) {
1079 if (flow->ibv_flow) {
1080 if (missing ^ !flow->drop)
1082 /* Verbs flow needs updating. */
1083 claim_zero(mlx4_glue->destroy_flow(flow->ibv_flow));
1084 flow->ibv_flow = NULL;
1086 mlx4_drop_put(priv->drop);
1088 mlx4_rss_detach(rss);
1091 err = mlx4_rss_attach(rss);
1094 msg = "cannot create indirection table or hash"
1095 " QP to associate flow rule with";
1100 /* A missing target queue drops traffic implicitly. */
1101 flow->drop = missing;
1106 mlx4_drop_get(priv);
1109 msg = "resources for drop flow rule cannot be created";
1112 qp = priv->drop->qp;
1117 flow->ibv_flow = mlx4_glue->create_flow(qp, flow->ibv_attr);
1121 mlx4_drop_put(priv->drop);
1123 mlx4_rss_detach(flow->rss);
1125 msg = "flow rule rejected by device";
1127 return rte_flow_error_set
1128 (error, err, RTE_FLOW_ERROR_TYPE_HANDLE, flow, msg);
1134 * @see rte_flow_create()
1137 static struct rte_flow *
1138 mlx4_flow_create(struct rte_eth_dev *dev,
1139 const struct rte_flow_attr *attr,
1140 const struct rte_flow_item pattern[],
1141 const struct rte_flow_action actions[],
1142 struct rte_flow_error *error)
1144 struct mlx4_priv *priv = dev->data->dev_private;
1145 struct rte_flow *flow;
1148 err = mlx4_flow_prepare(priv, attr, pattern, actions, error, &flow);
1151 err = mlx4_flow_toggle(priv, flow, priv->started, error);
1153 struct rte_flow *curr = LIST_FIRST(&priv->flows);
1155 /* New rules are inserted after internal ones. */
1156 if (!curr || !curr->internal) {
1157 LIST_INSERT_HEAD(&priv->flows, flow, next);
1159 while (LIST_NEXT(curr, next) &&
1160 LIST_NEXT(curr, next)->internal)
1161 curr = LIST_NEXT(curr, next);
1162 LIST_INSERT_AFTER(curr, flow, next);
1167 mlx4_rss_put(flow->rss);
1168 rte_flow_error_set(error, -err, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1175 * Configure isolated mode.
1177 * @see rte_flow_isolate()
1181 mlx4_flow_isolate(struct rte_eth_dev *dev,
1183 struct rte_flow_error *error)
1185 struct mlx4_priv *priv = dev->data->dev_private;
1187 if (!!enable == !!priv->isolated)
1189 priv->isolated = !!enable;
1190 if (mlx4_flow_sync(priv, error)) {
1191 priv->isolated = !enable;
1198 * Destroy a flow rule.
1200 * @see rte_flow_destroy()
1204 mlx4_flow_destroy(struct rte_eth_dev *dev,
1205 struct rte_flow *flow,
1206 struct rte_flow_error *error)
1208 struct mlx4_priv *priv = dev->data->dev_private;
1209 int err = mlx4_flow_toggle(priv, flow, 0, error);
1213 LIST_REMOVE(flow, next);
1215 mlx4_rss_put(flow->rss);
1221 * Destroy user-configured flow rules.
1223 * This function skips internal flows rules.
1225 * @see rte_flow_flush()
1229 mlx4_flow_flush(struct rte_eth_dev *dev,
1230 struct rte_flow_error *error)
1232 struct mlx4_priv *priv = dev->data->dev_private;
1233 struct rte_flow *flow = LIST_FIRST(&priv->flows);
1236 struct rte_flow *next = LIST_NEXT(flow, next);
1238 if (!flow->internal)
1239 mlx4_flow_destroy(dev, flow, error);
1246 * Helper function to determine the next configured VLAN filter.
1249 * Pointer to private structure.
1251 * VLAN ID to use as a starting point.
1254 * Next configured VLAN ID or a high value (>= 4096) if there is none.
1257 mlx4_flow_internal_next_vlan(struct mlx4_priv *priv, uint16_t vlan)
1259 while (vlan < 4096) {
1260 if (ETH_DEV(priv)->data->vlan_filter_conf.ids[vlan / 64] &
1261 (UINT64_C(1) << (vlan % 64)))
1269 * Generate internal flow rules.
1271 * Various flow rules are created depending on the mode the device is in:
1274 * port MAC + broadcast + catch-all (VLAN filtering is ignored).
1276 * port MAC/VLAN + broadcast + catch-all multicast.
1278 * port MAC/VLAN + broadcast MAC/VLAN.
1280 * About MAC flow rules:
1282 * - MAC flow rules are generated from @p dev->data->mac_addrs
1283 * (@p priv->mac array).
1284 * - An additional flow rule for Ethernet broadcasts is also generated.
1285 * - All these are per-VLAN if @p DEV_RX_OFFLOAD_VLAN_FILTER
1286 * is enabled and VLAN filters are configured.
1289 * Pointer to private structure.
1291 * Perform verbose error reporting if not NULL.
1294 * 0 on success, a negative errno value otherwise and rte_errno is set.
1297 mlx4_flow_internal(struct mlx4_priv *priv, struct rte_flow_error *error)
1299 struct rte_flow_attr attr = {
1300 .priority = MLX4_FLOW_PRIORITY_LAST,
1303 struct rte_flow_item_eth eth_spec;
1304 const struct rte_flow_item_eth eth_mask = {
1305 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
1307 const struct rte_flow_item_eth eth_allmulti = {
1308 .dst.addr_bytes = "\x01\x00\x00\x00\x00\x00",
1310 struct rte_flow_item_vlan vlan_spec;
1311 const struct rte_flow_item_vlan vlan_mask = {
1312 .tci = RTE_BE16(0x0fff),
1314 struct rte_flow_item pattern[] = {
1316 .type = MLX4_FLOW_ITEM_TYPE_INTERNAL,
1319 .type = RTE_FLOW_ITEM_TYPE_ETH,
1324 /* Replaced with VLAN if filtering is enabled. */
1325 .type = RTE_FLOW_ITEM_TYPE_END,
1328 .type = RTE_FLOW_ITEM_TYPE_END,
1332 * Round number of queues down to their previous power of 2 to
1333 * comply with RSS context limitations. Extra queues silently do not
1334 * get RSS by default.
1337 rte_align32pow2(ETH_DEV(priv)->data->nb_rx_queues + 1) >> 1;
1338 uint16_t queue[queues];
1339 struct rte_flow_action_rss action_rss = {
1340 .func = RTE_ETH_HASH_FUNCTION_DEFAULT,
1343 .key_len = MLX4_RSS_HASH_KEY_SIZE,
1344 .queue_num = queues,
1345 .key = mlx4_rss_hash_key_default,
1348 struct rte_flow_action actions[] = {
1350 .type = RTE_FLOW_ACTION_TYPE_RSS,
1351 .conf = &action_rss,
1354 .type = RTE_FLOW_ACTION_TYPE_END,
1357 struct rte_ether_addr *rule_mac = ð_spec.dst;
1358 rte_be16_t *rule_vlan =
1359 (ETH_DEV(priv)->data->dev_conf.rxmode.offloads &
1360 DEV_RX_OFFLOAD_VLAN_FILTER) &&
1361 !ETH_DEV(priv)->data->promiscuous ?
1365 struct rte_flow *flow;
1369 /* Nothing to be done if there are no Rx queues. */
1372 /* Prepare default RSS configuration. */
1373 for (i = 0; i != queues; ++i)
1376 * Set up VLAN item if filtering is enabled and at least one VLAN
1377 * filter is configured.
1380 vlan = mlx4_flow_internal_next_vlan(priv, 0);
1382 pattern[2] = (struct rte_flow_item){
1383 .type = RTE_FLOW_ITEM_TYPE_VLAN,
1388 *rule_vlan = rte_cpu_to_be_16(vlan);
1393 for (i = 0; i != RTE_DIM(priv->mac) + 1; ++i) {
1394 const struct rte_ether_addr *mac;
1396 /* Broadcasts are handled by an extra iteration. */
1397 if (i < RTE_DIM(priv->mac))
1398 mac = &priv->mac[i];
1400 mac = ð_mask.dst;
1401 if (rte_is_zero_ether_addr(mac))
1403 /* Check if MAC flow rule is already present. */
1404 for (flow = LIST_FIRST(&priv->flows);
1405 flow && flow->internal;
1406 flow = LIST_NEXT(flow, next)) {
1407 const struct ibv_flow_spec_eth *eth =
1408 (const void *)((uintptr_t)flow->ibv_attr +
1409 sizeof(*flow->ibv_attr));
1414 assert(flow->ibv_attr->type == IBV_FLOW_ATTR_NORMAL);
1415 assert(flow->ibv_attr->num_of_specs == 1);
1416 assert(eth->type == IBV_FLOW_SPEC_ETH);
1419 (eth->val.vlan_tag != *rule_vlan ||
1420 eth->mask.vlan_tag != RTE_BE16(0x0fff)))
1422 if (!rule_vlan && eth->mask.vlan_tag)
1424 for (j = 0; j != sizeof(mac->addr_bytes); ++j)
1425 if (eth->val.dst_mac[j] != mac->addr_bytes[j] ||
1426 eth->mask.dst_mac[j] != UINT8_C(0xff) ||
1427 eth->val.src_mac[j] != UINT8_C(0x00) ||
1428 eth->mask.src_mac[j] != UINT8_C(0x00))
1430 if (j != sizeof(mac->addr_bytes))
1432 if (flow->rss->queues != queues ||
1433 memcmp(flow->rss->queue_id, action_rss.queue,
1434 queues * sizeof(flow->rss->queue_id[0])))
1438 if (!flow || !flow->internal) {
1439 /* Not found, create a new flow rule. */
1440 memcpy(rule_mac, mac, sizeof(*mac));
1441 flow = mlx4_flow_create(ETH_DEV(priv), &attr, pattern,
1452 vlan = mlx4_flow_internal_next_vlan(priv, vlan + 1);
1456 /* Take care of promiscuous and all multicast flow rules. */
1457 if (ETH_DEV(priv)->data->promiscuous ||
1458 ETH_DEV(priv)->data->all_multicast) {
1459 for (flow = LIST_FIRST(&priv->flows);
1460 flow && flow->internal;
1461 flow = LIST_NEXT(flow, next)) {
1462 if (ETH_DEV(priv)->data->promiscuous) {
1466 assert(ETH_DEV(priv)->data->all_multicast);
1471 if (flow && flow->internal) {
1473 if (flow->rss->queues != queues ||
1474 memcmp(flow->rss->queue_id, action_rss.queue,
1475 queues * sizeof(flow->rss->queue_id[0])))
1478 if (!flow || !flow->internal) {
1479 /* Not found, create a new flow rule. */
1480 if (ETH_DEV(priv)->data->promiscuous) {
1481 pattern[1].spec = NULL;
1482 pattern[1].mask = NULL;
1484 assert(ETH_DEV(priv)->data->all_multicast);
1485 pattern[1].spec = ð_allmulti;
1486 pattern[1].mask = ð_allmulti;
1488 pattern[2] = pattern[3];
1489 flow = mlx4_flow_create(ETH_DEV(priv), &attr, pattern,
1496 assert(flow->promisc || flow->allmulti);
1500 /* Clear selection and clean up stale internal flow rules. */
1501 flow = LIST_FIRST(&priv->flows);
1502 while (flow && flow->internal) {
1503 struct rte_flow *next = LIST_NEXT(flow, next);
1506 claim_zero(mlx4_flow_destroy(ETH_DEV(priv), flow,
1516 * Synchronize flow rules.
1518 * This function synchronizes flow rules with the state of the device by
1519 * taking into account isolated mode and whether target queues are
1523 * Pointer to private structure.
1525 * Perform verbose error reporting if not NULL.
1528 * 0 on success, a negative errno value otherwise and rte_errno is set.
1531 mlx4_flow_sync(struct mlx4_priv *priv, struct rte_flow_error *error)
1533 struct rte_flow *flow;
1536 /* Internal flow rules are guaranteed to come first in the list. */
1537 if (priv->isolated) {
1539 * Get rid of them in isolated mode, stop at the first
1540 * non-internal rule found.
1542 for (flow = LIST_FIRST(&priv->flows);
1543 flow && flow->internal;
1544 flow = LIST_FIRST(&priv->flows))
1545 claim_zero(mlx4_flow_destroy(ETH_DEV(priv), flow,
1548 /* Refresh internal rules. */
1549 ret = mlx4_flow_internal(priv, error);
1553 /* Toggle the remaining flow rules . */
1554 LIST_FOREACH(flow, &priv->flows, next) {
1555 ret = mlx4_flow_toggle(priv, flow, priv->started, error);
1560 assert(!priv->drop);
1565 * Clean up all flow rules.
1567 * Unlike mlx4_flow_flush(), this function takes care of all remaining flow
1568 * rules regardless of whether they are internal or user-configured.
1571 * Pointer to private structure.
1574 mlx4_flow_clean(struct mlx4_priv *priv)
1576 struct rte_flow *flow;
1578 while ((flow = LIST_FIRST(&priv->flows)))
1579 mlx4_flow_destroy(ETH_DEV(priv), flow, NULL);
1580 assert(LIST_EMPTY(&priv->rss));
1583 static const struct rte_flow_ops mlx4_flow_ops = {
1584 .validate = mlx4_flow_validate,
1585 .create = mlx4_flow_create,
1586 .destroy = mlx4_flow_destroy,
1587 .flush = mlx4_flow_flush,
1588 .isolate = mlx4_flow_isolate,
1592 * Manage filter operations.
1595 * Pointer to Ethernet device structure.
1596 * @param filter_type
1599 * Operation to perform.
1601 * Pointer to operation-specific structure.
1604 * 0 on success, negative errno value otherwise and rte_errno is set.
1607 mlx4_filter_ctrl(struct rte_eth_dev *dev,
1608 enum rte_filter_type filter_type,
1609 enum rte_filter_op filter_op,
1612 switch (filter_type) {
1613 case RTE_ETH_FILTER_GENERIC:
1614 if (filter_op != RTE_ETH_FILTER_GET)
1616 *(const void **)arg = &mlx4_flow_ops;
1619 ERROR("%p: filter type (%d) not supported",
1620 (void *)dev, filter_type);
1623 rte_errno = ENOTSUP;