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