first public release
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_alarm.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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  *  version: DPDK.L.1.2.3-3
34  */
35 #include <stdio.h>
36 #include <stdint.h>
37 #include <signal.h>
38 #include <errno.h>
39 #include <string.h>
40 #include <sys/queue.h>
41 #include <sys/time.h>
42 #include <sys/timerfd.h>
43
44 #include <rte_memory.h>
45 #include <rte_memzone.h>
46 #include <rte_interrupts.h>
47 #include <rte_alarm.h>
48 #include <rte_common.h>
49 #include <rte_per_lcore.h>
50 #include <rte_tailq.h>
51 #include <rte_eal.h>
52 #include <rte_launch.h>
53 #include <rte_lcore.h>
54 #include <rte_errno.h>
55 #include <rte_malloc.h>
56 #include <rte_spinlock.h>
57 #include <eal_private.h>
58
59 #define NS_PER_US 1000
60 #define US_PER_MS 1000
61 #define MS_PER_S 1000
62 #define US_PER_S (US_PER_MS * MS_PER_S)
63
64 struct alarm_entry {
65         LIST_ENTRY(alarm_entry) next;
66         struct timeval time;
67         rte_eal_alarm_callback cb_fn;
68         void *cb_arg;
69         volatile int executing;
70 };
71
72 static LIST_HEAD(alarm_list, alarm_entry) alarm_list = LIST_HEAD_INITIALIZER();
73 static rte_spinlock_t alarm_list_lk = RTE_SPINLOCK_INITIALIZER;
74
75 static struct rte_intr_handle intr_handle = {.fd = -1 };
76 static int handler_registered = 0;
77 static void eal_alarm_callback(struct rte_intr_handle *hdl, void *arg);
78
79 int
80 rte_eal_alarm_init(void)
81 {
82         intr_handle.type = RTE_INTR_HANDLE_ALARM;
83         /* create a timerfd file descriptor */
84         intr_handle.fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
85         if (intr_handle.fd == -1)
86                 goto error;
87
88         return 0;
89
90 error:
91         rte_errno = errno;
92         return -1;
93 }
94
95 static void
96 eal_alarm_callback(struct rte_intr_handle *hdl __rte_unused,
97                 void *arg __rte_unused)
98 {
99         struct timeval now;
100         struct alarm_entry *ap;
101
102         rte_spinlock_lock(&alarm_list_lk);
103         while ((ap = LIST_FIRST(&alarm_list)) !=NULL &&
104                         gettimeofday(&now, NULL) == 0 &&
105                         (ap->time.tv_sec < now.tv_sec || (ap->time.tv_sec == now.tv_sec &&
106                                                 ap->time.tv_usec <= now.tv_usec))){
107                 ap->executing = 1;
108                 rte_spinlock_unlock(&alarm_list_lk);
109
110                 ap->cb_fn(ap->cb_arg);
111
112                 rte_spinlock_lock(&alarm_list_lk);
113                 LIST_REMOVE(ap, next);
114                 rte_free(ap);
115         }
116
117         if (!LIST_EMPTY(&alarm_list)) {
118                 struct itimerspec atime = { .it_interval = { 0, 0 } };
119
120                 ap = LIST_FIRST(&alarm_list);
121                 atime.it_value.tv_sec = ap->time.tv_sec;
122                 atime.it_value.tv_nsec = ap->time.tv_usec * NS_PER_US;
123                 /* perform borrow for subtraction if necessary */
124                 if (now.tv_usec > ap->time.tv_usec)
125                         atime.it_value.tv_sec--, atime.it_value.tv_nsec += US_PER_S * NS_PER_US;
126
127                 atime.it_value.tv_sec -= now.tv_sec;
128                 atime.it_value.tv_nsec -= now.tv_usec * NS_PER_US;
129                 timerfd_settime(intr_handle.fd, 0, &atime, NULL);
130         }
131         rte_spinlock_unlock(&alarm_list_lk);
132 }
133
134 int
135 rte_eal_alarm_set(uint64_t us, rte_eal_alarm_callback cb_fn, void *cb_arg)
136 {
137         struct timeval now;
138         int ret = 0;
139         struct alarm_entry *ap, *new_alarm;
140
141         /* Check parameters, including that us won't cause a uint64_t overflow */
142         if (us < 1 || us > (UINT64_MAX - US_PER_S) || cb_fn == NULL)
143                 return -EINVAL;
144
145         new_alarm = rte_malloc(NULL, sizeof(*new_alarm), 0);
146         if (new_alarm == NULL)
147                 return -ENOMEM;
148
149         /* use current time to calculate absolute time of alarm */
150         gettimeofday(&now, NULL);
151
152         new_alarm->cb_fn = cb_fn;
153         new_alarm->cb_arg = cb_arg;
154         new_alarm->time.tv_usec = (now.tv_usec + us) % US_PER_S;
155         new_alarm->time.tv_sec = now.tv_sec + ((now.tv_usec + us) / US_PER_S);
156         new_alarm->executing = 0;
157
158         rte_spinlock_lock(&alarm_list_lk);
159         if (!handler_registered) {
160                 ret |= rte_intr_callback_register(&intr_handle,
161                                 eal_alarm_callback, NULL);
162                 handler_registered = (ret == 0) ? 1 : 0;
163         }
164
165         if (LIST_EMPTY(&alarm_list))
166                 LIST_INSERT_HEAD(&alarm_list, new_alarm, next);
167         else {
168                 LIST_FOREACH(ap, &alarm_list, next) {
169                         if (ap->time.tv_sec > new_alarm->time.tv_sec ||
170                                         (ap->time.tv_sec == new_alarm->time.tv_sec &&
171                                                         ap->time.tv_usec > new_alarm->time.tv_usec)){
172                                 LIST_INSERT_BEFORE(ap, new_alarm, next);
173                                 break;
174                         }
175                         if (LIST_NEXT(ap, next) == NULL) {
176                                 LIST_INSERT_AFTER(ap, new_alarm, next);
177                                 break;
178                         }
179                 }
180         }
181
182         if (LIST_FIRST(&alarm_list) == new_alarm) {
183                 struct itimerspec alarm_time = {
184                         .it_interval = {0, 0},
185                         .it_value = {
186                                 .tv_sec = us / US_PER_S,
187                                 .tv_nsec = (us % US_PER_S) * NS_PER_US,
188                         },
189                 };
190                 ret |= timerfd_settime(intr_handle.fd, 0, &alarm_time, NULL);
191         }
192         rte_spinlock_unlock(&alarm_list_lk);
193
194         return ret;
195 }
196
197 int
198 rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn, void *cb_arg)
199 {
200         struct alarm_entry *ap, *ap_prev;
201         int count = 0;
202
203         if (!cb_fn)
204                 return -1;
205
206         rte_spinlock_lock(&alarm_list_lk);
207         /* remove any matches at the start of the list */
208         while ((ap = LIST_FIRST(&alarm_list)) != NULL &&
209                         cb_fn == ap->cb_fn && ap->executing == 0 &&
210                         (cb_arg == (void *)-1 || cb_arg == ap->cb_arg)) {
211                 LIST_REMOVE(ap, next);
212                 rte_free(ap);
213                 count++;
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 &&  ap->executing == 0 &&
221                                 (cb_arg == (void *)-1 || cb_arg == ap->cb_arg)) {
222                         LIST_REMOVE(ap,next);
223                         rte_free(ap);
224                         count++;
225                         ap = ap_prev;
226                 }
227                 ap_prev = ap;
228         }
229         rte_spinlock_unlock(&alarm_list_lk);
230         return count;
231 }
232