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 */
79 uint8_t pending_delete; /**< delete after callback is called */
80 rte_intr_unregister_callback_fn ucb_fn; /**< fn to call before cb is deleted */
83 struct rte_intr_source {
84 TAILQ_ENTRY(rte_intr_source) next;
85 struct rte_intr_handle intr_handle; /**< interrupt handle */
86 struct rte_intr_cb_list callbacks; /**< user callbacks */
90 /* global spinlock for interrupt data operation */
91 static rte_spinlock_t intr_lock = RTE_SPINLOCK_INITIALIZER;
93 /* union buffer for pipe read/write */
94 static union intr_pipefds intr_pipe;
96 /* interrupt sources list */
97 static struct rte_intr_source_list intr_sources;
99 /* interrupt handling thread */
100 static pthread_t intr_thread;
102 /* VFIO interrupts */
105 #define IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + sizeof(int))
106 /* irq set buffer length for queue interrupts and LSC interrupt */
107 #define MSIX_IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + \
108 sizeof(int) * (RTE_MAX_RXTX_INTR_VEC_ID + 1))
110 /* enable legacy (INTx) interrupts */
112 vfio_enable_intx(const struct rte_intr_handle *intr_handle)
114 struct vfio_irq_set irq_set;
118 irq_set.argsz = sizeof(irq_set);
119 irq_set.flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK;
120 irq_set.index = VFIO_PCI_INTX_IRQ_INDEX;
124 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, &irq_set);
127 RTE_LOG(ERR, EAL, "Error unmasking INTx interrupts for fd %d\n",
134 /* disable legacy (INTx) interrupts */
136 vfio_disable_intx(const struct rte_intr_handle *intr_handle)
138 struct vfio_irq_set irq_set;
141 /* mask interrupts */
142 irq_set.argsz = sizeof(irq_set);
143 irq_set.flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_MASK;
144 irq_set.index = VFIO_PCI_INTX_IRQ_INDEX;
148 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, &irq_set);
151 RTE_LOG(ERR, EAL, "Error masking INTx interrupts for fd %d\n",
158 /* enable MSI-X interrupts */
160 vfio_enable_msix(const struct rte_intr_handle *intr_handle)
162 char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
163 struct vfio_irq_set *irq_set;
166 if (intr_handle->nb_efd == 0)
169 len = sizeof(irq_set_buf);
171 irq_set = (struct vfio_irq_set *) irq_set_buf;
172 irq_set->argsz = len;
173 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
174 irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
175 irq_set->start = RTE_INTR_VEC_RXTX_OFFSET;
176 irq_set->count = intr_handle->nb_efd;
177 memcpy(&irq_set->data, intr_handle->efds,
178 sizeof(*intr_handle->efds) * intr_handle->nb_efd);
180 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
182 RTE_LOG(ERR, EAL, "Error enabling MSI-X interrupts for fd %d\n",
190 /* disable MSI-X interrupts */
192 vfio_disable_msix(const struct rte_intr_handle *intr_handle)
194 struct vfio_irq_set irq_set;
197 if (intr_handle->nb_efd == 0)
200 irq_set.argsz = sizeof(irq_set);
201 irq_set.flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
202 irq_set.index = VFIO_PCI_MSIX_IRQ_INDEX;
203 irq_set.start = RTE_INTR_VEC_RXTX_OFFSET;
204 irq_set.count = intr_handle->nb_efd;
206 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, &irq_set);
209 "Error disabling MSI-X interrupts for fd %d\n", intr_handle->fd);
214 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
215 /* enable req notifier */
217 vfio_enable_req(const struct rte_intr_handle *intr_handle)
220 char irq_set_buf[IRQ_SET_BUF_LEN];
221 struct vfio_irq_set *irq_set;
224 len = sizeof(irq_set_buf);
226 irq_set = (struct vfio_irq_set *) irq_set_buf;
227 irq_set->argsz = len;
229 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
230 VFIO_IRQ_SET_ACTION_TRIGGER;
231 irq_set->index = VFIO_PCI_REQ_IRQ_INDEX;
233 fd_ptr = (int *) &irq_set->data;
234 *fd_ptr = intr_handle->fd;
236 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
239 RTE_LOG(ERR, EAL, "Error enabling req interrupts for fd %d\n",
247 /* disable req notifier */
249 vfio_disable_req(const struct rte_intr_handle *intr_handle)
251 struct vfio_irq_set *irq_set;
252 char irq_set_buf[IRQ_SET_BUF_LEN];
255 len = sizeof(struct vfio_irq_set);
257 irq_set = (struct vfio_irq_set *) irq_set_buf;
258 irq_set->argsz = len;
260 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
261 irq_set->index = VFIO_PCI_REQ_IRQ_INDEX;
264 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
267 RTE_LOG(ERR, EAL, "Error disabling req interrupts for fd %d\n",
276 uio_intx_intr_disable(const struct rte_intr_handle *intr_handle)
278 unsigned char command_high;
280 /* use UIO config file descriptor for uio_pci_generic */
281 if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
283 "Error reading interrupts status for fd %d\n",
284 intr_handle->uio_cfg_fd);
287 /* disable interrupts */
289 if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
291 "Error disabling interrupts for fd %d\n",
292 intr_handle->uio_cfg_fd);
300 uio_intx_intr_enable(const struct rte_intr_handle *intr_handle)
302 unsigned char command_high;
304 /* use UIO config file descriptor for uio_pci_generic */
305 if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
307 "Error reading interrupts status for fd %d\n",
308 intr_handle->uio_cfg_fd);
311 /* enable interrupts */
312 command_high &= ~0x4;
313 if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
315 "Error enabling interrupts for fd %d\n",
316 intr_handle->uio_cfg_fd);
324 uio_intr_disable(const struct rte_intr_handle *intr_handle)
328 if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
330 "Error disabling interrupts for fd %d (%s)\n",
331 intr_handle->fd, strerror(errno));
338 uio_intr_enable(const struct rte_intr_handle *intr_handle)
342 if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
344 "Error enabling interrupts for fd %d (%s)\n",
345 intr_handle->fd, strerror(errno));
352 rte_intr_callback_register(const struct rte_intr_handle *intr_handle,
353 rte_intr_callback_fn cb, void *cb_arg)
355 int ret, wake_thread;
356 struct rte_intr_source *src;
357 struct rte_intr_callback *callback;
361 /* first do parameter checking */
362 if (intr_handle == NULL || intr_handle->fd < 0 || cb == NULL) {
364 "Registering with invalid input parameter\n");
368 /* allocate a new interrupt callback entity */
369 callback = calloc(1, sizeof(*callback));
370 if (callback == NULL) {
371 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
374 callback->cb_fn = cb;
375 callback->cb_arg = cb_arg;
376 callback->pending_delete = 0;
377 callback->ucb_fn = NULL;
379 rte_spinlock_lock(&intr_lock);
381 /* check if there is at least one callback registered for the fd */
382 TAILQ_FOREACH(src, &intr_sources, next) {
383 if (src->intr_handle.fd == intr_handle->fd) {
384 /* we had no interrupts for this */
385 if (TAILQ_EMPTY(&src->callbacks))
388 TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
394 /* no existing callbacks for this - add new source */
396 src = calloc(1, sizeof(*src));
398 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
402 src->intr_handle = *intr_handle;
403 TAILQ_INIT(&src->callbacks);
404 TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
405 TAILQ_INSERT_TAIL(&intr_sources, src, next);
411 rte_spinlock_unlock(&intr_lock);
414 * check if need to notify the pipe fd waited by epoll_wait to
415 * rebuild the wait list.
418 if (write(intr_pipe.writefd, "1", 1) < 0)
425 rte_intr_callback_unregister_pending(const struct rte_intr_handle *intr_handle,
426 rte_intr_callback_fn cb_fn, void *cb_arg,
427 rte_intr_unregister_callback_fn ucb_fn)
430 struct rte_intr_source *src;
431 struct rte_intr_callback *cb, *next;
433 /* do parameter checking first */
434 if (intr_handle == NULL || intr_handle->fd < 0) {
436 "Unregistering with invalid input parameter\n");
440 rte_spinlock_lock(&intr_lock);
442 /* check if the insterrupt source for the fd is existent */
443 TAILQ_FOREACH(src, &intr_sources, next)
444 if (src->intr_handle.fd == intr_handle->fd)
447 /* No interrupt source registered for the fd */
451 /* only usable if the source is active */
452 } else if (src->active == 0) {
458 /* walk through the callbacks and mark all that match. */
459 for (cb = TAILQ_FIRST(&src->callbacks); cb != NULL; cb = next) {
460 next = TAILQ_NEXT(cb, next);
461 if (cb->cb_fn == cb_fn && (cb_arg == (void *)-1 ||
462 cb->cb_arg == cb_arg)) {
463 cb->pending_delete = 1;
470 rte_spinlock_unlock(&intr_lock);
476 rte_intr_callback_unregister(const struct rte_intr_handle *intr_handle,
477 rte_intr_callback_fn cb_fn, void *cb_arg)
480 struct rte_intr_source *src;
481 struct rte_intr_callback *cb, *next;
483 /* do parameter checking first */
484 if (intr_handle == NULL || intr_handle->fd < 0) {
486 "Unregistering with invalid input parameter\n");
490 rte_spinlock_lock(&intr_lock);
492 /* check if the insterrupt source for the fd is existent */
493 TAILQ_FOREACH(src, &intr_sources, next)
494 if (src->intr_handle.fd == intr_handle->fd)
497 /* No interrupt source registered for the fd */
501 /* interrupt source has some active callbacks right now. */
502 } else if (src->active != 0) {
509 /*walk through the callbacks and remove all that match. */
510 for (cb = TAILQ_FIRST(&src->callbacks); cb != NULL; cb = next) {
512 next = TAILQ_NEXT(cb, next);
514 if (cb->cb_fn == cb_fn && (cb_arg == (void *)-1 ||
515 cb->cb_arg == cb_arg)) {
516 TAILQ_REMOVE(&src->callbacks, cb, next);
522 /* all callbacks for that source are removed. */
523 if (TAILQ_EMPTY(&src->callbacks)) {
524 TAILQ_REMOVE(&intr_sources, src, next);
529 rte_spinlock_unlock(&intr_lock);
531 /* notify the pipe fd waited by epoll_wait to rebuild the wait list */
532 if (ret >= 0 && write(intr_pipe.writefd, "1", 1) < 0) {
540 rte_intr_enable(const struct rte_intr_handle *intr_handle)
542 if (intr_handle && intr_handle->type == RTE_INTR_HANDLE_VDEV)
545 if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
548 switch (intr_handle->type){
549 /* write to the uio fd to enable the interrupt */
550 case RTE_INTR_HANDLE_UIO:
551 if (uio_intr_enable(intr_handle))
554 case RTE_INTR_HANDLE_UIO_INTX:
555 if (uio_intx_intr_enable(intr_handle))
558 /* not used at this moment */
559 case RTE_INTR_HANDLE_ALARM:
562 case RTE_INTR_HANDLE_VFIO_MSIX:
563 if (vfio_enable_msix(intr_handle))
566 case RTE_INTR_HANDLE_VFIO_MSI:
568 case RTE_INTR_HANDLE_VFIO_LEGACY:
569 if (vfio_enable_intx(intr_handle))
572 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
573 case RTE_INTR_HANDLE_VFIO_REQ:
574 if (vfio_enable_req(intr_handle))
579 /* not used at this moment */
580 case RTE_INTR_HANDLE_DEV_EVENT:
582 /* unknown handle type */
585 "Unknown handle type of fd %d\n",
594 rte_intr_disable(const struct rte_intr_handle *intr_handle)
596 if (intr_handle && intr_handle->type == RTE_INTR_HANDLE_VDEV)
599 if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
602 switch (intr_handle->type){
603 /* write to the uio fd to disable the interrupt */
604 case RTE_INTR_HANDLE_UIO:
605 if (uio_intr_disable(intr_handle))
608 case RTE_INTR_HANDLE_UIO_INTX:
609 if (uio_intx_intr_disable(intr_handle))
612 /* not used at this moment */
613 case RTE_INTR_HANDLE_ALARM:
616 case RTE_INTR_HANDLE_VFIO_MSIX:
617 if (vfio_disable_msix(intr_handle))
620 case RTE_INTR_HANDLE_VFIO_MSI:
622 case RTE_INTR_HANDLE_VFIO_LEGACY:
623 if (vfio_disable_intx(intr_handle))
626 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
627 case RTE_INTR_HANDLE_VFIO_REQ:
628 if (vfio_disable_req(intr_handle))
633 /* not used at this moment */
634 case RTE_INTR_HANDLE_DEV_EVENT:
636 /* unknown handle type */
639 "Unknown handle type of fd %d\n",
648 eal_intr_process_interrupts(struct epoll_event *events, int nfds)
651 int n, bytes_read, rv;
652 struct rte_intr_source *src;
653 struct rte_intr_callback *cb, *next;
654 union rte_intr_read_buffer buf;
655 struct rte_intr_callback active_cb;
657 for (n = 0; n < nfds; n++) {
660 * if the pipe fd is ready to read, return out to
661 * rebuild the wait list.
663 if (events[n].data.fd == intr_pipe.readfd){
664 int r = read(intr_pipe.readfd, buf.charbuf,
665 sizeof(buf.charbuf));
669 rte_spinlock_lock(&intr_lock);
670 TAILQ_FOREACH(src, &intr_sources, next)
671 if (src->intr_handle.fd ==
675 rte_spinlock_unlock(&intr_lock);
679 /* mark this interrupt source as active and release the lock. */
681 rte_spinlock_unlock(&intr_lock);
683 /* set the length to be read dor different handle type */
684 switch (src->intr_handle.type) {
685 case RTE_INTR_HANDLE_UIO:
686 case RTE_INTR_HANDLE_UIO_INTX:
687 bytes_read = sizeof(buf.uio_intr_count);
689 case RTE_INTR_HANDLE_ALARM:
690 bytes_read = sizeof(buf.timerfd_num);
693 case RTE_INTR_HANDLE_VFIO_MSIX:
694 case RTE_INTR_HANDLE_VFIO_MSI:
695 case RTE_INTR_HANDLE_VFIO_LEGACY:
696 bytes_read = sizeof(buf.vfio_intr_count);
698 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
699 case RTE_INTR_HANDLE_VFIO_REQ:
705 case RTE_INTR_HANDLE_VDEV:
706 case RTE_INTR_HANDLE_EXT:
710 case RTE_INTR_HANDLE_DEV_EVENT:
719 if (bytes_read > 0) {
721 * read out to clear the ready-to-be-read flag
724 bytes_read = read(events[n].data.fd, &buf, bytes_read);
725 if (bytes_read < 0) {
726 if (errno == EINTR || errno == EWOULDBLOCK)
729 RTE_LOG(ERR, EAL, "Error reading from file "
730 "descriptor %d: %s\n",
734 * The device is unplugged or buggy, remove
735 * it as an interrupt source and return to
736 * force the wait list to be rebuilt.
738 rte_spinlock_lock(&intr_lock);
739 TAILQ_REMOVE(&intr_sources, src, next);
740 rte_spinlock_unlock(&intr_lock);
742 for (cb = TAILQ_FIRST(&src->callbacks); cb;
744 next = TAILQ_NEXT(cb, next);
745 TAILQ_REMOVE(&src->callbacks, cb, next);
750 } else if (bytes_read == 0)
751 RTE_LOG(ERR, EAL, "Read nothing from file "
752 "descriptor %d\n", events[n].data.fd);
757 /* grab a lock, again to call callbacks and update status. */
758 rte_spinlock_lock(&intr_lock);
762 /* Finally, call all callbacks. */
763 TAILQ_FOREACH(cb, &src->callbacks, next) {
765 /* make a copy and unlock. */
767 rte_spinlock_unlock(&intr_lock);
769 /* call the actual callback */
770 active_cb.cb_fn(active_cb.cb_arg);
772 /*get the lock back. */
773 rte_spinlock_lock(&intr_lock);
776 /* we done with that interrupt source, release it. */
781 /* check if any callback are supposed to be removed */
782 for (cb = TAILQ_FIRST(&src->callbacks); cb != NULL; cb = next) {
783 next = TAILQ_NEXT(cb, next);
784 if (cb->pending_delete) {
785 TAILQ_REMOVE(&src->callbacks, cb, next);
787 cb->ucb_fn(&src->intr_handle, cb->cb_arg);
793 /* all callbacks for that source are removed. */
794 if (TAILQ_EMPTY(&src->callbacks)) {
795 TAILQ_REMOVE(&intr_sources, src, next);
799 /* notify the pipe fd waited by epoll_wait to rebuild the wait list */
800 if (rv >= 0 && write(intr_pipe.writefd, "1", 1) < 0) {
801 rte_spinlock_unlock(&intr_lock);
805 rte_spinlock_unlock(&intr_lock);
812 * It handles all the interrupts.
815 * epoll file descriptor.
817 * The number of file descriptors added in epoll.
823 eal_intr_handle_interrupts(int pfd, unsigned totalfds)
825 struct epoll_event events[totalfds];
829 nfds = epoll_wait(pfd, events, totalfds,
830 EAL_INTR_EPOLL_WAIT_FOREVER);
831 /* epoll_wait fail */
836 "epoll_wait returns with fail\n");
839 /* epoll_wait timeout, will never happens here */
842 /* epoll_wait has at least one fd ready to read */
843 if (eal_intr_process_interrupts(events, nfds) < 0)
849 * It builds/rebuilds up the epoll file descriptor with all the
850 * file descriptors being waited on. Then handles the interrupts.
858 static __attribute__((noreturn)) void *
859 eal_intr_thread_main(__rte_unused void *arg)
861 struct epoll_event ev;
863 /* host thread, never break out */
865 /* build up the epoll fd with all descriptors we are to
866 * wait on then pass it to the handle_interrupts function
868 static struct epoll_event pipe_event = {
869 .events = EPOLLIN | EPOLLPRI,
871 struct rte_intr_source *src;
874 /* create epoll fd */
875 int pfd = epoll_create(1);
877 rte_panic("Cannot create epoll instance\n");
879 pipe_event.data.fd = intr_pipe.readfd;
881 * add pipe fd into wait list, this pipe is used to
882 * rebuild the wait list.
884 if (epoll_ctl(pfd, EPOLL_CTL_ADD, intr_pipe.readfd,
886 rte_panic("Error adding fd to %d epoll_ctl, %s\n",
887 intr_pipe.readfd, strerror(errno));
891 rte_spinlock_lock(&intr_lock);
893 TAILQ_FOREACH(src, &intr_sources, next) {
894 if (src->callbacks.tqh_first == NULL)
895 continue; /* skip those with no callbacks */
896 ev.events = EPOLLIN | EPOLLPRI | EPOLLRDHUP | EPOLLHUP;
897 ev.data.fd = src->intr_handle.fd;
900 * add all the uio device file descriptor
903 if (epoll_ctl(pfd, EPOLL_CTL_ADD,
904 src->intr_handle.fd, &ev) < 0){
905 rte_panic("Error adding fd %d epoll_ctl, %s\n",
906 src->intr_handle.fd, strerror(errno));
911 rte_spinlock_unlock(&intr_lock);
912 /* serve the interrupt */
913 eal_intr_handle_interrupts(pfd, numfds);
916 * when we return, we need to rebuild the
917 * list of fds to monitor.
924 rte_eal_intr_init(void)
928 /* init the global interrupt source head */
929 TAILQ_INIT(&intr_sources);
932 * create a pipe which will be waited by epoll and notified to
933 * rebuild the wait list of epoll.
935 if (pipe(intr_pipe.pipefd) < 0) {
940 /* create the host thread to wait/handle the interrupt */
941 ret = rte_ctrl_thread_create(&intr_thread, "eal-intr-thread", NULL,
942 eal_intr_thread_main, NULL);
946 "Failed to create thread for interrupt handling\n");
953 eal_intr_proc_rxtx_intr(int fd, const struct rte_intr_handle *intr_handle)
955 union rte_intr_read_buffer buf;
959 switch (intr_handle->type) {
960 case RTE_INTR_HANDLE_UIO:
961 case RTE_INTR_HANDLE_UIO_INTX:
962 bytes_read = sizeof(buf.uio_intr_count);
965 case RTE_INTR_HANDLE_VFIO_MSIX:
966 case RTE_INTR_HANDLE_VFIO_MSI:
967 case RTE_INTR_HANDLE_VFIO_LEGACY:
968 bytes_read = sizeof(buf.vfio_intr_count);
971 case RTE_INTR_HANDLE_VDEV:
972 bytes_read = intr_handle->efd_counter_size;
973 /* For vdev, number of bytes to read is set by driver */
975 case RTE_INTR_HANDLE_EXT:
979 RTE_LOG(INFO, EAL, "unexpected intr type\n");
984 * read out to clear the ready-to-be-read flag
990 nbytes = read(fd, &buf, bytes_read);
992 if (errno == EINTR || errno == EWOULDBLOCK ||
996 "Error reading from fd %d: %s\n",
997 fd, strerror(errno));
998 } else if (nbytes == 0)
999 RTE_LOG(ERR, EAL, "Read nothing from fd %d\n", fd);
1005 eal_epoll_process_event(struct epoll_event *evs, unsigned int n,
1006 struct rte_epoll_event *events)
1008 unsigned int i, count = 0;
1009 struct rte_epoll_event *rev;
1011 for (i = 0; i < n; i++) {
1012 rev = evs[i].data.ptr;
1013 if (!rev || !rte_atomic32_cmpset(&rev->status, RTE_EPOLL_VALID,
1017 events[count].status = RTE_EPOLL_VALID;
1018 events[count].fd = rev->fd;
1019 events[count].epfd = rev->epfd;
1020 events[count].epdata.event = rev->epdata.event;
1021 events[count].epdata.data = rev->epdata.data;
1022 if (rev->epdata.cb_fun)
1023 rev->epdata.cb_fun(rev->fd,
1024 rev->epdata.cb_arg);
1026 rte_compiler_barrier();
1027 rev->status = RTE_EPOLL_VALID;
1034 eal_init_tls_epfd(void)
1036 int pfd = epoll_create(255);
1040 "Cannot create epoll instance\n");
1047 rte_intr_tls_epfd(void)
1049 if (RTE_PER_LCORE(_epfd) == -1)
1050 RTE_PER_LCORE(_epfd) = eal_init_tls_epfd();
1052 return RTE_PER_LCORE(_epfd);
1056 rte_epoll_wait(int epfd, struct rte_epoll_event *events,
1057 int maxevents, int timeout)
1059 struct epoll_event evs[maxevents];
1063 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1067 /* using per thread epoll fd */
1068 if (epfd == RTE_EPOLL_PER_THREAD)
1069 epfd = rte_intr_tls_epfd();
1072 rc = epoll_wait(epfd, evs, maxevents, timeout);
1073 if (likely(rc > 0)) {
1074 /* epoll_wait has at least one fd ready to read */
1075 rc = eal_epoll_process_event(evs, rc, events);
1077 } else if (rc < 0) {
1080 /* epoll_wait fail */
1081 RTE_LOG(ERR, EAL, "epoll_wait returns with fail %s\n",
1086 /* rc == 0, epoll_wait timed out */
1095 eal_epoll_data_safe_free(struct rte_epoll_event *ev)
1097 while (!rte_atomic32_cmpset(&ev->status, RTE_EPOLL_VALID,
1099 while (ev->status != RTE_EPOLL_VALID)
1101 memset(&ev->epdata, 0, sizeof(ev->epdata));
1107 rte_epoll_ctl(int epfd, int op, int fd,
1108 struct rte_epoll_event *event)
1110 struct epoll_event ev;
1113 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1117 /* using per thread epoll fd */
1118 if (epfd == RTE_EPOLL_PER_THREAD)
1119 epfd = rte_intr_tls_epfd();
1121 if (op == EPOLL_CTL_ADD) {
1122 event->status = RTE_EPOLL_VALID;
1123 event->fd = fd; /* ignore fd in event */
1125 ev.data.ptr = (void *)event;
1128 ev.events = event->epdata.event;
1129 if (epoll_ctl(epfd, op, fd, &ev) < 0) {
1130 RTE_LOG(ERR, EAL, "Error op %d fd %d epoll_ctl, %s\n",
1131 op, fd, strerror(errno));
1132 if (op == EPOLL_CTL_ADD)
1133 /* rollback status when CTL_ADD fail */
1134 event->status = RTE_EPOLL_INVALID;
1138 if (op == EPOLL_CTL_DEL && event->status != RTE_EPOLL_INVALID)
1139 eal_epoll_data_safe_free(event);
1145 rte_intr_rx_ctl(struct rte_intr_handle *intr_handle, int epfd,
1146 int op, unsigned int vec, void *data)
1148 struct rte_epoll_event *rev;
1149 struct rte_epoll_data *epdata;
1151 unsigned int efd_idx;
1154 efd_idx = (vec >= RTE_INTR_VEC_RXTX_OFFSET) ?
1155 (vec - RTE_INTR_VEC_RXTX_OFFSET) : vec;
1157 if (!intr_handle || intr_handle->nb_efd == 0 ||
1158 efd_idx >= intr_handle->nb_efd) {
1159 RTE_LOG(ERR, EAL, "Wrong intr vector number.\n");
1164 case RTE_INTR_EVENT_ADD:
1165 epfd_op = EPOLL_CTL_ADD;
1166 rev = &intr_handle->elist[efd_idx];
1167 if (rev->status != RTE_EPOLL_INVALID) {
1168 RTE_LOG(INFO, EAL, "Event already been added.\n");
1172 /* attach to intr vector fd */
1173 epdata = &rev->epdata;
1174 epdata->event = EPOLLIN | EPOLLPRI | EPOLLET;
1175 epdata->data = data;
1176 epdata->cb_fun = (rte_intr_event_cb_t)eal_intr_proc_rxtx_intr;
1177 epdata->cb_arg = (void *)intr_handle;
1178 rc = rte_epoll_ctl(epfd, epfd_op,
1179 intr_handle->efds[efd_idx], rev);
1182 "efd %d associated with vec %d added on epfd %d"
1183 "\n", rev->fd, vec, epfd);
1187 case RTE_INTR_EVENT_DEL:
1188 epfd_op = EPOLL_CTL_DEL;
1189 rev = &intr_handle->elist[efd_idx];
1190 if (rev->status == RTE_EPOLL_INVALID) {
1191 RTE_LOG(INFO, EAL, "Event does not exist.\n");
1195 rc = rte_epoll_ctl(rev->epfd, epfd_op, rev->fd, rev);
1200 RTE_LOG(ERR, EAL, "event op type mismatch\n");
1208 rte_intr_free_epoll_fd(struct rte_intr_handle *intr_handle)
1211 struct rte_epoll_event *rev;
1213 for (i = 0; i < intr_handle->nb_efd; i++) {
1214 rev = &intr_handle->elist[i];
1215 if (rev->status == RTE_EPOLL_INVALID)
1217 if (rte_epoll_ctl(rev->epfd, EPOLL_CTL_DEL, rev->fd, rev)) {
1218 /* force free if the entry valid */
1219 eal_epoll_data_safe_free(rev);
1220 rev->status = RTE_EPOLL_INVALID;
1226 rte_intr_efd_enable(struct rte_intr_handle *intr_handle, uint32_t nb_efd)
1230 uint32_t n = RTE_MIN(nb_efd, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
1232 assert(nb_efd != 0);
1234 if (intr_handle->type == RTE_INTR_HANDLE_VFIO_MSIX) {
1235 for (i = 0; i < n; i++) {
1236 fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
1239 "can't setup eventfd, error %i (%s)\n",
1240 errno, strerror(errno));
1243 intr_handle->efds[i] = fd;
1245 intr_handle->nb_efd = n;
1246 intr_handle->max_intr = NB_OTHER_INTR + n;
1247 } else if (intr_handle->type == RTE_INTR_HANDLE_VDEV) {
1248 /* only check, initialization would be done in vdev driver.*/
1249 if (intr_handle->efd_counter_size >
1250 sizeof(union rte_intr_read_buffer)) {
1251 RTE_LOG(ERR, EAL, "the efd_counter_size is oversized");
1255 intr_handle->efds[0] = intr_handle->fd;
1256 intr_handle->nb_efd = RTE_MIN(nb_efd, 1U);
1257 intr_handle->max_intr = NB_OTHER_INTR;
1264 rte_intr_efd_disable(struct rte_intr_handle *intr_handle)
1268 rte_intr_free_epoll_fd(intr_handle);
1269 if (intr_handle->max_intr > intr_handle->nb_efd) {
1270 for (i = 0; i < intr_handle->nb_efd; i++)
1271 close(intr_handle->efds[i]);
1273 intr_handle->nb_efd = 0;
1274 intr_handle->max_intr = 0;
1278 rte_intr_dp_is_en(struct rte_intr_handle *intr_handle)
1280 return !(!intr_handle->nb_efd);
1284 rte_intr_allow_others(struct rte_intr_handle *intr_handle)
1286 if (!rte_intr_dp_is_en(intr_handle))
1289 return !!(intr_handle->max_intr - intr_handle->nb_efd);
1293 rte_intr_cap_multiple(struct rte_intr_handle *intr_handle)
1295 if (intr_handle->type == RTE_INTR_HANDLE_VFIO_MSIX)
1298 if (intr_handle->type == RTE_INTR_HANDLE_VDEV)