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 : 253 : rte_eal_alarm_cleanup(void)
59 : : {
60 : : /* unregister callback using intr_handle in interrupt thread */
61 : 253 : int ret = rte_intr_callback_unregister_sync(intr_handle,
62 : : eal_alarm_callback, (void *)-1);
63 [ + + ]: 253 : if (ret >= 0) {
64 : 2 : rte_intr_instance_free(intr_handle);
65 : 2 : intr_handle = NULL;
66 : 2 : handler_registered = 0;
67 : : }
68 : 253 : }
69 : :
70 : : int
71 : 186 : rte_eal_alarm_init(void)
72 : : {
73 : :
74 : 186 : intr_handle = rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_PRIVATE);
75 [ - + ]: 186 : if (intr_handle == NULL) {
76 : 0 : EAL_LOG(ERR, "Fail to allocate intr_handle");
77 : 0 : goto error;
78 : : }
79 : :
80 [ - + ]: 186 : if (rte_intr_type_set(intr_handle, RTE_INTR_HANDLE_ALARM))
81 : 0 : goto error;
82 : :
83 : : /* create a timerfd file descriptor */
84 [ - + ]: 186 : if (rte_intr_fd_set(intr_handle,
85 : : timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK)))
86 : 0 : goto error;
87 : :
88 [ - + ]: 186 : if (rte_intr_fd_get(intr_handle) == -1)
89 : 0 : goto error;
90 : : return 0;
91 : :
92 : 0 : error:
93 : 0 : rte_intr_instance_free(intr_handle);
94 : 0 : rte_errno = errno;
95 : 0 : return -1;
96 : : }
97 : :
98 : : static void
99 : 1 : eal_alarm_callback(void *arg __rte_unused)
100 : : {
101 : : struct timespec now;
102 : : struct alarm_entry *ap;
103 : :
104 : : rte_spinlock_lock(&alarm_list_lk);
105 [ + - ]: 2 : while ((ap = LIST_FIRST(&alarm_list)) !=NULL &&
106 [ + - ]: 2 : clock_gettime(CLOCK_TYPE_ID, &now) == 0 &&
107 [ - + + + ]: 2 : (ap->time.tv_sec < now.tv_sec || (ap->time.tv_sec == now.tv_sec &&
108 [ + - ]: 1 : (ap->time.tv_usec * NS_PER_US) <= now.tv_nsec))) {
109 : 1 : ap->executing = 1;
110 : 1 : ap->executing_id = pthread_self();
111 : : rte_spinlock_unlock(&alarm_list_lk);
112 : :
113 : 1 : ap->cb_fn(ap->cb_arg);
114 : :
115 : : rte_spinlock_lock(&alarm_list_lk);
116 : :
117 [ + - ]: 1 : LIST_REMOVE(ap, next);
118 : 1 : free(ap);
119 : : }
120 : :
121 [ + - ]: 1 : if (!LIST_EMPTY(&alarm_list)) {
122 : 1 : struct itimerspec atime = { .it_interval = { 0, 0 } };
123 : :
124 : : ap = LIST_FIRST(&alarm_list);
125 : 1 : atime.it_value.tv_sec = ap->time.tv_sec;
126 : 1 : atime.it_value.tv_nsec = ap->time.tv_usec * NS_PER_US;
127 : : /* perform borrow for subtraction if necessary */
128 [ + - ]: 1 : if (now.tv_nsec > (ap->time.tv_usec * NS_PER_US))
129 : 1 : atime.it_value.tv_sec--, atime.it_value.tv_nsec += US_PER_S * NS_PER_US;
130 : :
131 : 1 : atime.it_value.tv_sec -= now.tv_sec;
132 : 1 : atime.it_value.tv_nsec -= now.tv_nsec;
133 : 1 : timerfd_settime(rte_intr_fd_get(intr_handle), 0, &atime, NULL);
134 : : }
135 : : rte_spinlock_unlock(&alarm_list_lk);
136 : 1 : }
137 : :
138 : : RTE_EXPORT_SYMBOL(rte_eal_alarm_set)
139 : : int
140 : 6 : rte_eal_alarm_set(uint64_t us, rte_eal_alarm_callback cb_fn, void *cb_arg)
141 : : {
142 : : struct timespec now;
143 : : int ret = 0;
144 : : struct alarm_entry *ap, *new_alarm;
145 : :
146 : : /* Check parameters, including that us won't cause a uint64_t overflow */
147 [ + + + + ]: 6 : if (us < 1 || us > (UINT64_MAX - US_PER_S) || cb_fn == NULL)
148 : : return -EINVAL;
149 : :
150 : 3 : new_alarm = calloc(1, sizeof(*new_alarm));
151 [ + - ]: 3 : if (new_alarm == NULL)
152 : : return -ENOMEM;
153 : :
154 : : /* use current time to calculate absolute time of alarm */
155 : 3 : clock_gettime(CLOCK_TYPE_ID, &now);
156 : :
157 : 3 : new_alarm->cb_fn = cb_fn;
158 : 3 : new_alarm->cb_arg = cb_arg;
159 : 3 : new_alarm->time.tv_usec = ((now.tv_nsec / NS_PER_US) + us) % US_PER_S;
160 : 3 : new_alarm->time.tv_sec = now.tv_sec + (((now.tv_nsec / NS_PER_US) + us) / US_PER_S);
161 : :
162 : : rte_spinlock_lock(&alarm_list_lk);
163 [ + + ]: 3 : if (!handler_registered) {
164 : : /* registration can fail, callback can be registered later */
165 [ + - ]: 2 : if (rte_intr_callback_register(intr_handle,
166 : : eal_alarm_callback, NULL) == 0)
167 : 2 : handler_registered = 1;
168 : : }
169 : :
170 [ + + ]: 3 : if (LIST_EMPTY(&alarm_list))
171 : 2 : LIST_INSERT_HEAD(&alarm_list, new_alarm, next);
172 : : else {
173 : : LIST_FOREACH(ap, &alarm_list, next) {
174 [ - + + - ]: 1 : if (ap->time.tv_sec > new_alarm->time.tv_sec ||
175 : 0 : (ap->time.tv_sec == new_alarm->time.tv_sec &&
176 [ # # ]: 0 : ap->time.tv_usec > new_alarm->time.tv_usec)){
177 : 0 : LIST_INSERT_BEFORE(ap, new_alarm, next);
178 : 0 : break;
179 : : }
180 [ + - ]: 1 : if (LIST_NEXT(ap, next) == NULL) {
181 : 1 : LIST_INSERT_AFTER(ap, new_alarm, next);
182 : 1 : break;
183 : : }
184 : : }
185 : : }
186 : :
187 [ + + ]: 3 : if (LIST_FIRST(&alarm_list) == new_alarm) {
188 : 2 : struct itimerspec alarm_time = {
189 : : .it_interval = {0, 0},
190 : : .it_value = {
191 : 2 : .tv_sec = us / US_PER_S,
192 : 2 : .tv_nsec = (us % US_PER_S) * NS_PER_US,
193 : : },
194 : : };
195 : 2 : ret |= timerfd_settime(rte_intr_fd_get(intr_handle), 0, &alarm_time, NULL);
196 : : }
197 : : rte_spinlock_unlock(&alarm_list_lk);
198 : :
199 : 3 : rte_eal_trace_alarm_set(us, cb_fn, cb_arg, ret);
200 : 3 : return ret;
201 : : }
202 : :
203 : : RTE_EXPORT_SYMBOL(rte_eal_alarm_cancel)
204 : : int
205 : 4 : rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn, void *cb_arg)
206 : : {
207 : : struct alarm_entry *ap, *ap_prev;
208 : : int count = 0;
209 : : int err = 0;
210 : : int executing;
211 : :
212 [ + + ]: 4 : if (!cb_fn) {
213 : 1 : rte_errno = EINVAL;
214 : 1 : return -1;
215 : : }
216 : :
217 : : do {
218 : : executing = 0;
219 : : rte_spinlock_lock(&alarm_list_lk);
220 : : /* remove any matches at the start of the list */
221 : 8 : while ((ap = LIST_FIRST(&alarm_list)) != NULL &&
222 [ + + + - : 5 : cb_fn == ap->cb_fn &&
- + ]
223 [ + + ]: 3 : (cb_arg == (void *)-1 || cb_arg == ap->cb_arg)) {
224 : :
225 [ + - ]: 2 : if (ap->executing == 0) {
226 [ - + ]: 2 : LIST_REMOVE(ap, next);
227 : 2 : free(ap);
228 : 2 : count++;
229 : : } else {
230 : : /* If calling from other context, mark that alarm is executing
231 : : * so loop can spin till it finish. Otherwise we are trying to
232 : : * cancel our self - mark it by EINPROGRESS */
233 [ # # ]: 0 : if (pthread_equal(ap->executing_id, pthread_self()) == 0)
234 : : executing++;
235 : : else
236 : : err = EINPROGRESS;
237 : :
238 : : break;
239 : : }
240 : : }
241 : : ap_prev = ap;
242 : :
243 : : /* now go through list, removing entries not at start */
244 [ + + ]: 4 : LIST_FOREACH(ap, &alarm_list, next) {
245 : : /* this won't be true first time through */
246 [ + - + - ]: 1 : if (cb_fn == ap->cb_fn &&
247 [ - + ]: 1 : (cb_arg == (void *)-1 || cb_arg == ap->cb_arg)) {
248 : :
249 [ # # ]: 0 : if (ap->executing == 0) {
250 [ # # ]: 0 : LIST_REMOVE(ap, next);
251 : 0 : free(ap);
252 : 0 : count++;
253 : : ap = ap_prev;
254 [ # # ]: 0 : } else if (pthread_equal(ap->executing_id, pthread_self()) == 0)
255 : 0 : executing++;
256 : : else
257 : : err = EINPROGRESS;
258 : : }
259 : : ap_prev = ap;
260 : : }
261 : :
262 : : rte_spinlock_unlock(&alarm_list_lk);
263 : :
264 : : /* Yield control to a second thread executing eal_alarm_callback to avoid
265 : : * its starvation, as it is waiting for the lock we have just released.
266 : : */
267 : 3 : sched_yield();
268 [ - + ]: 3 : } while (executing != 0);
269 : :
270 [ + + ]: 3 : if (count == 0 && err == 0)
271 : 1 : rte_errno = ENOENT;
272 [ - + ]: 2 : else if (err)
273 : 0 : rte_errno = err;
274 : :
275 : 3 : rte_eal_trace_alarm_cancel(cb_fn, cb_arg, count);
276 : 3 : return count;
277 : : }
|