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 #include <rte_ether.h>
51 #include <rte_malloc.h>
52 #include <rte_ethdev.h>
53 #include <rte_common.h>
55 #include <rte_flow_driver.h>
58 #include "mlx5_rxtx.h"
60 struct fdir_flow_desc {
67 enum hash_rxq_type type;
70 struct mlx5_fdir_filter {
71 LIST_ENTRY(mlx5_fdir_filter) next;
72 uint16_t queue; /* Queue assigned to if FDIR match. */
73 enum rte_eth_fdir_behavior behavior;
74 struct fdir_flow_desc desc;
75 struct ibv_exp_flow *flow;
78 LIST_HEAD(fdir_filter_list, mlx5_fdir_filter);
81 * Convert struct rte_eth_fdir_filter to mlx5 filter descriptor.
83 * @param[in] fdir_filter
84 * DPDK filter structure to convert.
86 * Resulting mlx5 filter descriptor.
91 fdir_filter_to_flow_desc(const struct rte_eth_fdir_filter *fdir_filter,
92 struct fdir_flow_desc *desc, enum rte_fdir_mode mode)
94 /* Initialize descriptor. */
95 memset(desc, 0, sizeof(*desc));
98 desc->vlan_tag = fdir_filter->input.flow_ext.vlan_tci;
100 /* Set MAC address. */
101 if (mode == RTE_FDIR_MODE_PERFECT_MAC_VLAN) {
102 rte_memcpy(desc->mac,
103 fdir_filter->input.flow.mac_vlan_flow.mac_addr.
106 desc->type = HASH_RXQ_ETH;
111 switch (fdir_filter->input.flow_type) {
112 case RTE_ETH_FLOW_NONFRAG_IPV4_UDP:
113 desc->type = HASH_RXQ_UDPV4;
115 case RTE_ETH_FLOW_NONFRAG_IPV4_TCP:
116 desc->type = HASH_RXQ_TCPV4;
118 case RTE_ETH_FLOW_NONFRAG_IPV4_OTHER:
119 desc->type = HASH_RXQ_IPV4;
121 case RTE_ETH_FLOW_NONFRAG_IPV6_UDP:
122 desc->type = HASH_RXQ_UDPV6;
124 case RTE_ETH_FLOW_NONFRAG_IPV6_TCP:
125 desc->type = HASH_RXQ_TCPV6;
127 case RTE_ETH_FLOW_NONFRAG_IPV6_OTHER:
128 desc->type = HASH_RXQ_IPV6;
134 /* Set flow values */
135 switch (fdir_filter->input.flow_type) {
136 case RTE_ETH_FLOW_NONFRAG_IPV4_UDP:
137 case RTE_ETH_FLOW_NONFRAG_IPV4_TCP:
138 desc->src_port = fdir_filter->input.flow.udp4_flow.src_port;
139 desc->dst_port = fdir_filter->input.flow.udp4_flow.dst_port;
141 case RTE_ETH_FLOW_NONFRAG_IPV4_OTHER:
142 desc->src_ip[0] = fdir_filter->input.flow.ip4_flow.src_ip;
143 desc->dst_ip[0] = fdir_filter->input.flow.ip4_flow.dst_ip;
145 case RTE_ETH_FLOW_NONFRAG_IPV6_UDP:
146 case RTE_ETH_FLOW_NONFRAG_IPV6_TCP:
147 desc->src_port = fdir_filter->input.flow.udp6_flow.src_port;
148 desc->dst_port = fdir_filter->input.flow.udp6_flow.dst_port;
150 case RTE_ETH_FLOW_NONFRAG_IPV6_OTHER:
151 rte_memcpy(desc->src_ip,
152 fdir_filter->input.flow.ipv6_flow.src_ip,
153 sizeof(desc->src_ip));
154 rte_memcpy(desc->dst_ip,
155 fdir_filter->input.flow.ipv6_flow.dst_ip,
156 sizeof(desc->dst_ip));
164 * Check if two flow descriptors overlap according to configured mask.
167 * Private structure that provides flow director mask.
169 * First flow descriptor to compare.
171 * Second flow descriptor to compare.
174 * Nonzero if descriptors overlap.
177 priv_fdir_overlap(const struct priv *priv,
178 const struct fdir_flow_desc *desc1,
179 const struct fdir_flow_desc *desc2)
181 const struct rte_eth_fdir_masks *mask =
182 &priv->dev->data->dev_conf.fdir_conf.mask;
185 if (desc1->type != desc2->type)
187 /* Ignore non masked bits. */
188 for (i = 0; i != RTE_DIM(desc1->mac); ++i)
189 if ((desc1->mac[i] & mask->mac_addr_byte_mask) !=
190 (desc2->mac[i] & mask->mac_addr_byte_mask))
192 if (((desc1->src_port & mask->src_port_mask) !=
193 (desc2->src_port & mask->src_port_mask)) ||
194 ((desc1->dst_port & mask->dst_port_mask) !=
195 (desc2->dst_port & mask->dst_port_mask)))
197 switch (desc1->type) {
201 if (((desc1->src_ip[0] & mask->ipv4_mask.src_ip) !=
202 (desc2->src_ip[0] & mask->ipv4_mask.src_ip)) ||
203 ((desc1->dst_ip[0] & mask->ipv4_mask.dst_ip) !=
204 (desc2->dst_ip[0] & mask->ipv4_mask.dst_ip)))
210 for (i = 0; i != RTE_DIM(desc1->src_ip); ++i)
211 if (((desc1->src_ip[i] & mask->ipv6_mask.src_ip[i]) !=
212 (desc2->src_ip[i] & mask->ipv6_mask.src_ip[i])) ||
213 ((desc1->dst_ip[i] & mask->ipv6_mask.dst_ip[i]) !=
214 (desc2->dst_ip[i] & mask->ipv6_mask.dst_ip[i])))
224 * Create flow director steering rule for a specific filter.
228 * @param mlx5_fdir_filter
229 * Filter to create a steering rule for.
231 * Flow director queue for matching packets.
234 * 0 on success, errno value on failure.
237 priv_fdir_flow_add(struct priv *priv,
238 struct mlx5_fdir_filter *mlx5_fdir_filter,
239 struct fdir_queue *fdir_queue)
241 struct ibv_exp_flow *flow;
242 struct fdir_flow_desc *desc = &mlx5_fdir_filter->desc;
243 enum rte_fdir_mode fdir_mode =
244 priv->dev->data->dev_conf.fdir_conf.mode;
245 struct rte_eth_fdir_masks *mask =
246 &priv->dev->data->dev_conf.fdir_conf.mask;
247 FLOW_ATTR_SPEC_ETH(data, priv_flow_attr(priv, NULL, 0, desc->type));
248 struct ibv_exp_flow_attr *attr = &data->attr;
249 uintptr_t spec_offset = (uintptr_t)&data->spec;
250 struct ibv_exp_flow_spec_eth *spec_eth;
251 struct ibv_exp_flow_spec_ipv4 *spec_ipv4;
252 struct ibv_exp_flow_spec_ipv6 *spec_ipv6;
253 struct ibv_exp_flow_spec_tcp_udp *spec_tcp_udp;
254 struct mlx5_fdir_filter *iter_fdir_filter;
257 /* Abort if an existing flow overlaps this one to avoid packet
258 * duplication, even if it targets another queue. */
259 LIST_FOREACH(iter_fdir_filter, priv->fdir_filter_list, next)
260 if ((iter_fdir_filter != mlx5_fdir_filter) &&
261 (iter_fdir_filter->flow != NULL) &&
262 (priv_fdir_overlap(priv,
263 &mlx5_fdir_filter->desc,
264 &iter_fdir_filter->desc)))
268 * No padding must be inserted by the compiler between attr and spec.
269 * This layout is expected by libibverbs.
271 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec_offset);
272 priv_flow_attr(priv, attr, sizeof(data), desc->type);
274 /* Set Ethernet spec */
275 spec_eth = (struct ibv_exp_flow_spec_eth *)spec_offset;
277 /* The first specification must be Ethernet. */
278 assert(spec_eth->type == IBV_EXP_FLOW_SPEC_ETH);
279 assert(spec_eth->size == sizeof(*spec_eth));
282 spec_eth->val.vlan_tag = desc->vlan_tag & mask->vlan_tci_mask;
283 spec_eth->mask.vlan_tag = mask->vlan_tci_mask;
285 /* Update priority */
288 if (fdir_mode == RTE_FDIR_MODE_PERFECT_MAC_VLAN) {
290 for (i = 0; i != RTE_DIM(spec_eth->mask.dst_mac); ++i) {
291 spec_eth->val.dst_mac[i] =
292 desc->mac[i] & mask->mac_addr_byte_mask;
293 spec_eth->mask.dst_mac[i] = mask->mac_addr_byte_mask;
298 switch (desc->type) {
302 spec_offset += spec_eth->size;
305 spec_ipv4 = (struct ibv_exp_flow_spec_ipv4 *)spec_offset;
307 /* The second specification must be IP. */
308 assert(spec_ipv4->type == IBV_EXP_FLOW_SPEC_IPV4);
309 assert(spec_ipv4->size == sizeof(*spec_ipv4));
311 spec_ipv4->val.src_ip =
312 desc->src_ip[0] & mask->ipv4_mask.src_ip;
313 spec_ipv4->val.dst_ip =
314 desc->dst_ip[0] & mask->ipv4_mask.dst_ip;
315 spec_ipv4->mask.src_ip = mask->ipv4_mask.src_ip;
316 spec_ipv4->mask.dst_ip = mask->ipv4_mask.dst_ip;
318 /* Update priority */
321 if (desc->type == HASH_RXQ_IPV4)
324 spec_offset += spec_ipv4->size;
329 spec_offset += spec_eth->size;
332 spec_ipv6 = (struct ibv_exp_flow_spec_ipv6 *)spec_offset;
334 /* The second specification must be IP. */
335 assert(spec_ipv6->type == IBV_EXP_FLOW_SPEC_IPV6);
336 assert(spec_ipv6->size == sizeof(*spec_ipv6));
338 for (i = 0; i != RTE_DIM(desc->src_ip); ++i) {
339 ((uint32_t *)spec_ipv6->val.src_ip)[i] =
340 desc->src_ip[i] & mask->ipv6_mask.src_ip[i];
341 ((uint32_t *)spec_ipv6->val.dst_ip)[i] =
342 desc->dst_ip[i] & mask->ipv6_mask.dst_ip[i];
344 rte_memcpy(spec_ipv6->mask.src_ip,
345 mask->ipv6_mask.src_ip,
346 sizeof(spec_ipv6->mask.src_ip));
347 rte_memcpy(spec_ipv6->mask.dst_ip,
348 mask->ipv6_mask.dst_ip,
349 sizeof(spec_ipv6->mask.dst_ip));
351 /* Update priority */
354 if (desc->type == HASH_RXQ_IPV6)
357 spec_offset += spec_ipv6->size;
360 ERROR("invalid flow attribute type");
364 /* Set TCP/UDP flow specification. */
365 spec_tcp_udp = (struct ibv_exp_flow_spec_tcp_udp *)spec_offset;
367 /* The third specification must be TCP/UDP. */
368 assert(spec_tcp_udp->type == IBV_EXP_FLOW_SPEC_TCP ||
369 spec_tcp_udp->type == IBV_EXP_FLOW_SPEC_UDP);
370 assert(spec_tcp_udp->size == sizeof(*spec_tcp_udp));
372 spec_tcp_udp->val.src_port = desc->src_port & mask->src_port_mask;
373 spec_tcp_udp->val.dst_port = desc->dst_port & mask->dst_port_mask;
374 spec_tcp_udp->mask.src_port = mask->src_port_mask;
375 spec_tcp_udp->mask.dst_port = mask->dst_port_mask;
377 /* Update priority */
383 flow = ibv_exp_create_flow(fdir_queue->qp, attr);
385 /* It's not clear whether errno is always set in this case. */
386 ERROR("%p: flow director configuration failed, errno=%d: %s",
388 (errno ? strerror(errno) : "Unknown error"));
394 DEBUG("%p: added flow director rule (%p)", (void *)priv, (void *)flow);
395 mlx5_fdir_filter->flow = flow;
400 * Destroy a flow director queue.
403 * Flow director queue to be destroyed.
406 priv_fdir_queue_destroy(struct priv *priv, struct fdir_queue *fdir_queue)
408 struct mlx5_fdir_filter *fdir_filter;
410 /* Disable filter flows still applying to this queue. */
411 LIST_FOREACH(fdir_filter, priv->fdir_filter_list, next) {
412 unsigned int idx = fdir_filter->queue;
413 struct rxq_ctrl *rxq_ctrl =
414 container_of((*priv->rxqs)[idx], struct rxq_ctrl, rxq);
416 assert(idx < priv->rxqs_n);
417 if (fdir_queue == rxq_ctrl->fdir_queue &&
418 fdir_filter->flow != NULL) {
419 claim_zero(ibv_exp_destroy_flow(fdir_filter->flow));
420 fdir_filter->flow = NULL;
423 assert(fdir_queue->qp);
424 claim_zero(ibv_destroy_qp(fdir_queue->qp));
425 assert(fdir_queue->ind_table);
426 claim_zero(ibv_exp_destroy_rwq_ind_table(fdir_queue->ind_table));
428 claim_zero(ibv_exp_destroy_wq(fdir_queue->wq));
430 claim_zero(ibv_destroy_cq(fdir_queue->cq));
432 memset(fdir_queue, 0x2a, sizeof(*fdir_queue));
434 rte_free(fdir_queue);
438 * Create a flow director queue.
443 * Work queue to route matched packets to, NULL if one needs to
447 * Related flow director queue on success, NULL otherwise.
449 static struct fdir_queue *
450 priv_fdir_queue_create(struct priv *priv, struct ibv_exp_wq *wq,
453 struct fdir_queue *fdir_queue;
455 fdir_queue = rte_calloc_socket(__func__, 1, sizeof(*fdir_queue),
458 ERROR("cannot allocate flow director queue");
464 fdir_queue->cq = ibv_exp_create_cq(
465 priv->ctx, 1, NULL, NULL, 0,
466 &(struct ibv_exp_cq_init_attr){
469 if (!fdir_queue->cq) {
470 ERROR("cannot create flow director CQ");
473 fdir_queue->wq = ibv_exp_create_wq(
475 &(struct ibv_exp_wq_init_attr){
476 .wq_type = IBV_EXP_WQT_RQ,
480 .cq = fdir_queue->cq,
482 if (!fdir_queue->wq) {
483 ERROR("cannot create flow director WQ");
488 fdir_queue->ind_table = ibv_exp_create_rwq_ind_table(
490 &(struct ibv_exp_rwq_ind_table_init_attr){
492 .log_ind_tbl_size = 0,
496 if (!fdir_queue->ind_table) {
497 ERROR("cannot create flow director indirection table");
500 fdir_queue->qp = ibv_exp_create_qp(
502 &(struct ibv_exp_qp_init_attr){
503 .qp_type = IBV_QPT_RAW_PACKET,
505 IBV_EXP_QP_INIT_ATTR_PD |
506 IBV_EXP_QP_INIT_ATTR_PORT |
507 IBV_EXP_QP_INIT_ATTR_RX_HASH,
509 .rx_hash_conf = &(struct ibv_exp_rx_hash_conf){
511 IBV_EXP_RX_HASH_FUNC_TOEPLITZ,
512 .rx_hash_key_len = rss_hash_default_key_len,
513 .rx_hash_key = rss_hash_default_key,
514 .rx_hash_fields_mask = 0,
515 .rwq_ind_tbl = fdir_queue->ind_table,
517 .port_num = priv->port,
519 if (!fdir_queue->qp) {
520 ERROR("cannot create flow director hash RX QP");
526 assert(!fdir_queue->qp);
527 if (fdir_queue->ind_table)
528 claim_zero(ibv_exp_destroy_rwq_ind_table
529 (fdir_queue->ind_table));
531 claim_zero(ibv_exp_destroy_wq(fdir_queue->wq));
533 claim_zero(ibv_destroy_cq(fdir_queue->cq));
534 rte_free(fdir_queue);
539 * Get flow director queue for a specific RX queue, create it in case
548 * Related flow director queue on success, NULL otherwise.
550 static struct fdir_queue *
551 priv_get_fdir_queue(struct priv *priv, uint16_t idx)
553 struct rxq_ctrl *rxq_ctrl =
554 container_of((*priv->rxqs)[idx], struct rxq_ctrl, rxq);
555 struct fdir_queue *fdir_queue = rxq_ctrl->fdir_queue;
557 assert(rxq_ctrl->wq);
558 if (fdir_queue == NULL) {
559 fdir_queue = priv_fdir_queue_create(priv, rxq_ctrl->wq,
561 rxq_ctrl->fdir_queue = fdir_queue;
567 * Get or flow director drop queue. Create it if it does not exist.
573 * Flow director drop queue on success, NULL otherwise.
575 static struct fdir_queue *
576 priv_get_fdir_drop_queue(struct priv *priv)
578 struct fdir_queue *fdir_queue = priv->fdir_drop_queue;
580 if (fdir_queue == NULL) {
581 unsigned int socket = SOCKET_ID_ANY;
583 /* Select a known NUMA socket if possible. */
584 if (priv->rxqs_n && (*priv->rxqs)[0])
585 socket = container_of((*priv->rxqs)[0],
586 struct rxq_ctrl, rxq)->socket;
587 fdir_queue = priv_fdir_queue_create(priv, NULL, socket);
588 priv->fdir_drop_queue = fdir_queue;
594 * Enable flow director filter and create steering rules.
598 * @param mlx5_fdir_filter
599 * Filter to create steering rule for.
602 * 0 on success, errno value on failure.
605 priv_fdir_filter_enable(struct priv *priv,
606 struct mlx5_fdir_filter *mlx5_fdir_filter)
608 struct fdir_queue *fdir_queue;
610 /* Check if flow already exists. */
611 if (mlx5_fdir_filter->flow != NULL)
614 /* Get fdir_queue for specific queue. */
615 if (mlx5_fdir_filter->behavior == RTE_ETH_FDIR_REJECT)
616 fdir_queue = priv_get_fdir_drop_queue(priv);
618 fdir_queue = priv_get_fdir_queue(priv,
619 mlx5_fdir_filter->queue);
621 if (fdir_queue == NULL) {
622 ERROR("failed to create flow director rxq for queue %d",
623 mlx5_fdir_filter->queue);
628 return priv_fdir_flow_add(priv, mlx5_fdir_filter, fdir_queue);
632 * Initialize flow director filters list.
638 * 0 on success, errno value on failure.
641 fdir_init_filters_list(struct priv *priv)
643 /* Filter list initialization should be done only once. */
644 if (priv->fdir_filter_list)
647 /* Create filters list. */
648 priv->fdir_filter_list =
649 rte_calloc(__func__, 1, sizeof(*priv->fdir_filter_list), 0);
651 if (priv->fdir_filter_list == NULL) {
654 ERROR("cannot allocate flow director filter list: %s",
659 LIST_INIT(priv->fdir_filter_list);
671 priv_fdir_filter_flush(struct priv *priv)
673 struct mlx5_fdir_filter *mlx5_fdir_filter;
675 while ((mlx5_fdir_filter = LIST_FIRST(priv->fdir_filter_list))) {
676 struct ibv_exp_flow *flow = mlx5_fdir_filter->flow;
678 DEBUG("%p: flushing flow director filter %p",
679 (void *)priv, (void *)mlx5_fdir_filter);
680 LIST_REMOVE(mlx5_fdir_filter, next);
682 claim_zero(ibv_exp_destroy_flow(flow));
683 rte_free(mlx5_fdir_filter);
688 * Remove all flow director filters and delete list.
694 priv_fdir_delete_filters_list(struct priv *priv)
696 priv_fdir_filter_flush(priv);
697 rte_free(priv->fdir_filter_list);
698 priv->fdir_filter_list = NULL;
702 * Disable flow director, remove all steering rules.
708 priv_fdir_disable(struct priv *priv)
711 struct mlx5_fdir_filter *mlx5_fdir_filter;
713 /* Run on every flow director filter and destroy flow handle. */
714 LIST_FOREACH(mlx5_fdir_filter, priv->fdir_filter_list, next) {
715 struct ibv_exp_flow *flow;
717 /* Only valid elements should be in the list */
718 assert(mlx5_fdir_filter != NULL);
719 flow = mlx5_fdir_filter->flow;
721 /* Destroy flow handle */
723 claim_zero(ibv_exp_destroy_flow(flow));
724 mlx5_fdir_filter->flow = NULL;
728 /* Destroy flow director context in each RX queue. */
729 for (i = 0; (i != priv->rxqs_n); i++) {
730 struct rxq_ctrl *rxq_ctrl;
732 if (!(*priv->rxqs)[i])
734 rxq_ctrl = container_of((*priv->rxqs)[i], struct rxq_ctrl, rxq);
735 if (!rxq_ctrl->fdir_queue)
737 priv_fdir_queue_destroy(priv, rxq_ctrl->fdir_queue);
738 rxq_ctrl->fdir_queue = NULL;
740 if (priv->fdir_drop_queue) {
741 priv_fdir_queue_destroy(priv, priv->fdir_drop_queue);
742 priv->fdir_drop_queue = NULL;
747 * Enable flow director, create steering rules.
753 priv_fdir_enable(struct priv *priv)
755 struct mlx5_fdir_filter *mlx5_fdir_filter;
757 /* Run on every fdir filter and create flow handle */
758 LIST_FOREACH(mlx5_fdir_filter, priv->fdir_filter_list, next) {
759 /* Only valid elements should be in the list */
760 assert(mlx5_fdir_filter != NULL);
762 priv_fdir_filter_enable(priv, mlx5_fdir_filter);
767 * Find specific filter in list.
772 * Flow director filter to find.
775 * Filter element if found, otherwise NULL.
777 static struct mlx5_fdir_filter *
778 priv_find_filter_in_list(struct priv *priv,
779 const struct rte_eth_fdir_filter *fdir_filter)
781 struct fdir_flow_desc desc;
782 struct mlx5_fdir_filter *mlx5_fdir_filter;
783 enum rte_fdir_mode fdir_mode = priv->dev->data->dev_conf.fdir_conf.mode;
785 /* Get flow director filter to look for. */
786 fdir_filter_to_flow_desc(fdir_filter, &desc, fdir_mode);
788 /* Look for the requested element. */
789 LIST_FOREACH(mlx5_fdir_filter, priv->fdir_filter_list, next) {
790 /* Only valid elements should be in the list. */
791 assert(mlx5_fdir_filter != NULL);
793 /* Return matching filter. */
794 if (!memcmp(&desc, &mlx5_fdir_filter->desc, sizeof(desc)))
795 return mlx5_fdir_filter;
798 /* Filter not found */
803 * Add new flow director filter and store it in list.
808 * Flow director filter to add.
811 * 0 on success, errno value on failure.
814 priv_fdir_filter_add(struct priv *priv,
815 const struct rte_eth_fdir_filter *fdir_filter)
817 struct mlx5_fdir_filter *mlx5_fdir_filter;
818 enum rte_fdir_mode fdir_mode = priv->dev->data->dev_conf.fdir_conf.mode;
821 /* Validate queue number. */
822 if (fdir_filter->action.rx_queue >= priv->rxqs_n) {
823 ERROR("invalid queue number %d", fdir_filter->action.rx_queue);
827 /* Duplicate filters are currently unsupported. */
828 mlx5_fdir_filter = priv_find_filter_in_list(priv, fdir_filter);
829 if (mlx5_fdir_filter != NULL) {
830 ERROR("filter already exists");
834 /* Create new flow director filter. */
836 rte_calloc(__func__, 1, sizeof(*mlx5_fdir_filter), 0);
837 if (mlx5_fdir_filter == NULL) {
839 ERROR("cannot allocate flow director filter: %s",
844 /* Set action parameters. */
845 mlx5_fdir_filter->queue = fdir_filter->action.rx_queue;
846 mlx5_fdir_filter->behavior = fdir_filter->action.behavior;
848 /* Convert to mlx5 filter descriptor. */
849 fdir_filter_to_flow_desc(fdir_filter,
850 &mlx5_fdir_filter->desc, fdir_mode);
852 /* Insert new filter into list. */
853 LIST_INSERT_HEAD(priv->fdir_filter_list, mlx5_fdir_filter, next);
855 DEBUG("%p: flow director filter %p added",
856 (void *)priv, (void *)mlx5_fdir_filter);
858 /* Enable filter immediately if device is started. */
860 err = priv_fdir_filter_enable(priv, mlx5_fdir_filter);
866 * Update queue for specific filter.
871 * Filter to be updated.
874 * 0 on success, errno value on failure.
877 priv_fdir_filter_update(struct priv *priv,
878 const struct rte_eth_fdir_filter *fdir_filter)
880 struct mlx5_fdir_filter *mlx5_fdir_filter;
882 /* Validate queue number. */
883 if (fdir_filter->action.rx_queue >= priv->rxqs_n) {
884 ERROR("invalid queue number %d", fdir_filter->action.rx_queue);
888 mlx5_fdir_filter = priv_find_filter_in_list(priv, fdir_filter);
889 if (mlx5_fdir_filter != NULL) {
890 struct ibv_exp_flow *flow = mlx5_fdir_filter->flow;
893 /* Update queue number. */
894 mlx5_fdir_filter->queue = fdir_filter->action.rx_queue;
896 /* Destroy flow handle. */
898 claim_zero(ibv_exp_destroy_flow(flow));
899 mlx5_fdir_filter->flow = NULL;
901 DEBUG("%p: flow director filter %p updated",
902 (void *)priv, (void *)mlx5_fdir_filter);
904 /* Enable filter if device is started. */
906 err = priv_fdir_filter_enable(priv, mlx5_fdir_filter);
911 /* Filter not found, create it. */
912 DEBUG("%p: filter not found for update, creating new filter",
914 return priv_fdir_filter_add(priv, fdir_filter);
918 * Delete specific filter.
923 * Filter to be deleted.
926 * 0 on success, errno value on failure.
929 priv_fdir_filter_delete(struct priv *priv,
930 const struct rte_eth_fdir_filter *fdir_filter)
932 struct mlx5_fdir_filter *mlx5_fdir_filter;
934 mlx5_fdir_filter = priv_find_filter_in_list(priv, fdir_filter);
935 if (mlx5_fdir_filter != NULL) {
936 struct ibv_exp_flow *flow = mlx5_fdir_filter->flow;
938 /* Remove element from list. */
939 LIST_REMOVE(mlx5_fdir_filter, next);
941 /* Destroy flow handle. */
943 claim_zero(ibv_exp_destroy_flow(flow));
944 mlx5_fdir_filter->flow = NULL;
947 DEBUG("%p: flow director filter %p deleted",
948 (void *)priv, (void *)mlx5_fdir_filter);
951 rte_free(mlx5_fdir_filter);
956 ERROR("%p: flow director delete failed, cannot find filter",
962 * Get flow director information.
966 * @param[out] fdir_info
967 * Resulting flow director information.
970 priv_fdir_info_get(struct priv *priv, struct rte_eth_fdir_info *fdir_info)
972 struct rte_eth_fdir_masks *mask =
973 &priv->dev->data->dev_conf.fdir_conf.mask;
975 fdir_info->mode = priv->dev->data->dev_conf.fdir_conf.mode;
976 fdir_info->guarant_spc = 0;
978 rte_memcpy(&fdir_info->mask, mask, sizeof(fdir_info->mask));
980 fdir_info->max_flexpayload = 0;
981 fdir_info->flow_types_mask[0] = 0;
983 fdir_info->flex_payload_unit = 0;
984 fdir_info->max_flex_payload_segment_num = 0;
985 fdir_info->flex_payload_limit = 0;
986 memset(&fdir_info->flex_conf, 0, sizeof(fdir_info->flex_conf));
990 * Deal with flow director operations.
993 * Pointer to private structure.
995 * Operation to perform.
997 * Pointer to operation-specific structure.
1000 * 0 on success, errno value on failure.
1003 priv_fdir_ctrl_func(struct priv *priv, enum rte_filter_op filter_op, void *arg)
1005 enum rte_fdir_mode fdir_mode =
1006 priv->dev->data->dev_conf.fdir_conf.mode;
1009 if (filter_op == RTE_ETH_FILTER_NOP)
1012 if (fdir_mode != RTE_FDIR_MODE_PERFECT &&
1013 fdir_mode != RTE_FDIR_MODE_PERFECT_MAC_VLAN) {
1014 ERROR("%p: flow director mode %d not supported",
1015 (void *)priv, fdir_mode);
1019 switch (filter_op) {
1020 case RTE_ETH_FILTER_ADD:
1021 ret = priv_fdir_filter_add(priv, arg);
1023 case RTE_ETH_FILTER_UPDATE:
1024 ret = priv_fdir_filter_update(priv, arg);
1026 case RTE_ETH_FILTER_DELETE:
1027 ret = priv_fdir_filter_delete(priv, arg);
1029 case RTE_ETH_FILTER_FLUSH:
1030 priv_fdir_filter_flush(priv);
1032 case RTE_ETH_FILTER_INFO:
1033 priv_fdir_info_get(priv, arg);
1036 DEBUG("%p: unknown operation %u", (void *)priv, filter_op);
1043 static const struct rte_flow_ops mlx5_flow_ops = {
1044 .validate = mlx5_flow_validate,
1045 .create = mlx5_flow_create,
1046 .destroy = mlx5_flow_destroy,
1047 .flush = mlx5_flow_flush,
1049 .isolate = mlx5_flow_isolate,
1053 * Manage filter operations.
1056 * Pointer to Ethernet device structure.
1057 * @param filter_type
1060 * Operation to perform.
1062 * Pointer to operation-specific structure.
1065 * 0 on success, negative errno value on failure.
1068 mlx5_dev_filter_ctrl(struct rte_eth_dev *dev,
1069 enum rte_filter_type filter_type,
1070 enum rte_filter_op filter_op,
1074 struct priv *priv = dev->data->dev_private;
1076 switch (filter_type) {
1077 case RTE_ETH_FILTER_GENERIC:
1078 if (filter_op != RTE_ETH_FILTER_GET)
1080 *(const void **)arg = &mlx5_flow_ops;
1082 case RTE_ETH_FILTER_FDIR:
1084 ret = priv_fdir_ctrl_func(priv, filter_op, arg);
1088 ERROR("%p: filter type (%d) not supported",
1089 (void *)dev, filter_type);