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 /* enable req notifier */
314 vfio_enable_req(const struct rte_intr_handle *intr_handle)
317 char irq_set_buf[IRQ_SET_BUF_LEN];
318 struct vfio_irq_set *irq_set;
321 len = sizeof(irq_set_buf);
323 irq_set = (struct vfio_irq_set *) irq_set_buf;
324 irq_set->argsz = len;
326 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
327 VFIO_IRQ_SET_ACTION_TRIGGER;
328 irq_set->index = VFIO_PCI_REQ_IRQ_INDEX;
330 fd_ptr = (int *) &irq_set->data;
331 *fd_ptr = intr_handle->fd;
333 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
336 RTE_LOG(ERR, EAL, "Error enabling req interrupts for fd %d\n",
344 /* disable req notifier */
346 vfio_disable_req(const struct rte_intr_handle *intr_handle)
348 struct vfio_irq_set *irq_set;
349 char irq_set_buf[IRQ_SET_BUF_LEN];
352 len = sizeof(struct vfio_irq_set);
354 irq_set = (struct vfio_irq_set *) irq_set_buf;
355 irq_set->argsz = len;
357 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
358 irq_set->index = VFIO_PCI_REQ_IRQ_INDEX;
361 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
364 RTE_LOG(ERR, EAL, "Error disabling req interrupts for fd %d\n",
372 uio_intx_intr_disable(const struct rte_intr_handle *intr_handle)
374 unsigned char command_high;
376 /* use UIO config file descriptor for uio_pci_generic */
377 if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
379 "Error reading interrupts status for fd %d\n",
380 intr_handle->uio_cfg_fd);
383 /* disable interrupts */
385 if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
387 "Error disabling interrupts for fd %d\n",
388 intr_handle->uio_cfg_fd);
396 uio_intx_intr_enable(const struct rte_intr_handle *intr_handle)
398 unsigned char command_high;
400 /* use UIO config file descriptor for uio_pci_generic */
401 if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
403 "Error reading interrupts status for fd %d\n",
404 intr_handle->uio_cfg_fd);
407 /* enable interrupts */
408 command_high &= ~0x4;
409 if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
411 "Error enabling interrupts for fd %d\n",
412 intr_handle->uio_cfg_fd);
420 uio_intr_disable(const struct rte_intr_handle *intr_handle)
424 if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
426 "Error disabling interrupts for fd %d (%s)\n",
427 intr_handle->fd, strerror(errno));
434 uio_intr_enable(const struct rte_intr_handle *intr_handle)
438 if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
440 "Error enabling interrupts for fd %d (%s)\n",
441 intr_handle->fd, strerror(errno));
448 rte_intr_callback_register(const struct rte_intr_handle *intr_handle,
449 rte_intr_callback_fn cb, void *cb_arg)
451 int ret, wake_thread;
452 struct rte_intr_source *src;
453 struct rte_intr_callback *callback;
457 /* first do parameter checking */
458 if (intr_handle == NULL || intr_handle->fd < 0 || cb == NULL) {
460 "Registering with invalid input parameter\n");
464 /* allocate a new interrupt callback entity */
465 callback = calloc(1, sizeof(*callback));
466 if (callback == NULL) {
467 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
470 callback->cb_fn = cb;
471 callback->cb_arg = cb_arg;
473 rte_spinlock_lock(&intr_lock);
475 /* check if there is at least one callback registered for the fd */
476 TAILQ_FOREACH(src, &intr_sources, next) {
477 if (src->intr_handle.fd == intr_handle->fd) {
478 /* we had no interrupts for this */
479 if (TAILQ_EMPTY(&src->callbacks))
482 TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
488 /* no existing callbacks for this - add new source */
490 src = calloc(1, sizeof(*src));
492 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
496 src->intr_handle = *intr_handle;
497 TAILQ_INIT(&src->callbacks);
498 TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
499 TAILQ_INSERT_TAIL(&intr_sources, src, next);
505 rte_spinlock_unlock(&intr_lock);
508 * check if need to notify the pipe fd waited by epoll_wait to
509 * rebuild the wait list.
512 if (write(intr_pipe.writefd, "1", 1) < 0)
519 rte_intr_callback_unregister(const struct rte_intr_handle *intr_handle,
520 rte_intr_callback_fn cb_fn, void *cb_arg)
523 struct rte_intr_source *src;
524 struct rte_intr_callback *cb, *next;
526 /* do parameter checking first */
527 if (intr_handle == NULL || intr_handle->fd < 0) {
529 "Unregistering with invalid input parameter\n");
533 rte_spinlock_lock(&intr_lock);
535 /* check if the insterrupt source for the fd is existent */
536 TAILQ_FOREACH(src, &intr_sources, next)
537 if (src->intr_handle.fd == intr_handle->fd)
540 /* No interrupt source registered for the fd */
544 /* interrupt source has some active callbacks right now. */
545 } else if (src->active != 0) {
552 /*walk through the callbacks and remove all that match. */
553 for (cb = TAILQ_FIRST(&src->callbacks); cb != NULL; cb = next) {
555 next = TAILQ_NEXT(cb, next);
557 if (cb->cb_fn == cb_fn && (cb_arg == (void *)-1 ||
558 cb->cb_arg == cb_arg)) {
559 TAILQ_REMOVE(&src->callbacks, cb, next);
565 /* all callbacks for that source are removed. */
566 if (TAILQ_EMPTY(&src->callbacks)) {
567 TAILQ_REMOVE(&intr_sources, src, next);
572 rte_spinlock_unlock(&intr_lock);
574 /* notify the pipe fd waited by epoll_wait to rebuild the wait list */
575 if (ret >= 0 && write(intr_pipe.writefd, "1", 1) < 0) {
583 rte_intr_enable(const struct rte_intr_handle *intr_handle)
585 if (intr_handle && intr_handle->type == RTE_INTR_HANDLE_VDEV)
588 if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
591 switch (intr_handle->type){
592 /* write to the uio fd to enable the interrupt */
593 case RTE_INTR_HANDLE_UIO:
594 if (uio_intr_enable(intr_handle))
597 case RTE_INTR_HANDLE_UIO_INTX:
598 if (uio_intx_intr_enable(intr_handle))
601 /* not used at this moment */
602 case RTE_INTR_HANDLE_ALARM:
605 case RTE_INTR_HANDLE_VFIO_MSIX:
606 if (vfio_enable_msix(intr_handle))
609 case RTE_INTR_HANDLE_VFIO_MSI:
610 if (vfio_enable_msi(intr_handle))
613 case RTE_INTR_HANDLE_VFIO_LEGACY:
614 if (vfio_enable_intx(intr_handle))
617 case RTE_INTR_HANDLE_VFIO_REQ:
618 if (vfio_enable_req(intr_handle))
622 /* not used at this moment */
623 case RTE_INTR_HANDLE_DEV_EVENT:
625 /* unknown handle type */
628 "Unknown handle type of fd %d\n",
637 rte_intr_disable(const struct rte_intr_handle *intr_handle)
639 if (intr_handle && intr_handle->type == RTE_INTR_HANDLE_VDEV)
642 if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
645 switch (intr_handle->type){
646 /* write to the uio fd to disable the interrupt */
647 case RTE_INTR_HANDLE_UIO:
648 if (uio_intr_disable(intr_handle))
651 case RTE_INTR_HANDLE_UIO_INTX:
652 if (uio_intx_intr_disable(intr_handle))
655 /* not used at this moment */
656 case RTE_INTR_HANDLE_ALARM:
659 case RTE_INTR_HANDLE_VFIO_MSIX:
660 if (vfio_disable_msix(intr_handle))
663 case RTE_INTR_HANDLE_VFIO_MSI:
664 if (vfio_disable_msi(intr_handle))
667 case RTE_INTR_HANDLE_VFIO_LEGACY:
668 if (vfio_disable_intx(intr_handle))
671 case RTE_INTR_HANDLE_VFIO_REQ:
672 if (vfio_disable_req(intr_handle))
677 /* not used at this moment */
678 case RTE_INTR_HANDLE_DEV_EVENT:
680 /* unknown handle type */
683 "Unknown handle type of fd %d\n",
692 eal_intr_process_interrupts(struct epoll_event *events, int nfds)
696 struct rte_intr_source *src;
697 struct rte_intr_callback *cb;
698 union rte_intr_read_buffer buf;
699 struct rte_intr_callback active_cb;
701 for (n = 0; n < nfds; n++) {
704 * if the pipe fd is ready to read, return out to
705 * rebuild the wait list.
707 if (events[n].data.fd == intr_pipe.readfd){
708 int r = read(intr_pipe.readfd, buf.charbuf,
709 sizeof(buf.charbuf));
713 rte_spinlock_lock(&intr_lock);
714 TAILQ_FOREACH(src, &intr_sources, next)
715 if (src->intr_handle.fd ==
719 rte_spinlock_unlock(&intr_lock);
723 /* mark this interrupt source as active and release the lock. */
725 rte_spinlock_unlock(&intr_lock);
727 /* set the length to be read dor different handle type */
728 switch (src->intr_handle.type) {
729 case RTE_INTR_HANDLE_UIO:
730 case RTE_INTR_HANDLE_UIO_INTX:
731 bytes_read = sizeof(buf.uio_intr_count);
733 case RTE_INTR_HANDLE_ALARM:
734 bytes_read = sizeof(buf.timerfd_num);
737 case RTE_INTR_HANDLE_VFIO_MSIX:
738 case RTE_INTR_HANDLE_VFIO_MSI:
739 case RTE_INTR_HANDLE_VFIO_LEGACY:
740 bytes_read = sizeof(buf.vfio_intr_count);
743 case RTE_INTR_HANDLE_VDEV:
744 case RTE_INTR_HANDLE_EXT:
748 case RTE_INTR_HANDLE_DEV_EVENT:
752 case RTE_INTR_HANDLE_VFIO_REQ:
761 if (bytes_read > 0) {
763 * read out to clear the ready-to-be-read flag
766 bytes_read = read(events[n].data.fd, &buf, bytes_read);
767 if (bytes_read < 0) {
768 if (errno == EINTR || errno == EWOULDBLOCK)
771 RTE_LOG(ERR, EAL, "Error reading from file "
772 "descriptor %d: %s\n",
775 } else if (bytes_read == 0)
776 RTE_LOG(ERR, EAL, "Read nothing from file "
777 "descriptor %d\n", events[n].data.fd);
782 /* grab a lock, again to call callbacks and update status. */
783 rte_spinlock_lock(&intr_lock);
787 /* Finally, call all callbacks. */
788 TAILQ_FOREACH(cb, &src->callbacks, next) {
790 /* make a copy and unlock. */
792 rte_spinlock_unlock(&intr_lock);
794 /* call the actual callback */
795 active_cb.cb_fn(active_cb.cb_arg);
797 /*get the lock back. */
798 rte_spinlock_lock(&intr_lock);
802 /* we done with that interrupt source, release it. */
804 rte_spinlock_unlock(&intr_lock);
811 * It handles all the interrupts.
814 * epoll file descriptor.
816 * The number of file descriptors added in epoll.
822 eal_intr_handle_interrupts(int pfd, unsigned totalfds)
824 struct epoll_event events[totalfds];
828 nfds = epoll_wait(pfd, events, totalfds,
829 EAL_INTR_EPOLL_WAIT_FOREVER);
830 /* epoll_wait fail */
835 "epoll_wait returns with fail\n");
838 /* epoll_wait timeout, will never happens here */
841 /* epoll_wait has at least one fd ready to read */
842 if (eal_intr_process_interrupts(events, nfds) < 0)
848 * It builds/rebuilds up the epoll file descriptor with all the
849 * file descriptors being waited on. Then handles the interrupts.
857 static __attribute__((noreturn)) void *
858 eal_intr_thread_main(__rte_unused void *arg)
860 struct epoll_event ev;
862 /* host thread, never break out */
864 /* build up the epoll fd with all descriptors we are to
865 * wait on then pass it to the handle_interrupts function
867 static struct epoll_event pipe_event = {
868 .events = EPOLLIN | EPOLLPRI,
870 struct rte_intr_source *src;
873 /* create epoll fd */
874 int pfd = epoll_create(1);
876 rte_panic("Cannot create epoll instance\n");
878 pipe_event.data.fd = intr_pipe.readfd;
880 * add pipe fd into wait list, this pipe is used to
881 * rebuild the wait list.
883 if (epoll_ctl(pfd, EPOLL_CTL_ADD, intr_pipe.readfd,
885 rte_panic("Error adding fd to %d epoll_ctl, %s\n",
886 intr_pipe.readfd, strerror(errno));
890 rte_spinlock_lock(&intr_lock);
892 TAILQ_FOREACH(src, &intr_sources, next) {
893 if (src->callbacks.tqh_first == NULL)
894 continue; /* skip those with no callbacks */
895 ev.events = EPOLLIN | EPOLLPRI | EPOLLRDHUP | EPOLLHUP;
896 ev.data.fd = src->intr_handle.fd;
899 * add all the uio device file descriptor
902 if (epoll_ctl(pfd, EPOLL_CTL_ADD,
903 src->intr_handle.fd, &ev) < 0){
904 rte_panic("Error adding fd %d epoll_ctl, %s\n",
905 src->intr_handle.fd, strerror(errno));
910 rte_spinlock_unlock(&intr_lock);
911 /* serve the interrupt */
912 eal_intr_handle_interrupts(pfd, numfds);
915 * when we return, we need to rebuild the
916 * list of fds to monitor.
923 rte_eal_intr_init(void)
927 /* init the global interrupt source head */
928 TAILQ_INIT(&intr_sources);
931 * create a pipe which will be waited by epoll and notified to
932 * rebuild the wait list of epoll.
934 if (pipe(intr_pipe.pipefd) < 0) {
939 /* create the host thread to wait/handle the interrupt */
940 ret = rte_ctrl_thread_create(&intr_thread, "eal-intr-thread", NULL,
941 eal_intr_thread_main, NULL);
945 "Failed to create thread for interrupt handling\n");
952 eal_intr_proc_rxtx_intr(int fd, const struct rte_intr_handle *intr_handle)
954 union rte_intr_read_buffer buf;
958 switch (intr_handle->type) {
959 case RTE_INTR_HANDLE_UIO:
960 case RTE_INTR_HANDLE_UIO_INTX:
961 bytes_read = sizeof(buf.uio_intr_count);
964 case RTE_INTR_HANDLE_VFIO_MSIX:
965 case RTE_INTR_HANDLE_VFIO_MSI:
966 case RTE_INTR_HANDLE_VFIO_LEGACY:
967 bytes_read = sizeof(buf.vfio_intr_count);
970 case RTE_INTR_HANDLE_VDEV:
971 bytes_read = intr_handle->efd_counter_size;
972 /* For vdev, number of bytes to read is set by driver */
974 case RTE_INTR_HANDLE_EXT:
978 RTE_LOG(INFO, EAL, "unexpected intr type\n");
983 * read out to clear the ready-to-be-read flag
989 nbytes = read(fd, &buf, bytes_read);
991 if (errno == EINTR || errno == EWOULDBLOCK ||
995 "Error reading from fd %d: %s\n",
996 fd, strerror(errno));
997 } else if (nbytes == 0)
998 RTE_LOG(ERR, EAL, "Read nothing from fd %d\n", fd);
1004 eal_epoll_process_event(struct epoll_event *evs, unsigned int n,
1005 struct rte_epoll_event *events)
1007 unsigned int i, count = 0;
1008 struct rte_epoll_event *rev;
1010 for (i = 0; i < n; i++) {
1011 rev = evs[i].data.ptr;
1012 if (!rev || !rte_atomic32_cmpset(&rev->status, RTE_EPOLL_VALID,
1016 events[count].status = RTE_EPOLL_VALID;
1017 events[count].fd = rev->fd;
1018 events[count].epfd = rev->epfd;
1019 events[count].epdata.event = rev->epdata.event;
1020 events[count].epdata.data = rev->epdata.data;
1021 if (rev->epdata.cb_fun)
1022 rev->epdata.cb_fun(rev->fd,
1023 rev->epdata.cb_arg);
1025 rte_compiler_barrier();
1026 rev->status = RTE_EPOLL_VALID;
1033 eal_init_tls_epfd(void)
1035 int pfd = epoll_create(255);
1039 "Cannot create epoll instance\n");
1046 rte_intr_tls_epfd(void)
1048 if (RTE_PER_LCORE(_epfd) == -1)
1049 RTE_PER_LCORE(_epfd) = eal_init_tls_epfd();
1051 return RTE_PER_LCORE(_epfd);
1055 rte_epoll_wait(int epfd, struct rte_epoll_event *events,
1056 int maxevents, int timeout)
1058 struct epoll_event evs[maxevents];
1062 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1066 /* using per thread epoll fd */
1067 if (epfd == RTE_EPOLL_PER_THREAD)
1068 epfd = rte_intr_tls_epfd();
1071 rc = epoll_wait(epfd, evs, maxevents, timeout);
1072 if (likely(rc > 0)) {
1073 /* epoll_wait has at least one fd ready to read */
1074 rc = eal_epoll_process_event(evs, rc, events);
1076 } else if (rc < 0) {
1079 /* epoll_wait fail */
1080 RTE_LOG(ERR, EAL, "epoll_wait returns with fail %s\n",
1085 /* rc == 0, epoll_wait timed out */
1094 eal_epoll_data_safe_free(struct rte_epoll_event *ev)
1096 while (!rte_atomic32_cmpset(&ev->status, RTE_EPOLL_VALID,
1098 while (ev->status != RTE_EPOLL_VALID)
1100 memset(&ev->epdata, 0, sizeof(ev->epdata));
1106 rte_epoll_ctl(int epfd, int op, int fd,
1107 struct rte_epoll_event *event)
1109 struct epoll_event ev;
1112 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1116 /* using per thread epoll fd */
1117 if (epfd == RTE_EPOLL_PER_THREAD)
1118 epfd = rte_intr_tls_epfd();
1120 if (op == EPOLL_CTL_ADD) {
1121 event->status = RTE_EPOLL_VALID;
1122 event->fd = fd; /* ignore fd in event */
1124 ev.data.ptr = (void *)event;
1127 ev.events = event->epdata.event;
1128 if (epoll_ctl(epfd, op, fd, &ev) < 0) {
1129 RTE_LOG(ERR, EAL, "Error op %d fd %d epoll_ctl, %s\n",
1130 op, fd, strerror(errno));
1131 if (op == EPOLL_CTL_ADD)
1132 /* rollback status when CTL_ADD fail */
1133 event->status = RTE_EPOLL_INVALID;
1137 if (op == EPOLL_CTL_DEL && event->status != RTE_EPOLL_INVALID)
1138 eal_epoll_data_safe_free(event);
1144 rte_intr_rx_ctl(struct rte_intr_handle *intr_handle, int epfd,
1145 int op, unsigned int vec, void *data)
1147 struct rte_epoll_event *rev;
1148 struct rte_epoll_data *epdata;
1150 unsigned int efd_idx;
1153 efd_idx = (vec >= RTE_INTR_VEC_RXTX_OFFSET) ?
1154 (vec - RTE_INTR_VEC_RXTX_OFFSET) : vec;
1156 if (!intr_handle || intr_handle->nb_efd == 0 ||
1157 efd_idx >= intr_handle->nb_efd) {
1158 RTE_LOG(ERR, EAL, "Wrong intr vector number.\n");
1163 case RTE_INTR_EVENT_ADD:
1164 epfd_op = EPOLL_CTL_ADD;
1165 rev = &intr_handle->elist[efd_idx];
1166 if (rev->status != RTE_EPOLL_INVALID) {
1167 RTE_LOG(INFO, EAL, "Event already been added.\n");
1171 /* attach to intr vector fd */
1172 epdata = &rev->epdata;
1173 epdata->event = EPOLLIN | EPOLLPRI | EPOLLET;
1174 epdata->data = data;
1175 epdata->cb_fun = (rte_intr_event_cb_t)eal_intr_proc_rxtx_intr;
1176 epdata->cb_arg = (void *)intr_handle;
1177 rc = rte_epoll_ctl(epfd, epfd_op,
1178 intr_handle->efds[efd_idx], rev);
1181 "efd %d associated with vec %d added on epfd %d"
1182 "\n", rev->fd, vec, epfd);
1186 case RTE_INTR_EVENT_DEL:
1187 epfd_op = EPOLL_CTL_DEL;
1188 rev = &intr_handle->elist[efd_idx];
1189 if (rev->status == RTE_EPOLL_INVALID) {
1190 RTE_LOG(INFO, EAL, "Event does not exist.\n");
1194 rc = rte_epoll_ctl(rev->epfd, epfd_op, rev->fd, rev);
1199 RTE_LOG(ERR, EAL, "event op type mismatch\n");
1207 rte_intr_free_epoll_fd(struct rte_intr_handle *intr_handle)
1210 struct rte_epoll_event *rev;
1212 for (i = 0; i < intr_handle->nb_efd; i++) {
1213 rev = &intr_handle->elist[i];
1214 if (rev->status == RTE_EPOLL_INVALID)
1216 if (rte_epoll_ctl(rev->epfd, EPOLL_CTL_DEL, rev->fd, rev)) {
1217 /* force free if the entry valid */
1218 eal_epoll_data_safe_free(rev);
1219 rev->status = RTE_EPOLL_INVALID;
1225 rte_intr_efd_enable(struct rte_intr_handle *intr_handle, uint32_t nb_efd)
1229 uint32_t n = RTE_MIN(nb_efd, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
1231 assert(nb_efd != 0);
1233 if (intr_handle->type == RTE_INTR_HANDLE_VFIO_MSIX) {
1234 for (i = 0; i < n; i++) {
1235 fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
1238 "can't setup eventfd, error %i (%s)\n",
1239 errno, strerror(errno));
1242 intr_handle->efds[i] = fd;
1244 intr_handle->nb_efd = n;
1245 intr_handle->max_intr = NB_OTHER_INTR + n;
1246 } else if (intr_handle->type == RTE_INTR_HANDLE_VDEV) {
1247 /* only check, initialization would be done in vdev driver.*/
1248 if (intr_handle->efd_counter_size >
1249 sizeof(union rte_intr_read_buffer)) {
1250 RTE_LOG(ERR, EAL, "the efd_counter_size is oversized");
1254 intr_handle->efds[0] = intr_handle->fd;
1255 intr_handle->nb_efd = RTE_MIN(nb_efd, 1U);
1256 intr_handle->max_intr = NB_OTHER_INTR;
1263 rte_intr_efd_disable(struct rte_intr_handle *intr_handle)
1267 rte_intr_free_epoll_fd(intr_handle);
1268 if (intr_handle->max_intr > intr_handle->nb_efd) {
1269 for (i = 0; i < intr_handle->nb_efd; i++)
1270 close(intr_handle->efds[i]);
1272 intr_handle->nb_efd = 0;
1273 intr_handle->max_intr = 0;
1277 rte_intr_dp_is_en(struct rte_intr_handle *intr_handle)
1279 return !(!intr_handle->nb_efd);
1283 rte_intr_allow_others(struct rte_intr_handle *intr_handle)
1285 if (!rte_intr_dp_is_en(intr_handle))
1288 return !!(intr_handle->max_intr - intr_handle->nb_efd);
1292 rte_intr_cap_multiple(struct rte_intr_handle *intr_handle)
1294 if (intr_handle->type == RTE_INTR_HANDLE_VFIO_MSIX)
1297 if (intr_handle->type == RTE_INTR_HANDLE_VDEV)