4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
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 Intel Corporation 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.
38 #include <sys/queue.h>
44 #include <sys/epoll.h>
45 #include <sys/signalfd.h>
46 #include <sys/ioctl.h>
47 #include <sys/eventfd.h>
51 #include <rte_common.h>
52 #include <rte_interrupts.h>
53 #include <rte_memory.h>
54 #include <rte_launch.h>
56 #include <rte_per_lcore.h>
57 #include <rte_lcore.h>
58 #include <rte_atomic.h>
59 #include <rte_branch_prediction.h>
60 #include <rte_debug.h>
62 #include <rte_malloc.h>
63 #include <rte_errno.h>
64 #include <rte_spinlock.h>
65 #include <rte_pause.h>
67 #include "eal_private.h"
69 #include "eal_thread.h"
71 #define EAL_INTR_EPOLL_WAIT_FOREVER (-1)
72 #define NB_OTHER_INTR 1
74 static RTE_DEFINE_PER_LCORE(int, _epfd) = -1; /**< epoll fd per thread */
90 * union buffer for reading on different devices
92 union rte_intr_read_buffer {
93 int uio_intr_count; /* for uio device */
95 uint64_t vfio_intr_count; /* for vfio device */
97 uint64_t timerfd_num; /* for timerfd */
98 char charbuf[16]; /* for others */
101 TAILQ_HEAD(rte_intr_cb_list, rte_intr_callback);
102 TAILQ_HEAD(rte_intr_source_list, rte_intr_source);
104 struct rte_intr_callback {
105 TAILQ_ENTRY(rte_intr_callback) next;
106 rte_intr_callback_fn cb_fn; /**< callback address */
107 void *cb_arg; /**< parameter for callback */
110 struct rte_intr_source {
111 TAILQ_ENTRY(rte_intr_source) next;
112 struct rte_intr_handle intr_handle; /**< interrupt handle */
113 struct rte_intr_cb_list callbacks; /**< user callbacks */
117 /* global spinlock for interrupt data operation */
118 static rte_spinlock_t intr_lock = RTE_SPINLOCK_INITIALIZER;
120 /* union buffer for pipe read/write */
121 static union intr_pipefds intr_pipe;
123 /* interrupt sources list */
124 static struct rte_intr_source_list intr_sources;
126 /* interrupt handling thread */
127 static pthread_t intr_thread;
129 /* VFIO interrupts */
132 #define IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + sizeof(int))
133 /* irq set buffer length for queue interrupts and LSC interrupt */
134 #define MSIX_IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + \
135 sizeof(int) * (RTE_MAX_RXTX_INTR_VEC_ID + 1))
137 /* enable legacy (INTx) interrupts */
139 vfio_enable_intx(const struct rte_intr_handle *intr_handle) {
140 struct vfio_irq_set *irq_set;
141 char irq_set_buf[IRQ_SET_BUF_LEN];
145 len = sizeof(irq_set_buf);
148 irq_set = (struct vfio_irq_set *) irq_set_buf;
149 irq_set->argsz = len;
151 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
152 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
154 fd_ptr = (int *) &irq_set->data;
155 *fd_ptr = intr_handle->fd;
157 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
160 RTE_LOG(ERR, EAL, "Error enabling INTx interrupts for fd %d\n",
165 /* unmask INTx after enabling */
166 memset(irq_set, 0, len);
167 len = sizeof(struct vfio_irq_set);
168 irq_set->argsz = len;
170 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK;
171 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
174 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
177 RTE_LOG(ERR, EAL, "Error unmasking INTx interrupts for fd %d\n",
184 /* disable legacy (INTx) interrupts */
186 vfio_disable_intx(const struct rte_intr_handle *intr_handle) {
187 struct vfio_irq_set *irq_set;
188 char irq_set_buf[IRQ_SET_BUF_LEN];
191 len = sizeof(struct vfio_irq_set);
193 /* mask interrupts before disabling */
194 irq_set = (struct vfio_irq_set *) irq_set_buf;
195 irq_set->argsz = len;
197 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_MASK;
198 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
201 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
204 RTE_LOG(ERR, EAL, "Error masking INTx interrupts for fd %d\n",
210 memset(irq_set, 0, len);
211 irq_set->argsz = len;
213 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
214 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
217 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
221 "Error disabling INTx interrupts for fd %d\n", intr_handle->fd);
227 /* enable MSI interrupts */
229 vfio_enable_msi(const struct rte_intr_handle *intr_handle) {
231 char irq_set_buf[IRQ_SET_BUF_LEN];
232 struct vfio_irq_set *irq_set;
235 len = sizeof(irq_set_buf);
237 irq_set = (struct vfio_irq_set *) irq_set_buf;
238 irq_set->argsz = len;
240 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
241 irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
243 fd_ptr = (int *) &irq_set->data;
244 *fd_ptr = intr_handle->fd;
246 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
249 RTE_LOG(ERR, EAL, "Error enabling MSI interrupts for fd %d\n",
256 /* disable MSI interrupts */
258 vfio_disable_msi(const struct rte_intr_handle *intr_handle) {
259 struct vfio_irq_set *irq_set;
260 char irq_set_buf[IRQ_SET_BUF_LEN];
263 len = sizeof(struct vfio_irq_set);
265 irq_set = (struct vfio_irq_set *) irq_set_buf;
266 irq_set->argsz = len;
268 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
269 irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
272 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
276 "Error disabling MSI interrupts for fd %d\n", intr_handle->fd);
281 /* enable MSI-X interrupts */
283 vfio_enable_msix(const struct rte_intr_handle *intr_handle) {
285 char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
286 struct vfio_irq_set *irq_set;
289 len = sizeof(irq_set_buf);
291 irq_set = (struct vfio_irq_set *) irq_set_buf;
292 irq_set->argsz = len;
293 /* 0 < irq_set->count < RTE_MAX_RXTX_INTR_VEC_ID + 1 */
294 irq_set->count = intr_handle->max_intr ?
295 (intr_handle->max_intr > RTE_MAX_RXTX_INTR_VEC_ID + 1 ?
296 RTE_MAX_RXTX_INTR_VEC_ID + 1 : intr_handle->max_intr) : 1;
297 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
298 irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
300 fd_ptr = (int *) &irq_set->data;
301 /* INTR vector offset 0 reserve for non-efds mapping */
302 fd_ptr[RTE_INTR_VEC_ZERO_OFFSET] = intr_handle->fd;
303 memcpy(&fd_ptr[RTE_INTR_VEC_RXTX_OFFSET], intr_handle->efds,
304 sizeof(*intr_handle->efds) * intr_handle->nb_efd);
306 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
309 RTE_LOG(ERR, EAL, "Error enabling MSI-X interrupts for fd %d\n",
317 /* disable MSI-X interrupts */
319 vfio_disable_msix(const struct rte_intr_handle *intr_handle) {
320 struct vfio_irq_set *irq_set;
321 char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
324 len = sizeof(struct vfio_irq_set);
326 irq_set = (struct vfio_irq_set *) irq_set_buf;
327 irq_set->argsz = len;
329 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
330 irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
333 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
337 "Error disabling MSI-X interrupts for fd %d\n", intr_handle->fd);
344 uio_intx_intr_disable(const struct rte_intr_handle *intr_handle)
346 unsigned char command_high;
348 /* use UIO config file descriptor for uio_pci_generic */
349 if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
351 "Error reading interrupts status for fd %d\n",
352 intr_handle->uio_cfg_fd);
355 /* disable interrupts */
357 if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
359 "Error disabling interrupts for fd %d\n",
360 intr_handle->uio_cfg_fd);
368 uio_intx_intr_enable(const struct rte_intr_handle *intr_handle)
370 unsigned char command_high;
372 /* use UIO config file descriptor for uio_pci_generic */
373 if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
375 "Error reading interrupts status for fd %d\n",
376 intr_handle->uio_cfg_fd);
379 /* enable interrupts */
380 command_high &= ~0x4;
381 if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
383 "Error enabling interrupts for fd %d\n",
384 intr_handle->uio_cfg_fd);
392 uio_intr_disable(const struct rte_intr_handle *intr_handle)
396 if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
398 "Error disabling interrupts for fd %d (%s)\n",
399 intr_handle->fd, strerror(errno));
406 uio_intr_enable(const struct rte_intr_handle *intr_handle)
410 if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
412 "Error enabling interrupts for fd %d (%s)\n",
413 intr_handle->fd, strerror(errno));
420 rte_intr_callback_register(const struct rte_intr_handle *intr_handle,
421 rte_intr_callback_fn cb, void *cb_arg)
423 int ret, wake_thread;
424 struct rte_intr_source *src;
425 struct rte_intr_callback *callback;
429 /* first do parameter checking */
430 if (intr_handle == NULL || intr_handle->fd < 0 || cb == NULL) {
432 "Registering with invalid input parameter\n");
436 /* allocate a new interrupt callback entity */
437 callback = rte_zmalloc("interrupt callback list",
438 sizeof(*callback), 0);
439 if (callback == NULL) {
440 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
443 callback->cb_fn = cb;
444 callback->cb_arg = cb_arg;
446 rte_spinlock_lock(&intr_lock);
448 /* check if there is at least one callback registered for the fd */
449 TAILQ_FOREACH(src, &intr_sources, next) {
450 if (src->intr_handle.fd == intr_handle->fd) {
451 /* we had no interrupts for this */
452 if TAILQ_EMPTY(&src->callbacks)
455 TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
461 /* no existing callbacks for this - add new source */
463 if ((src = rte_zmalloc("interrupt source list",
464 sizeof(*src), 0)) == NULL) {
465 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
469 src->intr_handle = *intr_handle;
470 TAILQ_INIT(&src->callbacks);
471 TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
472 TAILQ_INSERT_TAIL(&intr_sources, src, next);
478 rte_spinlock_unlock(&intr_lock);
481 * check if need to notify the pipe fd waited by epoll_wait to
482 * rebuild the wait list.
485 if (write(intr_pipe.writefd, "1", 1) < 0)
492 rte_intr_callback_unregister(const struct rte_intr_handle *intr_handle,
493 rte_intr_callback_fn cb_fn, void *cb_arg)
496 struct rte_intr_source *src;
497 struct rte_intr_callback *cb, *next;
499 /* do parameter checking first */
500 if (intr_handle == NULL || intr_handle->fd < 0) {
502 "Unregistering with invalid input parameter\n");
506 rte_spinlock_lock(&intr_lock);
508 /* check if the insterrupt source for the fd is existent */
509 TAILQ_FOREACH(src, &intr_sources, next)
510 if (src->intr_handle.fd == intr_handle->fd)
513 /* No interrupt source registered for the fd */
517 /* interrupt source has some active callbacks right now. */
518 } else if (src->active != 0) {
525 /*walk through the callbacks and remove all that match. */
526 for (cb = TAILQ_FIRST(&src->callbacks); cb != NULL; cb = next) {
528 next = TAILQ_NEXT(cb, next);
530 if (cb->cb_fn == cb_fn && (cb_arg == (void *)-1 ||
531 cb->cb_arg == cb_arg)) {
532 TAILQ_REMOVE(&src->callbacks, cb, next);
538 /* all callbacks for that source are removed. */
539 if (TAILQ_EMPTY(&src->callbacks)) {
540 TAILQ_REMOVE(&intr_sources, src, next);
545 rte_spinlock_unlock(&intr_lock);
547 /* notify the pipe fd waited by epoll_wait to rebuild the wait list */
548 if (ret >= 0 && write(intr_pipe.writefd, "1", 1) < 0) {
556 rte_intr_enable(const struct rte_intr_handle *intr_handle)
558 if (intr_handle && intr_handle->type == RTE_INTR_HANDLE_VDEV)
561 if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
564 switch (intr_handle->type){
565 /* write to the uio fd to enable the interrupt */
566 case RTE_INTR_HANDLE_UIO:
567 if (uio_intr_enable(intr_handle))
570 case RTE_INTR_HANDLE_UIO_INTX:
571 if (uio_intx_intr_enable(intr_handle))
574 /* not used at this moment */
575 case RTE_INTR_HANDLE_ALARM:
578 case RTE_INTR_HANDLE_VFIO_MSIX:
579 if (vfio_enable_msix(intr_handle))
582 case RTE_INTR_HANDLE_VFIO_MSI:
583 if (vfio_enable_msi(intr_handle))
586 case RTE_INTR_HANDLE_VFIO_LEGACY:
587 if (vfio_enable_intx(intr_handle))
591 /* unknown handle type */
594 "Unknown handle type of fd %d\n",
603 rte_intr_disable(const struct rte_intr_handle *intr_handle)
605 if (intr_handle && intr_handle->type == RTE_INTR_HANDLE_VDEV)
608 if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
611 switch (intr_handle->type){
612 /* write to the uio fd to disable the interrupt */
613 case RTE_INTR_HANDLE_UIO:
614 if (uio_intr_disable(intr_handle))
617 case RTE_INTR_HANDLE_UIO_INTX:
618 if (uio_intx_intr_disable(intr_handle))
621 /* not used at this moment */
622 case RTE_INTR_HANDLE_ALARM:
625 case RTE_INTR_HANDLE_VFIO_MSIX:
626 if (vfio_disable_msix(intr_handle))
629 case RTE_INTR_HANDLE_VFIO_MSI:
630 if (vfio_disable_msi(intr_handle))
633 case RTE_INTR_HANDLE_VFIO_LEGACY:
634 if (vfio_disable_intx(intr_handle))
638 /* unknown handle type */
641 "Unknown handle type of fd %d\n",
650 eal_intr_process_interrupts(struct epoll_event *events, int nfds)
654 struct rte_intr_source *src;
655 struct rte_intr_callback *cb;
656 union rte_intr_read_buffer buf;
657 struct rte_intr_callback active_cb;
659 for (n = 0; n < nfds; n++) {
662 * if the pipe fd is ready to read, return out to
663 * rebuild the wait list.
665 if (events[n].data.fd == intr_pipe.readfd){
666 int r = read(intr_pipe.readfd, buf.charbuf,
667 sizeof(buf.charbuf));
671 rte_spinlock_lock(&intr_lock);
672 TAILQ_FOREACH(src, &intr_sources, next)
673 if (src->intr_handle.fd ==
677 rte_spinlock_unlock(&intr_lock);
681 /* mark this interrupt source as active and release the lock. */
683 rte_spinlock_unlock(&intr_lock);
685 /* set the length to be read dor different handle type */
686 switch (src->intr_handle.type) {
687 case RTE_INTR_HANDLE_UIO:
688 case RTE_INTR_HANDLE_UIO_INTX:
689 bytes_read = sizeof(buf.uio_intr_count);
691 case RTE_INTR_HANDLE_ALARM:
692 bytes_read = sizeof(buf.timerfd_num);
695 case RTE_INTR_HANDLE_VFIO_MSIX:
696 case RTE_INTR_HANDLE_VFIO_MSI:
697 case RTE_INTR_HANDLE_VFIO_LEGACY:
698 bytes_read = sizeof(buf.vfio_intr_count);
701 case RTE_INTR_HANDLE_VDEV:
702 case RTE_INTR_HANDLE_EXT:
712 if (bytes_read > 0) {
714 * read out to clear the ready-to-be-read flag
717 bytes_read = read(events[n].data.fd, &buf, bytes_read);
718 if (bytes_read < 0) {
719 if (errno == EINTR || errno == EWOULDBLOCK)
722 RTE_LOG(ERR, EAL, "Error reading from file "
723 "descriptor %d: %s\n",
726 } else if (bytes_read == 0)
727 RTE_LOG(ERR, EAL, "Read nothing from file "
728 "descriptor %d\n", events[n].data.fd);
733 /* grab a lock, again to call callbacks and update status. */
734 rte_spinlock_lock(&intr_lock);
738 /* Finally, call all callbacks. */
739 TAILQ_FOREACH(cb, &src->callbacks, next) {
741 /* make a copy and unlock. */
743 rte_spinlock_unlock(&intr_lock);
745 /* call the actual callback */
746 active_cb.cb_fn(active_cb.cb_arg);
748 /*get the lock back. */
749 rte_spinlock_lock(&intr_lock);
753 /* we done with that interrupt source, release it. */
755 rte_spinlock_unlock(&intr_lock);
762 * It handles all the interrupts.
765 * epoll file descriptor.
767 * The number of file descriptors added in epoll.
773 eal_intr_handle_interrupts(int pfd, unsigned totalfds)
775 struct epoll_event events[totalfds];
779 nfds = epoll_wait(pfd, events, totalfds,
780 EAL_INTR_EPOLL_WAIT_FOREVER);
781 /* epoll_wait fail */
786 "epoll_wait returns with fail\n");
789 /* epoll_wait timeout, will never happens here */
792 /* epoll_wait has at least one fd ready to read */
793 if (eal_intr_process_interrupts(events, nfds) < 0)
799 * It builds/rebuilds up the epoll file descriptor with all the
800 * file descriptors being waited on. Then handles the interrupts.
808 static __attribute__((noreturn)) void *
809 eal_intr_thread_main(__rte_unused void *arg)
811 struct epoll_event ev;
813 /* host thread, never break out */
815 /* build up the epoll fd with all descriptors we are to
816 * wait on then pass it to the handle_interrupts function
818 static struct epoll_event pipe_event = {
819 .events = EPOLLIN | EPOLLPRI,
821 struct rte_intr_source *src;
824 /* create epoll fd */
825 int pfd = epoll_create(1);
827 rte_panic("Cannot create epoll instance\n");
829 pipe_event.data.fd = intr_pipe.readfd;
831 * add pipe fd into wait list, this pipe is used to
832 * rebuild the wait list.
834 if (epoll_ctl(pfd, EPOLL_CTL_ADD, intr_pipe.readfd,
836 rte_panic("Error adding fd to %d epoll_ctl, %s\n",
837 intr_pipe.readfd, strerror(errno));
841 rte_spinlock_lock(&intr_lock);
843 TAILQ_FOREACH(src, &intr_sources, next) {
844 if (src->callbacks.tqh_first == NULL)
845 continue; /* skip those with no callbacks */
846 ev.events = EPOLLIN | EPOLLPRI | EPOLLRDHUP | EPOLLHUP;
847 ev.data.fd = src->intr_handle.fd;
850 * add all the uio device file descriptor
853 if (epoll_ctl(pfd, EPOLL_CTL_ADD,
854 src->intr_handle.fd, &ev) < 0){
855 rte_panic("Error adding fd %d epoll_ctl, %s\n",
856 src->intr_handle.fd, strerror(errno));
861 rte_spinlock_unlock(&intr_lock);
862 /* serve the interrupt */
863 eal_intr_handle_interrupts(pfd, numfds);
866 * when we return, we need to rebuild the
867 * list of fds to monitor.
874 rte_eal_intr_init(void)
876 int ret = 0, ret_1 = 0;
877 char thread_name[RTE_MAX_THREAD_NAME_LEN];
879 /* init the global interrupt source head */
880 TAILQ_INIT(&intr_sources);
883 * create a pipe which will be waited by epoll and notified to
884 * rebuild the wait list of epoll.
886 if (pipe(intr_pipe.pipefd) < 0) {
891 /* create the host thread to wait/handle the interrupt */
892 ret = pthread_create(&intr_thread, NULL,
893 eal_intr_thread_main, NULL);
897 "Failed to create thread for interrupt handling\n");
899 /* Set thread_name for aid in debugging. */
900 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN,
902 ret_1 = rte_thread_setname(intr_thread, thread_name);
905 "Failed to set thread name for interrupt handling\n");
912 eal_intr_proc_rxtx_intr(int fd, const struct rte_intr_handle *intr_handle)
914 union rte_intr_read_buffer buf;
918 switch (intr_handle->type) {
919 case RTE_INTR_HANDLE_UIO:
920 case RTE_INTR_HANDLE_UIO_INTX:
921 bytes_read = sizeof(buf.uio_intr_count);
924 case RTE_INTR_HANDLE_VFIO_MSIX:
925 case RTE_INTR_HANDLE_VFIO_MSI:
926 case RTE_INTR_HANDLE_VFIO_LEGACY:
927 bytes_read = sizeof(buf.vfio_intr_count);
930 case RTE_INTR_HANDLE_VDEV:
931 /* for vdev, fd points to:
932 * a. eventfd which does not need to read out;
933 * b. datapath fd which needs PMD to read out.
936 case RTE_INTR_HANDLE_EXT:
940 RTE_LOG(INFO, EAL, "unexpected intr type\n");
945 * read out to clear the ready-to-be-read flag
949 nbytes = read(fd, &buf, bytes_read);
951 if (errno == EINTR || errno == EWOULDBLOCK ||
955 "Error reading from fd %d: %s\n",
956 fd, strerror(errno));
957 } else if (nbytes == 0)
958 RTE_LOG(ERR, EAL, "Read nothing from fd %d\n", fd);
964 eal_epoll_process_event(struct epoll_event *evs, unsigned int n,
965 struct rte_epoll_event *events)
967 unsigned int i, count = 0;
968 struct rte_epoll_event *rev;
970 for (i = 0; i < n; i++) {
971 rev = evs[i].data.ptr;
972 if (!rev || !rte_atomic32_cmpset(&rev->status, RTE_EPOLL_VALID,
976 events[count].status = RTE_EPOLL_VALID;
977 events[count].fd = rev->fd;
978 events[count].epfd = rev->epfd;
979 events[count].epdata.event = rev->epdata.event;
980 events[count].epdata.data = rev->epdata.data;
981 if (rev->epdata.cb_fun)
982 rev->epdata.cb_fun(rev->fd,
985 rte_compiler_barrier();
986 rev->status = RTE_EPOLL_VALID;
993 eal_init_tls_epfd(void)
995 int pfd = epoll_create(255);
999 "Cannot create epoll instance\n");
1006 rte_intr_tls_epfd(void)
1008 if (RTE_PER_LCORE(_epfd) == -1)
1009 RTE_PER_LCORE(_epfd) = eal_init_tls_epfd();
1011 return RTE_PER_LCORE(_epfd);
1015 rte_epoll_wait(int epfd, struct rte_epoll_event *events,
1016 int maxevents, int timeout)
1018 struct epoll_event evs[maxevents];
1022 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1026 /* using per thread epoll fd */
1027 if (epfd == RTE_EPOLL_PER_THREAD)
1028 epfd = rte_intr_tls_epfd();
1031 rc = epoll_wait(epfd, evs, maxevents, timeout);
1032 if (likely(rc > 0)) {
1033 /* epoll_wait has at least one fd ready to read */
1034 rc = eal_epoll_process_event(evs, rc, events);
1036 } else if (rc < 0) {
1039 /* epoll_wait fail */
1040 RTE_LOG(ERR, EAL, "epoll_wait returns with fail %s\n",
1045 /* rc == 0, epoll_wait timed out */
1054 eal_epoll_data_safe_free(struct rte_epoll_event *ev)
1056 while (!rte_atomic32_cmpset(&ev->status, RTE_EPOLL_VALID,
1058 while (ev->status != RTE_EPOLL_VALID)
1060 memset(&ev->epdata, 0, sizeof(ev->epdata));
1066 rte_epoll_ctl(int epfd, int op, int fd,
1067 struct rte_epoll_event *event)
1069 struct epoll_event ev;
1072 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1076 /* using per thread epoll fd */
1077 if (epfd == RTE_EPOLL_PER_THREAD)
1078 epfd = rte_intr_tls_epfd();
1080 if (op == EPOLL_CTL_ADD) {
1081 event->status = RTE_EPOLL_VALID;
1082 event->fd = fd; /* ignore fd in event */
1084 ev.data.ptr = (void *)event;
1087 ev.events = event->epdata.event;
1088 if (epoll_ctl(epfd, op, fd, &ev) < 0) {
1089 RTE_LOG(ERR, EAL, "Error op %d fd %d epoll_ctl, %s\n",
1090 op, fd, strerror(errno));
1091 if (op == EPOLL_CTL_ADD)
1092 /* rollback status when CTL_ADD fail */
1093 event->status = RTE_EPOLL_INVALID;
1097 if (op == EPOLL_CTL_DEL && event->status != RTE_EPOLL_INVALID)
1098 eal_epoll_data_safe_free(event);
1104 rte_intr_rx_ctl(struct rte_intr_handle *intr_handle, int epfd,
1105 int op, unsigned int vec, void *data)
1107 struct rte_epoll_event *rev;
1108 struct rte_epoll_data *epdata;
1110 unsigned int efd_idx;
1113 efd_idx = (vec >= RTE_INTR_VEC_RXTX_OFFSET) ?
1114 (vec - RTE_INTR_VEC_RXTX_OFFSET) : vec;
1116 if (!intr_handle || intr_handle->nb_efd == 0 ||
1117 efd_idx >= intr_handle->nb_efd) {
1118 RTE_LOG(ERR, EAL, "Wrong intr vector number.\n");
1123 case RTE_INTR_EVENT_ADD:
1124 epfd_op = EPOLL_CTL_ADD;
1125 rev = &intr_handle->elist[efd_idx];
1126 if (rev->status != RTE_EPOLL_INVALID) {
1127 RTE_LOG(INFO, EAL, "Event already been added.\n");
1131 /* attach to intr vector fd */
1132 epdata = &rev->epdata;
1133 epdata->event = EPOLLIN | EPOLLPRI | EPOLLET;
1134 epdata->data = data;
1135 epdata->cb_fun = (rte_intr_event_cb_t)eal_intr_proc_rxtx_intr;
1136 epdata->cb_arg = (void *)intr_handle;
1137 rc = rte_epoll_ctl(epfd, epfd_op,
1138 intr_handle->efds[efd_idx], rev);
1141 "efd %d associated with vec %d added on epfd %d"
1142 "\n", rev->fd, vec, epfd);
1146 case RTE_INTR_EVENT_DEL:
1147 epfd_op = EPOLL_CTL_DEL;
1148 rev = &intr_handle->elist[efd_idx];
1149 if (rev->status == RTE_EPOLL_INVALID) {
1150 RTE_LOG(INFO, EAL, "Event does not exist.\n");
1154 rc = rte_epoll_ctl(rev->epfd, epfd_op, rev->fd, rev);
1159 RTE_LOG(ERR, EAL, "event op type mismatch\n");
1167 rte_intr_free_epoll_fd(struct rte_intr_handle *intr_handle)
1170 struct rte_epoll_event *rev;
1172 for (i = 0; i < intr_handle->nb_efd; i++) {
1173 rev = &intr_handle->elist[i];
1174 if (rev->status == RTE_EPOLL_INVALID)
1176 if (rte_epoll_ctl(rev->epfd, EPOLL_CTL_DEL, rev->fd, rev)) {
1177 /* force free if the entry valid */
1178 eal_epoll_data_safe_free(rev);
1179 rev->status = RTE_EPOLL_INVALID;
1185 rte_intr_efd_enable(struct rte_intr_handle *intr_handle, uint32_t nb_efd)
1189 uint32_t n = RTE_MIN(nb_efd, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
1191 assert(nb_efd != 0);
1193 if (intr_handle->type == RTE_INTR_HANDLE_VFIO_MSIX) {
1194 for (i = 0; i < n; i++) {
1195 fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
1198 "can't setup eventfd, error %i (%s)\n",
1199 errno, strerror(errno));
1202 intr_handle->efds[i] = fd;
1204 intr_handle->nb_efd = n;
1205 intr_handle->max_intr = NB_OTHER_INTR + n;
1206 } else if (intr_handle->type == RTE_INTR_HANDLE_VDEV) {
1207 /* do nothing, and let vdev driver to initialize this struct */
1209 intr_handle->efds[0] = intr_handle->fd;
1210 intr_handle->nb_efd = RTE_MIN(nb_efd, 1U);
1211 intr_handle->max_intr = NB_OTHER_INTR;
1218 rte_intr_efd_disable(struct rte_intr_handle *intr_handle)
1222 rte_intr_free_epoll_fd(intr_handle);
1223 if (intr_handle->max_intr > intr_handle->nb_efd) {
1224 for (i = 0; i < intr_handle->nb_efd; i++)
1225 close(intr_handle->efds[i]);
1227 intr_handle->nb_efd = 0;
1228 intr_handle->max_intr = 0;
1232 rte_intr_dp_is_en(struct rte_intr_handle *intr_handle)
1234 return !(!intr_handle->nb_efd);
1238 rte_intr_allow_others(struct rte_intr_handle *intr_handle)
1240 if (!rte_intr_dp_is_en(intr_handle))
1243 return !!(intr_handle->max_intr - intr_handle->nb_efd);
1247 rte_intr_cap_multiple(struct rte_intr_handle *intr_handle)
1249 if (intr_handle->type == RTE_INTR_HANDLE_VFIO_MSIX)
1252 if (intr_handle->type == RTE_INTR_HANDLE_VDEV)