1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2018 Intel Corporation
11 #include <rte_errno.h>
12 #include <rte_lcore.h>
13 #include <rte_spinlock.h>
14 #include <rte_common.h>
15 #include <rte_interrupts.h>
17 #include "eal_private.h"
18 #include "eal_alarm_private.h"
20 #define MAX_INTR_EVENTS 16
23 * union buffer for reading on different devices
25 union rte_intr_read_buffer {
26 char charbuf[16]; /* for others */
29 TAILQ_HEAD(rte_intr_cb_list, rte_intr_callback);
30 TAILQ_HEAD(rte_intr_source_list, rte_intr_source);
32 struct rte_intr_callback {
33 TAILQ_ENTRY(rte_intr_callback) next;
34 rte_intr_callback_fn cb_fn; /**< callback address */
35 void *cb_arg; /**< parameter for callback */
38 struct rte_intr_source {
39 TAILQ_ENTRY(rte_intr_source) next;
40 struct rte_intr_handle intr_handle; /**< interrupt handle */
41 struct rte_intr_cb_list callbacks; /**< user callbacks */
45 /* global spinlock for interrupt data operation */
46 static rte_spinlock_t intr_lock = RTE_SPINLOCK_INITIALIZER;
48 /* interrupt sources list */
49 static struct rte_intr_source_list intr_sources;
51 /* interrupt handling thread */
52 static pthread_t intr_thread;
54 static volatile int kq = -1;
57 intr_source_to_kevent(const struct rte_intr_handle *ih, struct kevent *ke)
59 /* alarm callbacks are special case */
60 if (ih->type == RTE_INTR_HANDLE_ALARM) {
63 /* get soonest alarm timeout */
64 if (eal_alarm_get_timeout_ns(&timeout_ns) < 0)
67 ke->filter = EVFILT_TIMER;
68 /* timers are one shot */
69 ke->flags |= EV_ONESHOT;
70 ke->fflags = NOTE_NSECONDS;
71 ke->data = timeout_ns;
73 ke->filter = EVFILT_READ;
81 rte_intr_callback_register(const struct rte_intr_handle *intr_handle,
82 rte_intr_callback_fn cb, void *cb_arg)
84 struct rte_intr_callback *callback = NULL;
85 struct rte_intr_source *src = NULL;
88 /* first do parameter checking */
89 if (intr_handle == NULL || intr_handle->fd < 0 || cb == NULL) {
91 "Registering with invalid input parameter\n");
95 RTE_LOG(ERR, EAL, "Kqueue is not active: %d\n", kq);
99 /* allocate a new interrupt callback entity */
100 callback = calloc(1, sizeof(*callback));
101 if (callback == NULL) {
102 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
105 callback->cb_fn = cb;
106 callback->cb_arg = cb_arg;
108 rte_spinlock_lock(&intr_lock);
110 /* check if there is at least one callback registered for the fd */
111 TAILQ_FOREACH(src, &intr_sources, next) {
112 if (src->intr_handle.fd == intr_handle->fd) {
113 /* we had no interrupts for this */
114 if (TAILQ_EMPTY(&src->callbacks))
117 TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
123 /* no existing callbacks for this - add new source */
125 src = calloc(1, sizeof(*src));
127 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
131 src->intr_handle = *intr_handle;
132 TAILQ_INIT(&src->callbacks);
133 TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
134 TAILQ_INSERT_TAIL(&intr_sources, src, next);
140 /* add events to the queue. timer events are special as we need to
143 if (add_event || src->intr_handle.type == RTE_INTR_HANDLE_ALARM) {
146 memset(&ke, 0, sizeof(ke));
147 ke.flags = EV_ADD; /* mark for addition to the queue */
149 if (intr_source_to_kevent(intr_handle, &ke) < 0) {
150 RTE_LOG(ERR, EAL, "Cannot convert interrupt handle to kevent\n");
156 * add the intr file descriptor into wait list.
158 if (kevent(kq, &ke, 1, NULL, 0, NULL) < 0) {
159 RTE_LOG(ERR, EAL, "Error adding fd %d kevent, %s\n",
160 src->intr_handle.fd, strerror(errno));
165 rte_spinlock_unlock(&intr_lock);
171 TAILQ_REMOVE(&(src->callbacks), callback, next);
172 if (TAILQ_EMPTY(&(src->callbacks))) {
173 TAILQ_REMOVE(&intr_sources, src, next);
178 rte_spinlock_unlock(&intr_lock);
183 rte_intr_callback_unregister(const struct rte_intr_handle *intr_handle,
184 rte_intr_callback_fn cb_fn, void *cb_arg)
187 struct rte_intr_source *src;
188 struct rte_intr_callback *cb, *next;
190 /* do parameter checking first */
191 if (intr_handle == NULL || intr_handle->fd < 0) {
193 "Unregistering with invalid input parameter\n");
197 RTE_LOG(ERR, EAL, "Kqueue is not active\n");
201 rte_spinlock_lock(&intr_lock);
203 /* check if the insterrupt source for the fd is existent */
204 TAILQ_FOREACH(src, &intr_sources, next)
205 if (src->intr_handle.fd == intr_handle->fd)
208 /* No interrupt source registered for the fd */
212 /* interrupt source has some active callbacks right now. */
213 } else if (src->active != 0) {
222 /* remove it from the kqueue */
223 memset(&ke, 0, sizeof(ke));
224 ke.flags = EV_DELETE; /* mark for deletion from the queue */
226 if (intr_source_to_kevent(intr_handle, &ke) < 0) {
227 RTE_LOG(ERR, EAL, "Cannot convert to kevent\n");
233 * remove intr file descriptor from wait list.
235 if (kevent(kq, &ke, 1, NULL, 0, NULL) < 0) {
236 RTE_LOG(ERR, EAL, "Error removing fd %d kevent, %s\n",
237 src->intr_handle.fd, strerror(errno));
238 /* removing non-existent even is an expected condition
239 * in some circumstances (e.g. oneshot events).
243 /*walk through the callbacks and remove all that match. */
244 for (cb = TAILQ_FIRST(&src->callbacks); cb != NULL; cb = next) {
245 next = TAILQ_NEXT(cb, next);
246 if (cb->cb_fn == cb_fn && (cb_arg == (void *)-1 ||
247 cb->cb_arg == cb_arg)) {
248 TAILQ_REMOVE(&src->callbacks, cb, next);
254 /* all callbacks for that source are removed. */
255 if (TAILQ_EMPTY(&src->callbacks)) {
256 TAILQ_REMOVE(&intr_sources, src, next);
261 rte_spinlock_unlock(&intr_lock);
267 rte_intr_enable(const struct rte_intr_handle *intr_handle)
269 if (intr_handle && intr_handle->type == RTE_INTR_HANDLE_VDEV)
272 if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
275 switch (intr_handle->type) {
276 /* not used at this moment */
277 case RTE_INTR_HANDLE_ALARM:
279 /* not used at this moment */
280 case RTE_INTR_HANDLE_DEV_EVENT:
282 /* unknown handle type */
285 "Unknown handle type of fd %d\n",
294 rte_intr_disable(const struct rte_intr_handle *intr_handle)
296 if (intr_handle && intr_handle->type == RTE_INTR_HANDLE_VDEV)
299 if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
302 switch (intr_handle->type) {
303 /* not used at this moment */
304 case RTE_INTR_HANDLE_ALARM:
306 /* not used at this moment */
307 case RTE_INTR_HANDLE_DEV_EVENT:
309 /* unknown handle type */
312 "Unknown handle type of fd %d\n",
321 eal_intr_process_interrupts(struct kevent *events, int nfds)
323 struct rte_intr_callback active_cb;
324 union rte_intr_read_buffer buf;
325 struct rte_intr_callback *cb;
326 struct rte_intr_source *src;
330 for (n = 0; n < nfds; n++) {
331 int event_fd = events[n].ident;
333 rte_spinlock_lock(&intr_lock);
334 TAILQ_FOREACH(src, &intr_sources, next)
335 if (src->intr_handle.fd == event_fd)
338 rte_spinlock_unlock(&intr_lock);
342 /* mark this interrupt source as active and release the lock. */
344 rte_spinlock_unlock(&intr_lock);
346 /* set the length to be read dor different handle type */
347 switch (src->intr_handle.type) {
348 case RTE_INTR_HANDLE_ALARM:
352 case RTE_INTR_HANDLE_VDEV:
353 case RTE_INTR_HANDLE_EXT:
357 case RTE_INTR_HANDLE_DEV_EVENT:
366 if (bytes_read > 0) {
368 * read out to clear the ready-to-be-read flag
371 bytes_read = read(event_fd, &buf, bytes_read);
372 if (bytes_read < 0) {
373 if (errno == EINTR || errno == EWOULDBLOCK)
376 RTE_LOG(ERR, EAL, "Error reading from file "
377 "descriptor %d: %s\n",
380 } else if (bytes_read == 0)
381 RTE_LOG(ERR, EAL, "Read nothing from file "
382 "descriptor %d\n", event_fd);
387 /* grab a lock, again to call callbacks and update status. */
388 rte_spinlock_lock(&intr_lock);
391 /* Finally, call all callbacks. */
392 TAILQ_FOREACH(cb, &src->callbacks, next) {
394 /* make a copy and unlock. */
396 rte_spinlock_unlock(&intr_lock);
398 /* call the actual callback */
399 active_cb.cb_fn(active_cb.cb_arg);
401 /*get the lock back. */
402 rte_spinlock_lock(&intr_lock);
406 /* we done with that interrupt source, release it. */
408 rte_spinlock_unlock(&intr_lock);
413 eal_intr_thread_main(void *arg __rte_unused)
415 struct kevent events[MAX_INTR_EVENTS];
418 /* host thread, never break out */
420 /* do not change anything, just wait */
421 nfds = kevent(kq, NULL, 0, events, MAX_INTR_EVENTS, NULL);
428 "kevent returns with fail\n");
431 /* kevent timeout, will never happen here */
435 /* kevent has at least one fd ready to read */
436 eal_intr_process_interrupts(events, nfds);
444 rte_eal_intr_init(void)
448 /* init the global interrupt source head */
449 TAILQ_INIT(&intr_sources);
453 RTE_LOG(ERR, EAL, "Cannot create kqueue instance\n");
457 /* create the host thread to wait/handle the interrupt */
458 ret = rte_ctrl_thread_create(&intr_thread, "eal-intr-thread", NULL,
459 eal_intr_thread_main, NULL);
463 "Failed to create thread for interrupt handling\n");
470 rte_intr_rx_ctl(struct rte_intr_handle *intr_handle,
471 int epfd, int op, unsigned int vec, void *data)
473 RTE_SET_USED(intr_handle);
483 rte_intr_efd_enable(struct rte_intr_handle *intr_handle, uint32_t nb_efd)
485 RTE_SET_USED(intr_handle);
486 RTE_SET_USED(nb_efd);
492 rte_intr_efd_disable(struct rte_intr_handle *intr_handle)
494 RTE_SET_USED(intr_handle);
498 rte_intr_dp_is_en(struct rte_intr_handle *intr_handle)
500 RTE_SET_USED(intr_handle);
505 rte_intr_allow_others(struct rte_intr_handle *intr_handle)
507 RTE_SET_USED(intr_handle);
512 rte_intr_cap_multiple(struct rte_intr_handle *intr_handle)
514 RTE_SET_USED(intr_handle);
519 rte_epoll_wait(int epfd, struct rte_epoll_event *events,
520 int maxevents, int timeout)
523 RTE_SET_USED(events);
524 RTE_SET_USED(maxevents);
525 RTE_SET_USED(timeout);
531 rte_epoll_ctl(int epfd, int op, int fd, struct rte_epoll_event *event)
542 rte_intr_tls_epfd(void)
548 rte_intr_free_epoll_fd(struct rte_intr_handle *intr_handle)
550 RTE_SET_USED(intr_handle);