Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2014 Intel Corporation
3 : : */
4 : : #include <stdio.h>
5 : : #include <stdint.h>
6 : : #include <stdlib.h>
7 : : #include <errno.h>
8 : : #include <pthread.h>
9 : : #include <sys/queue.h>
10 : : #include <sys/time.h>
11 : : #include <sys/timerfd.h>
12 : :
13 : : #include <eal_export.h>
14 : : #include <eal_trace_internal.h>
15 : : #include <rte_interrupts.h>
16 : : #include <rte_alarm.h>
17 : : #include <rte_common.h>
18 : : #include <rte_errno.h>
19 : : #include <rte_spinlock.h>
20 : :
21 : : #include <eal_private.h>
22 : :
23 : : #ifndef TFD_NONBLOCK
24 : : #include <fcntl.h>
25 : : #define TFD_NONBLOCK O_NONBLOCK
26 : : #endif
27 : :
28 : : #define NS_PER_US 1000
29 : : #define US_PER_MS 1000
30 : : #define MS_PER_S 1000
31 : : #ifndef US_PER_S
32 : : #define US_PER_S (US_PER_MS * MS_PER_S)
33 : : #endif
34 : :
35 : : #ifdef CLOCK_MONOTONIC_RAW /* Defined in glibc bits/time.h */
36 : : #define CLOCK_TYPE_ID CLOCK_MONOTONIC_RAW
37 : : #else
38 : : #define CLOCK_TYPE_ID CLOCK_MONOTONIC
39 : : #endif
40 : :
41 : : struct alarm_entry {
42 : : LIST_ENTRY(alarm_entry) next;
43 : : struct timeval time;
44 : : rte_eal_alarm_callback cb_fn;
45 : : void *cb_arg;
46 : : volatile uint8_t executing;
47 : : volatile pthread_t executing_id;
48 : : };
49 : :
50 : : static LIST_HEAD(alarm_list, alarm_entry) alarm_list = LIST_HEAD_INITIALIZER();
51 : : static rte_spinlock_t alarm_list_lk = RTE_SPINLOCK_INITIALIZER;
52 : :
53 : : static struct rte_intr_handle *intr_handle;
54 : : static int handler_registered = 0;
55 : : static void eal_alarm_callback(void *arg);
56 : :
57 : : void
58 : 298 : rte_eal_alarm_cleanup(void)
59 : : {
60 : : /* unregister callback using intr_handle in interrupt thread */
61 : 298 : int ret = rte_intr_callback_unregister_sync(intr_handle,
62 : : eal_alarm_callback, (void *)-1);
63 [ + + ]: 298 : if (ret >= 0) {
64 : 2 : rte_intr_fd_close(intr_handle);
65 : 2 : rte_intr_instance_free(intr_handle);
66 : 2 : intr_handle = NULL;
67 : 2 : handler_registered = 0;
68 : : }
69 : 298 : }
70 : :
71 : : int
72 : 229 : rte_eal_alarm_init(void)
73 : : {
74 : :
75 : 229 : intr_handle = rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_PRIVATE);
76 [ - + ]: 229 : if (intr_handle == NULL) {
77 : 0 : EAL_LOG(ERR, "Fail to allocate intr_handle");
78 : 0 : goto error;
79 : : }
80 : :
81 [ - + ]: 229 : if (rte_intr_type_set(intr_handle, RTE_INTR_HANDLE_ALARM))
82 : 0 : goto error;
83 : :
84 : : /* create a timerfd file descriptor */
85 [ - + ]: 229 : if (rte_intr_fd_set(intr_handle,
86 : : timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK)))
87 : 0 : goto error;
88 : :
89 [ - + ]: 229 : if (rte_intr_fd_get(intr_handle) == -1)
90 : 0 : goto error;
91 : : return 0;
92 : :
93 : 0 : error:
94 : 0 : rte_intr_instance_free(intr_handle);
95 : 0 : rte_errno = errno;
96 : 0 : return -1;
97 : : }
98 : :
99 : : static void
100 : 3 : eal_alarm_callback(void *arg __rte_unused)
101 : : {
102 : : struct timespec now;
103 : : struct alarm_entry *ap;
104 : :
105 : : rte_spinlock_lock(&alarm_list_lk);
106 [ + - ]: 4 : while ((ap = LIST_FIRST(&alarm_list)) !=NULL &&
107 [ + + ]: 6 : clock_gettime(CLOCK_TYPE_ID, &now) == 0 &&
108 [ - + + + ]: 4 : (ap->time.tv_sec < now.tv_sec || (ap->time.tv_sec == now.tv_sec &&
109 [ + - ]: 3 : (ap->time.tv_usec * NS_PER_US) <= now.tv_nsec))) {
110 : 3 : ap->executing = 1;
111 : 3 : ap->executing_id = pthread_self();
112 : : rte_spinlock_unlock(&alarm_list_lk);
113 : :
114 : 3 : ap->cb_fn(ap->cb_arg);
115 : :
116 : : rte_spinlock_lock(&alarm_list_lk);
117 : :
118 [ + + ]: 3 : LIST_REMOVE(ap, next);
119 : 3 : free(ap);
120 : : }
121 : :
122 [ + + ]: 3 : if (!LIST_EMPTY(&alarm_list)) {
123 : 1 : struct itimerspec atime = { .it_interval = { 0, 0 } };
124 : :
125 : : ap = LIST_FIRST(&alarm_list);
126 : 1 : atime.it_value.tv_sec = ap->time.tv_sec;
127 : 1 : atime.it_value.tv_nsec = ap->time.tv_usec * NS_PER_US;
128 : : /* perform borrow for subtraction if necessary */
129 [ + - ]: 1 : if (now.tv_nsec > (ap->time.tv_usec * NS_PER_US)) {
130 : 1 : atime.it_value.tv_sec--;
131 : 1 : atime.it_value.tv_nsec += US_PER_S * NS_PER_US;
132 : : }
133 : :
134 : 1 : atime.it_value.tv_sec -= now.tv_sec;
135 : 1 : atime.it_value.tv_nsec -= now.tv_nsec;
136 : 1 : timerfd_settime(rte_intr_fd_get(intr_handle), 0, &atime, NULL);
137 : : }
138 : : rte_spinlock_unlock(&alarm_list_lk);
139 : 3 : }
140 : :
141 : : RTE_EXPORT_SYMBOL(rte_eal_alarm_set)
142 : : int
143 : 7 : rte_eal_alarm_set(uint64_t us, rte_eal_alarm_callback cb_fn, void *cb_arg)
144 : : {
145 : : struct timespec now;
146 : : int ret = 0;
147 : : struct alarm_entry *ap, *new_alarm;
148 : :
149 : : /* Check parameters, including that us won't cause a uint64_t overflow */
150 [ + + + + ]: 7 : if (us < 1 || us > (UINT64_MAX - US_PER_S) || cb_fn == NULL)
151 : : return -EINVAL;
152 : :
153 : 4 : new_alarm = calloc(1, sizeof(*new_alarm));
154 [ + - ]: 4 : if (new_alarm == NULL)
155 : : return -ENOMEM;
156 : :
157 : : /* use current time to calculate absolute time of alarm */
158 : 4 : clock_gettime(CLOCK_TYPE_ID, &now);
159 : :
160 : 4 : new_alarm->cb_fn = cb_fn;
161 : 4 : new_alarm->cb_arg = cb_arg;
162 : 4 : new_alarm->time.tv_usec = ((now.tv_nsec / NS_PER_US) + us) % US_PER_S;
163 : 4 : new_alarm->time.tv_sec = now.tv_sec + (((now.tv_nsec / NS_PER_US) + us) / US_PER_S);
164 : :
165 : : rte_spinlock_lock(&alarm_list_lk);
166 [ + + ]: 4 : if (!handler_registered) {
167 : : /* registration can fail, callback can be registered later */
168 [ + - ]: 2 : if (rte_intr_callback_register(intr_handle,
169 : : eal_alarm_callback, NULL) == 0)
170 : 2 : handler_registered = 1;
171 : : }
172 : :
173 [ + + ]: 4 : if (LIST_EMPTY(&alarm_list))
174 : 3 : LIST_INSERT_HEAD(&alarm_list, new_alarm, next);
175 : : else {
176 : : LIST_FOREACH(ap, &alarm_list, next) {
177 [ - + + - ]: 1 : if (ap->time.tv_sec > new_alarm->time.tv_sec ||
178 : 0 : (ap->time.tv_sec == new_alarm->time.tv_sec &&
179 [ # # ]: 0 : ap->time.tv_usec > new_alarm->time.tv_usec)){
180 : 0 : LIST_INSERT_BEFORE(ap, new_alarm, next);
181 : 0 : break;
182 : : }
183 [ + - ]: 1 : if (LIST_NEXT(ap, next) == NULL) {
184 : 1 : LIST_INSERT_AFTER(ap, new_alarm, next);
185 : 1 : break;
186 : : }
187 : : }
188 : : }
189 : :
190 [ + + ]: 4 : if (LIST_FIRST(&alarm_list) == new_alarm) {
191 : 3 : struct itimerspec alarm_time = {
192 : : .it_interval = {0, 0},
193 : : .it_value = {
194 : 3 : .tv_sec = us / US_PER_S,
195 : 3 : .tv_nsec = (us % US_PER_S) * NS_PER_US,
196 : : },
197 : : };
198 : 3 : ret |= timerfd_settime(rte_intr_fd_get(intr_handle), 0, &alarm_time, NULL);
199 : : }
200 : : rte_spinlock_unlock(&alarm_list_lk);
201 : :
202 : 4 : rte_eal_trace_alarm_set(us, cb_fn, cb_arg, ret);
203 : 4 : return ret;
204 : : }
205 : :
206 : : RTE_EXPORT_SYMBOL(rte_eal_alarm_cancel)
207 : : int
208 : 5 : rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn, void *cb_arg)
209 : : {
210 : : struct alarm_entry *ap, *ap_prev;
211 : : int count = 0;
212 : : int err = 0;
213 : : int executing;
214 : :
215 [ + + ]: 5 : if (!cb_fn) {
216 : 1 : rte_errno = EINVAL;
217 : 1 : return -1;
218 : : }
219 : :
220 : : do {
221 : : executing = 0;
222 : : rte_spinlock_lock(&alarm_list_lk);
223 : : /* remove any matches at the start of the list */
224 : 7 : while ((ap = LIST_FIRST(&alarm_list)) != NULL &&
225 [ + + + - : 5 : cb_fn == ap->cb_fn &&
- + ]
226 [ + + ]: 2 : (cb_arg == (void *)-1 || cb_arg == ap->cb_arg)) {
227 : :
228 [ + - ]: 1 : if (ap->executing == 0) {
229 [ - + ]: 1 : LIST_REMOVE(ap, next);
230 : 1 : free(ap);
231 : 1 : count++;
232 : : } else {
233 : : /* If calling from other context, mark that alarm is executing
234 : : * so loop can spin till it finish. Otherwise we are trying to
235 : : * cancel our self - mark it by EINPROGRESS */
236 [ # # ]: 0 : if (pthread_equal(ap->executing_id, pthread_self()) == 0)
237 : : executing++;
238 : : else
239 : : err = EINPROGRESS;
240 : :
241 : : break;
242 : : }
243 : : }
244 : : ap_prev = ap;
245 : :
246 : : /* now go through list, removing entries not at start */
247 [ + + ]: 5 : LIST_FOREACH(ap, &alarm_list, next) {
248 : : /* this won't be true first time through */
249 [ + - + - ]: 1 : if (cb_fn == ap->cb_fn &&
250 [ - + ]: 1 : (cb_arg == (void *)-1 || cb_arg == ap->cb_arg)) {
251 : :
252 [ # # ]: 0 : if (ap->executing == 0) {
253 [ # # ]: 0 : LIST_REMOVE(ap, next);
254 : 0 : free(ap);
255 : 0 : count++;
256 : : ap = ap_prev;
257 [ # # ]: 0 : } else if (pthread_equal(ap->executing_id, pthread_self()) == 0)
258 : 0 : executing++;
259 : : else
260 : : err = EINPROGRESS;
261 : : }
262 : : ap_prev = ap;
263 : : }
264 : :
265 : : rte_spinlock_unlock(&alarm_list_lk);
266 : :
267 : : /* Yield control to a second thread executing eal_alarm_callback to avoid
268 : : * its starvation, as it is waiting for the lock we have just released.
269 : : */
270 : 4 : sched_yield();
271 [ - + ]: 4 : } while (executing != 0);
272 : :
273 [ + + ]: 4 : if (count == 0 && err == 0)
274 : 3 : rte_errno = ENOENT;
275 [ - + ]: 1 : else if (err)
276 : 0 : rte_errno = err;
277 : :
278 : 4 : rte_eal_trace_alarm_cancel(cb_fn, cb_arg, count);
279 : 4 : return count;
280 : : }
|