eal: set name to threads
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_interrupts.c
index 55be263..a4b9506 100644 (file)
@@ -44,6 +44,7 @@
 #include <sys/epoll.h>
 #include <sys/signalfd.h>
 #include <sys/ioctl.h>
+#include <sys/eventfd.h>
 
 #include <rte_common.h>
 #include <rte_interrupts.h>
 
 #include "eal_private.h"
 #include "eal_vfio.h"
+#include "eal_thread.h"
 
 #define EAL_INTR_EPOLL_WAIT_FOREVER (-1)
+#define NB_OTHER_INTR               1
 
 static RTE_DEFINE_PER_LCORE(int, _epfd) = -1; /**< epoll fd per thread */
 
@@ -128,6 +131,9 @@ static pthread_t intr_thread;
 #ifdef VFIO_PRESENT
 
 #define IRQ_SET_BUF_LEN  (sizeof(struct vfio_irq_set) + sizeof(int))
+/* irq set buffer length for queue interrupts and LSC interrupt */
+#define MSIX_IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + \
+                             sizeof(int) * (RTE_MAX_RXTX_INTR_VEC_ID + 1))
 
 /* enable legacy (INTx) interrupts */
 static int
@@ -219,7 +225,7 @@ vfio_disable_intx(struct rte_intr_handle *intr_handle) {
        return 0;
 }
 
-/* enable MSI-X interrupts */
+/* enable MSI interrupts */
 static int
 vfio_enable_msi(struct rte_intr_handle *intr_handle) {
        int len, ret;
@@ -245,27 +251,10 @@ vfio_enable_msi(struct rte_intr_handle *intr_handle) {
                                                intr_handle->fd);
                return -1;
        }
-
-       /* manually trigger interrupt to enable it */
-       memset(irq_set, 0, len);
-       len = sizeof(struct vfio_irq_set);
-       irq_set->argsz = len;
-       irq_set->count = 1;
-       irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
-       irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
-       irq_set->start = 0;
-
-       ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
-
-       if (ret) {
-               RTE_LOG(ERR, EAL, "Error triggering MSI interrupts for fd %d\n",
-                                               intr_handle->fd);
-               return -1;
-       }
        return 0;
 }
 
-/* disable MSI-X interrupts */
+/* disable MSI interrupts */
 static int
 vfio_disable_msi(struct rte_intr_handle *intr_handle) {
        struct vfio_irq_set *irq_set;
@@ -294,7 +283,7 @@ vfio_disable_msi(struct rte_intr_handle *intr_handle) {
 static int
 vfio_enable_msix(struct rte_intr_handle *intr_handle) {
        int len, ret;
-       char irq_set_buf[IRQ_SET_BUF_LEN];
+       char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
        struct vfio_irq_set *irq_set;
        int *fd_ptr;
 
@@ -302,12 +291,18 @@ vfio_enable_msix(struct rte_intr_handle *intr_handle) {
 
        irq_set = (struct vfio_irq_set *) irq_set_buf;
        irq_set->argsz = len;
-       irq_set->count = 1;
+       if (!intr_handle->max_intr)
+               intr_handle->max_intr = 1;
+       else if (intr_handle->max_intr > RTE_MAX_RXTX_INTR_VEC_ID)
+               intr_handle->max_intr = RTE_MAX_RXTX_INTR_VEC_ID + 1;
+
+       irq_set->count = intr_handle->max_intr;
        irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
        irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
        irq_set->start = 0;
        fd_ptr = (int *) &irq_set->data;
-       *fd_ptr = intr_handle->fd;
+       memcpy(fd_ptr, intr_handle->efds, sizeof(intr_handle->efds));
+       fd_ptr[intr_handle->max_intr - 1] = intr_handle->fd;
 
        ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
 
@@ -317,22 +312,6 @@ vfio_enable_msix(struct rte_intr_handle *intr_handle) {
                return -1;
        }
 
-       /* manually trigger interrupt to enable it */
-       memset(irq_set, 0, len);
-       len = sizeof(struct vfio_irq_set);
-       irq_set->argsz = len;
-       irq_set->count = 1;
-       irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
-       irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
-       irq_set->start = 0;
-
-       ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
-
-       if (ret) {
-               RTE_LOG(ERR, EAL, "Error triggering MSI-X interrupts for fd %d\n",
-                                               intr_handle->fd);
-               return -1;
-       }
        return 0;
 }
 
@@ -340,7 +319,7 @@ vfio_enable_msix(struct rte_intr_handle *intr_handle) {
 static int
 vfio_disable_msix(struct rte_intr_handle *intr_handle) {
        struct vfio_irq_set *irq_set;
-       char irq_set_buf[IRQ_SET_BUF_LEN];
+       char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
        int len, ret;
 
        len = sizeof(struct vfio_irq_set);
@@ -700,6 +679,7 @@ eal_intr_process_interrupts(struct epoll_event *events, int nfds)
                /* set the length to be read dor different handle type */
                switch (src->intr_handle.type) {
                case RTE_INTR_HANDLE_UIO:
+               case RTE_INTR_HANDLE_UIO_INTX:
                        bytes_read = sizeof(buf.uio_intr_count);
                        break;
                case RTE_INTR_HANDLE_ALARM:
@@ -712,26 +692,30 @@ eal_intr_process_interrupts(struct epoll_event *events, int nfds)
                        bytes_read = sizeof(buf.vfio_intr_count);
                        break;
 #endif
+               case RTE_INTR_HANDLE_EXT:
                default:
                        bytes_read = 1;
                        break;
                }
 
-               /**
-                * read out to clear the ready-to-be-read flag
-                * for epoll_wait.
-                */
-               bytes_read = read(events[n].data.fd, &buf, bytes_read);
-               if (bytes_read < 0) {
-                       if (errno == EINTR || errno == EWOULDBLOCK)
-                               continue;
-
-                       RTE_LOG(ERR, EAL, "Error reading from file "
-                               "descriptor %d: %s\n", events[n].data.fd,
-                                                       strerror(errno));
-               } else if (bytes_read == 0)
-                       RTE_LOG(ERR, EAL, "Read nothing from file "
-                               "descriptor %d\n", events[n].data.fd);
+               if (src->intr_handle.type != RTE_INTR_HANDLE_EXT) {
+                       /**
+                        * read out to clear the ready-to-be-read flag
+                        * for epoll_wait.
+                        */
+                       bytes_read = read(events[n].data.fd, &buf, bytes_read);
+                       if (bytes_read < 0) {
+                               if (errno == EINTR || errno == EWOULDBLOCK)
+                                       continue;
+
+                               RTE_LOG(ERR, EAL, "Error reading from file "
+                                       "descriptor %d: %s\n",
+                                       events[n].data.fd,
+                                       strerror(errno));
+                       } else if (bytes_read == 0)
+                               RTE_LOG(ERR, EAL, "Read nothing from file "
+                                       "descriptor %d\n", events[n].data.fd);
+               }
 
                /* grab a lock, again to call callbacks and update status. */
                rte_spinlock_lock(&intr_lock);
@@ -877,7 +861,8 @@ eal_intr_thread_main(__rte_unused void *arg)
 int
 rte_eal_intr_init(void)
 {
-       int ret = 0;
+       int ret = 0, ret_1 = 0;
+       char thread_name[RTE_MAX_THREAD_NAME_LEN];
 
        /* init the global interrupt source head */
        TAILQ_INIT(&intr_sources);
@@ -892,13 +877,65 @@ rte_eal_intr_init(void)
        /* create the host thread to wait/handle the interrupt */
        ret = pthread_create(&intr_thread, NULL,
                        eal_intr_thread_main, NULL);
-       if (ret != 0)
+       if (ret != 0) {
                RTE_LOG(ERR, EAL,
                        "Failed to create thread for interrupt handling\n");
+       } else {
+               /* Set thread_name for aid in debugging. */
+               snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN,
+                       "eal-intr-thread");
+               ret_1 = pthread_setname_np(intr_thread, thread_name);
+               if (ret_1 != 0)
+                       RTE_LOG(ERR, EAL,
+                       "Failed to set thread name for interrupt handling\n");
+       }
 
        return -ret;
 }
 
+static void
+eal_intr_proc_rxtx_intr(int fd, const struct rte_intr_handle *intr_handle)
+{
+       union rte_intr_read_buffer buf;
+       int bytes_read = 1;
+
+       switch (intr_handle->type) {
+       case RTE_INTR_HANDLE_UIO:
+       case RTE_INTR_HANDLE_UIO_INTX:
+               bytes_read = sizeof(buf.uio_intr_count);
+               break;
+#ifdef VFIO_PRESENT
+       case RTE_INTR_HANDLE_VFIO_MSIX:
+       case RTE_INTR_HANDLE_VFIO_MSI:
+       case RTE_INTR_HANDLE_VFIO_LEGACY:
+               bytes_read = sizeof(buf.vfio_intr_count);
+               break;
+#endif
+       default:
+               bytes_read = 1;
+               RTE_LOG(INFO, EAL, "unexpected intr type\n");
+               break;
+       }
+
+       /**
+        * read out to clear the ready-to-be-read flag
+        * for epoll_wait.
+        */
+       do {
+               bytes_read = read(fd, &buf, bytes_read);
+               if (bytes_read < 0) {
+                       if (errno == EINTR || errno == EWOULDBLOCK ||
+                           errno == EAGAIN)
+                               continue;
+                       RTE_LOG(ERR, EAL,
+                               "Error reading from fd %d: %s\n",
+                               fd, strerror(errno));
+               } else if (bytes_read == 0)
+                       RTE_LOG(ERR, EAL, "Read nothing from fd %d\n", fd);
+               return;
+       } while (1);
+}
+
 static int
 eal_epoll_process_event(struct epoll_event *evs, unsigned int n,
                        struct rte_epoll_event *events)
@@ -980,6 +1017,9 @@ rte_epoll_wait(int epfd, struct rte_epoll_event *events,
                                strerror(errno));
                        rc = -1;
                        break;
+               } else {
+                       /* rc == 0, epoll_wait timed out */
+                       break;
                }
        }
 
@@ -1035,3 +1075,127 @@ rte_epoll_ctl(int epfd, int op, int fd,
 
        return 0;
 }
+
+int
+rte_intr_rx_ctl(struct rte_intr_handle *intr_handle, int epfd,
+               int op, unsigned int vec, void *data)
+{
+       struct rte_epoll_event *rev;
+       struct rte_epoll_data *epdata;
+       int epfd_op;
+       int rc = 0;
+
+       if (!intr_handle || intr_handle->nb_efd == 0 ||
+           vec >= intr_handle->nb_efd) {
+               RTE_LOG(ERR, EAL, "Wrong intr vector number.\n");
+               return -EPERM;
+       }
+
+       switch (op) {
+       case RTE_INTR_EVENT_ADD:
+               epfd_op = EPOLL_CTL_ADD;
+               rev = &intr_handle->elist[vec];
+               if (rev->status != RTE_EPOLL_INVALID) {
+                       RTE_LOG(INFO, EAL, "Event already been added.\n");
+                       return -EEXIST;
+               }
+
+               /* attach to intr vector fd */
+               epdata = &rev->epdata;
+               epdata->event  = EPOLLIN | EPOLLPRI | EPOLLET;
+               epdata->data   = data;
+               epdata->cb_fun = (rte_intr_event_cb_t)eal_intr_proc_rxtx_intr;
+               epdata->cb_arg = (void *)intr_handle;
+               rc = rte_epoll_ctl(epfd, epfd_op, intr_handle->efds[vec], rev);
+               if (!rc)
+                       RTE_LOG(DEBUG, EAL,
+                               "efd %d associated with vec %d added on epfd %d"
+                               "\n", rev->fd, vec, epfd);
+               else
+                       rc = -EPERM;
+               break;
+       case RTE_INTR_EVENT_DEL:
+               epfd_op = EPOLL_CTL_DEL;
+               rev = &intr_handle->elist[vec];
+               if (rev->status == RTE_EPOLL_INVALID) {
+                       RTE_LOG(INFO, EAL, "Event does not exist.\n");
+                       return -EPERM;
+               }
+
+               rc = rte_epoll_ctl(rev->epfd, epfd_op, rev->fd, rev);
+               if (rc)
+                       rc = -EPERM;
+               break;
+       default:
+               RTE_LOG(ERR, EAL, "event op type mismatch\n");
+               rc = -EPERM;
+       }
+
+       return rc;
+}
+
+int
+rte_intr_efd_enable(struct rte_intr_handle *intr_handle, uint32_t nb_efd)
+{
+       uint32_t i;
+       int fd;
+       uint32_t n = RTE_MIN(nb_efd, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
+
+       if (intr_handle->type == RTE_INTR_HANDLE_VFIO_MSIX) {
+               for (i = 0; i < n; i++) {
+                       fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
+                       if (fd < 0) {
+                               RTE_LOG(ERR, EAL,
+                                       "can't setup eventfd, error %i (%s)\n",
+                                       errno, strerror(errno));
+                               return -1;
+                       }
+                       intr_handle->efds[i] = fd;
+               }
+               intr_handle->nb_efd   = n;
+               intr_handle->max_intr = NB_OTHER_INTR + n;
+       } else {
+               intr_handle->efds[0]  = intr_handle->fd;
+               intr_handle->nb_efd   = RTE_MIN(nb_efd, 1U);
+               intr_handle->max_intr = NB_OTHER_INTR;
+       }
+
+       return 0;
+}
+
+void
+rte_intr_efd_disable(struct rte_intr_handle *intr_handle)
+{
+       uint32_t i;
+       struct rte_epoll_event *rev;
+
+       for (i = 0; i < intr_handle->nb_efd; i++) {
+               rev = &intr_handle->elist[i];
+               if (rev->status == RTE_EPOLL_INVALID)
+                       continue;
+               if (rte_epoll_ctl(rev->epfd, EPOLL_CTL_DEL, rev->fd, rev)) {
+                       /* force free if the entry valid */
+                       eal_epoll_data_safe_free(rev);
+                       rev->status = RTE_EPOLL_INVALID;
+               }
+       }
+
+       if (intr_handle->max_intr > intr_handle->nb_efd) {
+               for (i = 0; i < intr_handle->nb_efd; i++)
+                       close(intr_handle->efds[i]);
+       }
+       intr_handle->nb_efd = 0;
+       intr_handle->max_intr = 0;
+}
+
+int
+rte_intr_dp_is_en(struct rte_intr_handle *intr_handle)
+{
+       return !(!intr_handle->nb_efd);
+}
+
+int
+rte_intr_allow_others(struct rte_intr_handle *intr_handle)
+{
+       return !!(intr_handle->max_intr - intr_handle->nb_efd);
+}