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