4 * Copyright 2015 6WIND S.A.
5 * Copyright 2015 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.
41 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
43 #pragma GCC diagnostic ignored "-Wpedantic"
45 #include <infiniband/verbs.h>
47 #pragma GCC diagnostic error "-Wpedantic"
50 /* DPDK headers don't like -pedantic. */
52 #pragma GCC diagnostic ignored "-Wpedantic"
54 #include <rte_ether.h>
55 #include <rte_malloc.h>
56 #include <rte_ethdev.h>
57 #include <rte_common.h>
59 #include <rte_flow_driver.h>
61 #pragma GCC diagnostic error "-Wpedantic"
65 #include "mlx5_rxtx.h"
67 struct fdir_flow_desc {
74 enum hash_rxq_type type;
77 struct mlx5_fdir_filter {
78 LIST_ENTRY(mlx5_fdir_filter) next;
79 uint16_t queue; /* Queue assigned to if FDIR match. */
80 enum rte_eth_fdir_behavior behavior;
81 struct fdir_flow_desc desc;
82 struct ibv_exp_flow *flow;
85 LIST_HEAD(fdir_filter_list, mlx5_fdir_filter);
88 * Convert struct rte_eth_fdir_filter to mlx5 filter descriptor.
90 * @param[in] fdir_filter
91 * DPDK filter structure to convert.
93 * Resulting mlx5 filter descriptor.
98 fdir_filter_to_flow_desc(const struct rte_eth_fdir_filter *fdir_filter,
99 struct fdir_flow_desc *desc, enum rte_fdir_mode mode)
101 /* Initialize descriptor. */
102 memset(desc, 0, sizeof(*desc));
105 desc->vlan_tag = fdir_filter->input.flow_ext.vlan_tci;
107 /* Set MAC address. */
108 if (mode == RTE_FDIR_MODE_PERFECT_MAC_VLAN) {
109 rte_memcpy(desc->mac,
110 fdir_filter->input.flow.mac_vlan_flow.mac_addr.
113 desc->type = HASH_RXQ_ETH;
118 switch (fdir_filter->input.flow_type) {
119 case RTE_ETH_FLOW_NONFRAG_IPV4_UDP:
120 desc->type = HASH_RXQ_UDPV4;
122 case RTE_ETH_FLOW_NONFRAG_IPV4_TCP:
123 desc->type = HASH_RXQ_TCPV4;
125 case RTE_ETH_FLOW_NONFRAG_IPV4_OTHER:
126 desc->type = HASH_RXQ_IPV4;
128 case RTE_ETH_FLOW_NONFRAG_IPV6_UDP:
129 desc->type = HASH_RXQ_UDPV6;
131 case RTE_ETH_FLOW_NONFRAG_IPV6_TCP:
132 desc->type = HASH_RXQ_TCPV6;
134 case RTE_ETH_FLOW_NONFRAG_IPV6_OTHER:
135 desc->type = HASH_RXQ_IPV6;
141 /* Set flow values */
142 switch (fdir_filter->input.flow_type) {
143 case RTE_ETH_FLOW_NONFRAG_IPV4_UDP:
144 case RTE_ETH_FLOW_NONFRAG_IPV4_TCP:
145 desc->src_port = fdir_filter->input.flow.udp4_flow.src_port;
146 desc->dst_port = fdir_filter->input.flow.udp4_flow.dst_port;
147 case RTE_ETH_FLOW_NONFRAG_IPV4_OTHER:
148 desc->src_ip[0] = fdir_filter->input.flow.ip4_flow.src_ip;
149 desc->dst_ip[0] = fdir_filter->input.flow.ip4_flow.dst_ip;
151 case RTE_ETH_FLOW_NONFRAG_IPV6_UDP:
152 case RTE_ETH_FLOW_NONFRAG_IPV6_TCP:
153 desc->src_port = fdir_filter->input.flow.udp6_flow.src_port;
154 desc->dst_port = fdir_filter->input.flow.udp6_flow.dst_port;
156 case RTE_ETH_FLOW_NONFRAG_IPV6_OTHER:
157 rte_memcpy(desc->src_ip,
158 fdir_filter->input.flow.ipv6_flow.src_ip,
159 sizeof(desc->src_ip));
160 rte_memcpy(desc->dst_ip,
161 fdir_filter->input.flow.ipv6_flow.dst_ip,
162 sizeof(desc->dst_ip));
170 * Check if two flow descriptors overlap according to configured mask.
173 * Private structure that provides flow director mask.
175 * First flow descriptor to compare.
177 * Second flow descriptor to compare.
180 * Nonzero if descriptors overlap.
183 priv_fdir_overlap(const struct priv *priv,
184 const struct fdir_flow_desc *desc1,
185 const struct fdir_flow_desc *desc2)
187 const struct rte_eth_fdir_masks *mask =
188 &priv->dev->data->dev_conf.fdir_conf.mask;
191 if (desc1->type != desc2->type)
193 /* Ignore non masked bits. */
194 for (i = 0; i != RTE_DIM(desc1->mac); ++i)
195 if ((desc1->mac[i] & mask->mac_addr_byte_mask) !=
196 (desc2->mac[i] & mask->mac_addr_byte_mask))
198 if (((desc1->src_port & mask->src_port_mask) !=
199 (desc2->src_port & mask->src_port_mask)) ||
200 ((desc1->dst_port & mask->dst_port_mask) !=
201 (desc2->dst_port & mask->dst_port_mask)))
203 switch (desc1->type) {
207 if (((desc1->src_ip[0] & mask->ipv4_mask.src_ip) !=
208 (desc2->src_ip[0] & mask->ipv4_mask.src_ip)) ||
209 ((desc1->dst_ip[0] & mask->ipv4_mask.dst_ip) !=
210 (desc2->dst_ip[0] & mask->ipv4_mask.dst_ip)))
216 for (i = 0; i != RTE_DIM(desc1->src_ip); ++i)
217 if (((desc1->src_ip[i] & mask->ipv6_mask.src_ip[i]) !=
218 (desc2->src_ip[i] & mask->ipv6_mask.src_ip[i])) ||
219 ((desc1->dst_ip[i] & mask->ipv6_mask.dst_ip[i]) !=
220 (desc2->dst_ip[i] & mask->ipv6_mask.dst_ip[i])))
230 * Create flow director steering rule for a specific filter.
234 * @param mlx5_fdir_filter
235 * Filter to create a steering rule for.
237 * Flow director queue for matching packets.
240 * 0 on success, errno value on failure.
243 priv_fdir_flow_add(struct priv *priv,
244 struct mlx5_fdir_filter *mlx5_fdir_filter,
245 struct fdir_queue *fdir_queue)
247 struct ibv_exp_flow *flow;
248 struct fdir_flow_desc *desc = &mlx5_fdir_filter->desc;
249 enum rte_fdir_mode fdir_mode =
250 priv->dev->data->dev_conf.fdir_conf.mode;
251 struct rte_eth_fdir_masks *mask =
252 &priv->dev->data->dev_conf.fdir_conf.mask;
253 FLOW_ATTR_SPEC_ETH(data, priv_flow_attr(priv, NULL, 0, desc->type));
254 struct ibv_exp_flow_attr *attr = &data->attr;
255 uintptr_t spec_offset = (uintptr_t)&data->spec;
256 struct ibv_exp_flow_spec_eth *spec_eth;
257 struct ibv_exp_flow_spec_ipv4 *spec_ipv4;
258 struct ibv_exp_flow_spec_ipv6 *spec_ipv6;
259 struct ibv_exp_flow_spec_tcp_udp *spec_tcp_udp;
260 struct mlx5_fdir_filter *iter_fdir_filter;
263 /* Abort if an existing flow overlaps this one to avoid packet
264 * duplication, even if it targets another queue. */
265 LIST_FOREACH(iter_fdir_filter, priv->fdir_filter_list, next)
266 if ((iter_fdir_filter != mlx5_fdir_filter) &&
267 (iter_fdir_filter->flow != NULL) &&
268 (priv_fdir_overlap(priv,
269 &mlx5_fdir_filter->desc,
270 &iter_fdir_filter->desc)))
274 * No padding must be inserted by the compiler between attr and spec.
275 * This layout is expected by libibverbs.
277 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec_offset);
278 priv_flow_attr(priv, attr, sizeof(data), desc->type);
280 /* Set Ethernet spec */
281 spec_eth = (struct ibv_exp_flow_spec_eth *)spec_offset;
283 /* The first specification must be Ethernet. */
284 assert(spec_eth->type == IBV_EXP_FLOW_SPEC_ETH);
285 assert(spec_eth->size == sizeof(*spec_eth));
288 spec_eth->val.vlan_tag = desc->vlan_tag & mask->vlan_tci_mask;
289 spec_eth->mask.vlan_tag = mask->vlan_tci_mask;
291 /* Update priority */
294 if (fdir_mode == RTE_FDIR_MODE_PERFECT_MAC_VLAN) {
296 for (i = 0; i != RTE_DIM(spec_eth->mask.dst_mac); ++i) {
297 spec_eth->val.dst_mac[i] =
298 desc->mac[i] & mask->mac_addr_byte_mask;
299 spec_eth->mask.dst_mac[i] = mask->mac_addr_byte_mask;
304 switch (desc->type) {
308 spec_offset += spec_eth->size;
311 spec_ipv4 = (struct ibv_exp_flow_spec_ipv4 *)spec_offset;
313 /* The second specification must be IP. */
314 assert(spec_ipv4->type == IBV_EXP_FLOW_SPEC_IPV4);
315 assert(spec_ipv4->size == sizeof(*spec_ipv4));
317 spec_ipv4->val.src_ip =
318 desc->src_ip[0] & mask->ipv4_mask.src_ip;
319 spec_ipv4->val.dst_ip =
320 desc->dst_ip[0] & mask->ipv4_mask.dst_ip;
321 spec_ipv4->mask.src_ip = mask->ipv4_mask.src_ip;
322 spec_ipv4->mask.dst_ip = mask->ipv4_mask.dst_ip;
324 /* Update priority */
327 if (desc->type == HASH_RXQ_IPV4)
330 spec_offset += spec_ipv4->size;
335 spec_offset += spec_eth->size;
338 spec_ipv6 = (struct ibv_exp_flow_spec_ipv6 *)spec_offset;
340 /* The second specification must be IP. */
341 assert(spec_ipv6->type == IBV_EXP_FLOW_SPEC_IPV6);
342 assert(spec_ipv6->size == sizeof(*spec_ipv6));
344 for (i = 0; i != RTE_DIM(desc->src_ip); ++i) {
345 ((uint32_t *)spec_ipv6->val.src_ip)[i] =
346 desc->src_ip[i] & mask->ipv6_mask.src_ip[i];
347 ((uint32_t *)spec_ipv6->val.dst_ip)[i] =
348 desc->dst_ip[i] & mask->ipv6_mask.dst_ip[i];
350 rte_memcpy(spec_ipv6->mask.src_ip,
351 mask->ipv6_mask.src_ip,
352 sizeof(spec_ipv6->mask.src_ip));
353 rte_memcpy(spec_ipv6->mask.dst_ip,
354 mask->ipv6_mask.dst_ip,
355 sizeof(spec_ipv6->mask.dst_ip));
357 /* Update priority */
360 if (desc->type == HASH_RXQ_IPV6)
363 spec_offset += spec_ipv6->size;
366 ERROR("invalid flow attribute type");
370 /* Set TCP/UDP flow specification. */
371 spec_tcp_udp = (struct ibv_exp_flow_spec_tcp_udp *)spec_offset;
373 /* The third specification must be TCP/UDP. */
374 assert(spec_tcp_udp->type == IBV_EXP_FLOW_SPEC_TCP ||
375 spec_tcp_udp->type == IBV_EXP_FLOW_SPEC_UDP);
376 assert(spec_tcp_udp->size == sizeof(*spec_tcp_udp));
378 spec_tcp_udp->val.src_port = desc->src_port & mask->src_port_mask;
379 spec_tcp_udp->val.dst_port = desc->dst_port & mask->dst_port_mask;
380 spec_tcp_udp->mask.src_port = mask->src_port_mask;
381 spec_tcp_udp->mask.dst_port = mask->dst_port_mask;
383 /* Update priority */
389 flow = ibv_exp_create_flow(fdir_queue->qp, attr);
391 /* It's not clear whether errno is always set in this case. */
392 ERROR("%p: flow director configuration failed, errno=%d: %s",
394 (errno ? strerror(errno) : "Unknown error"));
400 DEBUG("%p: added flow director rule (%p)", (void *)priv, (void *)flow);
401 mlx5_fdir_filter->flow = flow;
406 * Destroy a flow director queue.
409 * Flow director queue to be destroyed.
412 priv_fdir_queue_destroy(struct priv *priv, struct fdir_queue *fdir_queue)
414 struct mlx5_fdir_filter *fdir_filter;
416 /* Disable filter flows still applying to this queue. */
417 LIST_FOREACH(fdir_filter, priv->fdir_filter_list, next) {
418 unsigned int idx = fdir_filter->queue;
419 struct rxq_ctrl *rxq_ctrl =
420 container_of((*priv->rxqs)[idx], struct rxq_ctrl, rxq);
422 assert(idx < priv->rxqs_n);
423 if (fdir_queue == rxq_ctrl->fdir_queue &&
424 fdir_filter->flow != NULL) {
425 claim_zero(ibv_exp_destroy_flow(fdir_filter->flow));
426 fdir_filter->flow = NULL;
429 assert(fdir_queue->qp);
430 claim_zero(ibv_destroy_qp(fdir_queue->qp));
431 assert(fdir_queue->ind_table);
432 claim_zero(ibv_exp_destroy_rwq_ind_table(fdir_queue->ind_table));
434 claim_zero(ibv_exp_destroy_wq(fdir_queue->wq));
436 claim_zero(ibv_destroy_cq(fdir_queue->cq));
438 memset(fdir_queue, 0x2a, sizeof(*fdir_queue));
440 rte_free(fdir_queue);
444 * Create a flow director queue.
449 * Work queue to route matched packets to, NULL if one needs to
453 * Related flow director queue on success, NULL otherwise.
455 static struct fdir_queue *
456 priv_fdir_queue_create(struct priv *priv, struct ibv_exp_wq *wq,
459 struct fdir_queue *fdir_queue;
461 fdir_queue = rte_calloc_socket(__func__, 1, sizeof(*fdir_queue),
464 ERROR("cannot allocate flow director queue");
470 fdir_queue->cq = ibv_exp_create_cq(
471 priv->ctx, 1, NULL, NULL, 0,
472 &(struct ibv_exp_cq_init_attr){
475 if (!fdir_queue->cq) {
476 ERROR("cannot create flow director CQ");
479 fdir_queue->wq = ibv_exp_create_wq(
481 &(struct ibv_exp_wq_init_attr){
482 .wq_type = IBV_EXP_WQT_RQ,
486 .cq = fdir_queue->cq,
488 if (!fdir_queue->wq) {
489 ERROR("cannot create flow director WQ");
494 fdir_queue->ind_table = ibv_exp_create_rwq_ind_table(
496 &(struct ibv_exp_rwq_ind_table_init_attr){
498 .log_ind_tbl_size = 0,
502 if (!fdir_queue->ind_table) {
503 ERROR("cannot create flow director indirection table");
506 fdir_queue->qp = ibv_exp_create_qp(
508 &(struct ibv_exp_qp_init_attr){
509 .qp_type = IBV_QPT_RAW_PACKET,
511 IBV_EXP_QP_INIT_ATTR_PD |
512 IBV_EXP_QP_INIT_ATTR_PORT |
513 IBV_EXP_QP_INIT_ATTR_RX_HASH,
515 .rx_hash_conf = &(struct ibv_exp_rx_hash_conf){
517 IBV_EXP_RX_HASH_FUNC_TOEPLITZ,
518 .rx_hash_key_len = rss_hash_default_key_len,
519 .rx_hash_key = rss_hash_default_key,
520 .rx_hash_fields_mask = 0,
521 .rwq_ind_tbl = fdir_queue->ind_table,
523 .port_num = priv->port,
525 if (!fdir_queue->qp) {
526 ERROR("cannot create flow director hash RX QP");
532 assert(!fdir_queue->qp);
533 if (fdir_queue->ind_table)
534 claim_zero(ibv_exp_destroy_rwq_ind_table
535 (fdir_queue->ind_table));
537 claim_zero(ibv_exp_destroy_wq(fdir_queue->wq));
539 claim_zero(ibv_destroy_cq(fdir_queue->cq));
540 rte_free(fdir_queue);
545 * Get flow director queue for a specific RX queue, create it in case
554 * Related flow director queue on success, NULL otherwise.
556 static struct fdir_queue *
557 priv_get_fdir_queue(struct priv *priv, uint16_t idx)
559 struct rxq_ctrl *rxq_ctrl =
560 container_of((*priv->rxqs)[idx], struct rxq_ctrl, rxq);
561 struct fdir_queue *fdir_queue = rxq_ctrl->fdir_queue;
563 assert(rxq_ctrl->wq);
564 if (fdir_queue == NULL) {
565 fdir_queue = priv_fdir_queue_create(priv, rxq_ctrl->wq,
567 rxq_ctrl->fdir_queue = fdir_queue;
573 * Get or flow director drop queue. Create it if it does not exist.
579 * Flow director drop queue on success, NULL otherwise.
581 static struct fdir_queue *
582 priv_get_fdir_drop_queue(struct priv *priv)
584 struct fdir_queue *fdir_queue = priv->fdir_drop_queue;
586 if (fdir_queue == NULL) {
587 unsigned int socket = SOCKET_ID_ANY;
589 /* Select a known NUMA socket if possible. */
590 if (priv->rxqs_n && (*priv->rxqs)[0])
591 socket = container_of((*priv->rxqs)[0],
592 struct rxq_ctrl, rxq)->socket;
593 fdir_queue = priv_fdir_queue_create(priv, NULL, socket);
594 priv->fdir_drop_queue = fdir_queue;
600 * Enable flow director filter and create steering rules.
604 * @param mlx5_fdir_filter
605 * Filter to create steering rule for.
608 * 0 on success, errno value on failure.
611 priv_fdir_filter_enable(struct priv *priv,
612 struct mlx5_fdir_filter *mlx5_fdir_filter)
614 struct fdir_queue *fdir_queue;
616 /* Check if flow already exists. */
617 if (mlx5_fdir_filter->flow != NULL)
620 /* Get fdir_queue for specific queue. */
621 if (mlx5_fdir_filter->behavior == RTE_ETH_FDIR_REJECT)
622 fdir_queue = priv_get_fdir_drop_queue(priv);
624 fdir_queue = priv_get_fdir_queue(priv,
625 mlx5_fdir_filter->queue);
627 if (fdir_queue == NULL) {
628 ERROR("failed to create flow director rxq for queue %d",
629 mlx5_fdir_filter->queue);
634 return priv_fdir_flow_add(priv, mlx5_fdir_filter, fdir_queue);
638 * Initialize flow director filters list.
644 * 0 on success, errno value on failure.
647 fdir_init_filters_list(struct priv *priv)
649 /* Filter list initialization should be done only once. */
650 if (priv->fdir_filter_list)
653 /* Create filters list. */
654 priv->fdir_filter_list =
655 rte_calloc(__func__, 1, sizeof(*priv->fdir_filter_list), 0);
657 if (priv->fdir_filter_list == NULL) {
660 ERROR("cannot allocate flow director filter list: %s",
665 LIST_INIT(priv->fdir_filter_list);
677 priv_fdir_filter_flush(struct priv *priv)
679 struct mlx5_fdir_filter *mlx5_fdir_filter;
681 while ((mlx5_fdir_filter = LIST_FIRST(priv->fdir_filter_list))) {
682 struct ibv_exp_flow *flow = mlx5_fdir_filter->flow;
684 DEBUG("%p: flushing flow director filter %p",
685 (void *)priv, (void *)mlx5_fdir_filter);
686 LIST_REMOVE(mlx5_fdir_filter, next);
688 claim_zero(ibv_exp_destroy_flow(flow));
689 rte_free(mlx5_fdir_filter);
694 * Remove all flow director filters and delete list.
700 priv_fdir_delete_filters_list(struct priv *priv)
702 priv_fdir_filter_flush(priv);
703 rte_free(priv->fdir_filter_list);
704 priv->fdir_filter_list = NULL;
708 * Disable flow director, remove all steering rules.
714 priv_fdir_disable(struct priv *priv)
717 struct mlx5_fdir_filter *mlx5_fdir_filter;
719 /* Run on every flow director filter and destroy flow handle. */
720 LIST_FOREACH(mlx5_fdir_filter, priv->fdir_filter_list, next) {
721 struct ibv_exp_flow *flow;
723 /* Only valid elements should be in the list */
724 assert(mlx5_fdir_filter != NULL);
725 flow = mlx5_fdir_filter->flow;
727 /* Destroy flow handle */
729 claim_zero(ibv_exp_destroy_flow(flow));
730 mlx5_fdir_filter->flow = NULL;
734 /* Destroy flow director context in each RX queue. */
735 for (i = 0; (i != priv->rxqs_n); i++) {
736 struct rxq_ctrl *rxq_ctrl;
738 if (!(*priv->rxqs)[i])
740 rxq_ctrl = container_of((*priv->rxqs)[i], struct rxq_ctrl, rxq);
741 if (!rxq_ctrl->fdir_queue)
743 priv_fdir_queue_destroy(priv, rxq_ctrl->fdir_queue);
744 rxq_ctrl->fdir_queue = NULL;
746 if (priv->fdir_drop_queue) {
747 priv_fdir_queue_destroy(priv, priv->fdir_drop_queue);
748 priv->fdir_drop_queue = NULL;
753 * Enable flow director, create steering rules.
759 priv_fdir_enable(struct priv *priv)
761 struct mlx5_fdir_filter *mlx5_fdir_filter;
763 /* Run on every fdir filter and create flow handle */
764 LIST_FOREACH(mlx5_fdir_filter, priv->fdir_filter_list, next) {
765 /* Only valid elements should be in the list */
766 assert(mlx5_fdir_filter != NULL);
768 priv_fdir_filter_enable(priv, mlx5_fdir_filter);
773 * Find specific filter in list.
778 * Flow director filter to find.
781 * Filter element if found, otherwise NULL.
783 static struct mlx5_fdir_filter *
784 priv_find_filter_in_list(struct priv *priv,
785 const struct rte_eth_fdir_filter *fdir_filter)
787 struct fdir_flow_desc desc;
788 struct mlx5_fdir_filter *mlx5_fdir_filter;
789 enum rte_fdir_mode fdir_mode = priv->dev->data->dev_conf.fdir_conf.mode;
791 /* Get flow director filter to look for. */
792 fdir_filter_to_flow_desc(fdir_filter, &desc, fdir_mode);
794 /* Look for the requested element. */
795 LIST_FOREACH(mlx5_fdir_filter, priv->fdir_filter_list, next) {
796 /* Only valid elements should be in the list. */
797 assert(mlx5_fdir_filter != NULL);
799 /* Return matching filter. */
800 if (!memcmp(&desc, &mlx5_fdir_filter->desc, sizeof(desc)))
801 return mlx5_fdir_filter;
804 /* Filter not found */
809 * Add new flow director filter and store it in list.
814 * Flow director filter to add.
817 * 0 on success, errno value on failure.
820 priv_fdir_filter_add(struct priv *priv,
821 const struct rte_eth_fdir_filter *fdir_filter)
823 struct mlx5_fdir_filter *mlx5_fdir_filter;
824 enum rte_fdir_mode fdir_mode = priv->dev->data->dev_conf.fdir_conf.mode;
827 /* Validate queue number. */
828 if (fdir_filter->action.rx_queue >= priv->rxqs_n) {
829 ERROR("invalid queue number %d", fdir_filter->action.rx_queue);
833 /* Duplicate filters are currently unsupported. */
834 mlx5_fdir_filter = priv_find_filter_in_list(priv, fdir_filter);
835 if (mlx5_fdir_filter != NULL) {
836 ERROR("filter already exists");
840 /* Create new flow director filter. */
842 rte_calloc(__func__, 1, sizeof(*mlx5_fdir_filter), 0);
843 if (mlx5_fdir_filter == NULL) {
845 ERROR("cannot allocate flow director filter: %s",
850 /* Set action parameters. */
851 mlx5_fdir_filter->queue = fdir_filter->action.rx_queue;
852 mlx5_fdir_filter->behavior = fdir_filter->action.behavior;
854 /* Convert to mlx5 filter descriptor. */
855 fdir_filter_to_flow_desc(fdir_filter,
856 &mlx5_fdir_filter->desc, fdir_mode);
858 /* Insert new filter into list. */
859 LIST_INSERT_HEAD(priv->fdir_filter_list, mlx5_fdir_filter, next);
861 DEBUG("%p: flow director filter %p added",
862 (void *)priv, (void *)mlx5_fdir_filter);
864 /* Enable filter immediately if device is started. */
866 err = priv_fdir_filter_enable(priv, mlx5_fdir_filter);
872 * Update queue for specific filter.
877 * Filter to be updated.
880 * 0 on success, errno value on failure.
883 priv_fdir_filter_update(struct priv *priv,
884 const struct rte_eth_fdir_filter *fdir_filter)
886 struct mlx5_fdir_filter *mlx5_fdir_filter;
888 /* Validate queue number. */
889 if (fdir_filter->action.rx_queue >= priv->rxqs_n) {
890 ERROR("invalid queue number %d", fdir_filter->action.rx_queue);
894 mlx5_fdir_filter = priv_find_filter_in_list(priv, fdir_filter);
895 if (mlx5_fdir_filter != NULL) {
896 struct ibv_exp_flow *flow = mlx5_fdir_filter->flow;
899 /* Update queue number. */
900 mlx5_fdir_filter->queue = fdir_filter->action.rx_queue;
902 /* Destroy flow handle. */
904 claim_zero(ibv_exp_destroy_flow(flow));
905 mlx5_fdir_filter->flow = NULL;
907 DEBUG("%p: flow director filter %p updated",
908 (void *)priv, (void *)mlx5_fdir_filter);
910 /* Enable filter if device is started. */
912 err = priv_fdir_filter_enable(priv, mlx5_fdir_filter);
917 /* Filter not found, create it. */
918 DEBUG("%p: filter not found for update, creating new filter",
920 return priv_fdir_filter_add(priv, fdir_filter);
924 * Delete specific filter.
929 * Filter to be deleted.
932 * 0 on success, errno value on failure.
935 priv_fdir_filter_delete(struct priv *priv,
936 const struct rte_eth_fdir_filter *fdir_filter)
938 struct mlx5_fdir_filter *mlx5_fdir_filter;
940 mlx5_fdir_filter = priv_find_filter_in_list(priv, fdir_filter);
941 if (mlx5_fdir_filter != NULL) {
942 struct ibv_exp_flow *flow = mlx5_fdir_filter->flow;
944 /* Remove element from list. */
945 LIST_REMOVE(mlx5_fdir_filter, next);
947 /* Destroy flow handle. */
949 claim_zero(ibv_exp_destroy_flow(flow));
950 mlx5_fdir_filter->flow = NULL;
953 DEBUG("%p: flow director filter %p deleted",
954 (void *)priv, (void *)mlx5_fdir_filter);
957 rte_free(mlx5_fdir_filter);
962 ERROR("%p: flow director delete failed, cannot find filter",
968 * Get flow director information.
972 * @param[out] fdir_info
973 * Resulting flow director information.
976 priv_fdir_info_get(struct priv *priv, struct rte_eth_fdir_info *fdir_info)
978 struct rte_eth_fdir_masks *mask =
979 &priv->dev->data->dev_conf.fdir_conf.mask;
981 fdir_info->mode = priv->dev->data->dev_conf.fdir_conf.mode;
982 fdir_info->guarant_spc = 0;
984 rte_memcpy(&fdir_info->mask, mask, sizeof(fdir_info->mask));
986 fdir_info->max_flexpayload = 0;
987 fdir_info->flow_types_mask[0] = 0;
989 fdir_info->flex_payload_unit = 0;
990 fdir_info->max_flex_payload_segment_num = 0;
991 fdir_info->flex_payload_limit = 0;
992 memset(&fdir_info->flex_conf, 0, sizeof(fdir_info->flex_conf));
996 * Deal with flow director operations.
999 * Pointer to private structure.
1001 * Operation to perform.
1003 * Pointer to operation-specific structure.
1006 * 0 on success, errno value on failure.
1009 priv_fdir_ctrl_func(struct priv *priv, enum rte_filter_op filter_op, void *arg)
1011 enum rte_fdir_mode fdir_mode =
1012 priv->dev->data->dev_conf.fdir_conf.mode;
1015 if (filter_op == RTE_ETH_FILTER_NOP)
1018 if (fdir_mode != RTE_FDIR_MODE_PERFECT &&
1019 fdir_mode != RTE_FDIR_MODE_PERFECT_MAC_VLAN) {
1020 ERROR("%p: flow director mode %d not supported",
1021 (void *)priv, fdir_mode);
1025 switch (filter_op) {
1026 case RTE_ETH_FILTER_ADD:
1027 ret = priv_fdir_filter_add(priv, arg);
1029 case RTE_ETH_FILTER_UPDATE:
1030 ret = priv_fdir_filter_update(priv, arg);
1032 case RTE_ETH_FILTER_DELETE:
1033 ret = priv_fdir_filter_delete(priv, arg);
1035 case RTE_ETH_FILTER_FLUSH:
1036 priv_fdir_filter_flush(priv);
1038 case RTE_ETH_FILTER_INFO:
1039 priv_fdir_info_get(priv, arg);
1042 DEBUG("%p: unknown operation %u", (void *)priv, filter_op);
1049 static const struct rte_flow_ops mlx5_flow_ops = {
1050 .validate = mlx5_flow_validate,
1051 .create = mlx5_flow_create,
1052 .destroy = mlx5_flow_destroy,
1053 .flush = mlx5_flow_flush,
1058 * Manage filter operations.
1061 * Pointer to Ethernet device structure.
1062 * @param filter_type
1065 * Operation to perform.
1067 * Pointer to operation-specific structure.
1070 * 0 on success, negative errno value on failure.
1073 mlx5_dev_filter_ctrl(struct rte_eth_dev *dev,
1074 enum rte_filter_type filter_type,
1075 enum rte_filter_op filter_op,
1079 struct priv *priv = dev->data->dev_private;
1081 switch (filter_type) {
1082 case RTE_ETH_FILTER_GENERIC:
1083 if (filter_op != RTE_ETH_FILTER_GET)
1085 *(const void **)arg = &mlx5_flow_ops;
1087 case RTE_ETH_FILTER_FDIR:
1089 ret = priv_fdir_ctrl_func(priv, filter_op, arg);
1093 ERROR("%p: filter type (%d) not supported",
1094 (void *)dev, filter_type);