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