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>
37 #include "eal_private.h"
39 #include "eal_thread.h"
41 #define EAL_INTR_EPOLL_WAIT_FOREVER (-1)
42 #define NB_OTHER_INTR 1
44 static RTE_DEFINE_PER_LCORE(int, _epfd) = -1; /**< epoll fd per thread */
60 * union buffer for reading on different devices
62 union rte_intr_read_buffer {
63 int uio_intr_count; /* for uio device */
65 uint64_t vfio_intr_count; /* for vfio device */
67 uint64_t timerfd_num; /* for timerfd */
68 char charbuf[16]; /* for others */
71 TAILQ_HEAD(rte_intr_cb_list, rte_intr_callback);
72 TAILQ_HEAD(rte_intr_source_list, rte_intr_source);
74 struct rte_intr_callback {
75 TAILQ_ENTRY(rte_intr_callback) next;
76 rte_intr_callback_fn cb_fn; /**< callback address */
77 void *cb_arg; /**< parameter for callback */
80 struct rte_intr_source {
81 TAILQ_ENTRY(rte_intr_source) next;
82 struct rte_intr_handle intr_handle; /**< interrupt handle */
83 struct rte_intr_cb_list callbacks; /**< user callbacks */
87 /* global spinlock for interrupt data operation */
88 static rte_spinlock_t intr_lock = RTE_SPINLOCK_INITIALIZER;
90 /* union buffer for pipe read/write */
91 static union intr_pipefds intr_pipe;
93 /* interrupt sources list */
94 static struct rte_intr_source_list intr_sources;
96 /* interrupt handling thread */
97 static pthread_t intr_thread;
102 #define IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + sizeof(int))
103 /* irq set buffer length for queue interrupts and LSC interrupt */
104 #define MSIX_IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + \
105 sizeof(int) * (RTE_MAX_RXTX_INTR_VEC_ID + 1))
107 /* enable legacy (INTx) interrupts */
109 vfio_enable_intx(const struct rte_intr_handle *intr_handle) {
110 struct vfio_irq_set *irq_set;
111 char irq_set_buf[IRQ_SET_BUF_LEN];
115 len = sizeof(irq_set_buf);
118 irq_set = (struct vfio_irq_set *) irq_set_buf;
119 irq_set->argsz = len;
121 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
122 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
124 fd_ptr = (int *) &irq_set->data;
125 *fd_ptr = intr_handle->fd;
127 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
130 RTE_LOG(ERR, EAL, "Error enabling INTx interrupts for fd %d\n",
135 /* unmask INTx after enabling */
136 memset(irq_set, 0, len);
137 len = sizeof(struct vfio_irq_set);
138 irq_set->argsz = len;
140 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK;
141 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
144 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
147 RTE_LOG(ERR, EAL, "Error unmasking INTx interrupts for fd %d\n",
154 /* disable legacy (INTx) interrupts */
156 vfio_disable_intx(const struct rte_intr_handle *intr_handle) {
157 struct vfio_irq_set *irq_set;
158 char irq_set_buf[IRQ_SET_BUF_LEN];
161 len = sizeof(struct vfio_irq_set);
163 /* mask interrupts before disabling */
164 irq_set = (struct vfio_irq_set *) irq_set_buf;
165 irq_set->argsz = len;
167 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_MASK;
168 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
171 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
174 RTE_LOG(ERR, EAL, "Error masking INTx interrupts for fd %d\n",
180 memset(irq_set, 0, len);
181 irq_set->argsz = len;
183 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
184 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
187 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
191 "Error disabling INTx interrupts for fd %d\n", intr_handle->fd);
197 /* enable MSI interrupts */
199 vfio_enable_msi(const struct rte_intr_handle *intr_handle) {
201 char irq_set_buf[IRQ_SET_BUF_LEN];
202 struct vfio_irq_set *irq_set;
205 len = sizeof(irq_set_buf);
207 irq_set = (struct vfio_irq_set *) irq_set_buf;
208 irq_set->argsz = len;
210 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
211 irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
213 fd_ptr = (int *) &irq_set->data;
214 *fd_ptr = intr_handle->fd;
216 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
219 RTE_LOG(ERR, EAL, "Error enabling MSI interrupts for fd %d\n",
226 /* disable MSI interrupts */
228 vfio_disable_msi(const struct rte_intr_handle *intr_handle) {
229 struct vfio_irq_set *irq_set;
230 char irq_set_buf[IRQ_SET_BUF_LEN];
233 len = sizeof(struct vfio_irq_set);
235 irq_set = (struct vfio_irq_set *) irq_set_buf;
236 irq_set->argsz = len;
238 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
239 irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
242 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
246 "Error disabling MSI interrupts for fd %d\n", intr_handle->fd);
251 /* enable MSI-X interrupts */
253 vfio_enable_msix(const struct rte_intr_handle *intr_handle) {
255 char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
256 struct vfio_irq_set *irq_set;
259 len = sizeof(irq_set_buf);
261 irq_set = (struct vfio_irq_set *) irq_set_buf;
262 irq_set->argsz = len;
263 /* 0 < irq_set->count < RTE_MAX_RXTX_INTR_VEC_ID + 1 */
264 irq_set->count = intr_handle->max_intr ?
265 (intr_handle->max_intr > RTE_MAX_RXTX_INTR_VEC_ID + 1 ?
266 RTE_MAX_RXTX_INTR_VEC_ID + 1 : intr_handle->max_intr) : 1;
267 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
268 irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
270 fd_ptr = (int *) &irq_set->data;
271 /* INTR vector offset 0 reserve for non-efds mapping */
272 fd_ptr[RTE_INTR_VEC_ZERO_OFFSET] = intr_handle->fd;
273 memcpy(&fd_ptr[RTE_INTR_VEC_RXTX_OFFSET], intr_handle->efds,
274 sizeof(*intr_handle->efds) * intr_handle->nb_efd);
276 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
279 RTE_LOG(ERR, EAL, "Error enabling MSI-X interrupts for fd %d\n",
287 /* disable MSI-X interrupts */
289 vfio_disable_msix(const struct rte_intr_handle *intr_handle) {
290 struct vfio_irq_set *irq_set;
291 char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
294 len = sizeof(struct vfio_irq_set);
296 irq_set = (struct vfio_irq_set *) irq_set_buf;
297 irq_set->argsz = len;
299 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
300 irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
303 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
307 "Error disabling MSI-X interrupts for fd %d\n", intr_handle->fd);
312 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
313 /* enable req notifier */
315 vfio_enable_req(const struct rte_intr_handle *intr_handle)
318 char irq_set_buf[IRQ_SET_BUF_LEN];
319 struct vfio_irq_set *irq_set;
322 len = sizeof(irq_set_buf);
324 irq_set = (struct vfio_irq_set *) irq_set_buf;
325 irq_set->argsz = len;
327 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
328 VFIO_IRQ_SET_ACTION_TRIGGER;
329 irq_set->index = VFIO_PCI_REQ_IRQ_INDEX;
331 fd_ptr = (int *) &irq_set->data;
332 *fd_ptr = intr_handle->fd;
334 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
337 RTE_LOG(ERR, EAL, "Error enabling req interrupts for fd %d\n",
345 /* disable req notifier */
347 vfio_disable_req(const struct rte_intr_handle *intr_handle)
349 struct vfio_irq_set *irq_set;
350 char irq_set_buf[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_REQ_IRQ_INDEX;
362 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
365 RTE_LOG(ERR, EAL, "Error disabling req interrupts for fd %d\n",
374 uio_intx_intr_disable(const struct rte_intr_handle *intr_handle)
376 unsigned char command_high;
378 /* use UIO config file descriptor for uio_pci_generic */
379 if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
381 "Error reading interrupts status for fd %d\n",
382 intr_handle->uio_cfg_fd);
385 /* disable interrupts */
387 if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
389 "Error disabling interrupts for fd %d\n",
390 intr_handle->uio_cfg_fd);
398 uio_intx_intr_enable(const struct rte_intr_handle *intr_handle)
400 unsigned char command_high;
402 /* use UIO config file descriptor for uio_pci_generic */
403 if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
405 "Error reading interrupts status for fd %d\n",
406 intr_handle->uio_cfg_fd);
409 /* enable interrupts */
410 command_high &= ~0x4;
411 if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
413 "Error enabling interrupts for fd %d\n",
414 intr_handle->uio_cfg_fd);
422 uio_intr_disable(const struct rte_intr_handle *intr_handle)
426 if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
428 "Error disabling interrupts for fd %d (%s)\n",
429 intr_handle->fd, strerror(errno));
436 uio_intr_enable(const struct rte_intr_handle *intr_handle)
440 if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
442 "Error enabling interrupts for fd %d (%s)\n",
443 intr_handle->fd, strerror(errno));
450 rte_intr_callback_register(const struct rte_intr_handle *intr_handle,
451 rte_intr_callback_fn cb, void *cb_arg)
453 int ret, wake_thread;
454 struct rte_intr_source *src;
455 struct rte_intr_callback *callback;
459 /* first do parameter checking */
460 if (intr_handle == NULL || intr_handle->fd < 0 || cb == NULL) {
462 "Registering with invalid input parameter\n");
466 /* allocate a new interrupt callback entity */
467 callback = calloc(1, sizeof(*callback));
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 src = calloc(1, sizeof(*src));
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))
619 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
620 case RTE_INTR_HANDLE_VFIO_REQ:
621 if (vfio_enable_req(intr_handle))
626 /* not used at this moment */
627 case RTE_INTR_HANDLE_DEV_EVENT:
629 /* unknown handle type */
632 "Unknown handle type of fd %d\n",
641 rte_intr_disable(const struct rte_intr_handle *intr_handle)
643 if (intr_handle && intr_handle->type == RTE_INTR_HANDLE_VDEV)
646 if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
649 switch (intr_handle->type){
650 /* write to the uio fd to disable the interrupt */
651 case RTE_INTR_HANDLE_UIO:
652 if (uio_intr_disable(intr_handle))
655 case RTE_INTR_HANDLE_UIO_INTX:
656 if (uio_intx_intr_disable(intr_handle))
659 /* not used at this moment */
660 case RTE_INTR_HANDLE_ALARM:
663 case RTE_INTR_HANDLE_VFIO_MSIX:
664 if (vfio_disable_msix(intr_handle))
667 case RTE_INTR_HANDLE_VFIO_MSI:
668 if (vfio_disable_msi(intr_handle))
671 case RTE_INTR_HANDLE_VFIO_LEGACY:
672 if (vfio_disable_intx(intr_handle))
675 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
676 case RTE_INTR_HANDLE_VFIO_REQ:
677 if (vfio_disable_req(intr_handle))
682 /* not used at this moment */
683 case RTE_INTR_HANDLE_DEV_EVENT:
685 /* unknown handle type */
688 "Unknown handle type of fd %d\n",
697 eal_intr_process_interrupts(struct epoll_event *events, int nfds)
701 struct rte_intr_source *src;
702 struct rte_intr_callback *cb;
703 union rte_intr_read_buffer buf;
704 struct rte_intr_callback active_cb;
706 for (n = 0; n < nfds; n++) {
709 * if the pipe fd is ready to read, return out to
710 * rebuild the wait list.
712 if (events[n].data.fd == intr_pipe.readfd){
713 int r = read(intr_pipe.readfd, buf.charbuf,
714 sizeof(buf.charbuf));
718 rte_spinlock_lock(&intr_lock);
719 TAILQ_FOREACH(src, &intr_sources, next)
720 if (src->intr_handle.fd ==
724 rte_spinlock_unlock(&intr_lock);
728 /* mark this interrupt source as active and release the lock. */
730 rte_spinlock_unlock(&intr_lock);
732 /* set the length to be read dor different handle type */
733 switch (src->intr_handle.type) {
734 case RTE_INTR_HANDLE_UIO:
735 case RTE_INTR_HANDLE_UIO_INTX:
736 bytes_read = sizeof(buf.uio_intr_count);
738 case RTE_INTR_HANDLE_ALARM:
739 bytes_read = sizeof(buf.timerfd_num);
742 case RTE_INTR_HANDLE_VFIO_MSIX:
743 case RTE_INTR_HANDLE_VFIO_MSI:
744 case RTE_INTR_HANDLE_VFIO_LEGACY:
745 bytes_read = sizeof(buf.vfio_intr_count);
747 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
748 case RTE_INTR_HANDLE_VFIO_REQ:
754 case RTE_INTR_HANDLE_VDEV:
755 case RTE_INTR_HANDLE_EXT:
759 case RTE_INTR_HANDLE_DEV_EVENT:
768 if (bytes_read > 0) {
770 * read out to clear the ready-to-be-read flag
773 bytes_read = read(events[n].data.fd, &buf, bytes_read);
774 if (bytes_read < 0) {
775 if (errno == EINTR || errno == EWOULDBLOCK)
778 RTE_LOG(ERR, EAL, "Error reading from file "
779 "descriptor %d: %s\n",
782 } else if (bytes_read == 0)
783 RTE_LOG(ERR, EAL, "Read nothing from file "
784 "descriptor %d\n", events[n].data.fd);
789 /* grab a lock, again to call callbacks and update status. */
790 rte_spinlock_lock(&intr_lock);
794 /* Finally, call all callbacks. */
795 TAILQ_FOREACH(cb, &src->callbacks, next) {
797 /* make a copy and unlock. */
799 rte_spinlock_unlock(&intr_lock);
801 /* call the actual callback */
802 active_cb.cb_fn(active_cb.cb_arg);
804 /*get the lock back. */
805 rte_spinlock_lock(&intr_lock);
809 /* we done with that interrupt source, release it. */
811 rte_spinlock_unlock(&intr_lock);
818 * It handles all the interrupts.
821 * epoll file descriptor.
823 * The number of file descriptors added in epoll.
829 eal_intr_handle_interrupts(int pfd, unsigned totalfds)
831 struct epoll_event events[totalfds];
835 nfds = epoll_wait(pfd, events, totalfds,
836 EAL_INTR_EPOLL_WAIT_FOREVER);
837 /* epoll_wait fail */
842 "epoll_wait returns with fail\n");
845 /* epoll_wait timeout, will never happens here */
848 /* epoll_wait has at least one fd ready to read */
849 if (eal_intr_process_interrupts(events, nfds) < 0)
855 * It builds/rebuilds up the epoll file descriptor with all the
856 * file descriptors being waited on. Then handles the interrupts.
864 static __attribute__((noreturn)) void *
865 eal_intr_thread_main(__rte_unused void *arg)
867 struct epoll_event ev;
869 /* host thread, never break out */
871 /* build up the epoll fd with all descriptors we are to
872 * wait on then pass it to the handle_interrupts function
874 static struct epoll_event pipe_event = {
875 .events = EPOLLIN | EPOLLPRI,
877 struct rte_intr_source *src;
880 /* create epoll fd */
881 int pfd = epoll_create(1);
883 rte_panic("Cannot create epoll instance\n");
885 pipe_event.data.fd = intr_pipe.readfd;
887 * add pipe fd into wait list, this pipe is used to
888 * rebuild the wait list.
890 if (epoll_ctl(pfd, EPOLL_CTL_ADD, intr_pipe.readfd,
892 rte_panic("Error adding fd to %d epoll_ctl, %s\n",
893 intr_pipe.readfd, strerror(errno));
897 rte_spinlock_lock(&intr_lock);
899 TAILQ_FOREACH(src, &intr_sources, next) {
900 if (src->callbacks.tqh_first == NULL)
901 continue; /* skip those with no callbacks */
902 ev.events = EPOLLIN | EPOLLPRI | EPOLLRDHUP | EPOLLHUP;
903 ev.data.fd = src->intr_handle.fd;
906 * add all the uio device file descriptor
909 if (epoll_ctl(pfd, EPOLL_CTL_ADD,
910 src->intr_handle.fd, &ev) < 0){
911 rte_panic("Error adding fd %d epoll_ctl, %s\n",
912 src->intr_handle.fd, strerror(errno));
917 rte_spinlock_unlock(&intr_lock);
918 /* serve the interrupt */
919 eal_intr_handle_interrupts(pfd, numfds);
922 * when we return, we need to rebuild the
923 * list of fds to monitor.
930 rte_eal_intr_init(void)
934 /* init the global interrupt source head */
935 TAILQ_INIT(&intr_sources);
938 * create a pipe which will be waited by epoll and notified to
939 * rebuild the wait list of epoll.
941 if (pipe(intr_pipe.pipefd) < 0) {
946 /* create the host thread to wait/handle the interrupt */
947 ret = rte_ctrl_thread_create(&intr_thread, "eal-intr-thread", NULL,
948 eal_intr_thread_main, NULL);
952 "Failed to create thread for interrupt handling\n");
959 eal_intr_proc_rxtx_intr(int fd, const struct rte_intr_handle *intr_handle)
961 union rte_intr_read_buffer buf;
965 switch (intr_handle->type) {
966 case RTE_INTR_HANDLE_UIO:
967 case RTE_INTR_HANDLE_UIO_INTX:
968 bytes_read = sizeof(buf.uio_intr_count);
971 case RTE_INTR_HANDLE_VFIO_MSIX:
972 case RTE_INTR_HANDLE_VFIO_MSI:
973 case RTE_INTR_HANDLE_VFIO_LEGACY:
974 bytes_read = sizeof(buf.vfio_intr_count);
977 case RTE_INTR_HANDLE_VDEV:
978 bytes_read = intr_handle->efd_counter_size;
979 /* For vdev, number of bytes to read is set by driver */
981 case RTE_INTR_HANDLE_EXT:
985 RTE_LOG(INFO, EAL, "unexpected intr type\n");
990 * read out to clear the ready-to-be-read flag
996 nbytes = read(fd, &buf, bytes_read);
998 if (errno == EINTR || errno == EWOULDBLOCK ||
1002 "Error reading from fd %d: %s\n",
1003 fd, strerror(errno));
1004 } else if (nbytes == 0)
1005 RTE_LOG(ERR, EAL, "Read nothing from fd %d\n", fd);
1011 eal_epoll_process_event(struct epoll_event *evs, unsigned int n,
1012 struct rte_epoll_event *events)
1014 unsigned int i, count = 0;
1015 struct rte_epoll_event *rev;
1017 for (i = 0; i < n; i++) {
1018 rev = evs[i].data.ptr;
1019 if (!rev || !rte_atomic32_cmpset(&rev->status, RTE_EPOLL_VALID,
1023 events[count].status = RTE_EPOLL_VALID;
1024 events[count].fd = rev->fd;
1025 events[count].epfd = rev->epfd;
1026 events[count].epdata.event = rev->epdata.event;
1027 events[count].epdata.data = rev->epdata.data;
1028 if (rev->epdata.cb_fun)
1029 rev->epdata.cb_fun(rev->fd,
1030 rev->epdata.cb_arg);
1032 rte_compiler_barrier();
1033 rev->status = RTE_EPOLL_VALID;
1040 eal_init_tls_epfd(void)
1042 int pfd = epoll_create(255);
1046 "Cannot create epoll instance\n");
1053 rte_intr_tls_epfd(void)
1055 if (RTE_PER_LCORE(_epfd) == -1)
1056 RTE_PER_LCORE(_epfd) = eal_init_tls_epfd();
1058 return RTE_PER_LCORE(_epfd);
1062 rte_epoll_wait(int epfd, struct rte_epoll_event *events,
1063 int maxevents, int timeout)
1065 struct epoll_event evs[maxevents];
1069 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1073 /* using per thread epoll fd */
1074 if (epfd == RTE_EPOLL_PER_THREAD)
1075 epfd = rte_intr_tls_epfd();
1078 rc = epoll_wait(epfd, evs, maxevents, timeout);
1079 if (likely(rc > 0)) {
1080 /* epoll_wait has at least one fd ready to read */
1081 rc = eal_epoll_process_event(evs, rc, events);
1083 } else if (rc < 0) {
1086 /* epoll_wait fail */
1087 RTE_LOG(ERR, EAL, "epoll_wait returns with fail %s\n",
1092 /* rc == 0, epoll_wait timed out */
1101 eal_epoll_data_safe_free(struct rte_epoll_event *ev)
1103 while (!rte_atomic32_cmpset(&ev->status, RTE_EPOLL_VALID,
1105 while (ev->status != RTE_EPOLL_VALID)
1107 memset(&ev->epdata, 0, sizeof(ev->epdata));
1113 rte_epoll_ctl(int epfd, int op, int fd,
1114 struct rte_epoll_event *event)
1116 struct epoll_event ev;
1119 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1123 /* using per thread epoll fd */
1124 if (epfd == RTE_EPOLL_PER_THREAD)
1125 epfd = rte_intr_tls_epfd();
1127 if (op == EPOLL_CTL_ADD) {
1128 event->status = RTE_EPOLL_VALID;
1129 event->fd = fd; /* ignore fd in event */
1131 ev.data.ptr = (void *)event;
1134 ev.events = event->epdata.event;
1135 if (epoll_ctl(epfd, op, fd, &ev) < 0) {
1136 RTE_LOG(ERR, EAL, "Error op %d fd %d epoll_ctl, %s\n",
1137 op, fd, strerror(errno));
1138 if (op == EPOLL_CTL_ADD)
1139 /* rollback status when CTL_ADD fail */
1140 event->status = RTE_EPOLL_INVALID;
1144 if (op == EPOLL_CTL_DEL && event->status != RTE_EPOLL_INVALID)
1145 eal_epoll_data_safe_free(event);
1151 rte_intr_rx_ctl(struct rte_intr_handle *intr_handle, int epfd,
1152 int op, unsigned int vec, void *data)
1154 struct rte_epoll_event *rev;
1155 struct rte_epoll_data *epdata;
1157 unsigned int efd_idx;
1160 efd_idx = (vec >= RTE_INTR_VEC_RXTX_OFFSET) ?
1161 (vec - RTE_INTR_VEC_RXTX_OFFSET) : vec;
1163 if (!intr_handle || intr_handle->nb_efd == 0 ||
1164 efd_idx >= intr_handle->nb_efd) {
1165 RTE_LOG(ERR, EAL, "Wrong intr vector number.\n");
1170 case RTE_INTR_EVENT_ADD:
1171 epfd_op = EPOLL_CTL_ADD;
1172 rev = &intr_handle->elist[efd_idx];
1173 if (rev->status != RTE_EPOLL_INVALID) {
1174 RTE_LOG(INFO, EAL, "Event already been added.\n");
1178 /* attach to intr vector fd */
1179 epdata = &rev->epdata;
1180 epdata->event = EPOLLIN | EPOLLPRI | EPOLLET;
1181 epdata->data = data;
1182 epdata->cb_fun = (rte_intr_event_cb_t)eal_intr_proc_rxtx_intr;
1183 epdata->cb_arg = (void *)intr_handle;
1184 rc = rte_epoll_ctl(epfd, epfd_op,
1185 intr_handle->efds[efd_idx], rev);
1188 "efd %d associated with vec %d added on epfd %d"
1189 "\n", rev->fd, vec, epfd);
1193 case RTE_INTR_EVENT_DEL:
1194 epfd_op = EPOLL_CTL_DEL;
1195 rev = &intr_handle->elist[efd_idx];
1196 if (rev->status == RTE_EPOLL_INVALID) {
1197 RTE_LOG(INFO, EAL, "Event does not exist.\n");
1201 rc = rte_epoll_ctl(rev->epfd, epfd_op, rev->fd, rev);
1206 RTE_LOG(ERR, EAL, "event op type mismatch\n");
1214 rte_intr_free_epoll_fd(struct rte_intr_handle *intr_handle)
1217 struct rte_epoll_event *rev;
1219 for (i = 0; i < intr_handle->nb_efd; i++) {
1220 rev = &intr_handle->elist[i];
1221 if (rev->status == RTE_EPOLL_INVALID)
1223 if (rte_epoll_ctl(rev->epfd, EPOLL_CTL_DEL, rev->fd, rev)) {
1224 /* force free if the entry valid */
1225 eal_epoll_data_safe_free(rev);
1226 rev->status = RTE_EPOLL_INVALID;
1232 rte_intr_efd_enable(struct rte_intr_handle *intr_handle, uint32_t nb_efd)
1236 uint32_t n = RTE_MIN(nb_efd, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
1238 assert(nb_efd != 0);
1240 if (intr_handle->type == RTE_INTR_HANDLE_VFIO_MSIX) {
1241 for (i = 0; i < n; i++) {
1242 fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
1245 "can't setup eventfd, error %i (%s)\n",
1246 errno, strerror(errno));
1249 intr_handle->efds[i] = fd;
1251 intr_handle->nb_efd = n;
1252 intr_handle->max_intr = NB_OTHER_INTR + n;
1253 } else if (intr_handle->type == RTE_INTR_HANDLE_VDEV) {
1254 /* only check, initialization would be done in vdev driver.*/
1255 if (intr_handle->efd_counter_size >
1256 sizeof(union rte_intr_read_buffer)) {
1257 RTE_LOG(ERR, EAL, "the efd_counter_size is oversized");
1261 intr_handle->efds[0] = intr_handle->fd;
1262 intr_handle->nb_efd = RTE_MIN(nb_efd, 1U);
1263 intr_handle->max_intr = NB_OTHER_INTR;
1270 rte_intr_efd_disable(struct rte_intr_handle *intr_handle)
1274 rte_intr_free_epoll_fd(intr_handle);
1275 if (intr_handle->max_intr > intr_handle->nb_efd) {
1276 for (i = 0; i < intr_handle->nb_efd; i++)
1277 close(intr_handle->efds[i]);
1279 intr_handle->nb_efd = 0;
1280 intr_handle->max_intr = 0;
1284 rte_intr_dp_is_en(struct rte_intr_handle *intr_handle)
1286 return !(!intr_handle->nb_efd);
1290 rte_intr_allow_others(struct rte_intr_handle *intr_handle)
1292 if (!rte_intr_dp_is_en(intr_handle))
1295 return !!(intr_handle->max_intr - intr_handle->nb_efd);
1299 rte_intr_cap_multiple(struct rte_intr_handle *intr_handle)
1301 if (intr_handle->type == RTE_INTR_HANDLE_VFIO_MSIX)
1304 if (intr_handle->type == RTE_INTR_HANDLE_VDEV)