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