4 * Copyright(c) 2010-2012 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.
33 * version: DPDK.L.1.2.3-3
40 #include <sys/queue.h>
47 #include <sys/epoll.h>
48 #include <sys/signalfd.h>
50 #include <rte_common.h>
51 #include <rte_interrupts.h>
52 #include <rte_memory.h>
53 #include <rte_memzone.h>
54 #include <rte_launch.h>
55 #include <rte_tailq.h>
57 #include <rte_per_lcore.h>
58 #include <rte_lcore.h>
59 #include <rte_atomic.h>
60 #include <rte_branch_prediction.h>
62 #include <rte_debug.h>
64 #include <rte_mempool.h>
66 #include <rte_malloc.h>
67 #include <rte_errno.h>
68 #include <rte_spinlock.h>
70 #include "eal_private.h"
72 #define EAL_INTR_EPOLL_WAIT_FOREVER (-1)
88 * union buffer for reading on different devices
90 union rte_intr_read_buffer {
91 int uio_intr_count; /* for uio device */
92 uint64_t timerfd_num; /* for timerfd */
93 char charbuf[16]; /* for others */
96 TAILQ_HEAD(rte_intr_cb_list, rte_intr_callback);
97 TAILQ_HEAD(rte_intr_source_list, rte_intr_source);
99 struct rte_intr_callback {
100 TAILQ_ENTRY(rte_intr_callback) next;
101 rte_intr_callback_fn cb_fn; /**< callback address */
102 void *cb_arg; /**< parameter for callback */
105 struct rte_intr_source {
106 TAILQ_ENTRY(rte_intr_source) next;
107 struct rte_intr_handle intr_handle; /**< interrupt handle */
108 struct rte_intr_cb_list callbacks; /**< user callbacks */
111 /* global spinlock for interrupt data operation */
112 static rte_spinlock_t intr_lock = RTE_SPINLOCK_INITIALIZER;
114 /* union buffer for pipe read/write */
115 static union intr_pipefds intr_pipe;
117 /* interrupt sources list */
118 static struct rte_intr_source_list intr_sources;
120 /* interrupt handling thread */
121 static pthread_t intr_thread;
124 rte_intr_callback_register(struct rte_intr_handle *intr_handle,
125 rte_intr_callback_fn cb, void *cb_arg)
128 struct rte_intr_source *src;
131 /* first do parameter checking */
132 if (intr_handle == NULL || intr_handle->fd < 0 || cb == NULL) {
134 "Registering with invalid input parameter\n");
138 /* allocate a new interrupt callback entity */
139 struct rte_intr_callback *callback =
140 rte_zmalloc("interrupt callback list",
141 sizeof(*callback), 0);
142 if (callback == NULL) {
143 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
146 callback->cb_fn = cb;
147 callback->cb_arg = cb_arg;
149 rte_spinlock_lock(&intr_lock);
151 /* check if there is at least one callback registered for the fd */
152 TAILQ_FOREACH(src, &intr_sources, next)
153 if (src->intr_handle.fd == intr_handle->fd) {
154 if (src->callbacks.tqh_first == NULL)
155 /* we had no interrupts for this */
158 TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
162 /* No callback registered for this fd */
164 /* no existing callbacks for this - add new source */
165 src = rte_zmalloc("interrupt source list", sizeof(*src), 0);
167 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
171 src->intr_handle = *intr_handle;
172 TAILQ_INIT(&src->callbacks);
174 TAILQ_INSERT_TAIL(&intr_sources, src, next);
175 TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
179 rte_spinlock_unlock(&intr_lock);
181 * check if need to notify the pipe fd waited by epoll_wait to
182 * rebuild the wait list.
185 if (write(intr_pipe.writefd, "1", 1) < 0)
191 rte_spinlock_unlock(&intr_lock);
197 rte_intr_callback_unregister(struct rte_intr_handle *intr_handle,
198 rte_intr_callback_fn cb_fn, void *cb_arg)
201 struct rte_intr_source *src;
202 struct rte_intr_callback *cb;
204 /* do parameter checking first */
205 if (intr_handle == NULL || intr_handle->fd < 0) {
207 "Unregistering with invalid input parameter\n");
211 rte_spinlock_lock(&intr_lock);
213 /* check if the insterrupt source for the fd is existent */
214 TAILQ_FOREACH(src, &intr_sources, next)
215 if (src->intr_handle.fd == intr_handle->fd)
218 /* No interrupt source registered for the fd */
225 TAILQ_FOREACH(cb, &src->callbacks, next) {
226 if (cb->cb_fn != cb_fn)
228 if (cb_arg == (void *)-1 || cb->cb_arg == cb_arg) {
229 TAILQ_REMOVE(&src->callbacks, cb, next);
234 if (src->callbacks.tqh_first == NULL) {
235 TAILQ_REMOVE(&intr_sources, src, next);
240 /* notify the pipe fd waited by epoll_wait to rebuild the wait list */
241 if (write(intr_pipe.writefd, "1", 1) < 0) {
246 rte_spinlock_unlock(&intr_lock);
251 rte_spinlock_unlock(&intr_lock);
257 rte_intr_enable(struct rte_intr_handle *intr_handle)
261 if (!intr_handle || intr_handle->fd < 0)
264 switch (intr_handle->type){
265 /* write to the uio fd to enable the interrupt */
266 case RTE_INTR_HANDLE_UIO:
267 if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
269 "Error enabling interrupts for fd %d\n",
274 /* not used at this moment */
275 case RTE_INTR_HANDLE_ALARM:
277 /* unkown handle type */
280 "Unknown handle type of fd %d\n",
289 rte_intr_disable(struct rte_intr_handle *intr_handle)
293 if (!intr_handle || intr_handle->fd < 0)
296 switch (intr_handle->type){
297 /* write to the uio fd to disable the interrupt */
298 case RTE_INTR_HANDLE_UIO:
299 if (write(intr_handle->fd, &value, sizeof(value)) < 0){
301 "Error enabling interrupts for fd %d\n",
306 /* not used at this moment */
307 case RTE_INTR_HANDLE_ALARM:
309 /* unkown handle type */
312 "Unknown handle type of fd %d\n",
321 eal_intr_process_interrupts(struct epoll_event *events, int nfds)
323 int n, i, active_cb, bytes_read;
324 struct rte_intr_source *src;
325 struct rte_intr_callback *cb;
326 union rte_intr_read_buffer buf;
327 struct rte_intr_callback active_cbs[32];
329 for (n = 0; n < nfds; n++) {
331 * if the pipe fd is ready to read, return out to
332 * rebuild the wait list.
334 if (events[n].data.fd == intr_pipe.readfd){
335 int r = read(intr_pipe.readfd, buf.charbuf,
336 sizeof(buf.charbuf));
340 rte_spinlock_lock(&intr_lock);
341 TAILQ_FOREACH(src, &intr_sources, next)
342 if (src->intr_handle.fd ==
346 rte_spinlock_unlock(&intr_lock);
350 /* for this source, make a copy of all the callbacks,
351 * then unlock the lock, so the callbacks can
352 * themselves manipulate the list for future
356 memset(active_cbs, 0, sizeof(active_cbs));
357 TAILQ_FOREACH(cb, &src->callbacks, next)
358 active_cbs[active_cb++] = *cb;
359 rte_spinlock_unlock(&intr_lock);
361 /* set the length to be read dor different handle type */
362 switch (src->intr_handle.type) {
363 case RTE_INTR_HANDLE_UIO:
366 case RTE_INTR_HANDLE_ALARM:
367 bytes_read = sizeof(uint64_t);
374 * read out to clear the ready-to-be-read flag
377 bytes_read = read(events[n].data.fd, &buf, bytes_read);
378 if (bytes_read < 0) {
379 RTE_LOG(ERR, EAL, "Error reading from file descriptor"
380 " %d, error: %d\n", events[n].data.fd, errno);
383 else if (bytes_read == 0) {
385 "Read nothing from file descriptor %d.\n",
390 * Finally, call all callbacks from the copy
393 for (i = 0; i < active_cb; i++) {
394 if (active_cbs[i].cb_fn == NULL)
396 active_cbs[i].cb_fn(&src->intr_handle,
397 active_cbs[i].cb_arg);
405 * It handles all the interrupts.
408 * epoll file descriptor.
410 * The number of file descriptors added in epoll.
416 eal_intr_handle_interrupts(int pfd, unsigned totalfds)
418 struct epoll_event events[totalfds];
422 nfds = epoll_wait(pfd, events, totalfds,
423 EAL_INTR_EPOLL_WAIT_FOREVER);
424 /* epoll_wait fail */
429 "epoll_wait returns with fail\n");
432 /* epoll_wait timeout, will never happens here */
435 /* epoll_wait has at least one fd ready to read */
436 if (eal_intr_process_interrupts(events, nfds) < 0)
442 * It builds/rebuilds up the epoll file descriptor with all the
443 * file descriptors being waited on. Then handles the interrupts.
451 static __attribute__((noreturn)) void *
452 eal_intr_thread_main(__rte_unused void *arg)
454 struct epoll_event ev;
456 /* host thread, never break out */
458 /* build up the epoll fd with all descriptors we are to
459 * wait on then pass it to the handle_interrupts function
461 static struct epoll_event pipe_event = {
462 .events = EPOLLIN | EPOLLPRI,
464 struct rte_intr_source *src;
467 /* create epoll fd */
468 int pfd = epoll_create(1);
470 rte_panic("Cannot create epoll instance\n");
472 pipe_event.data.fd = intr_pipe.readfd;
474 * add pipe fd into wait list, this pipe is used to
475 * rebuild the wait list.
477 if (epoll_ctl(pfd, EPOLL_CTL_ADD, intr_pipe.readfd,
479 rte_panic("Error adding fd to %d epoll_ctl, %s\n",
480 intr_pipe.readfd, strerror(errno));
484 rte_spinlock_lock(&intr_lock);
486 TAILQ_FOREACH(src, &intr_sources, next) {
487 if (src->callbacks.tqh_first == NULL)
488 continue; /* skip those with no callbacks */
489 ev.events = EPOLLIN | EPOLLPRI;
490 ev.data.fd = src->intr_handle.fd;
493 * add all the uio device file descriptor
496 if (epoll_ctl(pfd, EPOLL_CTL_ADD,
497 src->intr_handle.fd, &ev) < 0){
498 rte_panic("Error adding fd %d epoll_ctl, %s\n",
499 src->intr_handle.fd, strerror(errno));
504 rte_spinlock_unlock(&intr_lock);
505 /* serve the interrupt */
506 eal_intr_handle_interrupts(pfd, numfds);
509 * when we return, we need to rebuild the
510 * list of fds to monitor.
517 rte_eal_intr_init(void)
521 /* init the global interrupt source head */
522 TAILQ_INIT(&intr_sources);
525 * create a pipe which will be waited by epoll and notified to
526 * rebuild the wait list of epoll.
528 if (pipe(intr_pipe.pipefd) < 0)
531 /* create the host thread to wait/handle the interrupt */
532 ret = pthread_create(&intr_thread, NULL,
533 eal_intr_thread_main, NULL);
536 "Failed to create thread for interrupt handling\n");