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>
48 #include <rte_common.h>
49 #include <rte_interrupts.h>
50 #include <rte_memory.h>
51 #include <rte_memzone.h>
52 #include <rte_launch.h>
54 #include <rte_per_lcore.h>
55 #include <rte_lcore.h>
56 #include <rte_atomic.h>
57 #include <rte_branch_prediction.h>
59 #include <rte_debug.h>
61 #include <rte_mempool.h>
63 #include <rte_malloc.h>
64 #include <rte_errno.h>
65 #include <rte_spinlock.h>
67 #include "eal_private.h"
70 #define EAL_INTR_EPOLL_WAIT_FOREVER (-1)
72 static RTE_DEFINE_PER_LCORE(int, _epfd) = -1; /**< epoll fd per thread */
88 * union buffer for reading on different devices
90 union rte_intr_read_buffer {
91 int uio_intr_count; /* for uio device */
93 uint64_t vfio_intr_count; /* for vfio device */
95 uint64_t timerfd_num; /* for timerfd */
96 char charbuf[16]; /* for others */
99 TAILQ_HEAD(rte_intr_cb_list, rte_intr_callback);
100 TAILQ_HEAD(rte_intr_source_list, rte_intr_source);
102 struct rte_intr_callback {
103 TAILQ_ENTRY(rte_intr_callback) next;
104 rte_intr_callback_fn cb_fn; /**< callback address */
105 void *cb_arg; /**< parameter for callback */
108 struct rte_intr_source {
109 TAILQ_ENTRY(rte_intr_source) next;
110 struct rte_intr_handle intr_handle; /**< interrupt handle */
111 struct rte_intr_cb_list callbacks; /**< user callbacks */
115 /* global spinlock for interrupt data operation */
116 static rte_spinlock_t intr_lock = RTE_SPINLOCK_INITIALIZER;
118 /* union buffer for pipe read/write */
119 static union intr_pipefds intr_pipe;
121 /* interrupt sources list */
122 static struct rte_intr_source_list intr_sources;
124 /* interrupt handling thread */
125 static pthread_t intr_thread;
127 /* VFIO interrupts */
130 #define IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + sizeof(int))
132 /* enable legacy (INTx) interrupts */
134 vfio_enable_intx(struct rte_intr_handle *intr_handle) {
135 struct vfio_irq_set *irq_set;
136 char irq_set_buf[IRQ_SET_BUF_LEN];
140 len = sizeof(irq_set_buf);
143 irq_set = (struct vfio_irq_set *) irq_set_buf;
144 irq_set->argsz = len;
146 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
147 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
149 fd_ptr = (int *) &irq_set->data;
150 *fd_ptr = intr_handle->fd;
152 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
155 RTE_LOG(ERR, EAL, "Error enabling INTx interrupts for fd %d\n",
160 /* unmask INTx after enabling */
161 memset(irq_set, 0, len);
162 len = sizeof(struct vfio_irq_set);
163 irq_set->argsz = len;
165 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK;
166 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
169 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
172 RTE_LOG(ERR, EAL, "Error unmasking INTx interrupts for fd %d\n",
179 /* disable legacy (INTx) interrupts */
181 vfio_disable_intx(struct rte_intr_handle *intr_handle) {
182 struct vfio_irq_set *irq_set;
183 char irq_set_buf[IRQ_SET_BUF_LEN];
186 len = sizeof(struct vfio_irq_set);
188 /* mask interrupts before disabling */
189 irq_set = (struct vfio_irq_set *) irq_set_buf;
190 irq_set->argsz = len;
192 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK;
193 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
196 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
199 RTE_LOG(ERR, EAL, "Error unmasking INTx interrupts for fd %d\n",
205 memset(irq_set, 0, len);
206 irq_set->argsz = len;
208 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
209 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
212 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
216 "Error disabling INTx interrupts for fd %d\n", intr_handle->fd);
222 /* enable MSI-X interrupts */
224 vfio_enable_msi(struct rte_intr_handle *intr_handle) {
226 char irq_set_buf[IRQ_SET_BUF_LEN];
227 struct vfio_irq_set *irq_set;
230 len = sizeof(irq_set_buf);
232 irq_set = (struct vfio_irq_set *) irq_set_buf;
233 irq_set->argsz = len;
235 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
236 irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
238 fd_ptr = (int *) &irq_set->data;
239 *fd_ptr = intr_handle->fd;
241 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
244 RTE_LOG(ERR, EAL, "Error enabling MSI interrupts for fd %d\n",
249 /* manually trigger interrupt to enable it */
250 memset(irq_set, 0, len);
251 len = sizeof(struct vfio_irq_set);
252 irq_set->argsz = len;
254 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
255 irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
258 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
261 RTE_LOG(ERR, EAL, "Error triggering MSI interrupts for fd %d\n",
268 /* disable MSI-X interrupts */
270 vfio_disable_msi(struct rte_intr_handle *intr_handle) {
271 struct vfio_irq_set *irq_set;
272 char irq_set_buf[IRQ_SET_BUF_LEN];
275 len = sizeof(struct vfio_irq_set);
277 irq_set = (struct vfio_irq_set *) irq_set_buf;
278 irq_set->argsz = len;
280 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
281 irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
284 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
288 "Error disabling MSI interrupts for fd %d\n", intr_handle->fd);
293 /* enable MSI-X interrupts */
295 vfio_enable_msix(struct rte_intr_handle *intr_handle) {
297 char irq_set_buf[IRQ_SET_BUF_LEN];
298 struct vfio_irq_set *irq_set;
301 len = sizeof(irq_set_buf);
303 irq_set = (struct vfio_irq_set *) irq_set_buf;
304 irq_set->argsz = len;
306 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
307 irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
309 fd_ptr = (int *) &irq_set->data;
310 *fd_ptr = intr_handle->fd;
312 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
315 RTE_LOG(ERR, EAL, "Error enabling MSI-X interrupts for fd %d\n",
320 /* manually trigger interrupt to enable it */
321 memset(irq_set, 0, len);
322 len = sizeof(struct vfio_irq_set);
323 irq_set->argsz = len;
325 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
326 irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
329 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
332 RTE_LOG(ERR, EAL, "Error triggering MSI-X interrupts for fd %d\n",
339 /* disable MSI-X interrupts */
341 vfio_disable_msix(struct rte_intr_handle *intr_handle) {
342 struct vfio_irq_set *irq_set;
343 char irq_set_buf[IRQ_SET_BUF_LEN];
346 len = sizeof(struct vfio_irq_set);
348 irq_set = (struct vfio_irq_set *) irq_set_buf;
349 irq_set->argsz = len;
351 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
352 irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
355 ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
359 "Error disabling MSI-X interrupts for fd %d\n", intr_handle->fd);
366 uio_intx_intr_disable(struct rte_intr_handle *intr_handle)
368 unsigned char command_high;
370 /* use UIO config file descriptor for uio_pci_generic */
371 if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
373 "Error reading interrupts status for fd %d\n",
374 intr_handle->uio_cfg_fd);
377 /* disable interrupts */
379 if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
381 "Error disabling interrupts for fd %d\n",
382 intr_handle->uio_cfg_fd);
390 uio_intx_intr_enable(struct rte_intr_handle *intr_handle)
392 unsigned char command_high;
394 /* use UIO config file descriptor for uio_pci_generic */
395 if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
397 "Error reading interrupts status for fd %d\n",
398 intr_handle->uio_cfg_fd);
401 /* enable interrupts */
402 command_high &= ~0x4;
403 if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
405 "Error enabling interrupts for fd %d\n",
406 intr_handle->uio_cfg_fd);
414 uio_intr_disable(struct rte_intr_handle *intr_handle)
418 if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
420 "Error disabling interrupts for fd %d (%s)\n",
421 intr_handle->fd, strerror(errno));
428 uio_intr_enable(struct rte_intr_handle *intr_handle)
432 if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
434 "Error enabling interrupts for fd %d (%s)\n",
435 intr_handle->fd, strerror(errno));
442 rte_intr_callback_register(struct rte_intr_handle *intr_handle,
443 rte_intr_callback_fn cb, void *cb_arg)
445 int ret, wake_thread;
446 struct rte_intr_source *src;
447 struct rte_intr_callback *callback;
451 /* first do parameter checking */
452 if (intr_handle == NULL || intr_handle->fd < 0 || cb == NULL) {
454 "Registering with invalid input parameter\n");
458 /* allocate a new interrupt callback entity */
459 callback = rte_zmalloc("interrupt callback list",
460 sizeof(*callback), 0);
461 if (callback == NULL) {
462 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
465 callback->cb_fn = cb;
466 callback->cb_arg = cb_arg;
468 rte_spinlock_lock(&intr_lock);
470 /* check if there is at least one callback registered for the fd */
471 TAILQ_FOREACH(src, &intr_sources, next) {
472 if (src->intr_handle.fd == intr_handle->fd) {
473 /* we had no interrupts for this */
474 if TAILQ_EMPTY(&src->callbacks)
477 TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
483 /* no existing callbacks for this - add new source */
485 if ((src = rte_zmalloc("interrupt source list",
486 sizeof(*src), 0)) == NULL) {
487 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
491 src->intr_handle = *intr_handle;
492 TAILQ_INIT(&src->callbacks);
493 TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
494 TAILQ_INSERT_TAIL(&intr_sources, src, next);
500 rte_spinlock_unlock(&intr_lock);
503 * check if need to notify the pipe fd waited by epoll_wait to
504 * rebuild the wait list.
507 if (write(intr_pipe.writefd, "1", 1) < 0)
514 rte_intr_callback_unregister(struct rte_intr_handle *intr_handle,
515 rte_intr_callback_fn cb_fn, void *cb_arg)
518 struct rte_intr_source *src;
519 struct rte_intr_callback *cb, *next;
521 /* do parameter checking first */
522 if (intr_handle == NULL || intr_handle->fd < 0) {
524 "Unregistering with invalid input parameter\n");
528 rte_spinlock_lock(&intr_lock);
530 /* check if the insterrupt source for the fd is existent */
531 TAILQ_FOREACH(src, &intr_sources, next)
532 if (src->intr_handle.fd == intr_handle->fd)
535 /* No interrupt source registered for the fd */
539 /* interrupt source has some active callbacks right now. */
540 } else if (src->active != 0) {
547 /*walk through the callbacks and remove all that match. */
548 for (cb = TAILQ_FIRST(&src->callbacks); cb != NULL; cb = next) {
550 next = TAILQ_NEXT(cb, next);
552 if (cb->cb_fn == cb_fn && (cb_arg == (void *)-1 ||
553 cb->cb_arg == cb_arg)) {
554 TAILQ_REMOVE(&src->callbacks, cb, next);
560 /* all callbacks for that source are removed. */
561 if (TAILQ_EMPTY(&src->callbacks)) {
562 TAILQ_REMOVE(&intr_sources, src, next);
567 rte_spinlock_unlock(&intr_lock);
569 /* notify the pipe fd waited by epoll_wait to rebuild the wait list */
570 if (ret >= 0 && write(intr_pipe.writefd, "1", 1) < 0) {
578 rte_intr_enable(struct rte_intr_handle *intr_handle)
580 if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
583 switch (intr_handle->type){
584 /* write to the uio fd to enable the interrupt */
585 case RTE_INTR_HANDLE_UIO:
586 if (uio_intr_enable(intr_handle))
589 case RTE_INTR_HANDLE_UIO_INTX:
590 if (uio_intx_intr_enable(intr_handle))
593 /* not used at this moment */
594 case RTE_INTR_HANDLE_ALARM:
597 case RTE_INTR_HANDLE_VFIO_MSIX:
598 if (vfio_enable_msix(intr_handle))
601 case RTE_INTR_HANDLE_VFIO_MSI:
602 if (vfio_enable_msi(intr_handle))
605 case RTE_INTR_HANDLE_VFIO_LEGACY:
606 if (vfio_enable_intx(intr_handle))
610 /* unknown handle type */
613 "Unknown handle type of fd %d\n",
622 rte_intr_disable(struct rte_intr_handle *intr_handle)
624 if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
627 switch (intr_handle->type){
628 /* write to the uio fd to disable the interrupt */
629 case RTE_INTR_HANDLE_UIO:
630 if (uio_intr_disable(intr_handle))
633 case RTE_INTR_HANDLE_UIO_INTX:
634 if (uio_intx_intr_disable(intr_handle))
637 /* not used at this moment */
638 case RTE_INTR_HANDLE_ALARM:
641 case RTE_INTR_HANDLE_VFIO_MSIX:
642 if (vfio_disable_msix(intr_handle))
645 case RTE_INTR_HANDLE_VFIO_MSI:
646 if (vfio_disable_msi(intr_handle))
649 case RTE_INTR_HANDLE_VFIO_LEGACY:
650 if (vfio_disable_intx(intr_handle))
654 /* unknown handle type */
657 "Unknown handle type of fd %d\n",
666 eal_intr_process_interrupts(struct epoll_event *events, int nfds)
669 struct rte_intr_source *src;
670 struct rte_intr_callback *cb;
671 union rte_intr_read_buffer buf;
672 struct rte_intr_callback active_cb;
674 for (n = 0; n < nfds; n++) {
677 * if the pipe fd is ready to read, return out to
678 * rebuild the wait list.
680 if (events[n].data.fd == intr_pipe.readfd){
681 int r = read(intr_pipe.readfd, buf.charbuf,
682 sizeof(buf.charbuf));
686 rte_spinlock_lock(&intr_lock);
687 TAILQ_FOREACH(src, &intr_sources, next)
688 if (src->intr_handle.fd ==
692 rte_spinlock_unlock(&intr_lock);
696 /* mark this interrupt source as active and release the lock. */
698 rte_spinlock_unlock(&intr_lock);
700 /* set the length to be read dor different handle type */
701 switch (src->intr_handle.type) {
702 case RTE_INTR_HANDLE_UIO:
703 bytes_read = sizeof(buf.uio_intr_count);
705 case RTE_INTR_HANDLE_ALARM:
706 bytes_read = sizeof(buf.timerfd_num);
709 case RTE_INTR_HANDLE_VFIO_MSIX:
710 case RTE_INTR_HANDLE_VFIO_MSI:
711 case RTE_INTR_HANDLE_VFIO_LEGACY:
712 bytes_read = sizeof(buf.vfio_intr_count);
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", events[n].data.fd,
732 } else if (bytes_read == 0)
733 RTE_LOG(ERR, EAL, "Read nothing from file "
734 "descriptor %d\n", events[n].data.fd);
736 /* grab a lock, again to call callbacks and update status. */
737 rte_spinlock_lock(&intr_lock);
739 if (bytes_read > 0) {
741 /* Finally, call all callbacks. */
742 TAILQ_FOREACH(cb, &src->callbacks, next) {
744 /* make a copy and unlock. */
746 rte_spinlock_unlock(&intr_lock);
748 /* call the actual callback */
749 active_cb.cb_fn(&src->intr_handle,
752 /*get the lock back. */
753 rte_spinlock_lock(&intr_lock);
757 /* we done with that interrupt source, release it. */
759 rte_spinlock_unlock(&intr_lock);
766 * It handles all the interrupts.
769 * epoll file descriptor.
771 * The number of file descriptors added in epoll.
777 eal_intr_handle_interrupts(int pfd, unsigned totalfds)
779 struct epoll_event events[totalfds];
783 nfds = epoll_wait(pfd, events, totalfds,
784 EAL_INTR_EPOLL_WAIT_FOREVER);
785 /* epoll_wait fail */
790 "epoll_wait returns with fail\n");
793 /* epoll_wait timeout, will never happens here */
796 /* epoll_wait has at least one fd ready to read */
797 if (eal_intr_process_interrupts(events, nfds) < 0)
803 * It builds/rebuilds up the epoll file descriptor with all the
804 * file descriptors being waited on. Then handles the interrupts.
812 static __attribute__((noreturn)) void *
813 eal_intr_thread_main(__rte_unused void *arg)
815 struct epoll_event ev;
817 /* host thread, never break out */
819 /* build up the epoll fd with all descriptors we are to
820 * wait on then pass it to the handle_interrupts function
822 static struct epoll_event pipe_event = {
823 .events = EPOLLIN | EPOLLPRI,
825 struct rte_intr_source *src;
828 /* create epoll fd */
829 int pfd = epoll_create(1);
831 rte_panic("Cannot create epoll instance\n");
833 pipe_event.data.fd = intr_pipe.readfd;
835 * add pipe fd into wait list, this pipe is used to
836 * rebuild the wait list.
838 if (epoll_ctl(pfd, EPOLL_CTL_ADD, intr_pipe.readfd,
840 rte_panic("Error adding fd to %d epoll_ctl, %s\n",
841 intr_pipe.readfd, strerror(errno));
845 rte_spinlock_lock(&intr_lock);
847 TAILQ_FOREACH(src, &intr_sources, next) {
848 if (src->callbacks.tqh_first == NULL)
849 continue; /* skip those with no callbacks */
850 ev.events = EPOLLIN | EPOLLPRI;
851 ev.data.fd = src->intr_handle.fd;
854 * add all the uio device file descriptor
857 if (epoll_ctl(pfd, EPOLL_CTL_ADD,
858 src->intr_handle.fd, &ev) < 0){
859 rte_panic("Error adding fd %d epoll_ctl, %s\n",
860 src->intr_handle.fd, strerror(errno));
865 rte_spinlock_unlock(&intr_lock);
866 /* serve the interrupt */
867 eal_intr_handle_interrupts(pfd, numfds);
870 * when we return, we need to rebuild the
871 * list of fds to monitor.
878 rte_eal_intr_init(void)
882 /* init the global interrupt source head */
883 TAILQ_INIT(&intr_sources);
886 * create a pipe which will be waited by epoll and notified to
887 * rebuild the wait list of epoll.
889 if (pipe(intr_pipe.pipefd) < 0)
892 /* create the host thread to wait/handle the interrupt */
893 ret = pthread_create(&intr_thread, NULL,
894 eal_intr_thread_main, NULL);
897 "Failed to create thread for interrupt handling\n");
903 eal_epoll_process_event(struct epoll_event *evs, unsigned int n,
904 struct rte_epoll_event *events)
906 unsigned int i, count = 0;
907 struct rte_epoll_event *rev;
909 for (i = 0; i < n; i++) {
910 rev = evs[i].data.ptr;
911 if (!rev || !rte_atomic32_cmpset(&rev->status, RTE_EPOLL_VALID,
915 events[count].status = RTE_EPOLL_VALID;
916 events[count].fd = rev->fd;
917 events[count].epfd = rev->epfd;
918 events[count].epdata.event = rev->epdata.event;
919 events[count].epdata.data = rev->epdata.data;
920 if (rev->epdata.cb_fun)
921 rev->epdata.cb_fun(rev->fd,
924 rte_compiler_barrier();
925 rev->status = RTE_EPOLL_VALID;
932 eal_init_tls_epfd(void)
934 int pfd = epoll_create(255);
938 "Cannot create epoll instance\n");
945 rte_intr_tls_epfd(void)
947 if (RTE_PER_LCORE(_epfd) == -1)
948 RTE_PER_LCORE(_epfd) = eal_init_tls_epfd();
950 return RTE_PER_LCORE(_epfd);
954 rte_epoll_wait(int epfd, struct rte_epoll_event *events,
955 int maxevents, int timeout)
957 struct epoll_event evs[maxevents];
961 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
965 /* using per thread epoll fd */
966 if (epfd == RTE_EPOLL_PER_THREAD)
967 epfd = rte_intr_tls_epfd();
970 rc = epoll_wait(epfd, evs, maxevents, timeout);
971 if (likely(rc > 0)) {
972 /* epoll_wait has at least one fd ready to read */
973 rc = eal_epoll_process_event(evs, rc, events);
978 /* epoll_wait fail */
979 RTE_LOG(ERR, EAL, "epoll_wait returns with fail %s\n",
990 eal_epoll_data_safe_free(struct rte_epoll_event *ev)
992 while (!rte_atomic32_cmpset(&ev->status, RTE_EPOLL_VALID,
994 while (ev->status != RTE_EPOLL_VALID)
996 memset(&ev->epdata, 0, sizeof(ev->epdata));
1002 rte_epoll_ctl(int epfd, int op, int fd,
1003 struct rte_epoll_event *event)
1005 struct epoll_event ev;
1008 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1012 /* using per thread epoll fd */
1013 if (epfd == RTE_EPOLL_PER_THREAD)
1014 epfd = rte_intr_tls_epfd();
1016 if (op == EPOLL_CTL_ADD) {
1017 event->status = RTE_EPOLL_VALID;
1018 event->fd = fd; /* ignore fd in event */
1020 ev.data.ptr = (void *)event;
1023 ev.events = event->epdata.event;
1024 if (epoll_ctl(epfd, op, fd, &ev) < 0) {
1025 RTE_LOG(ERR, EAL, "Error op %d fd %d epoll_ctl, %s\n",
1026 op, fd, strerror(errno));
1027 if (op == EPOLL_CTL_ADD)
1028 /* rollback status when CTL_ADD fail */
1029 event->status = RTE_EPOLL_INVALID;
1033 if (op == EPOLL_CTL_DEL && event->status != RTE_EPOLL_INVALID)
1034 eal_epoll_data_safe_free(event);