New upstream version 17.11-rc3
[deb_dpdk.git] / lib / librte_eal / linuxapp / eal / eal_alarm.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 #include <stdio.h>
34 #include <stdint.h>
35 #include <signal.h>
36 #include <errno.h>
37 #include <string.h>
38 #include <sys/queue.h>
39 #include <sys/time.h>
40 #include <sys/timerfd.h>
41
42 #include <rte_memory.h>
43 #include <rte_interrupts.h>
44 #include <rte_alarm.h>
45 #include <rte_common.h>
46 #include <rte_per_lcore.h>
47 #include <rte_eal.h>
48 #include <rte_launch.h>
49 #include <rte_lcore.h>
50 #include <rte_errno.h>
51 #include <rte_malloc.h>
52 #include <rte_spinlock.h>
53 #include <eal_private.h>
54
55 #ifndef TFD_NONBLOCK
56 #include <fcntl.h>
57 #define TFD_NONBLOCK    O_NONBLOCK
58 #endif
59
60 #define NS_PER_US 1000
61 #define US_PER_MS 1000
62 #define MS_PER_S 1000
63 #define US_PER_S (US_PER_MS * MS_PER_S)
64
65 #ifdef CLOCK_MONOTONIC_RAW /* Defined in glibc bits/time.h */
66 #define CLOCK_TYPE_ID CLOCK_MONOTONIC_RAW
67 #else
68 #define CLOCK_TYPE_ID CLOCK_MONOTONIC
69 #endif
70
71 struct alarm_entry {
72         LIST_ENTRY(alarm_entry) next;
73         struct timeval time;
74         rte_eal_alarm_callback cb_fn;
75         void *cb_arg;
76         volatile uint8_t executing;
77         volatile pthread_t executing_id;
78 };
79
80 static LIST_HEAD(alarm_list, alarm_entry) alarm_list = LIST_HEAD_INITIALIZER();
81 static rte_spinlock_t alarm_list_lk = RTE_SPINLOCK_INITIALIZER;
82
83 static struct rte_intr_handle intr_handle = {.fd = -1 };
84 static int handler_registered = 0;
85 static void eal_alarm_callback(void *arg);
86
87 int
88 rte_eal_alarm_init(void)
89 {
90         intr_handle.type = RTE_INTR_HANDLE_ALARM;
91         /* create a timerfd file descriptor */
92         intr_handle.fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
93         if (intr_handle.fd == -1)
94                 goto error;
95
96         return 0;
97
98 error:
99         rte_errno = errno;
100         return -1;
101 }
102
103 static void
104 eal_alarm_callback(void *arg __rte_unused)
105 {
106         struct timespec now;
107         struct alarm_entry *ap;
108
109         rte_spinlock_lock(&alarm_list_lk);
110         while ((ap = LIST_FIRST(&alarm_list)) !=NULL &&
111                         clock_gettime(CLOCK_TYPE_ID, &now) == 0 &&
112                         (ap->time.tv_sec < now.tv_sec || (ap->time.tv_sec == now.tv_sec &&
113                                                 (ap->time.tv_usec * NS_PER_US) <= now.tv_nsec))) {
114                 ap->executing = 1;
115                 ap->executing_id = pthread_self();
116                 rte_spinlock_unlock(&alarm_list_lk);
117
118                 ap->cb_fn(ap->cb_arg);
119
120                 rte_spinlock_lock(&alarm_list_lk);
121
122                 LIST_REMOVE(ap, next);
123                 rte_free(ap);
124         }
125
126         if (!LIST_EMPTY(&alarm_list)) {
127                 struct itimerspec atime = { .it_interval = { 0, 0 } };
128
129                 ap = LIST_FIRST(&alarm_list);
130                 atime.it_value.tv_sec = ap->time.tv_sec;
131                 atime.it_value.tv_nsec = ap->time.tv_usec * NS_PER_US;
132                 /* perform borrow for subtraction if necessary */
133                 if (now.tv_nsec > (ap->time.tv_usec * NS_PER_US))
134                         atime.it_value.tv_sec--, atime.it_value.tv_nsec += US_PER_S * NS_PER_US;
135
136                 atime.it_value.tv_sec -= now.tv_sec;
137                 atime.it_value.tv_nsec -= now.tv_nsec;
138                 timerfd_settime(intr_handle.fd, 0, &atime, NULL);
139         }
140         rte_spinlock_unlock(&alarm_list_lk);
141 }
142
143 int
144 rte_eal_alarm_set(uint64_t us, rte_eal_alarm_callback cb_fn, void *cb_arg)
145 {
146         struct timespec now;
147         int ret = 0;
148         struct alarm_entry *ap, *new_alarm;
149
150         /* Check parameters, including that us won't cause a uint64_t overflow */
151         if (us < 1 || us > (UINT64_MAX - US_PER_S) || cb_fn == NULL)
152                 return -EINVAL;
153
154         new_alarm = rte_zmalloc(NULL, sizeof(*new_alarm), 0);
155         if (new_alarm == NULL)
156                 return -ENOMEM;
157
158         /* use current time to calculate absolute time of alarm */
159         clock_gettime(CLOCK_TYPE_ID, &now);
160
161         new_alarm->cb_fn = cb_fn;
162         new_alarm->cb_arg = cb_arg;
163         new_alarm->time.tv_usec = ((now.tv_nsec / NS_PER_US) + us) % US_PER_S;
164         new_alarm->time.tv_sec = now.tv_sec + (((now.tv_nsec / NS_PER_US) + us) / US_PER_S);
165
166         rte_spinlock_lock(&alarm_list_lk);
167         if (!handler_registered) {
168                 ret |= rte_intr_callback_register(&intr_handle,
169                                 eal_alarm_callback, NULL);
170                 handler_registered = (ret == 0) ? 1 : 0;
171         }
172
173         if (LIST_EMPTY(&alarm_list))
174                 LIST_INSERT_HEAD(&alarm_list, new_alarm, next);
175         else {
176                 LIST_FOREACH(ap, &alarm_list, next) {
177                         if (ap->time.tv_sec > new_alarm->time.tv_sec ||
178                                         (ap->time.tv_sec == new_alarm->time.tv_sec &&
179                                                         ap->time.tv_usec > new_alarm->time.tv_usec)){
180                                 LIST_INSERT_BEFORE(ap, new_alarm, next);
181                                 break;
182                         }
183                         if (LIST_NEXT(ap, next) == NULL) {
184                                 LIST_INSERT_AFTER(ap, new_alarm, next);
185                                 break;
186                         }
187                 }
188         }
189
190         if (LIST_FIRST(&alarm_list) == new_alarm) {
191                 struct itimerspec alarm_time = {
192                         .it_interval = {0, 0},
193                         .it_value = {
194                                 .tv_sec = us / US_PER_S,
195                                 .tv_nsec = (us % US_PER_S) * NS_PER_US,
196                         },
197                 };
198                 ret |= timerfd_settime(intr_handle.fd, 0, &alarm_time, NULL);
199         }
200         rte_spinlock_unlock(&alarm_list_lk);
201
202         return ret;
203 }
204
205 int
206 rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn, void *cb_arg)
207 {
208         struct alarm_entry *ap, *ap_prev;
209         int count = 0;
210         int err = 0;
211         int executing;
212
213         if (!cb_fn) {
214                 rte_errno = EINVAL;
215                 return -1;
216         }
217
218         do {
219                 executing = 0;
220                 rte_spinlock_lock(&alarm_list_lk);
221                 /* remove any matches at the start of the list */
222                 while ((ap = LIST_FIRST(&alarm_list)) != NULL &&
223                                 cb_fn == ap->cb_fn &&
224                                 (cb_arg == (void *)-1 || cb_arg == ap->cb_arg)) {
225
226                         if (ap->executing == 0) {
227                                 LIST_REMOVE(ap, next);
228                                 rte_free(ap);
229                                 count++;
230                         } else {
231                                 /* If calling from other context, mark that alarm is executing
232                                  * so loop can spin till it finish. Otherwise we are trying to
233                                  * cancel our self - mark it by EINPROGRESS */
234                                 if (pthread_equal(ap->executing_id, pthread_self()) == 0)
235                                         executing++;
236                                 else
237                                         err = EINPROGRESS;
238
239                                 break;
240                         }
241                 }
242                 ap_prev = ap;
243
244                 /* now go through list, removing entries not at start */
245                 LIST_FOREACH(ap, &alarm_list, next) {
246                         /* this won't be true first time through */
247                         if (cb_fn == ap->cb_fn &&
248                                         (cb_arg == (void *)-1 || cb_arg == ap->cb_arg)) {
249
250                                 if (ap->executing == 0) {
251                                         LIST_REMOVE(ap, next);
252                                         rte_free(ap);
253                                         count++;
254                                         ap = ap_prev;
255                                 } else if (pthread_equal(ap->executing_id, pthread_self()) == 0)
256                                         executing++;
257                                 else
258                                         err = EINPROGRESS;
259                         }
260                         ap_prev = ap;
261                 }
262                 rte_spinlock_unlock(&alarm_list_lk);
263         } while (executing != 0);
264
265         if (count == 0 && err == 0)
266                 rte_errno = ENOENT;
267         else if (err)
268                 rte_errno = err;
269
270         return count;
271 }