xref: /dpdk/lib/eal/freebsd/eal_alarm.c (revision a4835c22ccfb5c5ba0aa5b32ebbafc0df12bf75a)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 Intel Corporation
3  */
4 
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <pthread.h>
8 #include <fcntl.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <time.h>
13 #include <errno.h>
14 
15 #include <eal_trace_internal.h>
16 #include <rte_alarm.h>
17 #include <rte_cycles.h>
18 #include <rte_common.h>
19 #include <rte_errno.h>
20 #include <rte_interrupts.h>
21 #include <rte_spinlock.h>
22 
23 #include "eal_private.h"
24 #include "eal_alarm_private.h"
25 
26 #define NS_PER_US 1000
27 
28 #ifdef CLOCK_MONOTONIC_RAW /* Defined in glibc bits/time.h */
29 #define CLOCK_TYPE_ID CLOCK_MONOTONIC_RAW
30 #else
31 #define CLOCK_TYPE_ID CLOCK_MONOTONIC
32 #endif
33 
34 struct alarm_entry {
35 	LIST_ENTRY(alarm_entry) next;
36 	struct timespec time;
37 	rte_eal_alarm_callback cb_fn;
38 	void *cb_arg;
39 	volatile uint8_t executing;
40 	volatile pthread_t executing_id;
41 };
42 
43 static LIST_HEAD(alarm_list, alarm_entry) alarm_list = LIST_HEAD_INITIALIZER();
44 static rte_spinlock_t alarm_list_lk = RTE_SPINLOCK_INITIALIZER;
45 
46 static struct rte_intr_handle *intr_handle;
47 static void eal_alarm_callback(void *arg);
48 
49 void
50 rte_eal_alarm_cleanup(void)
51 {
52 	rte_intr_instance_free(intr_handle);
53 }
54 
55 int
56 rte_eal_alarm_init(void)
57 {
58 	int fd;
59 
60 	intr_handle = rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_PRIVATE);
61 	if (intr_handle == NULL) {
62 		EAL_LOG(ERR, "Fail to allocate intr_handle");
63 		goto error;
64 	}
65 
66 	if (rte_intr_type_set(intr_handle, RTE_INTR_HANDLE_ALARM))
67 		goto error;
68 
69 	if (rte_intr_fd_set(intr_handle, -1))
70 		goto error;
71 
72 	/* on FreeBSD, timers don't use fd's, and their identifiers are stored
73 	 * in separate namespace from fd's, so using any value is OK. however,
74 	 * EAL interrupts handler expects fd's to be unique, so use an actual fd
75 	 * to guarantee unique timer identifier.
76 	 */
77 	fd = open("/dev/zero", O_RDONLY);
78 
79 	if (rte_intr_fd_set(intr_handle, fd))
80 		goto error;
81 
82 	return 0;
83 error:
84 	rte_intr_instance_free(intr_handle);
85 	return -1;
86 }
87 
88 static inline int
89 timespec_cmp(const struct timespec *now, const struct timespec *at)
90 {
91 	if (now->tv_sec < at->tv_sec)
92 		return -1;
93 	if (now->tv_sec > at->tv_sec)
94 		return 1;
95 	if (now->tv_nsec < at->tv_nsec)
96 		return -1;
97 	if (now->tv_nsec > at->tv_nsec)
98 		return 1;
99 	return 0;
100 }
101 
102 static inline uint64_t
103 diff_ns(struct timespec *now, struct timespec *at)
104 {
105 	uint64_t now_ns, at_ns;
106 
107 	if (timespec_cmp(now, at) >= 0)
108 		return 0;
109 
110 	now_ns = now->tv_sec * NS_PER_S + now->tv_nsec;
111 	at_ns = at->tv_sec * NS_PER_S + at->tv_nsec;
112 
113 	return at_ns - now_ns;
114 }
115 
116 int
117 eal_alarm_get_timeout_ns(uint64_t *val)
118 {
119 	struct alarm_entry *ap;
120 	struct timespec now;
121 
122 	if (clock_gettime(CLOCK_TYPE_ID, &now) < 0)
123 		return -1;
124 
125 	if (LIST_EMPTY(&alarm_list))
126 		return -1;
127 
128 	ap = LIST_FIRST(&alarm_list);
129 
130 	*val = diff_ns(&now, &ap->time);
131 
132 	return 0;
133 }
134 
135 static int
136 unregister_current_callback(void)
137 {
138 	struct alarm_entry *ap;
139 	int ret = 0;
140 
141 	if (!LIST_EMPTY(&alarm_list)) {
142 		ap = LIST_FIRST(&alarm_list);
143 
144 		do {
145 			ret = rte_intr_callback_unregister(intr_handle,
146 				eal_alarm_callback, &ap->time);
147 		} while (ret == -EAGAIN);
148 	}
149 
150 	return ret;
151 }
152 
153 static int
154 register_first_callback(void)
155 {
156 	struct alarm_entry *ap;
157 	int ret = 0;
158 
159 	if (!LIST_EMPTY(&alarm_list)) {
160 		ap = LIST_FIRST(&alarm_list);
161 
162 		/* register a new callback */
163 		ret = rte_intr_callback_register(intr_handle,
164 				eal_alarm_callback, &ap->time);
165 	}
166 	return ret;
167 }
168 
169 static void
170 eal_alarm_callback(void *arg __rte_unused)
171 {
172 	struct timespec now;
173 	struct alarm_entry *ap;
174 
175 	if (clock_gettime(CLOCK_TYPE_ID, &now) < 0)
176 		return;
177 
178 	rte_spinlock_lock(&alarm_list_lk);
179 	ap = LIST_FIRST(&alarm_list);
180 
181 	while (ap != NULL && timespec_cmp(&now, &ap->time) >= 0) {
182 		ap->executing = 1;
183 		ap->executing_id = pthread_self();
184 		rte_spinlock_unlock(&alarm_list_lk);
185 
186 		ap->cb_fn(ap->cb_arg);
187 
188 		rte_spinlock_lock(&alarm_list_lk);
189 
190 		LIST_REMOVE(ap, next);
191 		free(ap);
192 
193 		ap = LIST_FIRST(&alarm_list);
194 	}
195 
196 	/* timer has been deleted from the kqueue, so recreate it if needed */
197 	register_first_callback();
198 
199 	rte_spinlock_unlock(&alarm_list_lk);
200 }
201 
202 
203 int
204 rte_eal_alarm_set(uint64_t us, rte_eal_alarm_callback cb_fn, void *cb_arg)
205 {
206 	struct alarm_entry *ap, *new_alarm;
207 	struct timespec now;
208 	uint64_t ns;
209 	int ret = 0;
210 
211 	/* check parameters, also ensure us won't cause a uint64_t overflow */
212 	if (us < 1 || us > (UINT64_MAX - US_PER_S) || cb_fn == NULL)
213 		return -EINVAL;
214 
215 	new_alarm = calloc(1, sizeof(*new_alarm));
216 	if (new_alarm == NULL)
217 		return -ENOMEM;
218 
219 	/* use current time to calculate absolute time of alarm */
220 	clock_gettime(CLOCK_TYPE_ID, &now);
221 
222 	ns = us * NS_PER_US;
223 
224 	new_alarm->cb_fn = cb_fn;
225 	new_alarm->cb_arg = cb_arg;
226 	new_alarm->time.tv_nsec = (now.tv_nsec + ns) % NS_PER_S;
227 	new_alarm->time.tv_sec = now.tv_sec + ((now.tv_nsec + ns) / NS_PER_S);
228 
229 	rte_spinlock_lock(&alarm_list_lk);
230 
231 	if (LIST_EMPTY(&alarm_list))
232 		LIST_INSERT_HEAD(&alarm_list, new_alarm, next);
233 	else {
234 		LIST_FOREACH(ap, &alarm_list, next) {
235 			if (timespec_cmp(&new_alarm->time, &ap->time) < 0) {
236 				LIST_INSERT_BEFORE(ap, new_alarm, next);
237 				break;
238 			}
239 			if (LIST_NEXT(ap, next) == NULL) {
240 				LIST_INSERT_AFTER(ap, new_alarm, next);
241 				break;
242 			}
243 		}
244 	}
245 
246 	/* re-register first callback just in case */
247 	register_first_callback();
248 
249 	rte_spinlock_unlock(&alarm_list_lk);
250 
251 	rte_eal_trace_alarm_set(us, cb_fn, cb_arg, ret);
252 	return ret;
253 }
254 
255 int
256 rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn, void *cb_arg)
257 {
258 	struct alarm_entry *ap, *ap_prev;
259 	int count = 0;
260 	int err = 0;
261 	int executing;
262 
263 	if (!cb_fn) {
264 		rte_errno = EINVAL;
265 		return -1;
266 	}
267 
268 	do {
269 		executing = 0;
270 		rte_spinlock_lock(&alarm_list_lk);
271 		/* remove any matches at the start of the list */
272 		while (1) {
273 			ap = LIST_FIRST(&alarm_list);
274 			if (ap == NULL)
275 				break;
276 			if (cb_fn != ap->cb_fn)
277 				break;
278 			if (cb_arg != ap->cb_arg && cb_arg != (void *) -1)
279 				break;
280 			if (ap->executing == 0) {
281 				LIST_REMOVE(ap, next);
282 				free(ap);
283 				count++;
284 			} else {
285 				/* If calling from other context, mark that
286 				 * alarm is executing so loop can spin till it
287 				 * finish. Otherwise we are trying to cancel
288 				 * ourselves - mark it by EINPROGRESS.
289 				 */
290 				if (pthread_equal(ap->executing_id,
291 						pthread_self()) == 0)
292 					executing++;
293 				else
294 					err = EINPROGRESS;
295 
296 				break;
297 			}
298 		}
299 		ap_prev = ap;
300 
301 		/* now go through list, removing entries not at start */
302 		LIST_FOREACH(ap, &alarm_list, next) {
303 			/* this won't be true first time through */
304 			if (cb_fn == ap->cb_fn &&
305 					(cb_arg == (void *)-1 ||
306 					 cb_arg == ap->cb_arg)) {
307 				if (ap->executing == 0) {
308 					LIST_REMOVE(ap, next);
309 					free(ap);
310 					count++;
311 					ap = ap_prev;
312 				} else if (pthread_equal(ap->executing_id,
313 							 pthread_self()) == 0) {
314 					executing++;
315 				} else {
316 					err = EINPROGRESS;
317 				}
318 			}
319 			ap_prev = ap;
320 		}
321 
322 		rte_spinlock_unlock(&alarm_list_lk);
323 
324 		/* Yield control to a second thread executing eal_alarm_callback to avoid
325 		 * its starvation, as it is waiting for the lock we have just released.
326 		 */
327 		sched_yield();
328 	} while (executing != 0);
329 
330 	if (count == 0 && err == 0)
331 		rte_errno = ENOENT;
332 	else if (err)
333 		rte_errno = err;
334 
335 	rte_spinlock_lock(&alarm_list_lk);
336 
337 	/* unregister if no alarms left, otherwise re-register first */
338 	if (LIST_EMPTY(&alarm_list))
339 		unregister_current_callback();
340 	else
341 		register_first_callback();
342 
343 	rte_spinlock_unlock(&alarm_list_lk);
344 
345 	rte_eal_trace_alarm_cancel(cb_fn, cb_arg, count);
346 	return count;
347 }
348