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