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_memzone.h>
55 #include <rte_launch.h>
57 #include <rte_per_lcore.h>
58 #include <rte_lcore.h>
59 #include <rte_atomic.h>
60 #include <rte_branch_prediction.h>
61 #include <rte_debug.h>
63 #include <rte_malloc.h>
64 #include <rte_errno.h>
65 #include <rte_spinlock.h>
66 #include <rte_pause.h>
69 #include "eal_private.h"
71 #include "eal_thread.h"
73 #define EAL_INTR_EPOLL_WAIT_FOREVER (-1)
74 #define NB_OTHER_INTR 1
76 static RTE_DEFINE_PER_LCORE(int, _epfd) = -1; /**< epoll fd per thread */
92 * union buffer for reading on different devices
94 union rte_intr_read_buffer {
95 int uio_intr_count; /* for uio device */
97 uint64_t vfio_intr_count; /* for vfio device */
99 uint64_t timerfd_num; /* for timerfd */
100 char charbuf[16]; /* for others */
103 TAILQ_HEAD(rte_intr_cb_list, rte_intr_callback);
104 TAILQ_HEAD(rte_intr_source_list, rte_intr_source);
106 struct rte_intr_callback {
107 TAILQ_ENTRY(rte_intr_callback) next;
108 rte_intr_callback_fn cb_fn; /**< callback address */
109 void *cb_arg; /**< parameter for callback */
112 struct rte_intr_source {
113 TAILQ_ENTRY(rte_intr_source) next;
114 struct rte_intr_handle intr_handle; /**< interrupt handle */
115 struct rte_intr_cb_list callbacks; /**< user callbacks */
119 /* global spinlock for interrupt data operation */
120 static rte_spinlock_t intr_lock = RTE_SPINLOCK_INITIALIZER;
122 /* union buffer for pipe read/write */
123 static union intr_pipefds intr_pipe;
125 /* interrupt sources list */
126 static struct rte_intr_source_list intr_sources;
128 /* interrupt handling thread */
129 static pthread_t intr_thread;
131 /* VFIO interrupts */
134 #define IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + sizeof(int))
135 /* irq set buffer length for queue interrupts and LSC interrupt */
136 #define MSIX_IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + \
137 sizeof(int) * (RTE_MAX_RXTX_INTR_VEC_ID + 1))
139 /* enable legacy (INTx) interrupts */
141 vfio_enable_intx(const struct rte_intr_handle *intr_handle) {
142 struct vfio_irq_set *irq_set;
143 char irq_set_buf[IRQ_SET_BUF_LEN];
147 len = sizeof(irq_set_buf);
150 irq_set = (struct vfio_irq_set *) irq_set_buf;
151 irq_set->argsz = len;
153 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
154 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
156 fd_ptr = (int *) &irq_set->data;
157 *fd_ptr = intr_handle->fd;
159 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
162 RTE_LOG(ERR, EAL, "Error enabling INTx interrupts for fd %d\n",
167 /* unmask INTx after enabling */
168 memset(irq_set, 0, len);
169 len = sizeof(struct vfio_irq_set);
170 irq_set->argsz = len;
172 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK;
173 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
176 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
179 RTE_LOG(ERR, EAL, "Error unmasking INTx interrupts for fd %d\n",
186 /* disable legacy (INTx) interrupts */
188 vfio_disable_intx(const struct rte_intr_handle *intr_handle) {
189 struct vfio_irq_set *irq_set;
190 char irq_set_buf[IRQ_SET_BUF_LEN];
193 len = sizeof(struct vfio_irq_set);
195 /* mask interrupts before disabling */
196 irq_set = (struct vfio_irq_set *) irq_set_buf;
197 irq_set->argsz = len;
199 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_MASK;
200 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
203 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
206 RTE_LOG(ERR, EAL, "Error masking INTx interrupts for fd %d\n",
212 memset(irq_set, 0, len);
213 irq_set->argsz = len;
215 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
216 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
219 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
223 "Error disabling INTx interrupts for fd %d\n", intr_handle->fd);
229 /* enable MSI interrupts */
231 vfio_enable_msi(const struct rte_intr_handle *intr_handle) {
233 char irq_set_buf[IRQ_SET_BUF_LEN];
234 struct vfio_irq_set *irq_set;
237 len = sizeof(irq_set_buf);
239 irq_set = (struct vfio_irq_set *) irq_set_buf;
240 irq_set->argsz = len;
242 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
243 irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
245 fd_ptr = (int *) &irq_set->data;
246 *fd_ptr = intr_handle->fd;
248 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
251 RTE_LOG(ERR, EAL, "Error enabling MSI interrupts for fd %d\n",
258 /* disable MSI interrupts */
260 vfio_disable_msi(const struct rte_intr_handle *intr_handle) {
261 struct vfio_irq_set *irq_set;
262 char irq_set_buf[IRQ_SET_BUF_LEN];
265 len = sizeof(struct vfio_irq_set);
267 irq_set = (struct vfio_irq_set *) irq_set_buf;
268 irq_set->argsz = len;
270 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
271 irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
274 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
278 "Error disabling MSI interrupts for fd %d\n", intr_handle->fd);
283 /* enable MSI-X interrupts */
285 vfio_enable_msix(const struct rte_intr_handle *intr_handle) {
287 char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
288 struct vfio_irq_set *irq_set;
291 len = sizeof(irq_set_buf);
293 irq_set = (struct vfio_irq_set *) irq_set_buf;
294 irq_set->argsz = len;
295 /* 0 < irq_set->count < RTE_MAX_RXTX_INTR_VEC_ID + 1 */
296 irq_set->count = intr_handle->max_intr ?
297 (intr_handle->max_intr > RTE_MAX_RXTX_INTR_VEC_ID + 1 ?
298 RTE_MAX_RXTX_INTR_VEC_ID + 1 : intr_handle->max_intr) : 1;
299 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
300 irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
302 fd_ptr = (int *) &irq_set->data;
303 /* INTR vector offset 0 reserve for non-efds mapping */
304 fd_ptr[RTE_INTR_VEC_ZERO_OFFSET] = intr_handle->fd;
305 memcpy(&fd_ptr[RTE_INTR_VEC_RXTX_OFFSET], intr_handle->efds,
306 sizeof(*intr_handle->efds) * intr_handle->nb_efd);
308 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
311 RTE_LOG(ERR, EAL, "Error enabling MSI-X interrupts for fd %d\n",
319 /* disable MSI-X interrupts */
321 vfio_disable_msix(const struct rte_intr_handle *intr_handle) {
322 struct vfio_irq_set *irq_set;
323 char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
326 len = sizeof(struct vfio_irq_set);
328 irq_set = (struct vfio_irq_set *) irq_set_buf;
329 irq_set->argsz = len;
331 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
332 irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
335 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
339 "Error disabling MSI-X interrupts for fd %d\n", intr_handle->fd);
346 uio_intx_intr_disable(const struct rte_intr_handle *intr_handle)
348 unsigned char command_high;
350 /* use UIO config file descriptor for uio_pci_generic */
351 if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
353 "Error reading interrupts status for fd %d\n",
354 intr_handle->uio_cfg_fd);
357 /* disable interrupts */
359 if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
361 "Error disabling interrupts for fd %d\n",
362 intr_handle->uio_cfg_fd);
370 uio_intx_intr_enable(const struct rte_intr_handle *intr_handle)
372 unsigned char command_high;
374 /* use UIO config file descriptor for uio_pci_generic */
375 if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
377 "Error reading interrupts status for fd %d\n",
378 intr_handle->uio_cfg_fd);
381 /* enable interrupts */
382 command_high &= ~0x4;
383 if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
385 "Error enabling interrupts for fd %d\n",
386 intr_handle->uio_cfg_fd);
394 uio_intr_disable(const struct rte_intr_handle *intr_handle)
398 if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
400 "Error disabling interrupts for fd %d (%s)\n",
401 intr_handle->fd, strerror(errno));
408 uio_intr_enable(const struct rte_intr_handle *intr_handle)
412 if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
414 "Error enabling interrupts for fd %d (%s)\n",
415 intr_handle->fd, strerror(errno));
422 rte_intr_callback_register(const struct rte_intr_handle *intr_handle,
423 rte_intr_callback_fn cb, void *cb_arg)
425 int ret, wake_thread;
426 struct rte_intr_source *src;
427 struct rte_intr_callback *callback;
431 /* first do parameter checking */
432 if (intr_handle == NULL || intr_handle->fd < 0 || cb == NULL) {
434 "Registering with invalid input parameter\n");
438 /* allocate a new interrupt callback entity */
439 callback = rte_zmalloc("interrupt callback list",
440 sizeof(*callback), 0);
441 if (callback == NULL) {
442 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
445 callback->cb_fn = cb;
446 callback->cb_arg = cb_arg;
448 rte_spinlock_lock(&intr_lock);
450 /* check if there is at least one callback registered for the fd */
451 TAILQ_FOREACH(src, &intr_sources, next) {
452 if (src->intr_handle.fd == intr_handle->fd) {
453 /* we had no interrupts for this */
454 if TAILQ_EMPTY(&src->callbacks)
457 TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
463 /* no existing callbacks for this - add new source */
465 if ((src = rte_zmalloc("interrupt source list",
466 sizeof(*src), 0)) == NULL) {
467 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
471 src->intr_handle = *intr_handle;
472 TAILQ_INIT(&src->callbacks);
473 TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
474 TAILQ_INSERT_TAIL(&intr_sources, src, next);
480 rte_spinlock_unlock(&intr_lock);
483 * check if need to notify the pipe fd waited by epoll_wait to
484 * rebuild the wait list.
487 if (write(intr_pipe.writefd, "1", 1) < 0)
494 rte_intr_callback_unregister(const struct rte_intr_handle *intr_handle,
495 rte_intr_callback_fn cb_fn, void *cb_arg)
498 struct rte_intr_source *src;
499 struct rte_intr_callback *cb, *next;
501 /* do parameter checking first */
502 if (intr_handle == NULL || intr_handle->fd < 0) {
504 "Unregistering with invalid input parameter\n");
508 rte_spinlock_lock(&intr_lock);
510 /* check if the insterrupt source for the fd is existent */
511 TAILQ_FOREACH(src, &intr_sources, next)
512 if (src->intr_handle.fd == intr_handle->fd)
515 /* No interrupt source registered for the fd */
519 /* interrupt source has some active callbacks right now. */
520 } else if (src->active != 0) {
527 /*walk through the callbacks and remove all that match. */
528 for (cb = TAILQ_FIRST(&src->callbacks); cb != NULL; cb = next) {
530 next = TAILQ_NEXT(cb, next);
532 if (cb->cb_fn == cb_fn && (cb_arg == (void *)-1 ||
533 cb->cb_arg == cb_arg)) {
534 TAILQ_REMOVE(&src->callbacks, cb, next);
540 /* all callbacks for that source are removed. */
541 if (TAILQ_EMPTY(&src->callbacks)) {
542 TAILQ_REMOVE(&intr_sources, src, next);
547 rte_spinlock_unlock(&intr_lock);
549 /* notify the pipe fd waited by epoll_wait to rebuild the wait list */
550 if (ret >= 0 && write(intr_pipe.writefd, "1", 1) < 0) {
558 rte_intr_enable(const struct rte_intr_handle *intr_handle)
560 if (intr_handle && intr_handle->type == RTE_INTR_HANDLE_VDEV)
563 if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
566 switch (intr_handle->type){
567 /* write to the uio fd to enable the interrupt */
568 case RTE_INTR_HANDLE_UIO:
569 if (uio_intr_enable(intr_handle))
572 case RTE_INTR_HANDLE_UIO_INTX:
573 if (uio_intx_intr_enable(intr_handle))
576 /* not used at this moment */
577 case RTE_INTR_HANDLE_ALARM:
580 case RTE_INTR_HANDLE_VFIO_MSIX:
581 if (vfio_enable_msix(intr_handle))
584 case RTE_INTR_HANDLE_VFIO_MSI:
585 if (vfio_enable_msi(intr_handle))
588 case RTE_INTR_HANDLE_VFIO_LEGACY:
589 if (vfio_enable_intx(intr_handle))
593 /* unknown handle type */
596 "Unknown handle type of fd %d\n",
605 rte_intr_disable(const struct rte_intr_handle *intr_handle)
607 if (intr_handle && intr_handle->type == RTE_INTR_HANDLE_VDEV)
610 if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
613 switch (intr_handle->type){
614 /* write to the uio fd to disable the interrupt */
615 case RTE_INTR_HANDLE_UIO:
616 if (uio_intr_disable(intr_handle))
619 case RTE_INTR_HANDLE_UIO_INTX:
620 if (uio_intx_intr_disable(intr_handle))
623 /* not used at this moment */
624 case RTE_INTR_HANDLE_ALARM:
627 case RTE_INTR_HANDLE_VFIO_MSIX:
628 if (vfio_disable_msix(intr_handle))
631 case RTE_INTR_HANDLE_VFIO_MSI:
632 if (vfio_disable_msi(intr_handle))
635 case RTE_INTR_HANDLE_VFIO_LEGACY:
636 if (vfio_disable_intx(intr_handle))
640 /* unknown handle type */
643 "Unknown handle type of fd %d\n",
652 eal_intr_process_interrupts(struct epoll_event *events, int nfds)
656 struct rte_intr_source *src;
657 struct rte_intr_callback *cb;
658 union rte_intr_read_buffer buf;
659 struct rte_intr_callback active_cb;
661 for (n = 0; n < nfds; n++) {
664 * if the pipe fd is ready to read, return out to
665 * rebuild the wait list.
667 if (events[n].data.fd == intr_pipe.readfd){
668 int r = read(intr_pipe.readfd, buf.charbuf,
669 sizeof(buf.charbuf));
673 rte_spinlock_lock(&intr_lock);
674 TAILQ_FOREACH(src, &intr_sources, next)
675 if (src->intr_handle.fd ==
679 rte_spinlock_unlock(&intr_lock);
683 /* mark this interrupt source as active and release the lock. */
685 rte_spinlock_unlock(&intr_lock);
687 /* set the length to be read dor different handle type */
688 switch (src->intr_handle.type) {
689 case RTE_INTR_HANDLE_UIO:
690 case RTE_INTR_HANDLE_UIO_INTX:
691 bytes_read = sizeof(buf.uio_intr_count);
693 case RTE_INTR_HANDLE_ALARM:
694 bytes_read = sizeof(buf.timerfd_num);
697 case RTE_INTR_HANDLE_VFIO_MSIX:
698 case RTE_INTR_HANDLE_VFIO_MSI:
699 case RTE_INTR_HANDLE_VFIO_LEGACY:
700 bytes_read = sizeof(buf.vfio_intr_count);
703 case RTE_INTR_HANDLE_VDEV:
704 case RTE_INTR_HANDLE_EXT:
714 if (bytes_read > 0) {
716 * read out to clear the ready-to-be-read flag
719 bytes_read = read(events[n].data.fd, &buf, bytes_read);
720 if (bytes_read < 0) {
721 if (errno == EINTR || errno == EWOULDBLOCK)
724 RTE_LOG(ERR, EAL, "Error reading from file "
725 "descriptor %d: %s\n",
728 } else if (bytes_read == 0)
729 RTE_LOG(ERR, EAL, "Read nothing from file "
730 "descriptor %d\n", events[n].data.fd);
735 /* grab a lock, again to call callbacks and update status. */
736 rte_spinlock_lock(&intr_lock);
740 /* Finally, call all callbacks. */
741 TAILQ_FOREACH(cb, &src->callbacks, next) {
743 /* make a copy and unlock. */
745 rte_spinlock_unlock(&intr_lock);
747 /* call the actual callback */
748 active_cb.cb_fn(active_cb.cb_arg);
750 /*get the lock back. */
751 rte_spinlock_lock(&intr_lock);
755 /* we done with that interrupt source, release it. */
757 rte_spinlock_unlock(&intr_lock);
764 * It handles all the interrupts.
767 * epoll file descriptor.
769 * The number of file descriptors added in epoll.
775 eal_intr_handle_interrupts(int pfd, unsigned totalfds)
777 struct epoll_event events[totalfds];
781 nfds = epoll_wait(pfd, events, totalfds,
782 EAL_INTR_EPOLL_WAIT_FOREVER);
783 /* epoll_wait fail */
788 "epoll_wait returns with fail\n");
791 /* epoll_wait timeout, will never happens here */
794 /* epoll_wait has at least one fd ready to read */
795 if (eal_intr_process_interrupts(events, nfds) < 0)
801 * It builds/rebuilds up the epoll file descriptor with all the
802 * file descriptors being waited on. Then handles the interrupts.
810 static __attribute__((noreturn)) void *
811 eal_intr_thread_main(__rte_unused void *arg)
813 struct epoll_event ev;
815 /* host thread, never break out */
817 /* build up the epoll fd with all descriptors we are to
818 * wait on then pass it to the handle_interrupts function
820 static struct epoll_event pipe_event = {
821 .events = EPOLLIN | EPOLLPRI,
823 struct rte_intr_source *src;
826 /* create epoll fd */
827 int pfd = epoll_create(1);
829 rte_panic("Cannot create epoll instance\n");
831 pipe_event.data.fd = intr_pipe.readfd;
833 * add pipe fd into wait list, this pipe is used to
834 * rebuild the wait list.
836 if (epoll_ctl(pfd, EPOLL_CTL_ADD, intr_pipe.readfd,
838 rte_panic("Error adding fd to %d epoll_ctl, %s\n",
839 intr_pipe.readfd, strerror(errno));
843 rte_spinlock_lock(&intr_lock);
845 TAILQ_FOREACH(src, &intr_sources, next) {
846 if (src->callbacks.tqh_first == NULL)
847 continue; /* skip those with no callbacks */
848 ev.events = EPOLLIN | EPOLLPRI | EPOLLRDHUP | EPOLLHUP;
849 ev.data.fd = src->intr_handle.fd;
852 * add all the uio device file descriptor
855 if (epoll_ctl(pfd, EPOLL_CTL_ADD,
856 src->intr_handle.fd, &ev) < 0){
857 rte_panic("Error adding fd %d epoll_ctl, %s\n",
858 src->intr_handle.fd, strerror(errno));
863 rte_spinlock_unlock(&intr_lock);
864 /* serve the interrupt */
865 eal_intr_handle_interrupts(pfd, numfds);
868 * when we return, we need to rebuild the
869 * list of fds to monitor.
876 rte_eal_intr_init(void)
878 int ret = 0, ret_1 = 0;
879 char thread_name[RTE_MAX_THREAD_NAME_LEN];
881 /* init the global interrupt source head */
882 TAILQ_INIT(&intr_sources);
885 * create a pipe which will be waited by epoll and notified to
886 * rebuild the wait list of epoll.
888 if (pipe(intr_pipe.pipefd) < 0) {
893 /* create the host thread to wait/handle the interrupt */
894 ret = pthread_create(&intr_thread, NULL,
895 eal_intr_thread_main, NULL);
899 "Failed to create thread for interrupt handling\n");
901 /* Set thread_name for aid in debugging. */
902 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN,
904 ret_1 = rte_thread_setname(intr_thread, thread_name);
907 "Failed to set thread name for interrupt handling\n");
914 eal_intr_proc_rxtx_intr(int fd, const struct rte_intr_handle *intr_handle)
916 union rte_intr_read_buffer buf;
920 switch (intr_handle->type) {
921 case RTE_INTR_HANDLE_UIO:
922 case RTE_INTR_HANDLE_UIO_INTX:
923 bytes_read = sizeof(buf.uio_intr_count);
926 case RTE_INTR_HANDLE_VFIO_MSIX:
927 case RTE_INTR_HANDLE_VFIO_MSI:
928 case RTE_INTR_HANDLE_VFIO_LEGACY:
929 bytes_read = sizeof(buf.vfio_intr_count);
932 case RTE_INTR_HANDLE_VDEV:
933 /* for vdev, fd points to:
934 * a. eventfd which does not need to read out;
935 * b. datapath fd which needs PMD to read out.
938 case RTE_INTR_HANDLE_EXT:
942 RTE_LOG(INFO, EAL, "unexpected intr type\n");
947 * read out to clear the ready-to-be-read flag
951 nbytes = read(fd, &buf, bytes_read);
953 if (errno == EINTR || errno == EWOULDBLOCK ||
957 "Error reading from fd %d: %s\n",
958 fd, strerror(errno));
959 } else if (nbytes == 0)
960 RTE_LOG(ERR, EAL, "Read nothing from fd %d\n", fd);
966 eal_epoll_process_event(struct epoll_event *evs, unsigned int n,
967 struct rte_epoll_event *events)
969 unsigned int i, count = 0;
970 struct rte_epoll_event *rev;
972 for (i = 0; i < n; i++) {
973 rev = evs[i].data.ptr;
974 if (!rev || !rte_atomic32_cmpset(&rev->status, RTE_EPOLL_VALID,
978 events[count].status = RTE_EPOLL_VALID;
979 events[count].fd = rev->fd;
980 events[count].epfd = rev->epfd;
981 events[count].epdata.event = rev->epdata.event;
982 events[count].epdata.data = rev->epdata.data;
983 if (rev->epdata.cb_fun)
984 rev->epdata.cb_fun(rev->fd,
987 rte_compiler_barrier();
988 rev->status = RTE_EPOLL_VALID;
995 eal_init_tls_epfd(void)
997 int pfd = epoll_create(255);
1001 "Cannot create epoll instance\n");
1008 rte_intr_tls_epfd(void)
1010 if (RTE_PER_LCORE(_epfd) == -1)
1011 RTE_PER_LCORE(_epfd) = eal_init_tls_epfd();
1013 return RTE_PER_LCORE(_epfd);
1017 rte_epoll_wait(int epfd, struct rte_epoll_event *events,
1018 int maxevents, int timeout)
1020 struct epoll_event evs[maxevents];
1024 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1028 /* using per thread epoll fd */
1029 if (epfd == RTE_EPOLL_PER_THREAD)
1030 epfd = rte_intr_tls_epfd();
1033 rc = epoll_wait(epfd, evs, maxevents, timeout);
1034 if (likely(rc > 0)) {
1035 /* epoll_wait has at least one fd ready to read */
1036 rc = eal_epoll_process_event(evs, rc, events);
1038 } else if (rc < 0) {
1041 /* epoll_wait fail */
1042 RTE_LOG(ERR, EAL, "epoll_wait returns with fail %s\n",
1047 /* rc == 0, epoll_wait timed out */
1056 eal_epoll_data_safe_free(struct rte_epoll_event *ev)
1058 while (!rte_atomic32_cmpset(&ev->status, RTE_EPOLL_VALID,
1060 while (ev->status != RTE_EPOLL_VALID)
1062 memset(&ev->epdata, 0, sizeof(ev->epdata));
1068 rte_epoll_ctl(int epfd, int op, int fd,
1069 struct rte_epoll_event *event)
1071 struct epoll_event ev;
1074 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1078 /* using per thread epoll fd */
1079 if (epfd == RTE_EPOLL_PER_THREAD)
1080 epfd = rte_intr_tls_epfd();
1082 if (op == EPOLL_CTL_ADD) {
1083 event->status = RTE_EPOLL_VALID;
1084 event->fd = fd; /* ignore fd in event */
1086 ev.data.ptr = (void *)event;
1089 ev.events = event->epdata.event;
1090 if (epoll_ctl(epfd, op, fd, &ev) < 0) {
1091 RTE_LOG(ERR, EAL, "Error op %d fd %d epoll_ctl, %s\n",
1092 op, fd, strerror(errno));
1093 if (op == EPOLL_CTL_ADD)
1094 /* rollback status when CTL_ADD fail */
1095 event->status = RTE_EPOLL_INVALID;
1099 if (op == EPOLL_CTL_DEL && event->status != RTE_EPOLL_INVALID)
1100 eal_epoll_data_safe_free(event);
1106 rte_intr_rx_ctl(struct rte_intr_handle *intr_handle, int epfd,
1107 int op, unsigned int vec, void *data)
1109 struct rte_epoll_event *rev;
1110 struct rte_epoll_data *epdata;
1112 unsigned int efd_idx;
1115 efd_idx = (vec >= RTE_INTR_VEC_RXTX_OFFSET) ?
1116 (vec - RTE_INTR_VEC_RXTX_OFFSET) : vec;
1118 if (!intr_handle || intr_handle->nb_efd == 0 ||
1119 efd_idx >= intr_handle->nb_efd) {
1120 RTE_LOG(ERR, EAL, "Wrong intr vector number.\n");
1125 case RTE_INTR_EVENT_ADD:
1126 epfd_op = EPOLL_CTL_ADD;
1127 rev = &intr_handle->elist[efd_idx];
1128 if (rev->status != RTE_EPOLL_INVALID) {
1129 RTE_LOG(INFO, EAL, "Event already been added.\n");
1133 /* attach to intr vector fd */
1134 epdata = &rev->epdata;
1135 epdata->event = EPOLLIN | EPOLLPRI | EPOLLET;
1136 epdata->data = data;
1137 epdata->cb_fun = (rte_intr_event_cb_t)eal_intr_proc_rxtx_intr;
1138 epdata->cb_arg = (void *)intr_handle;
1139 rc = rte_epoll_ctl(epfd, epfd_op,
1140 intr_handle->efds[efd_idx], rev);
1143 "efd %d associated with vec %d added on epfd %d"
1144 "\n", rev->fd, vec, epfd);
1148 case RTE_INTR_EVENT_DEL:
1149 epfd_op = EPOLL_CTL_DEL;
1150 rev = &intr_handle->elist[efd_idx];
1151 if (rev->status == RTE_EPOLL_INVALID) {
1152 RTE_LOG(INFO, EAL, "Event does not exist.\n");
1156 rc = rte_epoll_ctl(rev->epfd, epfd_op, rev->fd, rev);
1161 RTE_LOG(ERR, EAL, "event op type mismatch\n");
1169 rte_intr_free_epoll_fd(struct rte_intr_handle *intr_handle)
1172 struct rte_epoll_event *rev;
1174 for (i = 0; i < intr_handle->nb_efd; i++) {
1175 rev = &intr_handle->elist[i];
1176 if (rev->status == RTE_EPOLL_INVALID)
1178 if (rte_epoll_ctl(rev->epfd, EPOLL_CTL_DEL, rev->fd, rev)) {
1179 /* force free if the entry valid */
1180 eal_epoll_data_safe_free(rev);
1181 rev->status = RTE_EPOLL_INVALID;
1187 rte_intr_efd_enable(struct rte_intr_handle *intr_handle, uint32_t nb_efd)
1191 uint32_t n = RTE_MIN(nb_efd, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
1193 assert(nb_efd != 0);
1195 if (intr_handle->type == RTE_INTR_HANDLE_VFIO_MSIX) {
1196 for (i = 0; i < n; i++) {
1197 fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
1200 "can't setup eventfd, error %i (%s)\n",
1201 errno, strerror(errno));
1204 intr_handle->efds[i] = fd;
1206 intr_handle->nb_efd = n;
1207 intr_handle->max_intr = NB_OTHER_INTR + n;
1208 } else if (intr_handle->type == RTE_INTR_HANDLE_VDEV) {
1209 /* do nothing, and let vdev driver to initialize this struct */
1211 intr_handle->efds[0] = intr_handle->fd;
1212 intr_handle->nb_efd = RTE_MIN(nb_efd, 1U);
1213 intr_handle->max_intr = NB_OTHER_INTR;
1220 rte_intr_efd_disable(struct rte_intr_handle *intr_handle)
1224 rte_intr_free_epoll_fd(intr_handle);
1225 if (intr_handle->max_intr > intr_handle->nb_efd) {
1226 for (i = 0; i < intr_handle->nb_efd; i++)
1227 close(intr_handle->efds[i]);
1229 intr_handle->nb_efd = 0;
1230 intr_handle->max_intr = 0;
1234 rte_intr_dp_is_en(struct rte_intr_handle *intr_handle)
1236 return !(!intr_handle->nb_efd);
1240 rte_intr_allow_others(struct rte_intr_handle *intr_handle)
1242 if (!rte_intr_dp_is_en(intr_handle))
1245 return !!(intr_handle->max_intr - intr_handle->nb_efd);
1249 rte_intr_cap_multiple(struct rte_intr_handle *intr_handle)
1251 if (intr_handle->type == RTE_INTR_HANDLE_VFIO_MSIX)
1254 if (intr_handle->type == RTE_INTR_HANDLE_VDEV)