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>
64 #include <rte_malloc.h>
65 #include <rte_errno.h>
66 #include <rte_spinlock.h>
68 #include "eal_private.h"
70 #include "eal_thread.h"
72 #define EAL_INTR_EPOLL_WAIT_FOREVER (-1)
73 #define NB_OTHER_INTR 1
75 static RTE_DEFINE_PER_LCORE(int, _epfd) = -1; /**< epoll fd per thread */
91 * union buffer for reading on different devices
93 union rte_intr_read_buffer {
94 int uio_intr_count; /* for uio device */
96 uint64_t vfio_intr_count; /* for vfio device */
98 uint64_t timerfd_num; /* for timerfd */
99 char charbuf[16]; /* for others */
102 TAILQ_HEAD(rte_intr_cb_list, rte_intr_callback);
103 TAILQ_HEAD(rte_intr_source_list, rte_intr_source);
105 struct rte_intr_callback {
106 TAILQ_ENTRY(rte_intr_callback) next;
107 rte_intr_callback_fn cb_fn; /**< callback address */
108 void *cb_arg; /**< parameter for callback */
111 struct rte_intr_source {
112 TAILQ_ENTRY(rte_intr_source) next;
113 struct rte_intr_handle intr_handle; /**< interrupt handle */
114 struct rte_intr_cb_list callbacks; /**< user callbacks */
118 /* global spinlock for interrupt data operation */
119 static rte_spinlock_t intr_lock = RTE_SPINLOCK_INITIALIZER;
121 /* union buffer for pipe read/write */
122 static union intr_pipefds intr_pipe;
124 /* interrupt sources list */
125 static struct rte_intr_source_list intr_sources;
127 /* interrupt handling thread */
128 static pthread_t intr_thread;
130 /* VFIO interrupts */
133 #define IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + sizeof(int))
134 /* irq set buffer length for queue interrupts and LSC interrupt */
135 #define MSIX_IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + \
136 sizeof(int) * (RTE_MAX_RXTX_INTR_VEC_ID + 1))
138 /* enable legacy (INTx) interrupts */
140 vfio_enable_intx(const struct rte_intr_handle *intr_handle) {
141 struct vfio_irq_set *irq_set;
142 char irq_set_buf[IRQ_SET_BUF_LEN];
146 len = sizeof(irq_set_buf);
149 irq_set = (struct vfio_irq_set *) irq_set_buf;
150 irq_set->argsz = len;
152 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
153 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
155 fd_ptr = (int *) &irq_set->data;
156 *fd_ptr = intr_handle->fd;
158 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
161 RTE_LOG(ERR, EAL, "Error enabling INTx interrupts for fd %d\n",
166 /* unmask INTx after enabling */
167 memset(irq_set, 0, len);
168 len = sizeof(struct vfio_irq_set);
169 irq_set->argsz = len;
171 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK;
172 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
175 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
178 RTE_LOG(ERR, EAL, "Error unmasking INTx interrupts for fd %d\n",
185 /* disable legacy (INTx) interrupts */
187 vfio_disable_intx(const struct rte_intr_handle *intr_handle) {
188 struct vfio_irq_set *irq_set;
189 char irq_set_buf[IRQ_SET_BUF_LEN];
192 len = sizeof(struct vfio_irq_set);
194 /* mask interrupts before disabling */
195 irq_set = (struct vfio_irq_set *) irq_set_buf;
196 irq_set->argsz = len;
198 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_MASK;
199 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
202 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
205 RTE_LOG(ERR, EAL, "Error masking INTx interrupts for fd %d\n",
211 memset(irq_set, 0, len);
212 irq_set->argsz = len;
214 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
215 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
218 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
222 "Error disabling INTx interrupts for fd %d\n", intr_handle->fd);
228 /* enable MSI interrupts */
230 vfio_enable_msi(const struct rte_intr_handle *intr_handle) {
232 char irq_set_buf[IRQ_SET_BUF_LEN];
233 struct vfio_irq_set *irq_set;
236 len = sizeof(irq_set_buf);
238 irq_set = (struct vfio_irq_set *) irq_set_buf;
239 irq_set->argsz = len;
241 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
242 irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
244 fd_ptr = (int *) &irq_set->data;
245 *fd_ptr = intr_handle->fd;
247 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
250 RTE_LOG(ERR, EAL, "Error enabling MSI interrupts for fd %d\n",
257 /* disable MSI interrupts */
259 vfio_disable_msi(const struct rte_intr_handle *intr_handle) {
260 struct vfio_irq_set *irq_set;
261 char irq_set_buf[IRQ_SET_BUF_LEN];
264 len = sizeof(struct vfio_irq_set);
266 irq_set = (struct vfio_irq_set *) irq_set_buf;
267 irq_set->argsz = len;
269 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
270 irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
273 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
277 "Error disabling MSI interrupts for fd %d\n", intr_handle->fd);
283 get_max_intr(const struct rte_intr_handle *intr_handle)
285 struct rte_intr_source *src;
287 TAILQ_FOREACH(src, &intr_sources, next) {
288 if (src->intr_handle.fd != intr_handle->fd)
291 if (src->intr_handle.max_intr < intr_handle->max_intr)
292 src->intr_handle.max_intr = intr_handle->max_intr;
293 if (!src->intr_handle.max_intr)
294 src->intr_handle.max_intr = 1;
295 else if (src->intr_handle.max_intr > RTE_MAX_RXTX_INTR_VEC_ID)
296 src->intr_handle.max_intr
297 = RTE_MAX_RXTX_INTR_VEC_ID + 1;
299 return src->intr_handle.max_intr;
305 /* enable MSI-X interrupts */
307 vfio_enable_msix(const struct rte_intr_handle *intr_handle) {
309 char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
310 struct vfio_irq_set *irq_set;
313 len = sizeof(irq_set_buf);
315 irq_set = (struct vfio_irq_set *) irq_set_buf;
316 irq_set->argsz = len;
318 ret = get_max_intr(intr_handle);
320 RTE_LOG(ERR, EAL, "Invalid number of MSI-X irqs for fd %d\n",
325 irq_set->count = ret;
326 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
327 irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
329 fd_ptr = (int *) &irq_set->data;
330 /* INTR vector offset 0 reserve for non-efds mapping */
331 fd_ptr[RTE_INTR_VEC_ZERO_OFFSET] = intr_handle->fd;
332 memcpy(&fd_ptr[RTE_INTR_VEC_RXTX_OFFSET], intr_handle->efds,
333 sizeof(*intr_handle->efds) * intr_handle->nb_efd);
335 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
338 RTE_LOG(ERR, EAL, "Error enabling MSI-X interrupts for fd %d\n",
346 /* disable MSI-X interrupts */
348 vfio_disable_msix(const struct rte_intr_handle *intr_handle) {
349 struct vfio_irq_set *irq_set;
350 char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
353 len = sizeof(struct vfio_irq_set);
355 irq_set = (struct vfio_irq_set *) irq_set_buf;
356 irq_set->argsz = len;
358 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
359 irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
362 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
366 "Error disabling MSI-X interrupts for fd %d\n", intr_handle->fd);
373 uio_intx_intr_disable(const struct rte_intr_handle *intr_handle)
375 unsigned char command_high;
377 /* use UIO config file descriptor for uio_pci_generic */
378 if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
380 "Error reading interrupts status for fd %d\n",
381 intr_handle->uio_cfg_fd);
384 /* disable interrupts */
386 if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
388 "Error disabling interrupts for fd %d\n",
389 intr_handle->uio_cfg_fd);
397 uio_intx_intr_enable(const struct rte_intr_handle *intr_handle)
399 unsigned char command_high;
401 /* use UIO config file descriptor for uio_pci_generic */
402 if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
404 "Error reading interrupts status for fd %d\n",
405 intr_handle->uio_cfg_fd);
408 /* enable interrupts */
409 command_high &= ~0x4;
410 if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
412 "Error enabling interrupts for fd %d\n",
413 intr_handle->uio_cfg_fd);
421 uio_intr_disable(const struct rte_intr_handle *intr_handle)
425 if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
427 "Error disabling interrupts for fd %d (%s)\n",
428 intr_handle->fd, strerror(errno));
435 uio_intr_enable(const struct rte_intr_handle *intr_handle)
439 if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
441 "Error enabling interrupts for fd %d (%s)\n",
442 intr_handle->fd, strerror(errno));
449 rte_intr_callback_register(const struct rte_intr_handle *intr_handle,
450 rte_intr_callback_fn cb, void *cb_arg)
452 int ret, wake_thread;
453 struct rte_intr_source *src;
454 struct rte_intr_callback *callback;
458 /* first do parameter checking */
459 if (intr_handle == NULL || intr_handle->fd < 0 || cb == NULL) {
461 "Registering with invalid input parameter\n");
465 /* allocate a new interrupt callback entity */
466 callback = rte_zmalloc("interrupt callback list",
467 sizeof(*callback), 0);
468 if (callback == NULL) {
469 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
472 callback->cb_fn = cb;
473 callback->cb_arg = cb_arg;
475 rte_spinlock_lock(&intr_lock);
477 /* check if there is at least one callback registered for the fd */
478 TAILQ_FOREACH(src, &intr_sources, next) {
479 if (src->intr_handle.fd == intr_handle->fd) {
480 /* we had no interrupts for this */
481 if TAILQ_EMPTY(&src->callbacks)
484 TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
490 /* no existing callbacks for this - add new source */
492 if ((src = rte_zmalloc("interrupt source list",
493 sizeof(*src), 0)) == NULL) {
494 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
498 src->intr_handle = *intr_handle;
499 TAILQ_INIT(&src->callbacks);
500 TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
501 TAILQ_INSERT_TAIL(&intr_sources, src, next);
507 rte_spinlock_unlock(&intr_lock);
510 * check if need to notify the pipe fd waited by epoll_wait to
511 * rebuild the wait list.
514 if (write(intr_pipe.writefd, "1", 1) < 0)
521 rte_intr_callback_unregister(const struct rte_intr_handle *intr_handle,
522 rte_intr_callback_fn cb_fn, void *cb_arg)
525 struct rte_intr_source *src;
526 struct rte_intr_callback *cb, *next;
528 /* do parameter checking first */
529 if (intr_handle == NULL || intr_handle->fd < 0) {
531 "Unregistering with invalid input parameter\n");
535 rte_spinlock_lock(&intr_lock);
537 /* check if the insterrupt source for the fd is existent */
538 TAILQ_FOREACH(src, &intr_sources, next)
539 if (src->intr_handle.fd == intr_handle->fd)
542 /* No interrupt source registered for the fd */
546 /* interrupt source has some active callbacks right now. */
547 } else if (src->active != 0) {
554 /*walk through the callbacks and remove all that match. */
555 for (cb = TAILQ_FIRST(&src->callbacks); cb != NULL; cb = next) {
557 next = TAILQ_NEXT(cb, next);
559 if (cb->cb_fn == cb_fn && (cb_arg == (void *)-1 ||
560 cb->cb_arg == cb_arg)) {
561 TAILQ_REMOVE(&src->callbacks, cb, next);
567 /* all callbacks for that source are removed. */
568 if (TAILQ_EMPTY(&src->callbacks)) {
569 TAILQ_REMOVE(&intr_sources, src, next);
574 rte_spinlock_unlock(&intr_lock);
576 /* notify the pipe fd waited by epoll_wait to rebuild the wait list */
577 if (ret >= 0 && write(intr_pipe.writefd, "1", 1) < 0) {
585 rte_intr_enable(const struct rte_intr_handle *intr_handle)
587 if (intr_handle && intr_handle->type == RTE_INTR_HANDLE_VDEV)
590 if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
593 switch (intr_handle->type){
594 /* write to the uio fd to enable the interrupt */
595 case RTE_INTR_HANDLE_UIO:
596 if (uio_intr_enable(intr_handle))
599 case RTE_INTR_HANDLE_UIO_INTX:
600 if (uio_intx_intr_enable(intr_handle))
603 /* not used at this moment */
604 case RTE_INTR_HANDLE_ALARM:
607 case RTE_INTR_HANDLE_VFIO_MSIX:
608 if (vfio_enable_msix(intr_handle))
611 case RTE_INTR_HANDLE_VFIO_MSI:
612 if (vfio_enable_msi(intr_handle))
615 case RTE_INTR_HANDLE_VFIO_LEGACY:
616 if (vfio_enable_intx(intr_handle))
620 /* unknown handle type */
623 "Unknown handle type of fd %d\n",
632 rte_intr_disable(const struct rte_intr_handle *intr_handle)
634 if (intr_handle && intr_handle->type == RTE_INTR_HANDLE_VDEV)
637 if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
640 switch (intr_handle->type){
641 /* write to the uio fd to disable the interrupt */
642 case RTE_INTR_HANDLE_UIO:
643 if (uio_intr_disable(intr_handle))
646 case RTE_INTR_HANDLE_UIO_INTX:
647 if (uio_intx_intr_disable(intr_handle))
650 /* not used at this moment */
651 case RTE_INTR_HANDLE_ALARM:
654 case RTE_INTR_HANDLE_VFIO_MSIX:
655 if (vfio_disable_msix(intr_handle))
658 case RTE_INTR_HANDLE_VFIO_MSI:
659 if (vfio_disable_msi(intr_handle))
662 case RTE_INTR_HANDLE_VFIO_LEGACY:
663 if (vfio_disable_intx(intr_handle))
667 /* unknown handle type */
670 "Unknown handle type of fd %d\n",
679 eal_intr_process_interrupts(struct epoll_event *events, int nfds)
683 struct rte_intr_source *src;
684 struct rte_intr_callback *cb;
685 union rte_intr_read_buffer buf;
686 struct rte_intr_callback active_cb;
688 for (n = 0; n < nfds; n++) {
691 * if the pipe fd is ready to read, return out to
692 * rebuild the wait list.
694 if (events[n].data.fd == intr_pipe.readfd){
695 int r = read(intr_pipe.readfd, buf.charbuf,
696 sizeof(buf.charbuf));
700 rte_spinlock_lock(&intr_lock);
701 TAILQ_FOREACH(src, &intr_sources, next)
702 if (src->intr_handle.fd ==
706 rte_spinlock_unlock(&intr_lock);
710 /* mark this interrupt source as active and release the lock. */
712 rte_spinlock_unlock(&intr_lock);
714 /* set the length to be read dor different handle type */
715 switch (src->intr_handle.type) {
716 case RTE_INTR_HANDLE_UIO:
717 case RTE_INTR_HANDLE_UIO_INTX:
718 bytes_read = sizeof(buf.uio_intr_count);
720 case RTE_INTR_HANDLE_ALARM:
721 bytes_read = sizeof(buf.timerfd_num);
724 case RTE_INTR_HANDLE_VFIO_MSIX:
725 case RTE_INTR_HANDLE_VFIO_MSI:
726 case RTE_INTR_HANDLE_VFIO_LEGACY:
727 bytes_read = sizeof(buf.vfio_intr_count);
730 case RTE_INTR_HANDLE_VDEV:
731 case RTE_INTR_HANDLE_EXT:
741 if (bytes_read > 0) {
743 * read out to clear the ready-to-be-read flag
746 bytes_read = read(events[n].data.fd, &buf, bytes_read);
747 if (bytes_read < 0) {
748 if (errno == EINTR || errno == EWOULDBLOCK)
751 RTE_LOG(ERR, EAL, "Error reading from file "
752 "descriptor %d: %s\n",
755 } else if (bytes_read == 0)
756 RTE_LOG(ERR, EAL, "Read nothing from file "
757 "descriptor %d\n", events[n].data.fd);
762 /* grab a lock, again to call callbacks and update status. */
763 rte_spinlock_lock(&intr_lock);
767 /* Finally, call all callbacks. */
768 TAILQ_FOREACH(cb, &src->callbacks, next) {
770 /* make a copy and unlock. */
772 rte_spinlock_unlock(&intr_lock);
774 /* call the actual callback */
775 active_cb.cb_fn(&src->intr_handle,
778 /*get the lock back. */
779 rte_spinlock_lock(&intr_lock);
783 /* we done with that interrupt source, release it. */
785 rte_spinlock_unlock(&intr_lock);
792 * It handles all the interrupts.
795 * epoll file descriptor.
797 * The number of file descriptors added in epoll.
803 eal_intr_handle_interrupts(int pfd, unsigned totalfds)
805 struct epoll_event events[totalfds];
809 nfds = epoll_wait(pfd, events, totalfds,
810 EAL_INTR_EPOLL_WAIT_FOREVER);
811 /* epoll_wait fail */
816 "epoll_wait returns with fail\n");
819 /* epoll_wait timeout, will never happens here */
822 /* epoll_wait has at least one fd ready to read */
823 if (eal_intr_process_interrupts(events, nfds) < 0)
829 * It builds/rebuilds up the epoll file descriptor with all the
830 * file descriptors being waited on. Then handles the interrupts.
838 static __attribute__((noreturn)) void *
839 eal_intr_thread_main(__rte_unused void *arg)
841 struct epoll_event ev;
843 /* host thread, never break out */
845 /* build up the epoll fd with all descriptors we are to
846 * wait on then pass it to the handle_interrupts function
848 static struct epoll_event pipe_event = {
849 .events = EPOLLIN | EPOLLPRI,
851 struct rte_intr_source *src;
854 /* create epoll fd */
855 int pfd = epoll_create(1);
857 rte_panic("Cannot create epoll instance\n");
859 pipe_event.data.fd = intr_pipe.readfd;
861 * add pipe fd into wait list, this pipe is used to
862 * rebuild the wait list.
864 if (epoll_ctl(pfd, EPOLL_CTL_ADD, intr_pipe.readfd,
866 rte_panic("Error adding fd to %d epoll_ctl, %s\n",
867 intr_pipe.readfd, strerror(errno));
871 rte_spinlock_lock(&intr_lock);
873 TAILQ_FOREACH(src, &intr_sources, next) {
874 if (src->callbacks.tqh_first == NULL)
875 continue; /* skip those with no callbacks */
876 ev.events = EPOLLIN | EPOLLPRI | EPOLLRDHUP | EPOLLHUP;
877 ev.data.fd = src->intr_handle.fd;
880 * add all the uio device file descriptor
883 if (epoll_ctl(pfd, EPOLL_CTL_ADD,
884 src->intr_handle.fd, &ev) < 0){
885 rte_panic("Error adding fd %d epoll_ctl, %s\n",
886 src->intr_handle.fd, strerror(errno));
891 rte_spinlock_unlock(&intr_lock);
892 /* serve the interrupt */
893 eal_intr_handle_interrupts(pfd, numfds);
896 * when we return, we need to rebuild the
897 * list of fds to monitor.
904 rte_eal_intr_init(void)
906 int ret = 0, ret_1 = 0;
907 char thread_name[RTE_MAX_THREAD_NAME_LEN];
909 /* init the global interrupt source head */
910 TAILQ_INIT(&intr_sources);
913 * create a pipe which will be waited by epoll and notified to
914 * rebuild the wait list of epoll.
916 if (pipe(intr_pipe.pipefd) < 0) {
921 /* create the host thread to wait/handle the interrupt */
922 ret = pthread_create(&intr_thread, NULL,
923 eal_intr_thread_main, NULL);
927 "Failed to create thread for interrupt handling\n");
929 /* Set thread_name for aid in debugging. */
930 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN,
932 ret_1 = rte_thread_setname(intr_thread, thread_name);
935 "Failed to set thread name for interrupt handling\n");
942 eal_intr_proc_rxtx_intr(int fd, const struct rte_intr_handle *intr_handle)
944 union rte_intr_read_buffer buf;
948 switch (intr_handle->type) {
949 case RTE_INTR_HANDLE_UIO:
950 case RTE_INTR_HANDLE_UIO_INTX:
951 bytes_read = sizeof(buf.uio_intr_count);
954 case RTE_INTR_HANDLE_VFIO_MSIX:
955 case RTE_INTR_HANDLE_VFIO_MSI:
956 case RTE_INTR_HANDLE_VFIO_LEGACY:
957 bytes_read = sizeof(buf.vfio_intr_count);
960 case RTE_INTR_HANDLE_VDEV:
961 /* for vdev, fd points to:
962 * a. eventfd which does not need to read out;
963 * b. datapath fd which needs PMD to read out.
966 case RTE_INTR_HANDLE_EXT:
970 RTE_LOG(INFO, EAL, "unexpected intr type\n");
975 * read out to clear the ready-to-be-read flag
979 nbytes = read(fd, &buf, bytes_read);
981 if (errno == EINTR || errno == EWOULDBLOCK ||
985 "Error reading from fd %d: %s\n",
986 fd, strerror(errno));
987 } else if (nbytes == 0)
988 RTE_LOG(ERR, EAL, "Read nothing from fd %d\n", fd);
994 eal_epoll_process_event(struct epoll_event *evs, unsigned int n,
995 struct rte_epoll_event *events)
997 unsigned int i, count = 0;
998 struct rte_epoll_event *rev;
1000 for (i = 0; i < n; i++) {
1001 rev = evs[i].data.ptr;
1002 if (!rev || !rte_atomic32_cmpset(&rev->status, RTE_EPOLL_VALID,
1006 events[count].status = RTE_EPOLL_VALID;
1007 events[count].fd = rev->fd;
1008 events[count].epfd = rev->epfd;
1009 events[count].epdata.event = rev->epdata.event;
1010 events[count].epdata.data = rev->epdata.data;
1011 if (rev->epdata.cb_fun)
1012 rev->epdata.cb_fun(rev->fd,
1013 rev->epdata.cb_arg);
1015 rte_compiler_barrier();
1016 rev->status = RTE_EPOLL_VALID;
1023 eal_init_tls_epfd(void)
1025 int pfd = epoll_create(255);
1029 "Cannot create epoll instance\n");
1036 rte_intr_tls_epfd(void)
1038 if (RTE_PER_LCORE(_epfd) == -1)
1039 RTE_PER_LCORE(_epfd) = eal_init_tls_epfd();
1041 return RTE_PER_LCORE(_epfd);
1045 rte_epoll_wait(int epfd, struct rte_epoll_event *events,
1046 int maxevents, int timeout)
1048 struct epoll_event evs[maxevents];
1052 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1056 /* using per thread epoll fd */
1057 if (epfd == RTE_EPOLL_PER_THREAD)
1058 epfd = rte_intr_tls_epfd();
1061 rc = epoll_wait(epfd, evs, maxevents, timeout);
1062 if (likely(rc > 0)) {
1063 /* epoll_wait has at least one fd ready to read */
1064 rc = eal_epoll_process_event(evs, rc, events);
1066 } else if (rc < 0) {
1069 /* epoll_wait fail */
1070 RTE_LOG(ERR, EAL, "epoll_wait returns with fail %s\n",
1075 /* rc == 0, epoll_wait timed out */
1084 eal_epoll_data_safe_free(struct rte_epoll_event *ev)
1086 while (!rte_atomic32_cmpset(&ev->status, RTE_EPOLL_VALID,
1088 while (ev->status != RTE_EPOLL_VALID)
1090 memset(&ev->epdata, 0, sizeof(ev->epdata));
1096 rte_epoll_ctl(int epfd, int op, int fd,
1097 struct rte_epoll_event *event)
1099 struct epoll_event ev;
1102 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1106 /* using per thread epoll fd */
1107 if (epfd == RTE_EPOLL_PER_THREAD)
1108 epfd = rte_intr_tls_epfd();
1110 if (op == EPOLL_CTL_ADD) {
1111 event->status = RTE_EPOLL_VALID;
1112 event->fd = fd; /* ignore fd in event */
1114 ev.data.ptr = (void *)event;
1117 ev.events = event->epdata.event;
1118 if (epoll_ctl(epfd, op, fd, &ev) < 0) {
1119 RTE_LOG(ERR, EAL, "Error op %d fd %d epoll_ctl, %s\n",
1120 op, fd, strerror(errno));
1121 if (op == EPOLL_CTL_ADD)
1122 /* rollback status when CTL_ADD fail */
1123 event->status = RTE_EPOLL_INVALID;
1127 if (op == EPOLL_CTL_DEL && event->status != RTE_EPOLL_INVALID)
1128 eal_epoll_data_safe_free(event);
1134 rte_intr_rx_ctl(struct rte_intr_handle *intr_handle, int epfd,
1135 int op, unsigned int vec, void *data)
1137 struct rte_epoll_event *rev;
1138 struct rte_epoll_data *epdata;
1140 unsigned int efd_idx;
1143 efd_idx = (vec >= RTE_INTR_VEC_RXTX_OFFSET) ?
1144 (vec - RTE_INTR_VEC_RXTX_OFFSET) : vec;
1146 if (!intr_handle || intr_handle->nb_efd == 0 ||
1147 efd_idx >= intr_handle->nb_efd) {
1148 RTE_LOG(ERR, EAL, "Wrong intr vector number.\n");
1153 case RTE_INTR_EVENT_ADD:
1154 epfd_op = EPOLL_CTL_ADD;
1155 rev = &intr_handle->elist[efd_idx];
1156 if (rev->status != RTE_EPOLL_INVALID) {
1157 RTE_LOG(INFO, EAL, "Event already been added.\n");
1161 /* attach to intr vector fd */
1162 epdata = &rev->epdata;
1163 epdata->event = EPOLLIN | EPOLLPRI | EPOLLET;
1164 epdata->data = data;
1165 epdata->cb_fun = (rte_intr_event_cb_t)eal_intr_proc_rxtx_intr;
1166 epdata->cb_arg = (void *)intr_handle;
1167 rc = rte_epoll_ctl(epfd, epfd_op,
1168 intr_handle->efds[efd_idx], rev);
1171 "efd %d associated with vec %d added on epfd %d"
1172 "\n", rev->fd, vec, epfd);
1176 case RTE_INTR_EVENT_DEL:
1177 epfd_op = EPOLL_CTL_DEL;
1178 rev = &intr_handle->elist[efd_idx];
1179 if (rev->status == RTE_EPOLL_INVALID) {
1180 RTE_LOG(INFO, EAL, "Event does not exist.\n");
1184 rc = rte_epoll_ctl(rev->epfd, epfd_op, rev->fd, rev);
1189 RTE_LOG(ERR, EAL, "event op type mismatch\n");
1197 rte_intr_free_epoll_fd(struct rte_intr_handle *intr_handle)
1200 struct rte_epoll_event *rev;
1202 for (i = 0; i < intr_handle->nb_efd; i++) {
1203 rev = &intr_handle->elist[i];
1204 if (rev->status == RTE_EPOLL_INVALID)
1206 if (rte_epoll_ctl(rev->epfd, EPOLL_CTL_DEL, rev->fd, rev)) {
1207 /* force free if the entry valid */
1208 eal_epoll_data_safe_free(rev);
1209 rev->status = RTE_EPOLL_INVALID;
1215 rte_intr_efd_enable(struct rte_intr_handle *intr_handle, uint32_t nb_efd)
1219 uint32_t n = RTE_MIN(nb_efd, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
1221 assert(nb_efd != 0);
1223 if (intr_handle->type == RTE_INTR_HANDLE_VFIO_MSIX) {
1224 for (i = 0; i < n; i++) {
1225 fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
1228 "can't setup eventfd, error %i (%s)\n",
1229 errno, strerror(errno));
1232 intr_handle->efds[i] = fd;
1234 intr_handle->nb_efd = n;
1235 intr_handle->max_intr = NB_OTHER_INTR + n;
1236 } else if (intr_handle->type == RTE_INTR_HANDLE_VDEV) {
1237 /* do nothing, and let vdev driver to initialize this struct */
1239 intr_handle->efds[0] = intr_handle->fd;
1240 intr_handle->nb_efd = RTE_MIN(nb_efd, 1U);
1241 intr_handle->max_intr = NB_OTHER_INTR;
1248 rte_intr_efd_disable(struct rte_intr_handle *intr_handle)
1252 rte_intr_free_epoll_fd(intr_handle);
1253 if (intr_handle->max_intr > intr_handle->nb_efd) {
1254 for (i = 0; i < intr_handle->nb_efd; i++)
1255 close(intr_handle->efds[i]);
1257 intr_handle->nb_efd = 0;
1258 intr_handle->max_intr = 0;
1262 rte_intr_dp_is_en(struct rte_intr_handle *intr_handle)
1264 return !(!intr_handle->nb_efd);
1268 rte_intr_allow_others(struct rte_intr_handle *intr_handle)
1270 if (!rte_intr_dp_is_en(intr_handle))
1273 return !!(intr_handle->max_intr - intr_handle->nb_efd);
1277 rte_intr_cap_multiple(struct rte_intr_handle *intr_handle)
1279 if (intr_handle->type == RTE_INTR_HANDLE_VFIO_MSIX)
1282 if (intr_handle->type == RTE_INTR_HANDLE_VDEV)