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