1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
15 #include <sys/epoll.h>
16 #include <sys/signalfd.h>
17 #include <sys/ioctl.h>
18 #include <sys/eventfd.h>
22 #include <rte_common.h>
23 #include <rte_interrupts.h>
24 #include <rte_memory.h>
25 #include <rte_launch.h>
27 #include <rte_per_lcore.h>
28 #include <rte_lcore.h>
29 #include <rte_atomic.h>
30 #include <rte_branch_prediction.h>
31 #include <rte_debug.h>
33 #include <rte_errno.h>
34 #include <rte_spinlock.h>
35 #include <rte_pause.h>
38 #include "eal_private.h"
40 #include "eal_thread.h"
42 #define EAL_INTR_EPOLL_WAIT_FOREVER (-1)
43 #define NB_OTHER_INTR 1
45 static RTE_DEFINE_PER_LCORE(int, _epfd) = -1; /**< epoll fd per thread */
61 * union buffer for reading on different devices
63 union rte_intr_read_buffer {
64 int uio_intr_count; /* for uio device */
66 uint64_t vfio_intr_count; /* for vfio device */
68 uint64_t timerfd_num; /* for timerfd */
69 char charbuf[16]; /* for others */
72 TAILQ_HEAD(rte_intr_cb_list, rte_intr_callback);
73 TAILQ_HEAD(rte_intr_source_list, rte_intr_source);
75 struct rte_intr_callback {
76 TAILQ_ENTRY(rte_intr_callback) next;
77 rte_intr_callback_fn cb_fn; /**< callback address */
78 void *cb_arg; /**< parameter for callback */
81 struct rte_intr_source {
82 TAILQ_ENTRY(rte_intr_source) next;
83 struct rte_intr_handle intr_handle; /**< interrupt handle */
84 struct rte_intr_cb_list callbacks; /**< user callbacks */
88 /* global spinlock for interrupt data operation */
89 static rte_spinlock_t intr_lock = RTE_SPINLOCK_INITIALIZER;
91 /* union buffer for pipe read/write */
92 static union intr_pipefds intr_pipe;
94 /* interrupt sources list */
95 static struct rte_intr_source_list intr_sources;
97 /* interrupt handling thread */
98 static pthread_t intr_thread;
100 /* VFIO interrupts */
103 #define IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + sizeof(int))
104 /* irq set buffer length for queue interrupts and LSC interrupt */
105 #define MSIX_IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + \
106 sizeof(int) * (RTE_MAX_RXTX_INTR_VEC_ID + 1))
108 /* enable legacy (INTx) interrupts */
110 vfio_enable_intx(const struct rte_intr_handle *intr_handle) {
111 struct vfio_irq_set *irq_set;
112 char irq_set_buf[IRQ_SET_BUF_LEN];
116 len = sizeof(irq_set_buf);
119 irq_set = (struct vfio_irq_set *) irq_set_buf;
120 irq_set->argsz = len;
122 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
123 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
125 fd_ptr = (int *) &irq_set->data;
126 *fd_ptr = intr_handle->fd;
128 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
131 RTE_LOG(ERR, EAL, "Error enabling INTx interrupts for fd %d\n",
136 /* unmask INTx after enabling */
137 memset(irq_set, 0, len);
138 len = sizeof(struct vfio_irq_set);
139 irq_set->argsz = len;
141 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK;
142 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
145 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
148 RTE_LOG(ERR, EAL, "Error unmasking INTx interrupts for fd %d\n",
155 /* disable legacy (INTx) interrupts */
157 vfio_disable_intx(const struct rte_intr_handle *intr_handle) {
158 struct vfio_irq_set *irq_set;
159 char irq_set_buf[IRQ_SET_BUF_LEN];
162 len = sizeof(struct vfio_irq_set);
164 /* mask interrupts before disabling */
165 irq_set = (struct vfio_irq_set *) irq_set_buf;
166 irq_set->argsz = len;
168 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_MASK;
169 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
172 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
175 RTE_LOG(ERR, EAL, "Error masking INTx interrupts for fd %d\n",
181 memset(irq_set, 0, len);
182 irq_set->argsz = len;
184 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
185 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
188 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
192 "Error disabling INTx interrupts for fd %d\n", intr_handle->fd);
198 /* enable MSI interrupts */
200 vfio_enable_msi(const struct rte_intr_handle *intr_handle) {
202 char irq_set_buf[IRQ_SET_BUF_LEN];
203 struct vfio_irq_set *irq_set;
206 len = sizeof(irq_set_buf);
208 irq_set = (struct vfio_irq_set *) irq_set_buf;
209 irq_set->argsz = len;
211 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
212 irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
214 fd_ptr = (int *) &irq_set->data;
215 *fd_ptr = intr_handle->fd;
217 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
220 RTE_LOG(ERR, EAL, "Error enabling MSI interrupts for fd %d\n",
227 /* disable MSI interrupts */
229 vfio_disable_msi(const struct rte_intr_handle *intr_handle) {
230 struct vfio_irq_set *irq_set;
231 char irq_set_buf[IRQ_SET_BUF_LEN];
234 len = sizeof(struct vfio_irq_set);
236 irq_set = (struct vfio_irq_set *) irq_set_buf;
237 irq_set->argsz = len;
239 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
240 irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
243 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
247 "Error disabling MSI interrupts for fd %d\n", intr_handle->fd);
252 /* enable MSI-X interrupts */
254 vfio_enable_msix(const struct rte_intr_handle *intr_handle) {
256 char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
257 struct vfio_irq_set *irq_set;
260 len = sizeof(irq_set_buf);
262 irq_set = (struct vfio_irq_set *) irq_set_buf;
263 irq_set->argsz = len;
264 /* 0 < irq_set->count < RTE_MAX_RXTX_INTR_VEC_ID + 1 */
265 irq_set->count = intr_handle->max_intr ?
266 (intr_handle->max_intr > RTE_MAX_RXTX_INTR_VEC_ID + 1 ?
267 RTE_MAX_RXTX_INTR_VEC_ID + 1 : intr_handle->max_intr) : 1;
268 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
269 irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
271 fd_ptr = (int *) &irq_set->data;
272 /* INTR vector offset 0 reserve for non-efds mapping */
273 fd_ptr[RTE_INTR_VEC_ZERO_OFFSET] = intr_handle->fd;
274 memcpy(&fd_ptr[RTE_INTR_VEC_RXTX_OFFSET], intr_handle->efds,
275 sizeof(*intr_handle->efds) * intr_handle->nb_efd);
277 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
280 RTE_LOG(ERR, EAL, "Error enabling MSI-X interrupts for fd %d\n",
288 /* disable MSI-X interrupts */
290 vfio_disable_msix(const struct rte_intr_handle *intr_handle) {
291 struct vfio_irq_set *irq_set;
292 char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
295 len = sizeof(struct vfio_irq_set);
297 irq_set = (struct vfio_irq_set *) irq_set_buf;
298 irq_set->argsz = len;
300 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
301 irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
304 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
308 "Error disabling MSI-X interrupts for fd %d\n", intr_handle->fd);
313 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
314 /* enable req notifier */
316 vfio_enable_req(const struct rte_intr_handle *intr_handle)
319 char irq_set_buf[IRQ_SET_BUF_LEN];
320 struct vfio_irq_set *irq_set;
323 len = sizeof(irq_set_buf);
325 irq_set = (struct vfio_irq_set *) irq_set_buf;
326 irq_set->argsz = len;
328 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
329 VFIO_IRQ_SET_ACTION_TRIGGER;
330 irq_set->index = VFIO_PCI_REQ_IRQ_INDEX;
332 fd_ptr = (int *) &irq_set->data;
333 *fd_ptr = intr_handle->fd;
335 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
338 RTE_LOG(ERR, EAL, "Error enabling req interrupts for fd %d\n",
346 /* disable req notifier */
348 vfio_disable_req(const struct rte_intr_handle *intr_handle)
350 struct vfio_irq_set *irq_set;
351 char irq_set_buf[IRQ_SET_BUF_LEN];
354 len = sizeof(struct vfio_irq_set);
356 irq_set = (struct vfio_irq_set *) irq_set_buf;
357 irq_set->argsz = len;
359 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
360 irq_set->index = VFIO_PCI_REQ_IRQ_INDEX;
363 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
366 RTE_LOG(ERR, EAL, "Error disabling req interrupts for fd %d\n",
375 uio_intx_intr_disable(const struct rte_intr_handle *intr_handle)
377 unsigned char command_high;
379 /* use UIO config file descriptor for uio_pci_generic */
380 if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
382 "Error reading interrupts status for fd %d\n",
383 intr_handle->uio_cfg_fd);
386 /* disable interrupts */
388 if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
390 "Error disabling interrupts for fd %d\n",
391 intr_handle->uio_cfg_fd);
399 uio_intx_intr_enable(const struct rte_intr_handle *intr_handle)
401 unsigned char command_high;
403 /* use UIO config file descriptor for uio_pci_generic */
404 if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
406 "Error reading interrupts status for fd %d\n",
407 intr_handle->uio_cfg_fd);
410 /* enable interrupts */
411 command_high &= ~0x4;
412 if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
414 "Error enabling interrupts for fd %d\n",
415 intr_handle->uio_cfg_fd);
423 uio_intr_disable(const struct rte_intr_handle *intr_handle)
427 if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
429 "Error disabling interrupts for fd %d (%s)\n",
430 intr_handle->fd, strerror(errno));
437 uio_intr_enable(const struct rte_intr_handle *intr_handle)
441 if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
443 "Error enabling interrupts for fd %d (%s)\n",
444 intr_handle->fd, strerror(errno));
451 rte_intr_callback_register(const struct rte_intr_handle *intr_handle,
452 rte_intr_callback_fn cb, void *cb_arg)
454 int ret, wake_thread;
455 struct rte_intr_source *src;
456 struct rte_intr_callback *callback;
460 /* first do parameter checking */
461 if (intr_handle == NULL || intr_handle->fd < 0 || cb == NULL) {
463 "Registering with invalid input parameter\n");
467 /* allocate a new interrupt callback entity */
468 callback = calloc(1, sizeof(*callback));
469 if (callback == NULL) {
470 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
473 callback->cb_fn = cb;
474 callback->cb_arg = cb_arg;
476 rte_spinlock_lock(&intr_lock);
478 /* check if there is at least one callback registered for the fd */
479 TAILQ_FOREACH(src, &intr_sources, next) {
480 if (src->intr_handle.fd == intr_handle->fd) {
481 /* we had no interrupts for this */
482 if (TAILQ_EMPTY(&src->callbacks))
485 TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
491 /* no existing callbacks for this - add new source */
493 src = calloc(1, sizeof(*src));
495 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
499 src->intr_handle = *intr_handle;
500 TAILQ_INIT(&src->callbacks);
501 TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
502 TAILQ_INSERT_TAIL(&intr_sources, src, next);
508 rte_spinlock_unlock(&intr_lock);
511 * check if need to notify the pipe fd waited by epoll_wait to
512 * rebuild the wait list.
515 if (write(intr_pipe.writefd, "1", 1) < 0)
522 rte_intr_callback_unregister(const struct rte_intr_handle *intr_handle,
523 rte_intr_callback_fn cb_fn, void *cb_arg)
526 struct rte_intr_source *src;
527 struct rte_intr_callback *cb, *next;
529 /* do parameter checking first */
530 if (intr_handle == NULL || intr_handle->fd < 0) {
532 "Unregistering with invalid input parameter\n");
536 rte_spinlock_lock(&intr_lock);
538 /* check if the insterrupt source for the fd is existent */
539 TAILQ_FOREACH(src, &intr_sources, next)
540 if (src->intr_handle.fd == intr_handle->fd)
543 /* No interrupt source registered for the fd */
547 /* interrupt source has some active callbacks right now. */
548 } else if (src->active != 0) {
555 /*walk through the callbacks and remove all that match. */
556 for (cb = TAILQ_FIRST(&src->callbacks); cb != NULL; cb = next) {
558 next = TAILQ_NEXT(cb, next);
560 if (cb->cb_fn == cb_fn && (cb_arg == (void *)-1 ||
561 cb->cb_arg == cb_arg)) {
562 TAILQ_REMOVE(&src->callbacks, cb, next);
568 /* all callbacks for that source are removed. */
569 if (TAILQ_EMPTY(&src->callbacks)) {
570 TAILQ_REMOVE(&intr_sources, src, next);
575 rte_spinlock_unlock(&intr_lock);
577 /* notify the pipe fd waited by epoll_wait to rebuild the wait list */
578 if (ret >= 0 && write(intr_pipe.writefd, "1", 1) < 0) {
586 rte_intr_enable(const struct rte_intr_handle *intr_handle)
588 if (intr_handle && intr_handle->type == RTE_INTR_HANDLE_VDEV)
591 if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
594 switch (intr_handle->type){
595 /* write to the uio fd to enable the interrupt */
596 case RTE_INTR_HANDLE_UIO:
597 if (uio_intr_enable(intr_handle))
600 case RTE_INTR_HANDLE_UIO_INTX:
601 if (uio_intx_intr_enable(intr_handle))
604 /* not used at this moment */
605 case RTE_INTR_HANDLE_ALARM:
608 case RTE_INTR_HANDLE_VFIO_MSIX:
609 if (vfio_enable_msix(intr_handle))
612 case RTE_INTR_HANDLE_VFIO_MSI:
613 if (vfio_enable_msi(intr_handle))
616 case RTE_INTR_HANDLE_VFIO_LEGACY:
617 if (vfio_enable_intx(intr_handle))
620 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
621 case RTE_INTR_HANDLE_VFIO_REQ:
622 if (vfio_enable_req(intr_handle))
627 /* not used at this moment */
628 case RTE_INTR_HANDLE_DEV_EVENT:
630 /* unknown handle type */
633 "Unknown handle type of fd %d\n",
642 rte_intr_disable(const struct rte_intr_handle *intr_handle)
644 if (intr_handle && intr_handle->type == RTE_INTR_HANDLE_VDEV)
647 if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
650 switch (intr_handle->type){
651 /* write to the uio fd to disable the interrupt */
652 case RTE_INTR_HANDLE_UIO:
653 if (uio_intr_disable(intr_handle))
656 case RTE_INTR_HANDLE_UIO_INTX:
657 if (uio_intx_intr_disable(intr_handle))
660 /* not used at this moment */
661 case RTE_INTR_HANDLE_ALARM:
664 case RTE_INTR_HANDLE_VFIO_MSIX:
665 if (vfio_disable_msix(intr_handle))
668 case RTE_INTR_HANDLE_VFIO_MSI:
669 if (vfio_disable_msi(intr_handle))
672 case RTE_INTR_HANDLE_VFIO_LEGACY:
673 if (vfio_disable_intx(intr_handle))
676 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
677 case RTE_INTR_HANDLE_VFIO_REQ:
678 if (vfio_disable_req(intr_handle))
683 /* not used at this moment */
684 case RTE_INTR_HANDLE_DEV_EVENT:
686 /* unknown handle type */
689 "Unknown handle type of fd %d\n",
698 eal_intr_process_interrupts(struct epoll_event *events, int nfds)
702 struct rte_intr_source *src;
703 struct rte_intr_callback *cb;
704 union rte_intr_read_buffer buf;
705 struct rte_intr_callback active_cb;
707 for (n = 0; n < nfds; n++) {
710 * if the pipe fd is ready to read, return out to
711 * rebuild the wait list.
713 if (events[n].data.fd == intr_pipe.readfd){
714 int r = read(intr_pipe.readfd, buf.charbuf,
715 sizeof(buf.charbuf));
719 rte_spinlock_lock(&intr_lock);
720 TAILQ_FOREACH(src, &intr_sources, next)
721 if (src->intr_handle.fd ==
725 rte_spinlock_unlock(&intr_lock);
729 /* mark this interrupt source as active and release the lock. */
731 rte_spinlock_unlock(&intr_lock);
733 /* set the length to be read dor different handle type */
734 switch (src->intr_handle.type) {
735 case RTE_INTR_HANDLE_UIO:
736 case RTE_INTR_HANDLE_UIO_INTX:
737 bytes_read = sizeof(buf.uio_intr_count);
739 case RTE_INTR_HANDLE_ALARM:
740 bytes_read = sizeof(buf.timerfd_num);
743 case RTE_INTR_HANDLE_VFIO_MSIX:
744 case RTE_INTR_HANDLE_VFIO_MSI:
745 case RTE_INTR_HANDLE_VFIO_LEGACY:
746 bytes_read = sizeof(buf.vfio_intr_count);
748 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
749 case RTE_INTR_HANDLE_VFIO_REQ:
755 case RTE_INTR_HANDLE_VDEV:
756 case RTE_INTR_HANDLE_EXT:
760 case RTE_INTR_HANDLE_DEV_EVENT:
769 if (bytes_read > 0) {
771 * read out to clear the ready-to-be-read flag
774 bytes_read = read(events[n].data.fd, &buf, bytes_read);
775 if (bytes_read < 0) {
776 if (errno == EINTR || errno == EWOULDBLOCK)
779 RTE_LOG(ERR, EAL, "Error reading from file "
780 "descriptor %d: %s\n",
783 } else if (bytes_read == 0)
784 RTE_LOG(ERR, EAL, "Read nothing from file "
785 "descriptor %d\n", events[n].data.fd);
790 /* grab a lock, again to call callbacks and update status. */
791 rte_spinlock_lock(&intr_lock);
795 /* Finally, call all callbacks. */
796 TAILQ_FOREACH(cb, &src->callbacks, next) {
798 /* make a copy and unlock. */
800 rte_spinlock_unlock(&intr_lock);
802 /* call the actual callback */
803 active_cb.cb_fn(active_cb.cb_arg);
805 /*get the lock back. */
806 rte_spinlock_lock(&intr_lock);
810 /* we done with that interrupt source, release it. */
812 rte_spinlock_unlock(&intr_lock);
819 * It handles all the interrupts.
822 * epoll file descriptor.
824 * The number of file descriptors added in epoll.
830 eal_intr_handle_interrupts(int pfd, unsigned totalfds)
832 struct epoll_event events[totalfds];
836 nfds = epoll_wait(pfd, events, totalfds,
837 EAL_INTR_EPOLL_WAIT_FOREVER);
838 /* epoll_wait fail */
843 "epoll_wait returns with fail\n");
846 /* epoll_wait timeout, will never happens here */
849 /* epoll_wait has at least one fd ready to read */
850 if (eal_intr_process_interrupts(events, nfds) < 0)
856 * It builds/rebuilds up the epoll file descriptor with all the
857 * file descriptors being waited on. Then handles the interrupts.
865 static __attribute__((noreturn)) void *
866 eal_intr_thread_main(__rte_unused void *arg)
868 struct epoll_event ev;
870 /* host thread, never break out */
872 /* build up the epoll fd with all descriptors we are to
873 * wait on then pass it to the handle_interrupts function
875 static struct epoll_event pipe_event = {
876 .events = EPOLLIN | EPOLLPRI,
878 struct rte_intr_source *src;
881 /* create epoll fd */
882 int pfd = epoll_create(1);
884 rte_panic("Cannot create epoll instance\n");
886 pipe_event.data.fd = intr_pipe.readfd;
888 * add pipe fd into wait list, this pipe is used to
889 * rebuild the wait list.
891 if (epoll_ctl(pfd, EPOLL_CTL_ADD, intr_pipe.readfd,
893 rte_panic("Error adding fd to %d epoll_ctl, %s\n",
894 intr_pipe.readfd, strerror(errno));
898 rte_spinlock_lock(&intr_lock);
900 TAILQ_FOREACH(src, &intr_sources, next) {
901 if (src->callbacks.tqh_first == NULL)
902 continue; /* skip those with no callbacks */
903 ev.events = EPOLLIN | EPOLLPRI | EPOLLRDHUP | EPOLLHUP;
904 ev.data.fd = src->intr_handle.fd;
907 * add all the uio device file descriptor
910 if (epoll_ctl(pfd, EPOLL_CTL_ADD,
911 src->intr_handle.fd, &ev) < 0){
912 rte_panic("Error adding fd %d epoll_ctl, %s\n",
913 src->intr_handle.fd, strerror(errno));
918 rte_spinlock_unlock(&intr_lock);
919 /* serve the interrupt */
920 eal_intr_handle_interrupts(pfd, numfds);
923 * when we return, we need to rebuild the
924 * list of fds to monitor.
931 rte_eal_intr_init(void)
935 /* init the global interrupt source head */
936 TAILQ_INIT(&intr_sources);
939 * create a pipe which will be waited by epoll and notified to
940 * rebuild the wait list of epoll.
942 if (pipe(intr_pipe.pipefd) < 0) {
947 /* create the host thread to wait/handle the interrupt */
948 ret = rte_ctrl_thread_create(&intr_thread, "eal-intr-thread", NULL,
949 eal_intr_thread_main, NULL);
953 "Failed to create thread for interrupt handling\n");
960 eal_intr_proc_rxtx_intr(int fd, const struct rte_intr_handle *intr_handle)
962 union rte_intr_read_buffer buf;
966 switch (intr_handle->type) {
967 case RTE_INTR_HANDLE_UIO:
968 case RTE_INTR_HANDLE_UIO_INTX:
969 bytes_read = sizeof(buf.uio_intr_count);
972 case RTE_INTR_HANDLE_VFIO_MSIX:
973 case RTE_INTR_HANDLE_VFIO_MSI:
974 case RTE_INTR_HANDLE_VFIO_LEGACY:
975 bytes_read = sizeof(buf.vfio_intr_count);
978 case RTE_INTR_HANDLE_VDEV:
979 bytes_read = intr_handle->efd_counter_size;
980 /* For vdev, number of bytes to read is set by driver */
982 case RTE_INTR_HANDLE_EXT:
986 RTE_LOG(INFO, EAL, "unexpected intr type\n");
991 * read out to clear the ready-to-be-read flag
997 nbytes = read(fd, &buf, bytes_read);
999 if (errno == EINTR || errno == EWOULDBLOCK ||
1003 "Error reading from fd %d: %s\n",
1004 fd, strerror(errno));
1005 } else if (nbytes == 0)
1006 RTE_LOG(ERR, EAL, "Read nothing from fd %d\n", fd);
1012 eal_epoll_process_event(struct epoll_event *evs, unsigned int n,
1013 struct rte_epoll_event *events)
1015 unsigned int i, count = 0;
1016 struct rte_epoll_event *rev;
1018 for (i = 0; i < n; i++) {
1019 rev = evs[i].data.ptr;
1020 if (!rev || !rte_atomic32_cmpset(&rev->status, RTE_EPOLL_VALID,
1024 events[count].status = RTE_EPOLL_VALID;
1025 events[count].fd = rev->fd;
1026 events[count].epfd = rev->epfd;
1027 events[count].epdata.event = rev->epdata.event;
1028 events[count].epdata.data = rev->epdata.data;
1029 if (rev->epdata.cb_fun)
1030 rev->epdata.cb_fun(rev->fd,
1031 rev->epdata.cb_arg);
1033 rte_compiler_barrier();
1034 rev->status = RTE_EPOLL_VALID;
1041 eal_init_tls_epfd(void)
1043 int pfd = epoll_create(255);
1047 "Cannot create epoll instance\n");
1054 rte_intr_tls_epfd(void)
1056 if (RTE_PER_LCORE(_epfd) == -1)
1057 RTE_PER_LCORE(_epfd) = eal_init_tls_epfd();
1059 return RTE_PER_LCORE(_epfd);
1063 rte_epoll_wait(int epfd, struct rte_epoll_event *events,
1064 int maxevents, int timeout)
1066 struct epoll_event evs[maxevents];
1070 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1074 /* using per thread epoll fd */
1075 if (epfd == RTE_EPOLL_PER_THREAD)
1076 epfd = rte_intr_tls_epfd();
1079 rc = epoll_wait(epfd, evs, maxevents, timeout);
1080 if (likely(rc > 0)) {
1081 /* epoll_wait has at least one fd ready to read */
1082 rc = eal_epoll_process_event(evs, rc, events);
1084 } else if (rc < 0) {
1087 /* epoll_wait fail */
1088 RTE_LOG(ERR, EAL, "epoll_wait returns with fail %s\n",
1093 /* rc == 0, epoll_wait timed out */
1102 eal_epoll_data_safe_free(struct rte_epoll_event *ev)
1104 while (!rte_atomic32_cmpset(&ev->status, RTE_EPOLL_VALID,
1106 while (ev->status != RTE_EPOLL_VALID)
1108 memset(&ev->epdata, 0, sizeof(ev->epdata));
1114 rte_epoll_ctl(int epfd, int op, int fd,
1115 struct rte_epoll_event *event)
1117 struct epoll_event ev;
1120 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1124 /* using per thread epoll fd */
1125 if (epfd == RTE_EPOLL_PER_THREAD)
1126 epfd = rte_intr_tls_epfd();
1128 if (op == EPOLL_CTL_ADD) {
1129 event->status = RTE_EPOLL_VALID;
1130 event->fd = fd; /* ignore fd in event */
1132 ev.data.ptr = (void *)event;
1135 ev.events = event->epdata.event;
1136 if (epoll_ctl(epfd, op, fd, &ev) < 0) {
1137 RTE_LOG(ERR, EAL, "Error op %d fd %d epoll_ctl, %s\n",
1138 op, fd, strerror(errno));
1139 if (op == EPOLL_CTL_ADD)
1140 /* rollback status when CTL_ADD fail */
1141 event->status = RTE_EPOLL_INVALID;
1145 if (op == EPOLL_CTL_DEL && event->status != RTE_EPOLL_INVALID)
1146 eal_epoll_data_safe_free(event);
1152 rte_intr_rx_ctl(struct rte_intr_handle *intr_handle, int epfd,
1153 int op, unsigned int vec, void *data)
1155 struct rte_epoll_event *rev;
1156 struct rte_epoll_data *epdata;
1158 unsigned int efd_idx;
1161 efd_idx = (vec >= RTE_INTR_VEC_RXTX_OFFSET) ?
1162 (vec - RTE_INTR_VEC_RXTX_OFFSET) : vec;
1164 if (!intr_handle || intr_handle->nb_efd == 0 ||
1165 efd_idx >= intr_handle->nb_efd) {
1166 RTE_LOG(ERR, EAL, "Wrong intr vector number.\n");
1171 case RTE_INTR_EVENT_ADD:
1172 epfd_op = EPOLL_CTL_ADD;
1173 rev = &intr_handle->elist[efd_idx];
1174 if (rev->status != RTE_EPOLL_INVALID) {
1175 RTE_LOG(INFO, EAL, "Event already been added.\n");
1179 /* attach to intr vector fd */
1180 epdata = &rev->epdata;
1181 epdata->event = EPOLLIN | EPOLLPRI | EPOLLET;
1182 epdata->data = data;
1183 epdata->cb_fun = (rte_intr_event_cb_t)eal_intr_proc_rxtx_intr;
1184 epdata->cb_arg = (void *)intr_handle;
1185 rc = rte_epoll_ctl(epfd, epfd_op,
1186 intr_handle->efds[efd_idx], rev);
1189 "efd %d associated with vec %d added on epfd %d"
1190 "\n", rev->fd, vec, epfd);
1194 case RTE_INTR_EVENT_DEL:
1195 epfd_op = EPOLL_CTL_DEL;
1196 rev = &intr_handle->elist[efd_idx];
1197 if (rev->status == RTE_EPOLL_INVALID) {
1198 RTE_LOG(INFO, EAL, "Event does not exist.\n");
1202 rc = rte_epoll_ctl(rev->epfd, epfd_op, rev->fd, rev);
1207 RTE_LOG(ERR, EAL, "event op type mismatch\n");
1215 rte_intr_free_epoll_fd(struct rte_intr_handle *intr_handle)
1218 struct rte_epoll_event *rev;
1220 for (i = 0; i < intr_handle->nb_efd; i++) {
1221 rev = &intr_handle->elist[i];
1222 if (rev->status == RTE_EPOLL_INVALID)
1224 if (rte_epoll_ctl(rev->epfd, EPOLL_CTL_DEL, rev->fd, rev)) {
1225 /* force free if the entry valid */
1226 eal_epoll_data_safe_free(rev);
1227 rev->status = RTE_EPOLL_INVALID;
1233 rte_intr_efd_enable(struct rte_intr_handle *intr_handle, uint32_t nb_efd)
1237 uint32_t n = RTE_MIN(nb_efd, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
1239 assert(nb_efd != 0);
1241 if (intr_handle->type == RTE_INTR_HANDLE_VFIO_MSIX) {
1242 for (i = 0; i < n; i++) {
1243 fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
1246 "can't setup eventfd, error %i (%s)\n",
1247 errno, strerror(errno));
1250 intr_handle->efds[i] = fd;
1252 intr_handle->nb_efd = n;
1253 intr_handle->max_intr = NB_OTHER_INTR + n;
1254 } else if (intr_handle->type == RTE_INTR_HANDLE_VDEV) {
1255 /* only check, initialization would be done in vdev driver.*/
1256 if (intr_handle->efd_counter_size >
1257 sizeof(union rte_intr_read_buffer)) {
1258 RTE_LOG(ERR, EAL, "the efd_counter_size is oversized");
1262 intr_handle->efds[0] = intr_handle->fd;
1263 intr_handle->nb_efd = RTE_MIN(nb_efd, 1U);
1264 intr_handle->max_intr = NB_OTHER_INTR;
1271 rte_intr_efd_disable(struct rte_intr_handle *intr_handle)
1275 rte_intr_free_epoll_fd(intr_handle);
1276 if (intr_handle->max_intr > intr_handle->nb_efd) {
1277 for (i = 0; i < intr_handle->nb_efd; i++)
1278 close(intr_handle->efds[i]);
1280 intr_handle->nb_efd = 0;
1281 intr_handle->max_intr = 0;
1285 rte_intr_dp_is_en(struct rte_intr_handle *intr_handle)
1287 return !(!intr_handle->nb_efd);
1291 rte_intr_allow_others(struct rte_intr_handle *intr_handle)
1293 if (!rte_intr_dp_is_en(intr_handle))
1296 return !!(intr_handle->max_intr - intr_handle->nb_efd);
1300 rte_intr_cap_multiple(struct rte_intr_handle *intr_handle)
1302 if (intr_handle->type == RTE_INTR_HANDLE_VFIO_MSIX)
1305 if (intr_handle->type == RTE_INTR_HANDLE_VDEV)