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