4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <sys/queue.h>
44 #include <sys/epoll.h>
45 #include <sys/signalfd.h>
46 #include <sys/ioctl.h>
47 #include <sys/eventfd.h>
49 #include <rte_common.h>
50 #include <rte_interrupts.h>
51 #include <rte_memory.h>
52 #include <rte_memzone.h>
53 #include <rte_launch.h>
55 #include <rte_per_lcore.h>
56 #include <rte_lcore.h>
57 #include <rte_atomic.h>
58 #include <rte_branch_prediction.h>
60 #include <rte_debug.h>
62 #include <rte_mempool.h>
64 #include <rte_malloc.h>
65 #include <rte_errno.h>
66 #include <rte_spinlock.h>
68 #include "eal_private.h"
70 #include "eal_thread.h"
72 #define EAL_INTR_EPOLL_WAIT_FOREVER (-1)
73 #define NB_OTHER_INTR 1
75 static RTE_DEFINE_PER_LCORE(int, _epfd) = -1; /**< epoll fd per thread */
91 * union buffer for reading on different devices
93 union rte_intr_read_buffer {
94 int uio_intr_count; /* for uio device */
96 uint64_t vfio_intr_count; /* for vfio device */
98 uint64_t timerfd_num; /* for timerfd */
99 char charbuf[16]; /* for others */
102 TAILQ_HEAD(rte_intr_cb_list, rte_intr_callback);
103 TAILQ_HEAD(rte_intr_source_list, rte_intr_source);
105 struct rte_intr_callback {
106 TAILQ_ENTRY(rte_intr_callback) next;
107 rte_intr_callback_fn cb_fn; /**< callback address */
108 void *cb_arg; /**< parameter for callback */
111 struct rte_intr_source {
112 TAILQ_ENTRY(rte_intr_source) next;
113 struct rte_intr_handle intr_handle; /**< interrupt handle */
114 struct rte_intr_cb_list callbacks; /**< user callbacks */
118 /* global spinlock for interrupt data operation */
119 static rte_spinlock_t intr_lock = RTE_SPINLOCK_INITIALIZER;
121 /* union buffer for pipe read/write */
122 static union intr_pipefds intr_pipe;
124 /* interrupt sources list */
125 static struct rte_intr_source_list intr_sources;
127 /* interrupt handling thread */
128 static pthread_t intr_thread;
130 /* VFIO interrupts */
133 #define IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + sizeof(int))
134 /* irq set buffer length for queue interrupts and LSC interrupt */
135 #define MSIX_IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + \
136 sizeof(int) * (RTE_MAX_RXTX_INTR_VEC_ID + 1))
138 /* enable legacy (INTx) interrupts */
140 vfio_enable_intx(struct rte_intr_handle *intr_handle) {
141 struct vfio_irq_set *irq_set;
142 char irq_set_buf[IRQ_SET_BUF_LEN];
146 len = sizeof(irq_set_buf);
149 irq_set = (struct vfio_irq_set *) irq_set_buf;
150 irq_set->argsz = len;
152 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
153 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
155 fd_ptr = (int *) &irq_set->data;
156 *fd_ptr = intr_handle->fd;
158 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
161 RTE_LOG(ERR, EAL, "Error enabling INTx interrupts for fd %d\n",
166 /* unmask INTx after enabling */
167 memset(irq_set, 0, len);
168 len = sizeof(struct vfio_irq_set);
169 irq_set->argsz = len;
171 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK;
172 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
175 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
178 RTE_LOG(ERR, EAL, "Error unmasking INTx interrupts for fd %d\n",
185 /* disable legacy (INTx) interrupts */
187 vfio_disable_intx(struct rte_intr_handle *intr_handle) {
188 struct vfio_irq_set *irq_set;
189 char irq_set_buf[IRQ_SET_BUF_LEN];
192 len = sizeof(struct vfio_irq_set);
194 /* mask interrupts before disabling */
195 irq_set = (struct vfio_irq_set *) irq_set_buf;
196 irq_set->argsz = len;
198 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK;
199 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
202 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
205 RTE_LOG(ERR, EAL, "Error unmasking INTx interrupts for fd %d\n",
211 memset(irq_set, 0, len);
212 irq_set->argsz = len;
214 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
215 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
218 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
222 "Error disabling INTx interrupts for fd %d\n", intr_handle->fd);
228 /* enable MSI interrupts */
230 vfio_enable_msi(struct rte_intr_handle *intr_handle) {
232 char irq_set_buf[IRQ_SET_BUF_LEN];
233 struct vfio_irq_set *irq_set;
236 len = sizeof(irq_set_buf);
238 irq_set = (struct vfio_irq_set *) irq_set_buf;
239 irq_set->argsz = len;
241 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
242 irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
244 fd_ptr = (int *) &irq_set->data;
245 *fd_ptr = intr_handle->fd;
247 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
250 RTE_LOG(ERR, EAL, "Error enabling MSI interrupts for fd %d\n",
257 /* disable MSI interrupts */
259 vfio_disable_msi(struct rte_intr_handle *intr_handle) {
260 struct vfio_irq_set *irq_set;
261 char irq_set_buf[IRQ_SET_BUF_LEN];
264 len = sizeof(struct vfio_irq_set);
266 irq_set = (struct vfio_irq_set *) irq_set_buf;
267 irq_set->argsz = len;
269 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
270 irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
273 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
277 "Error disabling MSI interrupts for fd %d\n", intr_handle->fd);
282 /* enable MSI-X interrupts */
284 vfio_enable_msix(struct rte_intr_handle *intr_handle) {
286 char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
287 struct vfio_irq_set *irq_set;
290 len = sizeof(irq_set_buf);
292 irq_set = (struct vfio_irq_set *) irq_set_buf;
293 irq_set->argsz = len;
294 if (!intr_handle->max_intr)
295 intr_handle->max_intr = 1;
296 else if (intr_handle->max_intr > RTE_MAX_RXTX_INTR_VEC_ID)
297 intr_handle->max_intr = RTE_MAX_RXTX_INTR_VEC_ID + 1;
299 irq_set->count = intr_handle->max_intr;
300 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
301 irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
303 fd_ptr = (int *) &irq_set->data;
304 memcpy(fd_ptr, intr_handle->efds, sizeof(intr_handle->efds));
305 fd_ptr[intr_handle->max_intr - 1] = intr_handle->fd;
307 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
310 RTE_LOG(ERR, EAL, "Error enabling MSI-X interrupts for fd %d\n",
318 /* disable MSI-X interrupts */
320 vfio_disable_msix(struct rte_intr_handle *intr_handle) {
321 struct vfio_irq_set *irq_set;
322 char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
325 len = sizeof(struct vfio_irq_set);
327 irq_set = (struct vfio_irq_set *) irq_set_buf;
328 irq_set->argsz = len;
330 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
331 irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
334 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
338 "Error disabling MSI-X interrupts for fd %d\n", intr_handle->fd);
345 uio_intx_intr_disable(struct rte_intr_handle *intr_handle)
347 unsigned char command_high;
349 /* use UIO config file descriptor for uio_pci_generic */
350 if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
352 "Error reading interrupts status for fd %d\n",
353 intr_handle->uio_cfg_fd);
356 /* disable interrupts */
358 if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
360 "Error disabling interrupts for fd %d\n",
361 intr_handle->uio_cfg_fd);
369 uio_intx_intr_enable(struct rte_intr_handle *intr_handle)
371 unsigned char command_high;
373 /* use UIO config file descriptor for uio_pci_generic */
374 if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
376 "Error reading interrupts status for fd %d\n",
377 intr_handle->uio_cfg_fd);
380 /* enable interrupts */
381 command_high &= ~0x4;
382 if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
384 "Error enabling interrupts for fd %d\n",
385 intr_handle->uio_cfg_fd);
393 uio_intr_disable(struct rte_intr_handle *intr_handle)
397 if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
399 "Error disabling interrupts for fd %d (%s)\n",
400 intr_handle->fd, strerror(errno));
407 uio_intr_enable(struct rte_intr_handle *intr_handle)
411 if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
413 "Error enabling interrupts for fd %d (%s)\n",
414 intr_handle->fd, strerror(errno));
421 rte_intr_callback_register(struct rte_intr_handle *intr_handle,
422 rte_intr_callback_fn cb, void *cb_arg)
424 int ret, wake_thread;
425 struct rte_intr_source *src;
426 struct rte_intr_callback *callback;
430 /* first do parameter checking */
431 if (intr_handle == NULL || intr_handle->fd < 0 || cb == NULL) {
433 "Registering with invalid input parameter\n");
437 /* allocate a new interrupt callback entity */
438 callback = rte_zmalloc("interrupt callback list",
439 sizeof(*callback), 0);
440 if (callback == NULL) {
441 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
444 callback->cb_fn = cb;
445 callback->cb_arg = cb_arg;
447 rte_spinlock_lock(&intr_lock);
449 /* check if there is at least one callback registered for the fd */
450 TAILQ_FOREACH(src, &intr_sources, next) {
451 if (src->intr_handle.fd == intr_handle->fd) {
452 /* we had no interrupts for this */
453 if TAILQ_EMPTY(&src->callbacks)
456 TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
462 /* no existing callbacks for this - add new source */
464 if ((src = rte_zmalloc("interrupt source list",
465 sizeof(*src), 0)) == NULL) {
466 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
470 src->intr_handle = *intr_handle;
471 TAILQ_INIT(&src->callbacks);
472 TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
473 TAILQ_INSERT_TAIL(&intr_sources, src, next);
479 rte_spinlock_unlock(&intr_lock);
482 * check if need to notify the pipe fd waited by epoll_wait to
483 * rebuild the wait list.
486 if (write(intr_pipe.writefd, "1", 1) < 0)
493 rte_intr_callback_unregister(struct rte_intr_handle *intr_handle,
494 rte_intr_callback_fn cb_fn, void *cb_arg)
497 struct rte_intr_source *src;
498 struct rte_intr_callback *cb, *next;
500 /* do parameter checking first */
501 if (intr_handle == NULL || intr_handle->fd < 0) {
503 "Unregistering with invalid input parameter\n");
507 rte_spinlock_lock(&intr_lock);
509 /* check if the insterrupt source for the fd is existent */
510 TAILQ_FOREACH(src, &intr_sources, next)
511 if (src->intr_handle.fd == intr_handle->fd)
514 /* No interrupt source registered for the fd */
518 /* interrupt source has some active callbacks right now. */
519 } else if (src->active != 0) {
526 /*walk through the callbacks and remove all that match. */
527 for (cb = TAILQ_FIRST(&src->callbacks); cb != NULL; cb = next) {
529 next = TAILQ_NEXT(cb, next);
531 if (cb->cb_fn == cb_fn && (cb_arg == (void *)-1 ||
532 cb->cb_arg == cb_arg)) {
533 TAILQ_REMOVE(&src->callbacks, cb, next);
539 /* all callbacks for that source are removed. */
540 if (TAILQ_EMPTY(&src->callbacks)) {
541 TAILQ_REMOVE(&intr_sources, src, next);
546 rte_spinlock_unlock(&intr_lock);
548 /* notify the pipe fd waited by epoll_wait to rebuild the wait list */
549 if (ret >= 0 && write(intr_pipe.writefd, "1", 1) < 0) {
557 rte_intr_enable(struct rte_intr_handle *intr_handle)
559 if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
562 switch (intr_handle->type){
563 /* write to the uio fd to enable the interrupt */
564 case RTE_INTR_HANDLE_UIO:
565 if (uio_intr_enable(intr_handle))
568 case RTE_INTR_HANDLE_UIO_INTX:
569 if (uio_intx_intr_enable(intr_handle))
572 /* not used at this moment */
573 case RTE_INTR_HANDLE_ALARM:
576 case RTE_INTR_HANDLE_VFIO_MSIX:
577 if (vfio_enable_msix(intr_handle))
580 case RTE_INTR_HANDLE_VFIO_MSI:
581 if (vfio_enable_msi(intr_handle))
584 case RTE_INTR_HANDLE_VFIO_LEGACY:
585 if (vfio_enable_intx(intr_handle))
589 /* unknown handle type */
592 "Unknown handle type of fd %d\n",
601 rte_intr_disable(struct rte_intr_handle *intr_handle)
603 if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
606 switch (intr_handle->type){
607 /* write to the uio fd to disable the interrupt */
608 case RTE_INTR_HANDLE_UIO:
609 if (uio_intr_disable(intr_handle))
612 case RTE_INTR_HANDLE_UIO_INTX:
613 if (uio_intx_intr_disable(intr_handle))
616 /* not used at this moment */
617 case RTE_INTR_HANDLE_ALARM:
620 case RTE_INTR_HANDLE_VFIO_MSIX:
621 if (vfio_disable_msix(intr_handle))
624 case RTE_INTR_HANDLE_VFIO_MSI:
625 if (vfio_disable_msi(intr_handle))
628 case RTE_INTR_HANDLE_VFIO_LEGACY:
629 if (vfio_disable_intx(intr_handle))
633 /* unknown handle type */
636 "Unknown handle type of fd %d\n",
645 eal_intr_process_interrupts(struct epoll_event *events, int nfds)
648 struct rte_intr_source *src;
649 struct rte_intr_callback *cb;
650 union rte_intr_read_buffer buf;
651 struct rte_intr_callback active_cb;
653 for (n = 0; n < nfds; n++) {
656 * if the pipe fd is ready to read, return out to
657 * rebuild the wait list.
659 if (events[n].data.fd == intr_pipe.readfd){
660 int r = read(intr_pipe.readfd, buf.charbuf,
661 sizeof(buf.charbuf));
665 rte_spinlock_lock(&intr_lock);
666 TAILQ_FOREACH(src, &intr_sources, next)
667 if (src->intr_handle.fd ==
671 rte_spinlock_unlock(&intr_lock);
675 /* mark this interrupt source as active and release the lock. */
677 rte_spinlock_unlock(&intr_lock);
679 /* set the length to be read dor different handle type */
680 switch (src->intr_handle.type) {
681 case RTE_INTR_HANDLE_UIO:
682 case RTE_INTR_HANDLE_UIO_INTX:
683 bytes_read = sizeof(buf.uio_intr_count);
685 case RTE_INTR_HANDLE_ALARM:
686 bytes_read = sizeof(buf.timerfd_num);
689 case RTE_INTR_HANDLE_VFIO_MSIX:
690 case RTE_INTR_HANDLE_VFIO_MSI:
691 case RTE_INTR_HANDLE_VFIO_LEGACY:
692 bytes_read = sizeof(buf.vfio_intr_count);
695 case RTE_INTR_HANDLE_EXT:
701 if (src->intr_handle.type != RTE_INTR_HANDLE_EXT) {
703 * read out to clear the ready-to-be-read flag
706 bytes_read = read(events[n].data.fd, &buf, bytes_read);
707 if (bytes_read < 0) {
708 if (errno == EINTR || errno == EWOULDBLOCK)
711 RTE_LOG(ERR, EAL, "Error reading from file "
712 "descriptor %d: %s\n",
715 } else if (bytes_read == 0)
716 RTE_LOG(ERR, EAL, "Read nothing from file "
717 "descriptor %d\n", events[n].data.fd);
720 /* grab a lock, again to call callbacks and update status. */
721 rte_spinlock_lock(&intr_lock);
723 if (bytes_read > 0) {
725 /* Finally, call all callbacks. */
726 TAILQ_FOREACH(cb, &src->callbacks, next) {
728 /* make a copy and unlock. */
730 rte_spinlock_unlock(&intr_lock);
732 /* call the actual callback */
733 active_cb.cb_fn(&src->intr_handle,
736 /*get the lock back. */
737 rte_spinlock_lock(&intr_lock);
741 /* we done with that interrupt source, release it. */
743 rte_spinlock_unlock(&intr_lock);
750 * It handles all the interrupts.
753 * epoll file descriptor.
755 * The number of file descriptors added in epoll.
761 eal_intr_handle_interrupts(int pfd, unsigned totalfds)
763 struct epoll_event events[totalfds];
767 nfds = epoll_wait(pfd, events, totalfds,
768 EAL_INTR_EPOLL_WAIT_FOREVER);
769 /* epoll_wait fail */
774 "epoll_wait returns with fail\n");
777 /* epoll_wait timeout, will never happens here */
780 /* epoll_wait has at least one fd ready to read */
781 if (eal_intr_process_interrupts(events, nfds) < 0)
787 * It builds/rebuilds up the epoll file descriptor with all the
788 * file descriptors being waited on. Then handles the interrupts.
796 static __attribute__((noreturn)) void *
797 eal_intr_thread_main(__rte_unused void *arg)
799 struct epoll_event ev;
801 /* host thread, never break out */
803 /* build up the epoll fd with all descriptors we are to
804 * wait on then pass it to the handle_interrupts function
806 static struct epoll_event pipe_event = {
807 .events = EPOLLIN | EPOLLPRI,
809 struct rte_intr_source *src;
812 /* create epoll fd */
813 int pfd = epoll_create(1);
815 rte_panic("Cannot create epoll instance\n");
817 pipe_event.data.fd = intr_pipe.readfd;
819 * add pipe fd into wait list, this pipe is used to
820 * rebuild the wait list.
822 if (epoll_ctl(pfd, EPOLL_CTL_ADD, intr_pipe.readfd,
824 rte_panic("Error adding fd to %d epoll_ctl, %s\n",
825 intr_pipe.readfd, strerror(errno));
829 rte_spinlock_lock(&intr_lock);
831 TAILQ_FOREACH(src, &intr_sources, next) {
832 if (src->callbacks.tqh_first == NULL)
833 continue; /* skip those with no callbacks */
834 ev.events = EPOLLIN | EPOLLPRI;
835 ev.data.fd = src->intr_handle.fd;
838 * add all the uio device file descriptor
841 if (epoll_ctl(pfd, EPOLL_CTL_ADD,
842 src->intr_handle.fd, &ev) < 0){
843 rte_panic("Error adding fd %d epoll_ctl, %s\n",
844 src->intr_handle.fd, strerror(errno));
849 rte_spinlock_unlock(&intr_lock);
850 /* serve the interrupt */
851 eal_intr_handle_interrupts(pfd, numfds);
854 * when we return, we need to rebuild the
855 * list of fds to monitor.
862 rte_eal_intr_init(void)
864 int ret = 0, ret_1 = 0;
865 char thread_name[RTE_MAX_THREAD_NAME_LEN];
867 /* init the global interrupt source head */
868 TAILQ_INIT(&intr_sources);
871 * create a pipe which will be waited by epoll and notified to
872 * rebuild the wait list of epoll.
874 if (pipe(intr_pipe.pipefd) < 0)
877 /* create the host thread to wait/handle the interrupt */
878 ret = pthread_create(&intr_thread, NULL,
879 eal_intr_thread_main, NULL);
882 "Failed to create thread for interrupt handling\n");
884 /* Set thread_name for aid in debugging. */
885 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN,
887 ret_1 = pthread_setname_np(intr_thread, thread_name);
890 "Failed to set thread name for interrupt handling\n");
897 eal_intr_proc_rxtx_intr(int fd, const struct rte_intr_handle *intr_handle)
899 union rte_intr_read_buffer buf;
902 switch (intr_handle->type) {
903 case RTE_INTR_HANDLE_UIO:
904 case RTE_INTR_HANDLE_UIO_INTX:
905 bytes_read = sizeof(buf.uio_intr_count);
908 case RTE_INTR_HANDLE_VFIO_MSIX:
909 case RTE_INTR_HANDLE_VFIO_MSI:
910 case RTE_INTR_HANDLE_VFIO_LEGACY:
911 bytes_read = sizeof(buf.vfio_intr_count);
916 RTE_LOG(INFO, EAL, "unexpected intr type\n");
921 * read out to clear the ready-to-be-read flag
925 bytes_read = read(fd, &buf, bytes_read);
926 if (bytes_read < 0) {
927 if (errno == EINTR || errno == EWOULDBLOCK ||
931 "Error reading from fd %d: %s\n",
932 fd, strerror(errno));
933 } else if (bytes_read == 0)
934 RTE_LOG(ERR, EAL, "Read nothing from fd %d\n", fd);
940 eal_epoll_process_event(struct epoll_event *evs, unsigned int n,
941 struct rte_epoll_event *events)
943 unsigned int i, count = 0;
944 struct rte_epoll_event *rev;
946 for (i = 0; i < n; i++) {
947 rev = evs[i].data.ptr;
948 if (!rev || !rte_atomic32_cmpset(&rev->status, RTE_EPOLL_VALID,
952 events[count].status = RTE_EPOLL_VALID;
953 events[count].fd = rev->fd;
954 events[count].epfd = rev->epfd;
955 events[count].epdata.event = rev->epdata.event;
956 events[count].epdata.data = rev->epdata.data;
957 if (rev->epdata.cb_fun)
958 rev->epdata.cb_fun(rev->fd,
961 rte_compiler_barrier();
962 rev->status = RTE_EPOLL_VALID;
969 eal_init_tls_epfd(void)
971 int pfd = epoll_create(255);
975 "Cannot create epoll instance\n");
982 rte_intr_tls_epfd(void)
984 if (RTE_PER_LCORE(_epfd) == -1)
985 RTE_PER_LCORE(_epfd) = eal_init_tls_epfd();
987 return RTE_PER_LCORE(_epfd);
991 rte_epoll_wait(int epfd, struct rte_epoll_event *events,
992 int maxevents, int timeout)
994 struct epoll_event evs[maxevents];
998 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1002 /* using per thread epoll fd */
1003 if (epfd == RTE_EPOLL_PER_THREAD)
1004 epfd = rte_intr_tls_epfd();
1007 rc = epoll_wait(epfd, evs, maxevents, timeout);
1008 if (likely(rc > 0)) {
1009 /* epoll_wait has at least one fd ready to read */
1010 rc = eal_epoll_process_event(evs, rc, events);
1012 } else if (rc < 0) {
1015 /* epoll_wait fail */
1016 RTE_LOG(ERR, EAL, "epoll_wait returns with fail %s\n",
1021 /* rc == 0, epoll_wait timed out */
1030 eal_epoll_data_safe_free(struct rte_epoll_event *ev)
1032 while (!rte_atomic32_cmpset(&ev->status, RTE_EPOLL_VALID,
1034 while (ev->status != RTE_EPOLL_VALID)
1036 memset(&ev->epdata, 0, sizeof(ev->epdata));
1042 rte_epoll_ctl(int epfd, int op, int fd,
1043 struct rte_epoll_event *event)
1045 struct epoll_event ev;
1048 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1052 /* using per thread epoll fd */
1053 if (epfd == RTE_EPOLL_PER_THREAD)
1054 epfd = rte_intr_tls_epfd();
1056 if (op == EPOLL_CTL_ADD) {
1057 event->status = RTE_EPOLL_VALID;
1058 event->fd = fd; /* ignore fd in event */
1060 ev.data.ptr = (void *)event;
1063 ev.events = event->epdata.event;
1064 if (epoll_ctl(epfd, op, fd, &ev) < 0) {
1065 RTE_LOG(ERR, EAL, "Error op %d fd %d epoll_ctl, %s\n",
1066 op, fd, strerror(errno));
1067 if (op == EPOLL_CTL_ADD)
1068 /* rollback status when CTL_ADD fail */
1069 event->status = RTE_EPOLL_INVALID;
1073 if (op == EPOLL_CTL_DEL && event->status != RTE_EPOLL_INVALID)
1074 eal_epoll_data_safe_free(event);
1080 rte_intr_rx_ctl(struct rte_intr_handle *intr_handle, int epfd,
1081 int op, unsigned int vec, void *data)
1083 struct rte_epoll_event *rev;
1084 struct rte_epoll_data *epdata;
1088 if (!intr_handle || intr_handle->nb_efd == 0 ||
1089 vec >= intr_handle->nb_efd) {
1090 RTE_LOG(ERR, EAL, "Wrong intr vector number.\n");
1095 case RTE_INTR_EVENT_ADD:
1096 epfd_op = EPOLL_CTL_ADD;
1097 rev = &intr_handle->elist[vec];
1098 if (rev->status != RTE_EPOLL_INVALID) {
1099 RTE_LOG(INFO, EAL, "Event already been added.\n");
1103 /* attach to intr vector fd */
1104 epdata = &rev->epdata;
1105 epdata->event = EPOLLIN | EPOLLPRI | EPOLLET;
1106 epdata->data = data;
1107 epdata->cb_fun = (rte_intr_event_cb_t)eal_intr_proc_rxtx_intr;
1108 epdata->cb_arg = (void *)intr_handle;
1109 rc = rte_epoll_ctl(epfd, epfd_op, intr_handle->efds[vec], 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[vec];
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_efd_enable(struct rte_intr_handle *intr_handle, uint32_t nb_efd)
1142 uint32_t n = RTE_MIN(nb_efd, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
1144 if (intr_handle->type == RTE_INTR_HANDLE_VFIO_MSIX) {
1145 for (i = 0; i < n; i++) {
1146 fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
1149 "can't setup eventfd, error %i (%s)\n",
1150 errno, strerror(errno));
1153 intr_handle->efds[i] = fd;
1155 intr_handle->nb_efd = n;
1156 intr_handle->max_intr = NB_OTHER_INTR + n;
1158 intr_handle->efds[0] = intr_handle->fd;
1159 intr_handle->nb_efd = RTE_MIN(nb_efd, 1U);
1160 intr_handle->max_intr = NB_OTHER_INTR;
1167 rte_intr_efd_disable(struct rte_intr_handle *intr_handle)
1170 struct rte_epoll_event *rev;
1172 for (i = 0; i < intr_handle->nb_efd; i++) {
1173 rev = &intr_handle->elist[i];
1174 if (rev->status == RTE_EPOLL_INVALID)
1176 if (rte_epoll_ctl(rev->epfd, EPOLL_CTL_DEL, rev->fd, rev)) {
1177 /* force free if the entry valid */
1178 eal_epoll_data_safe_free(rev);
1179 rev->status = RTE_EPOLL_INVALID;
1183 if (intr_handle->max_intr > intr_handle->nb_efd) {
1184 for (i = 0; i < intr_handle->nb_efd; i++)
1185 close(intr_handle->efds[i]);
1187 intr_handle->nb_efd = 0;
1188 intr_handle->max_intr = 0;
1192 rte_intr_dp_is_en(struct rte_intr_handle *intr_handle)
1194 return !(!intr_handle->nb_efd);
1198 rte_intr_allow_others(struct rte_intr_handle *intr_handle)
1200 return !!(intr_handle->max_intr - intr_handle->nb_efd);