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, *next;
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",
784 * The device is unplugged or buggy, remove
785 * it as an interrupt source and return to
786 * force the wait list to be rebuilt.
788 rte_spinlock_lock(&intr_lock);
789 TAILQ_REMOVE(&intr_sources, src, next);
790 rte_spinlock_unlock(&intr_lock);
792 for (cb = TAILQ_FIRST(&src->callbacks); cb;
794 next = TAILQ_NEXT(cb, next);
795 TAILQ_REMOVE(&src->callbacks, cb, next);
800 } else if (bytes_read == 0)
801 RTE_LOG(ERR, EAL, "Read nothing from file "
802 "descriptor %d\n", events[n].data.fd);
807 /* grab a lock, again to call callbacks and update status. */
808 rte_spinlock_lock(&intr_lock);
812 /* Finally, call all callbacks. */
813 TAILQ_FOREACH(cb, &src->callbacks, next) {
815 /* make a copy and unlock. */
817 rte_spinlock_unlock(&intr_lock);
819 /* call the actual callback */
820 active_cb.cb_fn(active_cb.cb_arg);
822 /*get the lock back. */
823 rte_spinlock_lock(&intr_lock);
827 /* we done with that interrupt source, release it. */
829 rte_spinlock_unlock(&intr_lock);
836 * It handles all the interrupts.
839 * epoll file descriptor.
841 * The number of file descriptors added in epoll.
847 eal_intr_handle_interrupts(int pfd, unsigned totalfds)
849 struct epoll_event events[totalfds];
853 nfds = epoll_wait(pfd, events, totalfds,
854 EAL_INTR_EPOLL_WAIT_FOREVER);
855 /* epoll_wait fail */
860 "epoll_wait returns with fail\n");
863 /* epoll_wait timeout, will never happens here */
866 /* epoll_wait has at least one fd ready to read */
867 if (eal_intr_process_interrupts(events, nfds) < 0)
873 * It builds/rebuilds up the epoll file descriptor with all the
874 * file descriptors being waited on. Then handles the interrupts.
882 static __attribute__((noreturn)) void *
883 eal_intr_thread_main(__rte_unused void *arg)
885 struct epoll_event ev;
887 /* host thread, never break out */
889 /* build up the epoll fd with all descriptors we are to
890 * wait on then pass it to the handle_interrupts function
892 static struct epoll_event pipe_event = {
893 .events = EPOLLIN | EPOLLPRI,
895 struct rte_intr_source *src;
898 /* create epoll fd */
899 int pfd = epoll_create(1);
901 rte_panic("Cannot create epoll instance\n");
903 pipe_event.data.fd = intr_pipe.readfd;
905 * add pipe fd into wait list, this pipe is used to
906 * rebuild the wait list.
908 if (epoll_ctl(pfd, EPOLL_CTL_ADD, intr_pipe.readfd,
910 rte_panic("Error adding fd to %d epoll_ctl, %s\n",
911 intr_pipe.readfd, strerror(errno));
915 rte_spinlock_lock(&intr_lock);
917 TAILQ_FOREACH(src, &intr_sources, next) {
918 if (src->callbacks.tqh_first == NULL)
919 continue; /* skip those with no callbacks */
920 ev.events = EPOLLIN | EPOLLPRI | EPOLLRDHUP | EPOLLHUP;
921 ev.data.fd = src->intr_handle.fd;
924 * add all the uio device file descriptor
927 if (epoll_ctl(pfd, EPOLL_CTL_ADD,
928 src->intr_handle.fd, &ev) < 0){
929 rte_panic("Error adding fd %d epoll_ctl, %s\n",
930 src->intr_handle.fd, strerror(errno));
935 rte_spinlock_unlock(&intr_lock);
936 /* serve the interrupt */
937 eal_intr_handle_interrupts(pfd, numfds);
940 * when we return, we need to rebuild the
941 * list of fds to monitor.
948 rte_eal_intr_init(void)
952 /* init the global interrupt source head */
953 TAILQ_INIT(&intr_sources);
956 * create a pipe which will be waited by epoll and notified to
957 * rebuild the wait list of epoll.
959 if (pipe(intr_pipe.pipefd) < 0) {
964 /* create the host thread to wait/handle the interrupt */
965 ret = rte_ctrl_thread_create(&intr_thread, "eal-intr-thread", NULL,
966 eal_intr_thread_main, NULL);
970 "Failed to create thread for interrupt handling\n");
977 eal_intr_proc_rxtx_intr(int fd, const struct rte_intr_handle *intr_handle)
979 union rte_intr_read_buffer buf;
983 switch (intr_handle->type) {
984 case RTE_INTR_HANDLE_UIO:
985 case RTE_INTR_HANDLE_UIO_INTX:
986 bytes_read = sizeof(buf.uio_intr_count);
989 case RTE_INTR_HANDLE_VFIO_MSIX:
990 case RTE_INTR_HANDLE_VFIO_MSI:
991 case RTE_INTR_HANDLE_VFIO_LEGACY:
992 bytes_read = sizeof(buf.vfio_intr_count);
995 case RTE_INTR_HANDLE_VDEV:
996 bytes_read = intr_handle->efd_counter_size;
997 /* For vdev, number of bytes to read is set by driver */
999 case RTE_INTR_HANDLE_EXT:
1003 RTE_LOG(INFO, EAL, "unexpected intr type\n");
1008 * read out to clear the ready-to-be-read flag
1011 if (bytes_read == 0)
1014 nbytes = read(fd, &buf, bytes_read);
1016 if (errno == EINTR || errno == EWOULDBLOCK ||
1020 "Error reading from fd %d: %s\n",
1021 fd, strerror(errno));
1022 } else if (nbytes == 0)
1023 RTE_LOG(ERR, EAL, "Read nothing from fd %d\n", fd);
1029 eal_epoll_process_event(struct epoll_event *evs, unsigned int n,
1030 struct rte_epoll_event *events)
1032 unsigned int i, count = 0;
1033 struct rte_epoll_event *rev;
1035 for (i = 0; i < n; i++) {
1036 rev = evs[i].data.ptr;
1037 if (!rev || !rte_atomic32_cmpset(&rev->status, RTE_EPOLL_VALID,
1041 events[count].status = RTE_EPOLL_VALID;
1042 events[count].fd = rev->fd;
1043 events[count].epfd = rev->epfd;
1044 events[count].epdata.event = rev->epdata.event;
1045 events[count].epdata.data = rev->epdata.data;
1046 if (rev->epdata.cb_fun)
1047 rev->epdata.cb_fun(rev->fd,
1048 rev->epdata.cb_arg);
1050 rte_compiler_barrier();
1051 rev->status = RTE_EPOLL_VALID;
1058 eal_init_tls_epfd(void)
1060 int pfd = epoll_create(255);
1064 "Cannot create epoll instance\n");
1071 rte_intr_tls_epfd(void)
1073 if (RTE_PER_LCORE(_epfd) == -1)
1074 RTE_PER_LCORE(_epfd) = eal_init_tls_epfd();
1076 return RTE_PER_LCORE(_epfd);
1080 rte_epoll_wait(int epfd, struct rte_epoll_event *events,
1081 int maxevents, int timeout)
1083 struct epoll_event evs[maxevents];
1087 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1091 /* using per thread epoll fd */
1092 if (epfd == RTE_EPOLL_PER_THREAD)
1093 epfd = rte_intr_tls_epfd();
1096 rc = epoll_wait(epfd, evs, maxevents, timeout);
1097 if (likely(rc > 0)) {
1098 /* epoll_wait has at least one fd ready to read */
1099 rc = eal_epoll_process_event(evs, rc, events);
1101 } else if (rc < 0) {
1104 /* epoll_wait fail */
1105 RTE_LOG(ERR, EAL, "epoll_wait returns with fail %s\n",
1110 /* rc == 0, epoll_wait timed out */
1119 eal_epoll_data_safe_free(struct rte_epoll_event *ev)
1121 while (!rte_atomic32_cmpset(&ev->status, RTE_EPOLL_VALID,
1123 while (ev->status != RTE_EPOLL_VALID)
1125 memset(&ev->epdata, 0, sizeof(ev->epdata));
1131 rte_epoll_ctl(int epfd, int op, int fd,
1132 struct rte_epoll_event *event)
1134 struct epoll_event ev;
1137 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1141 /* using per thread epoll fd */
1142 if (epfd == RTE_EPOLL_PER_THREAD)
1143 epfd = rte_intr_tls_epfd();
1145 if (op == EPOLL_CTL_ADD) {
1146 event->status = RTE_EPOLL_VALID;
1147 event->fd = fd; /* ignore fd in event */
1149 ev.data.ptr = (void *)event;
1152 ev.events = event->epdata.event;
1153 if (epoll_ctl(epfd, op, fd, &ev) < 0) {
1154 RTE_LOG(ERR, EAL, "Error op %d fd %d epoll_ctl, %s\n",
1155 op, fd, strerror(errno));
1156 if (op == EPOLL_CTL_ADD)
1157 /* rollback status when CTL_ADD fail */
1158 event->status = RTE_EPOLL_INVALID;
1162 if (op == EPOLL_CTL_DEL && event->status != RTE_EPOLL_INVALID)
1163 eal_epoll_data_safe_free(event);
1169 rte_intr_rx_ctl(struct rte_intr_handle *intr_handle, int epfd,
1170 int op, unsigned int vec, void *data)
1172 struct rte_epoll_event *rev;
1173 struct rte_epoll_data *epdata;
1175 unsigned int efd_idx;
1178 efd_idx = (vec >= RTE_INTR_VEC_RXTX_OFFSET) ?
1179 (vec - RTE_INTR_VEC_RXTX_OFFSET) : vec;
1181 if (!intr_handle || intr_handle->nb_efd == 0 ||
1182 efd_idx >= intr_handle->nb_efd) {
1183 RTE_LOG(ERR, EAL, "Wrong intr vector number.\n");
1188 case RTE_INTR_EVENT_ADD:
1189 epfd_op = EPOLL_CTL_ADD;
1190 rev = &intr_handle->elist[efd_idx];
1191 if (rev->status != RTE_EPOLL_INVALID) {
1192 RTE_LOG(INFO, EAL, "Event already been added.\n");
1196 /* attach to intr vector fd */
1197 epdata = &rev->epdata;
1198 epdata->event = EPOLLIN | EPOLLPRI | EPOLLET;
1199 epdata->data = data;
1200 epdata->cb_fun = (rte_intr_event_cb_t)eal_intr_proc_rxtx_intr;
1201 epdata->cb_arg = (void *)intr_handle;
1202 rc = rte_epoll_ctl(epfd, epfd_op,
1203 intr_handle->efds[efd_idx], rev);
1206 "efd %d associated with vec %d added on epfd %d"
1207 "\n", rev->fd, vec, epfd);
1211 case RTE_INTR_EVENT_DEL:
1212 epfd_op = EPOLL_CTL_DEL;
1213 rev = &intr_handle->elist[efd_idx];
1214 if (rev->status == RTE_EPOLL_INVALID) {
1215 RTE_LOG(INFO, EAL, "Event does not exist.\n");
1219 rc = rte_epoll_ctl(rev->epfd, epfd_op, rev->fd, rev);
1224 RTE_LOG(ERR, EAL, "event op type mismatch\n");
1232 rte_intr_free_epoll_fd(struct rte_intr_handle *intr_handle)
1235 struct rte_epoll_event *rev;
1237 for (i = 0; i < intr_handle->nb_efd; i++) {
1238 rev = &intr_handle->elist[i];
1239 if (rev->status == RTE_EPOLL_INVALID)
1241 if (rte_epoll_ctl(rev->epfd, EPOLL_CTL_DEL, rev->fd, rev)) {
1242 /* force free if the entry valid */
1243 eal_epoll_data_safe_free(rev);
1244 rev->status = RTE_EPOLL_INVALID;
1250 rte_intr_efd_enable(struct rte_intr_handle *intr_handle, uint32_t nb_efd)
1254 uint32_t n = RTE_MIN(nb_efd, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
1256 assert(nb_efd != 0);
1258 if (intr_handle->type == RTE_INTR_HANDLE_VFIO_MSIX) {
1259 for (i = 0; i < n; i++) {
1260 fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
1263 "can't setup eventfd, error %i (%s)\n",
1264 errno, strerror(errno));
1267 intr_handle->efds[i] = fd;
1269 intr_handle->nb_efd = n;
1270 intr_handle->max_intr = NB_OTHER_INTR + n;
1271 } else if (intr_handle->type == RTE_INTR_HANDLE_VDEV) {
1272 /* only check, initialization would be done in vdev driver.*/
1273 if (intr_handle->efd_counter_size >
1274 sizeof(union rte_intr_read_buffer)) {
1275 RTE_LOG(ERR, EAL, "the efd_counter_size is oversized");
1279 intr_handle->efds[0] = intr_handle->fd;
1280 intr_handle->nb_efd = RTE_MIN(nb_efd, 1U);
1281 intr_handle->max_intr = NB_OTHER_INTR;
1288 rte_intr_efd_disable(struct rte_intr_handle *intr_handle)
1292 rte_intr_free_epoll_fd(intr_handle);
1293 if (intr_handle->max_intr > intr_handle->nb_efd) {
1294 for (i = 0; i < intr_handle->nb_efd; i++)
1295 close(intr_handle->efds[i]);
1297 intr_handle->nb_efd = 0;
1298 intr_handle->max_intr = 0;
1302 rte_intr_dp_is_en(struct rte_intr_handle *intr_handle)
1304 return !(!intr_handle->nb_efd);
1308 rte_intr_allow_others(struct rte_intr_handle *intr_handle)
1310 if (!rte_intr_dp_is_en(intr_handle))
1313 return !!(intr_handle->max_intr - intr_handle->nb_efd);
1317 rte_intr_cap_multiple(struct rte_intr_handle *intr_handle)
1319 if (intr_handle->type == RTE_INTR_HANDLE_VFIO_MSIX)
1322 if (intr_handle->type == RTE_INTR_HANDLE_VDEV)