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_malloc.h>
34 #include <rte_errno.h>
35 #include <rte_spinlock.h>
36 #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);
315 uio_intx_intr_disable(const struct rte_intr_handle *intr_handle)
317 unsigned char command_high;
319 /* use UIO config file descriptor for uio_pci_generic */
320 if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
322 "Error reading interrupts status for fd %d\n",
323 intr_handle->uio_cfg_fd);
326 /* disable interrupts */
328 if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
330 "Error disabling interrupts for fd %d\n",
331 intr_handle->uio_cfg_fd);
339 uio_intx_intr_enable(const struct rte_intr_handle *intr_handle)
341 unsigned char command_high;
343 /* use UIO config file descriptor for uio_pci_generic */
344 if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
346 "Error reading interrupts status for fd %d\n",
347 intr_handle->uio_cfg_fd);
350 /* enable interrupts */
351 command_high &= ~0x4;
352 if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
354 "Error enabling interrupts for fd %d\n",
355 intr_handle->uio_cfg_fd);
363 uio_intr_disable(const struct rte_intr_handle *intr_handle)
367 if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
369 "Error disabling interrupts for fd %d (%s)\n",
370 intr_handle->fd, strerror(errno));
377 uio_intr_enable(const struct rte_intr_handle *intr_handle)
381 if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
383 "Error enabling interrupts for fd %d (%s)\n",
384 intr_handle->fd, strerror(errno));
391 rte_intr_callback_register(const struct rte_intr_handle *intr_handle,
392 rte_intr_callback_fn cb, void *cb_arg)
394 int ret, wake_thread;
395 struct rte_intr_source *src;
396 struct rte_intr_callback *callback;
400 /* first do parameter checking */
401 if (intr_handle == NULL || intr_handle->fd < 0 || cb == NULL) {
403 "Registering with invalid input parameter\n");
407 /* allocate a new interrupt callback entity */
408 callback = rte_zmalloc("interrupt callback list",
409 sizeof(*callback), 0);
410 if (callback == NULL) {
411 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
414 callback->cb_fn = cb;
415 callback->cb_arg = cb_arg;
417 rte_spinlock_lock(&intr_lock);
419 /* check if there is at least one callback registered for the fd */
420 TAILQ_FOREACH(src, &intr_sources, next) {
421 if (src->intr_handle.fd == intr_handle->fd) {
422 /* we had no interrupts for this */
423 if TAILQ_EMPTY(&src->callbacks)
426 TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
432 /* no existing callbacks for this - add new source */
434 if ((src = rte_zmalloc("interrupt source list",
435 sizeof(*src), 0)) == NULL) {
436 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
440 src->intr_handle = *intr_handle;
441 TAILQ_INIT(&src->callbacks);
442 TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
443 TAILQ_INSERT_TAIL(&intr_sources, src, next);
449 rte_spinlock_unlock(&intr_lock);
452 * check if need to notify the pipe fd waited by epoll_wait to
453 * rebuild the wait list.
456 if (write(intr_pipe.writefd, "1", 1) < 0)
463 rte_intr_callback_unregister(const struct rte_intr_handle *intr_handle,
464 rte_intr_callback_fn cb_fn, void *cb_arg)
467 struct rte_intr_source *src;
468 struct rte_intr_callback *cb, *next;
470 /* do parameter checking first */
471 if (intr_handle == NULL || intr_handle->fd < 0) {
473 "Unregistering with invalid input parameter\n");
477 rte_spinlock_lock(&intr_lock);
479 /* check if the insterrupt source for the fd is existent */
480 TAILQ_FOREACH(src, &intr_sources, next)
481 if (src->intr_handle.fd == intr_handle->fd)
484 /* No interrupt source registered for the fd */
488 /* interrupt source has some active callbacks right now. */
489 } else if (src->active != 0) {
496 /*walk through the callbacks and remove all that match. */
497 for (cb = TAILQ_FIRST(&src->callbacks); cb != NULL; cb = next) {
499 next = TAILQ_NEXT(cb, next);
501 if (cb->cb_fn == cb_fn && (cb_arg == (void *)-1 ||
502 cb->cb_arg == cb_arg)) {
503 TAILQ_REMOVE(&src->callbacks, cb, next);
509 /* all callbacks for that source are removed. */
510 if (TAILQ_EMPTY(&src->callbacks)) {
511 TAILQ_REMOVE(&intr_sources, src, next);
516 rte_spinlock_unlock(&intr_lock);
518 /* notify the pipe fd waited by epoll_wait to rebuild the wait list */
519 if (ret >= 0 && write(intr_pipe.writefd, "1", 1) < 0) {
527 rte_intr_enable(const struct rte_intr_handle *intr_handle)
529 if (intr_handle && intr_handle->type == RTE_INTR_HANDLE_VDEV)
532 if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
535 switch (intr_handle->type){
536 /* write to the uio fd to enable the interrupt */
537 case RTE_INTR_HANDLE_UIO:
538 if (uio_intr_enable(intr_handle))
541 case RTE_INTR_HANDLE_UIO_INTX:
542 if (uio_intx_intr_enable(intr_handle))
545 /* not used at this moment */
546 case RTE_INTR_HANDLE_ALARM:
549 case RTE_INTR_HANDLE_VFIO_MSIX:
550 if (vfio_enable_msix(intr_handle))
553 case RTE_INTR_HANDLE_VFIO_MSI:
554 if (vfio_enable_msi(intr_handle))
557 case RTE_INTR_HANDLE_VFIO_LEGACY:
558 if (vfio_enable_intx(intr_handle))
562 /* unknown handle type */
565 "Unknown handle type of fd %d\n",
574 rte_intr_disable(const struct rte_intr_handle *intr_handle)
576 if (intr_handle && intr_handle->type == RTE_INTR_HANDLE_VDEV)
579 if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
582 switch (intr_handle->type){
583 /* write to the uio fd to disable the interrupt */
584 case RTE_INTR_HANDLE_UIO:
585 if (uio_intr_disable(intr_handle))
588 case RTE_INTR_HANDLE_UIO_INTX:
589 if (uio_intx_intr_disable(intr_handle))
592 /* not used at this moment */
593 case RTE_INTR_HANDLE_ALARM:
596 case RTE_INTR_HANDLE_VFIO_MSIX:
597 if (vfio_disable_msix(intr_handle))
600 case RTE_INTR_HANDLE_VFIO_MSI:
601 if (vfio_disable_msi(intr_handle))
604 case RTE_INTR_HANDLE_VFIO_LEGACY:
605 if (vfio_disable_intx(intr_handle))
609 /* unknown handle type */
612 "Unknown handle type of fd %d\n",
621 eal_intr_process_interrupts(struct epoll_event *events, int nfds)
625 struct rte_intr_source *src;
626 struct rte_intr_callback *cb;
627 union rte_intr_read_buffer buf;
628 struct rte_intr_callback active_cb;
630 for (n = 0; n < nfds; n++) {
633 * if the pipe fd is ready to read, return out to
634 * rebuild the wait list.
636 if (events[n].data.fd == intr_pipe.readfd){
637 int r = read(intr_pipe.readfd, buf.charbuf,
638 sizeof(buf.charbuf));
642 rte_spinlock_lock(&intr_lock);
643 TAILQ_FOREACH(src, &intr_sources, next)
644 if (src->intr_handle.fd ==
648 rte_spinlock_unlock(&intr_lock);
652 /* mark this interrupt source as active and release the lock. */
654 rte_spinlock_unlock(&intr_lock);
656 /* set the length to be read dor different handle type */
657 switch (src->intr_handle.type) {
658 case RTE_INTR_HANDLE_UIO:
659 case RTE_INTR_HANDLE_UIO_INTX:
660 bytes_read = sizeof(buf.uio_intr_count);
662 case RTE_INTR_HANDLE_ALARM:
663 bytes_read = sizeof(buf.timerfd_num);
666 case RTE_INTR_HANDLE_VFIO_MSIX:
667 case RTE_INTR_HANDLE_VFIO_MSI:
668 case RTE_INTR_HANDLE_VFIO_LEGACY:
669 bytes_read = sizeof(buf.vfio_intr_count);
672 case RTE_INTR_HANDLE_VDEV:
673 case RTE_INTR_HANDLE_EXT:
683 if (bytes_read > 0) {
685 * read out to clear the ready-to-be-read flag
688 bytes_read = read(events[n].data.fd, &buf, bytes_read);
689 if (bytes_read < 0) {
690 if (errno == EINTR || errno == EWOULDBLOCK)
693 RTE_LOG(ERR, EAL, "Error reading from file "
694 "descriptor %d: %s\n",
697 } else if (bytes_read == 0)
698 RTE_LOG(ERR, EAL, "Read nothing from file "
699 "descriptor %d\n", events[n].data.fd);
704 /* grab a lock, again to call callbacks and update status. */
705 rte_spinlock_lock(&intr_lock);
709 /* Finally, call all callbacks. */
710 TAILQ_FOREACH(cb, &src->callbacks, next) {
712 /* make a copy and unlock. */
714 rte_spinlock_unlock(&intr_lock);
716 /* call the actual callback */
717 active_cb.cb_fn(active_cb.cb_arg);
719 /*get the lock back. */
720 rte_spinlock_lock(&intr_lock);
724 /* we done with that interrupt source, release it. */
726 rte_spinlock_unlock(&intr_lock);
733 * It handles all the interrupts.
736 * epoll file descriptor.
738 * The number of file descriptors added in epoll.
744 eal_intr_handle_interrupts(int pfd, unsigned totalfds)
746 struct epoll_event events[totalfds];
750 nfds = epoll_wait(pfd, events, totalfds,
751 EAL_INTR_EPOLL_WAIT_FOREVER);
752 /* epoll_wait fail */
757 "epoll_wait returns with fail\n");
760 /* epoll_wait timeout, will never happens here */
763 /* epoll_wait has at least one fd ready to read */
764 if (eal_intr_process_interrupts(events, nfds) < 0)
770 * It builds/rebuilds up the epoll file descriptor with all the
771 * file descriptors being waited on. Then handles the interrupts.
779 static __attribute__((noreturn)) void *
780 eal_intr_thread_main(__rte_unused void *arg)
782 struct epoll_event ev;
784 /* host thread, never break out */
786 /* build up the epoll fd with all descriptors we are to
787 * wait on then pass it to the handle_interrupts function
789 static struct epoll_event pipe_event = {
790 .events = EPOLLIN | EPOLLPRI,
792 struct rte_intr_source *src;
795 /* create epoll fd */
796 int pfd = epoll_create(1);
798 rte_panic("Cannot create epoll instance\n");
800 pipe_event.data.fd = intr_pipe.readfd;
802 * add pipe fd into wait list, this pipe is used to
803 * rebuild the wait list.
805 if (epoll_ctl(pfd, EPOLL_CTL_ADD, intr_pipe.readfd,
807 rte_panic("Error adding fd to %d epoll_ctl, %s\n",
808 intr_pipe.readfd, strerror(errno));
812 rte_spinlock_lock(&intr_lock);
814 TAILQ_FOREACH(src, &intr_sources, next) {
815 if (src->callbacks.tqh_first == NULL)
816 continue; /* skip those with no callbacks */
817 ev.events = EPOLLIN | EPOLLPRI | EPOLLRDHUP | EPOLLHUP;
818 ev.data.fd = src->intr_handle.fd;
821 * add all the uio device file descriptor
824 if (epoll_ctl(pfd, EPOLL_CTL_ADD,
825 src->intr_handle.fd, &ev) < 0){
826 rte_panic("Error adding fd %d epoll_ctl, %s\n",
827 src->intr_handle.fd, strerror(errno));
832 rte_spinlock_unlock(&intr_lock);
833 /* serve the interrupt */
834 eal_intr_handle_interrupts(pfd, numfds);
837 * when we return, we need to rebuild the
838 * list of fds to monitor.
845 rte_eal_intr_init(void)
847 int ret = 0, ret_1 = 0;
848 char thread_name[RTE_MAX_THREAD_NAME_LEN];
850 /* init the global interrupt source head */
851 TAILQ_INIT(&intr_sources);
854 * create a pipe which will be waited by epoll and notified to
855 * rebuild the wait list of epoll.
857 if (pipe(intr_pipe.pipefd) < 0) {
862 /* create the host thread to wait/handle the interrupt */
863 ret = pthread_create(&intr_thread, NULL,
864 eal_intr_thread_main, NULL);
868 "Failed to create thread for interrupt handling\n");
870 /* Set thread_name for aid in debugging. */
871 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN,
873 ret_1 = rte_thread_setname(intr_thread, thread_name);
876 "Failed to set thread name for interrupt handling\n");
883 eal_intr_proc_rxtx_intr(int fd, const struct rte_intr_handle *intr_handle)
885 union rte_intr_read_buffer buf;
889 switch (intr_handle->type) {
890 case RTE_INTR_HANDLE_UIO:
891 case RTE_INTR_HANDLE_UIO_INTX:
892 bytes_read = sizeof(buf.uio_intr_count);
895 case RTE_INTR_HANDLE_VFIO_MSIX:
896 case RTE_INTR_HANDLE_VFIO_MSI:
897 case RTE_INTR_HANDLE_VFIO_LEGACY:
898 bytes_read = sizeof(buf.vfio_intr_count);
901 case RTE_INTR_HANDLE_VDEV:
902 bytes_read = intr_handle->efd_counter_size;
903 /* For vdev, number of bytes to read is set by driver */
905 case RTE_INTR_HANDLE_EXT:
909 RTE_LOG(INFO, EAL, "unexpected intr type\n");
914 * read out to clear the ready-to-be-read flag
920 nbytes = read(fd, &buf, bytes_read);
922 if (errno == EINTR || errno == EWOULDBLOCK ||
926 "Error reading from fd %d: %s\n",
927 fd, strerror(errno));
928 } else if (nbytes == 0)
929 RTE_LOG(ERR, EAL, "Read nothing from fd %d\n", fd);
935 eal_epoll_process_event(struct epoll_event *evs, unsigned int n,
936 struct rte_epoll_event *events)
938 unsigned int i, count = 0;
939 struct rte_epoll_event *rev;
941 for (i = 0; i < n; i++) {
942 rev = evs[i].data.ptr;
943 if (!rev || !rte_atomic32_cmpset(&rev->status, RTE_EPOLL_VALID,
947 events[count].status = RTE_EPOLL_VALID;
948 events[count].fd = rev->fd;
949 events[count].epfd = rev->epfd;
950 events[count].epdata.event = rev->epdata.event;
951 events[count].epdata.data = rev->epdata.data;
952 if (rev->epdata.cb_fun)
953 rev->epdata.cb_fun(rev->fd,
956 rte_compiler_barrier();
957 rev->status = RTE_EPOLL_VALID;
964 eal_init_tls_epfd(void)
966 int pfd = epoll_create(255);
970 "Cannot create epoll instance\n");
977 rte_intr_tls_epfd(void)
979 if (RTE_PER_LCORE(_epfd) == -1)
980 RTE_PER_LCORE(_epfd) = eal_init_tls_epfd();
982 return RTE_PER_LCORE(_epfd);
986 rte_epoll_wait(int epfd, struct rte_epoll_event *events,
987 int maxevents, int timeout)
989 struct epoll_event evs[maxevents];
993 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
997 /* using per thread epoll fd */
998 if (epfd == RTE_EPOLL_PER_THREAD)
999 epfd = rte_intr_tls_epfd();
1002 rc = epoll_wait(epfd, evs, maxevents, timeout);
1003 if (likely(rc > 0)) {
1004 /* epoll_wait has at least one fd ready to read */
1005 rc = eal_epoll_process_event(evs, rc, events);
1007 } else if (rc < 0) {
1010 /* epoll_wait fail */
1011 RTE_LOG(ERR, EAL, "epoll_wait returns with fail %s\n",
1016 /* rc == 0, epoll_wait timed out */
1025 eal_epoll_data_safe_free(struct rte_epoll_event *ev)
1027 while (!rte_atomic32_cmpset(&ev->status, RTE_EPOLL_VALID,
1029 while (ev->status != RTE_EPOLL_VALID)
1031 memset(&ev->epdata, 0, sizeof(ev->epdata));
1037 rte_epoll_ctl(int epfd, int op, int fd,
1038 struct rte_epoll_event *event)
1040 struct epoll_event ev;
1043 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1047 /* using per thread epoll fd */
1048 if (epfd == RTE_EPOLL_PER_THREAD)
1049 epfd = rte_intr_tls_epfd();
1051 if (op == EPOLL_CTL_ADD) {
1052 event->status = RTE_EPOLL_VALID;
1053 event->fd = fd; /* ignore fd in event */
1055 ev.data.ptr = (void *)event;
1058 ev.events = event->epdata.event;
1059 if (epoll_ctl(epfd, op, fd, &ev) < 0) {
1060 RTE_LOG(ERR, EAL, "Error op %d fd %d epoll_ctl, %s\n",
1061 op, fd, strerror(errno));
1062 if (op == EPOLL_CTL_ADD)
1063 /* rollback status when CTL_ADD fail */
1064 event->status = RTE_EPOLL_INVALID;
1068 if (op == EPOLL_CTL_DEL && event->status != RTE_EPOLL_INVALID)
1069 eal_epoll_data_safe_free(event);
1075 rte_intr_rx_ctl(struct rte_intr_handle *intr_handle, int epfd,
1076 int op, unsigned int vec, void *data)
1078 struct rte_epoll_event *rev;
1079 struct rte_epoll_data *epdata;
1081 unsigned int efd_idx;
1084 efd_idx = (vec >= RTE_INTR_VEC_RXTX_OFFSET) ?
1085 (vec - RTE_INTR_VEC_RXTX_OFFSET) : vec;
1087 if (!intr_handle || intr_handle->nb_efd == 0 ||
1088 efd_idx >= intr_handle->nb_efd) {
1089 RTE_LOG(ERR, EAL, "Wrong intr vector number.\n");
1094 case RTE_INTR_EVENT_ADD:
1095 epfd_op = EPOLL_CTL_ADD;
1096 rev = &intr_handle->elist[efd_idx];
1097 if (rev->status != RTE_EPOLL_INVALID) {
1098 RTE_LOG(INFO, EAL, "Event already been added.\n");
1102 /* attach to intr vector fd */
1103 epdata = &rev->epdata;
1104 epdata->event = EPOLLIN | EPOLLPRI | EPOLLET;
1105 epdata->data = data;
1106 epdata->cb_fun = (rte_intr_event_cb_t)eal_intr_proc_rxtx_intr;
1107 epdata->cb_arg = (void *)intr_handle;
1108 rc = rte_epoll_ctl(epfd, epfd_op,
1109 intr_handle->efds[efd_idx], rev);
1112 "efd %d associated with vec %d added on epfd %d"
1113 "\n", rev->fd, vec, epfd);
1117 case RTE_INTR_EVENT_DEL:
1118 epfd_op = EPOLL_CTL_DEL;
1119 rev = &intr_handle->elist[efd_idx];
1120 if (rev->status == RTE_EPOLL_INVALID) {
1121 RTE_LOG(INFO, EAL, "Event does not exist.\n");
1125 rc = rte_epoll_ctl(rev->epfd, epfd_op, rev->fd, rev);
1130 RTE_LOG(ERR, EAL, "event op type mismatch\n");
1138 rte_intr_free_epoll_fd(struct rte_intr_handle *intr_handle)
1141 struct rte_epoll_event *rev;
1143 for (i = 0; i < intr_handle->nb_efd; i++) {
1144 rev = &intr_handle->elist[i];
1145 if (rev->status == RTE_EPOLL_INVALID)
1147 if (rte_epoll_ctl(rev->epfd, EPOLL_CTL_DEL, rev->fd, rev)) {
1148 /* force free if the entry valid */
1149 eal_epoll_data_safe_free(rev);
1150 rev->status = RTE_EPOLL_INVALID;
1156 rte_intr_efd_enable(struct rte_intr_handle *intr_handle, uint32_t nb_efd)
1160 uint32_t n = RTE_MIN(nb_efd, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
1162 assert(nb_efd != 0);
1164 if (intr_handle->type == RTE_INTR_HANDLE_VFIO_MSIX) {
1165 for (i = 0; i < n; i++) {
1166 fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
1169 "can't setup eventfd, error %i (%s)\n",
1170 errno, strerror(errno));
1173 intr_handle->efds[i] = fd;
1175 intr_handle->nb_efd = n;
1176 intr_handle->max_intr = NB_OTHER_INTR + n;
1177 } else if (intr_handle->type == RTE_INTR_HANDLE_VDEV) {
1178 /* only check, initialization would be done in vdev driver.*/
1179 if (intr_handle->efd_counter_size >
1180 sizeof(union rte_intr_read_buffer)) {
1181 RTE_LOG(ERR, EAL, "the efd_counter_size is oversized");
1185 intr_handle->efds[0] = intr_handle->fd;
1186 intr_handle->nb_efd = RTE_MIN(nb_efd, 1U);
1187 intr_handle->max_intr = NB_OTHER_INTR;
1194 rte_intr_efd_disable(struct rte_intr_handle *intr_handle)
1198 rte_intr_free_epoll_fd(intr_handle);
1199 if (intr_handle->max_intr > intr_handle->nb_efd) {
1200 for (i = 0; i < intr_handle->nb_efd; i++)
1201 close(intr_handle->efds[i]);
1203 intr_handle->nb_efd = 0;
1204 intr_handle->max_intr = 0;
1208 rte_intr_dp_is_en(struct rte_intr_handle *intr_handle)
1210 return !(!intr_handle->nb_efd);
1214 rte_intr_allow_others(struct rte_intr_handle *intr_handle)
1216 if (!rte_intr_dp_is_en(intr_handle))
1219 return !!(intr_handle->max_intr - intr_handle->nb_efd);
1223 rte_intr_cap_multiple(struct rte_intr_handle *intr_handle)
1225 if (intr_handle->type == RTE_INTR_HANDLE_VFIO_MSIX)
1228 if (intr_handle->type == RTE_INTR_HANDLE_VDEV)