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>
40 #include <sys/timerfd.h>
42 #include <rte_memory.h>
43 #include <rte_memzone.h>
44 #include <rte_interrupts.h>
45 #include <rte_alarm.h>
46 #include <rte_common.h>
47 #include <rte_per_lcore.h>
49 #include <rte_launch.h>
50 #include <rte_lcore.h>
51 #include <rte_errno.h>
52 #include <rte_malloc.h>
53 #include <rte_spinlock.h>
54 #include <eal_private.h>
58 #define TFD_NONBLOCK O_NONBLOCK
61 #define NS_PER_US 1000
62 #define US_PER_MS 1000
64 #define US_PER_S (US_PER_MS * MS_PER_S)
66 #ifdef CLOCK_MONOTONIC_RAW /* Defined in glibc bits/time.h */
67 #define CLOCK_TYPE_ID CLOCK_MONOTONIC_RAW
69 #define CLOCK_TYPE_ID CLOCK_MONOTONIC
73 LIST_ENTRY(alarm_entry) next;
75 rte_eal_alarm_callback cb_fn;
77 volatile uint8_t executing;
78 volatile pthread_t executing_id;
81 static LIST_HEAD(alarm_list, alarm_entry) alarm_list = LIST_HEAD_INITIALIZER();
82 static rte_spinlock_t alarm_list_lk = RTE_SPINLOCK_INITIALIZER;
84 static struct rte_intr_handle intr_handle = {.fd = -1 };
85 static int handler_registered = 0;
86 static void eal_alarm_callback(void *arg);
89 rte_eal_alarm_init(void)
91 intr_handle.type = RTE_INTR_HANDLE_ALARM;
92 /* create a timerfd file descriptor */
93 intr_handle.fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
94 if (intr_handle.fd == -1)
105 eal_alarm_callback(void *arg __rte_unused)
108 struct alarm_entry *ap;
110 rte_spinlock_lock(&alarm_list_lk);
111 while ((ap = LIST_FIRST(&alarm_list)) !=NULL &&
112 clock_gettime(CLOCK_TYPE_ID, &now) == 0 &&
113 (ap->time.tv_sec < now.tv_sec || (ap->time.tv_sec == now.tv_sec &&
114 (ap->time.tv_usec * NS_PER_US) <= now.tv_nsec))) {
116 ap->executing_id = pthread_self();
117 rte_spinlock_unlock(&alarm_list_lk);
119 ap->cb_fn(ap->cb_arg);
121 rte_spinlock_lock(&alarm_list_lk);
123 LIST_REMOVE(ap, next);
127 if (!LIST_EMPTY(&alarm_list)) {
128 struct itimerspec atime = { .it_interval = { 0, 0 } };
130 ap = LIST_FIRST(&alarm_list);
131 atime.it_value.tv_sec = ap->time.tv_sec;
132 atime.it_value.tv_nsec = ap->time.tv_usec * NS_PER_US;
133 /* perform borrow for subtraction if necessary */
134 if (now.tv_nsec > (ap->time.tv_usec * NS_PER_US))
135 atime.it_value.tv_sec--, atime.it_value.tv_nsec += US_PER_S * NS_PER_US;
137 atime.it_value.tv_sec -= now.tv_sec;
138 atime.it_value.tv_nsec -= now.tv_nsec;
139 timerfd_settime(intr_handle.fd, 0, &atime, NULL);
141 rte_spinlock_unlock(&alarm_list_lk);
145 rte_eal_alarm_set(uint64_t us, rte_eal_alarm_callback cb_fn, void *cb_arg)
149 struct alarm_entry *ap, *new_alarm;
151 /* Check parameters, including that us won't cause a uint64_t overflow */
152 if (us < 1 || us > (UINT64_MAX - US_PER_S) || cb_fn == NULL)
155 new_alarm = rte_zmalloc(NULL, sizeof(*new_alarm), 0);
156 if (new_alarm == NULL)
159 /* use current time to calculate absolute time of alarm */
160 clock_gettime(CLOCK_TYPE_ID, &now);
162 new_alarm->cb_fn = cb_fn;
163 new_alarm->cb_arg = cb_arg;
164 new_alarm->time.tv_usec = ((now.tv_nsec / NS_PER_US) + us) % US_PER_S;
165 new_alarm->time.tv_sec = now.tv_sec + (((now.tv_nsec / NS_PER_US) + us) / US_PER_S);
167 rte_spinlock_lock(&alarm_list_lk);
168 if (!handler_registered) {
169 ret |= rte_intr_callback_register(&intr_handle,
170 eal_alarm_callback, NULL);
171 handler_registered = (ret == 0) ? 1 : 0;
174 if (LIST_EMPTY(&alarm_list))
175 LIST_INSERT_HEAD(&alarm_list, new_alarm, next);
177 LIST_FOREACH(ap, &alarm_list, next) {
178 if (ap->time.tv_sec > new_alarm->time.tv_sec ||
179 (ap->time.tv_sec == new_alarm->time.tv_sec &&
180 ap->time.tv_usec > new_alarm->time.tv_usec)){
181 LIST_INSERT_BEFORE(ap, new_alarm, next);
184 if (LIST_NEXT(ap, next) == NULL) {
185 LIST_INSERT_AFTER(ap, new_alarm, next);
191 if (LIST_FIRST(&alarm_list) == new_alarm) {
192 struct itimerspec alarm_time = {
193 .it_interval = {0, 0},
195 .tv_sec = us / US_PER_S,
196 .tv_nsec = (us % US_PER_S) * NS_PER_US,
199 ret |= timerfd_settime(intr_handle.fd, 0, &alarm_time, NULL);
201 rte_spinlock_unlock(&alarm_list_lk);
207 rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn, void *cb_arg)
209 struct alarm_entry *ap, *ap_prev;
221 rte_spinlock_lock(&alarm_list_lk);
222 /* remove any matches at the start of the list */
223 while ((ap = LIST_FIRST(&alarm_list)) != NULL &&
224 cb_fn == ap->cb_fn &&
225 (cb_arg == (void *)-1 || cb_arg == ap->cb_arg)) {
227 if (ap->executing == 0) {
228 LIST_REMOVE(ap, next);
232 /* If calling from other context, mark that alarm is executing
233 * so loop can spin till it finish. Otherwise we are trying to
234 * cancel our self - mark it by EINPROGRESS */
235 if (pthread_equal(ap->executing_id, pthread_self()) == 0)
245 /* now go through list, removing entries not at start */
246 LIST_FOREACH(ap, &alarm_list, next) {
247 /* this won't be true first time through */
248 if (cb_fn == ap->cb_fn &&
249 (cb_arg == (void *)-1 || cb_arg == ap->cb_arg)) {
251 if (ap->executing == 0) {
252 LIST_REMOVE(ap, next);
256 } else if (pthread_equal(ap->executing_id, pthread_self()) == 0)
263 rte_spinlock_unlock(&alarm_list_lk);
264 } while (executing != 0);
266 if (count == 0 && err == 0)