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 : 246 : rte_eal_alarm_cleanup(void)
59 : : {
60 : : /* unregister callback using intr_handle in interrupt thread */
61 : 246 : int ret = rte_intr_callback_unregister_sync(intr_handle,
62 : : eal_alarm_callback, (void *)-1);
63 [ + + ]: 246 : if (ret >= 0) {
64 : 2 : rte_intr_instance_free(intr_handle);
65 : 2 : intr_handle = NULL;
66 : 2 : handler_registered = 0;
67 : : }
68 : 246 : }
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--;
130 : 1 : atime.it_value.tv_nsec += US_PER_S * NS_PER_US;
131 : : }
132 : :
133 : 1 : atime.it_value.tv_sec -= now.tv_sec;
134 : 1 : atime.it_value.tv_nsec -= now.tv_nsec;
135 : 1 : timerfd_settime(rte_intr_fd_get(intr_handle), 0, &atime, NULL);
136 : : }
137 : : rte_spinlock_unlock(&alarm_list_lk);
138 : 1 : }
139 : :
140 : : RTE_EXPORT_SYMBOL(rte_eal_alarm_set)
141 : : int
142 : 6 : rte_eal_alarm_set(uint64_t us, rte_eal_alarm_callback cb_fn, void *cb_arg)
143 : : {
144 : : struct timespec now;
145 : : int ret = 0;
146 : : struct alarm_entry *ap, *new_alarm;
147 : :
148 : : /* Check parameters, including that us won't cause a uint64_t overflow */
149 [ + + + + ]: 6 : if (us < 1 || us > (UINT64_MAX - US_PER_S) || cb_fn == NULL)
150 : : return -EINVAL;
151 : :
152 : 3 : new_alarm = calloc(1, sizeof(*new_alarm));
153 [ + - ]: 3 : if (new_alarm == NULL)
154 : : return -ENOMEM;
155 : :
156 : : /* use current time to calculate absolute time of alarm */
157 : 3 : clock_gettime(CLOCK_TYPE_ID, &now);
158 : :
159 : 3 : new_alarm->cb_fn = cb_fn;
160 : 3 : new_alarm->cb_arg = cb_arg;
161 : 3 : new_alarm->time.tv_usec = ((now.tv_nsec / NS_PER_US) + us) % US_PER_S;
162 : 3 : new_alarm->time.tv_sec = now.tv_sec + (((now.tv_nsec / NS_PER_US) + us) / US_PER_S);
163 : :
164 : : rte_spinlock_lock(&alarm_list_lk);
165 [ + + ]: 3 : if (!handler_registered) {
166 : : /* registration can fail, callback can be registered later */
167 [ + - ]: 2 : if (rte_intr_callback_register(intr_handle,
168 : : eal_alarm_callback, NULL) == 0)
169 : 2 : handler_registered = 1;
170 : : }
171 : :
172 [ + + ]: 3 : if (LIST_EMPTY(&alarm_list))
173 : 2 : LIST_INSERT_HEAD(&alarm_list, new_alarm, next);
174 : : else {
175 : : LIST_FOREACH(ap, &alarm_list, next) {
176 [ - + + - ]: 1 : if (ap->time.tv_sec > new_alarm->time.tv_sec ||
177 : 0 : (ap->time.tv_sec == new_alarm->time.tv_sec &&
178 [ # # ]: 0 : ap->time.tv_usec > new_alarm->time.tv_usec)){
179 : 0 : LIST_INSERT_BEFORE(ap, new_alarm, next);
180 : 0 : break;
181 : : }
182 [ + - ]: 1 : if (LIST_NEXT(ap, next) == NULL) {
183 : 1 : LIST_INSERT_AFTER(ap, new_alarm, next);
184 : 1 : break;
185 : : }
186 : : }
187 : : }
188 : :
189 [ + + ]: 3 : if (LIST_FIRST(&alarm_list) == new_alarm) {
190 : 2 : struct itimerspec alarm_time = {
191 : : .it_interval = {0, 0},
192 : : .it_value = {
193 : 2 : .tv_sec = us / US_PER_S,
194 : 2 : .tv_nsec = (us % US_PER_S) * NS_PER_US,
195 : : },
196 : : };
197 : 2 : ret |= timerfd_settime(rte_intr_fd_get(intr_handle), 0, &alarm_time, NULL);
198 : : }
199 : : rte_spinlock_unlock(&alarm_list_lk);
200 : :
201 : 3 : rte_eal_trace_alarm_set(us, cb_fn, cb_arg, ret);
202 : 3 : return ret;
203 : : }
204 : :
205 : : RTE_EXPORT_SYMBOL(rte_eal_alarm_cancel)
206 : : int
207 : 4 : rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn, void *cb_arg)
208 : : {
209 : : struct alarm_entry *ap, *ap_prev;
210 : : int count = 0;
211 : : int err = 0;
212 : : int executing;
213 : :
214 [ + + ]: 4 : if (!cb_fn) {
215 : 1 : rte_errno = EINVAL;
216 : 1 : return -1;
217 : : }
218 : :
219 : : do {
220 : : executing = 0;
221 : : rte_spinlock_lock(&alarm_list_lk);
222 : : /* remove any matches at the start of the list */
223 : 8 : while ((ap = LIST_FIRST(&alarm_list)) != NULL &&
224 [ + + + - : 5 : cb_fn == ap->cb_fn &&
- + ]
225 [ + + ]: 3 : (cb_arg == (void *)-1 || cb_arg == ap->cb_arg)) {
226 : :
227 [ + - ]: 2 : if (ap->executing == 0) {
228 [ - + ]: 2 : LIST_REMOVE(ap, next);
229 : 2 : free(ap);
230 : 2 : count++;
231 : : } else {
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 [ # # ]: 0 : if (pthread_equal(ap->executing_id, pthread_self()) == 0)
236 : : executing++;
237 : : else
238 : : err = EINPROGRESS;
239 : :
240 : : break;
241 : : }
242 : : }
243 : : ap_prev = ap;
244 : :
245 : : /* now go through list, removing entries not at start */
246 [ + + ]: 4 : LIST_FOREACH(ap, &alarm_list, next) {
247 : : /* this won't be true first time through */
248 [ + - + - ]: 1 : if (cb_fn == ap->cb_fn &&
249 [ - + ]: 1 : (cb_arg == (void *)-1 || cb_arg == ap->cb_arg)) {
250 : :
251 [ # # ]: 0 : if (ap->executing == 0) {
252 [ # # ]: 0 : LIST_REMOVE(ap, next);
253 : 0 : free(ap);
254 : 0 : count++;
255 : : ap = ap_prev;
256 [ # # ]: 0 : } else if (pthread_equal(ap->executing_id, pthread_self()) == 0)
257 : 0 : executing++;
258 : : else
259 : : err = EINPROGRESS;
260 : : }
261 : : ap_prev = ap;
262 : : }
263 : :
264 : : rte_spinlock_unlock(&alarm_list_lk);
265 : :
266 : : /* Yield control to a second thread executing eal_alarm_callback to avoid
267 : : * its starvation, as it is waiting for the lock we have just released.
268 : : */
269 : 3 : sched_yield();
270 [ - + ]: 3 : } while (executing != 0);
271 : :
272 [ + + ]: 3 : if (count == 0 && err == 0)
273 : 1 : rte_errno = ENOENT;
274 [ - + ]: 2 : else if (err)
275 : 0 : rte_errno = err;
276 : :
277 : 3 : rte_eal_trace_alarm_cancel(cb_fn, cb_arg, count);
278 : 3 : return count;
279 : : }
|