net/mlx4: refactor interrupt FD settings
authorAdrien Mazarguil <adrien.mazarguil@6wind.com>
Fri, 1 Sep 2017 08:06:52 +0000 (10:06 +0200)
committerFerruh Yigit <ferruh.yigit@intel.com>
Fri, 6 Oct 2017 00:49:48 +0000 (02:49 +0200)
File descriptors used for interrupts processing must be made non-blocking.

Doing so as soon as they are opened instead of waiting until they are
needed is more efficient as it avoids performing redundant system calls and
run through their associated error-handling code later on.

Signed-off-by: Adrien Mazarguil <adrien.mazarguil@6wind.com>
drivers/net/mlx4/Makefile
drivers/net/mlx4/mlx4.c
drivers/net/mlx4/mlx4.h
drivers/net/mlx4/mlx4_utils.c [new file with mode: 0644]
drivers/net/mlx4/mlx4_utils.h

index 20692f0..8a03154 100644 (file)
@@ -37,6 +37,7 @@ LIB = librte_pmd_mlx4.a
 # Sources.
 SRCS-$(CONFIG_RTE_LIBRTE_MLX4_PMD) += mlx4.c
 SRCS-$(CONFIG_RTE_LIBRTE_MLX4_PMD) += mlx4_flow.c
+SRCS-$(CONFIG_RTE_LIBRTE_MLX4_PMD) += mlx4_utils.c
 
 # Basic CFLAGS.
 CFLAGS += -O3
index 42606ed..2db2b0e 100644 (file)
@@ -48,7 +48,6 @@
 #include <netinet/in.h>
 #include <linux/ethtool.h>
 #include <linux/sockios.h>
-#include <fcntl.h>
 
 #include <rte_ether.h>
 #include <rte_ethdev.h>
@@ -1800,6 +1799,12 @@ rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
                              (void *)dev, strerror(rte_errno));
                        goto error;
                }
+               if (mlx4_fd_set_non_blocking(tmpl.channel->fd) < 0) {
+                       ERROR("%p: unable to make Rx interrupt completion"
+                             " channel non-blocking: %s",
+                             (void *)dev, strerror(rte_errno));
+                       goto error;
+               }
        }
        tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, tmpl.channel, 0);
        if (tmpl.cq == NULL) {
@@ -2836,7 +2841,6 @@ static int
 priv_dev_interrupt_handler_install(struct priv *priv,
                                   struct rte_eth_dev *dev)
 {
-       int flags;
        int rc;
 
        /*
@@ -2847,29 +2851,17 @@ priv_dev_interrupt_handler_install(struct priv *priv,
            priv->intr_conf.rmv &&
            priv->intr_handle.fd)
                return 0;
-       assert(priv->ctx->async_fd > 0);
-       flags = fcntl(priv->ctx->async_fd, F_GETFL);
-       rc = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
-       if (rc < 0) {
-               rte_errno = errno ? errno : EINVAL;
-               INFO("failed to change file descriptor async event queue");
-               dev->data->dev_conf.intr_conf.lsc = 0;
-               dev->data->dev_conf.intr_conf.rmv = 0;
-               return -rte_errno;
-       } else {
-               priv->intr_handle.fd = priv->ctx->async_fd;
-               rc = rte_intr_callback_register(&priv->intr_handle,
-                                                mlx4_dev_interrupt_handler,
-                                                dev);
-               if (rc) {
-                       rte_errno = -rc;
-                       ERROR("rte_intr_callback_register failed "
-                             " (rte_errno: %s)", strerror(rte_errno));
-                       priv->intr_handle.fd = -1;
-                       return -rte_errno;
-               }
-       }
-       return 0;
+       priv->intr_handle.fd = priv->ctx->async_fd;
+       rc = rte_intr_callback_register(&priv->intr_handle,
+                                       mlx4_dev_interrupt_handler,
+                                       dev);
+       if (!rc)
+               return 0;
+       rte_errno = -rc;
+       ERROR("rte_intr_callback_register failed (rte_errno: %s)",
+             strerror(rte_errno));
+       priv->intr_handle.fd = -1;
+       return -rte_errno;
 }
 
 /**
@@ -3010,9 +3002,6 @@ priv_rx_intr_vec_enable(struct priv *priv)
        }
        for (i = 0; i != n; ++i) {
                struct rxq *rxq = (*priv->rxqs)[i];
-               int fd;
-               int flags;
-               int rc;
 
                /* Skip queues that cannot request interrupts. */
                if (!rxq || !rxq->channel) {
@@ -3030,18 +3019,8 @@ priv_rx_intr_vec_enable(struct priv *priv)
                        priv_rx_intr_vec_disable(priv);
                        return -rte_errno;
                }
-               fd = rxq->channel->fd;
-               flags = fcntl(fd, F_GETFL);
-               rc = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
-               if (rc < 0) {
-                       rte_errno = errno;
-                       ERROR("failed to make Rx interrupt file descriptor"
-                             " %d non-blocking for queue index %d", fd, i);
-                       priv_rx_intr_vec_disable(priv);
-                       return -rte_errno;
-               }
                intr_handle->intr_vec[i] = RTE_INTR_VEC_RXTX_OFFSET + count;
-               intr_handle->efds[count] = fd;
+               intr_handle->efds[count] = rxq->channel->fd;
                count++;
        }
        if (!count)
@@ -3358,6 +3337,12 @@ mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
                        DEBUG("port %d is not active: \"%s\" (%d)",
                              port, ibv_port_state_str(port_attr.state),
                              port_attr.state);
+               /* Make asynchronous FD non-blocking to handle interrupts. */
+               if (mlx4_fd_set_non_blocking(ctx->async_fd) < 0) {
+                       ERROR("cannot make asynchronous FD non-blocking: %s",
+                             strerror(rte_errno));
+                       goto port_error;
+               }
                /* Allocate protection domain. */
                pd = ibv_alloc_pd(ctx);
                if (pd == NULL) {
index 47ea409..b61f5f2 100644 (file)
 #pragma GCC diagnostic error "-Wpedantic"
 #endif
 
+#include <rte_ethdev.h>
+#include <rte_ether.h>
+#include <rte_interrupts.h>
+
 /* Request send completion once in every 64 sends, might be less. */
 #define MLX4_PMD_TX_PER_COMP_REQ 64
 
diff --git a/drivers/net/mlx4/mlx4_utils.c b/drivers/net/mlx4/mlx4_utils.c
new file mode 100644 (file)
index 0000000..fcf76c9
--- /dev/null
@@ -0,0 +1,66 @@
+/*-
+ *   BSD LICENSE
+ *
+ *   Copyright 2017 6WIND S.A.
+ *   Copyright 2017 Mellanox
+ *
+ *   Redistribution and use in source and binary forms, with or without
+ *   modification, are permitted provided that the following conditions
+ *   are met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in
+ *       the documentation and/or other materials provided with the
+ *       distribution.
+ *     * Neither the name of 6WIND S.A. nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ * @file
+ * Utility functions used by the mlx4 driver.
+ */
+
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+
+#include <rte_errno.h>
+
+#include "mlx4_utils.h"
+
+/**
+ * Make a file descriptor non-blocking.
+ *
+ * @param fd
+ *   File descriptor to alter.
+ *
+ * @return
+ *   0 on success, negative errno value otherwise and rte_errno is set.
+ */
+int
+mlx4_fd_set_non_blocking(int fd)
+{
+       int ret = fcntl(fd, F_GETFL);
+
+       if (ret != -1 && !fcntl(fd, F_SETFL, ret | O_NONBLOCK))
+               return 0;
+       assert(errno);
+       rte_errno = errno;
+       return -rte_errno;
+}
index 30f96c2..9b178f5 100644 (file)
@@ -95,4 +95,8 @@ pmd_drv_log_basename(const char *s)
 #define WARN(...) PMD_DRV_LOG(WARNING, __VA_ARGS__)
 #define ERROR(...) PMD_DRV_LOG(ERR, __VA_ARGS__)
 
+/* mlx4_utils.c */
+
+int mlx4_fd_set_non_blocking(int fd);
+
 #endif /* MLX4_UTILS_H_ */