2 /* $KAME: timer.c,v 1.9 2002/06/10 19:59:47 itojun Exp $ */
5 * SPDX-License-Identifier: BSD-3-Clause
7 * Copyright (C) 1998 WIDE Project.
8 * Copyright (C) 2011 Hiroki Sato <hrs@FreeBSD.org>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the project nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/queue.h>
37 #include <sys/socket.h>
40 #include <net/if_dl.h>
41 #include <netinet/in.h>
52 #include "timer_subr.h"
55 struct rtadvd_timer_head_t ra_timer =
56 TAILQ_HEAD_INITIALIZER(ra_timer);
57 static struct timespec tm_limit;
58 static struct timespec tm_max;
61 rtadvd_timer_init(void)
63 /* Generate maximum time in timespec. */
64 tm_limit.tv_sec = (-1) & ~((time_t)1 << ((sizeof(tm_max.tv_sec) * 8) - 1));
65 tm_limit.tv_nsec = (-1) & ~((long)1 << ((sizeof(tm_max.tv_nsec) * 8) - 1));
67 TAILQ_INIT(&ra_timer);
71 rtadvd_update_timeout_handler(void)
75 TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
76 switch (ifi->ifi_state) {
77 case IFI_STATE_CONFIGURED:
78 case IFI_STATE_TRANSITIVE:
79 if (ifi->ifi_ra_timer != NULL)
82 syslog(LOG_DEBUG, "<%s> add timer for %s (idx=%d)",
83 __func__, ifi->ifi_ifname, ifi->ifi_ifindex);
84 ifi->ifi_ra_timer = rtadvd_add_timer(ra_timeout,
85 ra_timer_update, ifi, ifi);
86 ra_timer_update((void *)ifi,
87 &ifi->ifi_ra_timer->rat_tm);
88 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
91 case IFI_STATE_UNCONFIGURED:
92 if (ifi->ifi_ra_timer == NULL)
96 "<%s> remove timer for %s (idx=%d)", __func__,
97 ifi->ifi_ifname, ifi->ifi_ifindex);
98 rtadvd_remove_timer(ifi->ifi_ra_timer);
99 ifi->ifi_ra_timer = NULL;
107 struct rtadvd_timer *
108 rtadvd_add_timer(struct rtadvd_timer *(*timeout)(void *),
109 void (*update)(void *, struct timespec *),
110 void *timeodata, void *updatedata)
112 struct rtadvd_timer *rat;
114 if (timeout == NULL) {
116 "<%s> timeout function unspecified", __func__);
120 rat = malloc(sizeof(*rat));
123 "<%s> can't allocate memory", __func__);
126 memset(rat, 0, sizeof(*rat));
128 rat->rat_expire = timeout;
129 rat->rat_update = update;
130 rat->rat_expire_data = timeodata;
131 rat->rat_update_data = updatedata;
132 rat->rat_tm = tm_max;
134 /* link into chain */
135 TAILQ_INSERT_TAIL(&ra_timer, rat, rat_next);
141 rtadvd_remove_timer(struct rtadvd_timer *rat)
147 TAILQ_REMOVE(&ra_timer, rat, rat_next);
152 * Check expiration for each timer. If a timer expires,
153 * call the expire function for the timer and update the timer.
154 * Return the next interval for select() call.
157 rtadvd_check_timer(void)
159 static struct timespec returnval;
161 struct rtadvd_timer *rat;
163 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
165 TAILQ_FOREACH(rat, &ra_timer, rat_next) {
166 if (TS_CMP(&rat->rat_tm, &now, <=)) {
167 if (((*rat->rat_expire)(rat->rat_expire_data) == NULL))
168 continue; /* the timer was removed */
170 (*rat->rat_update)(rat->rat_update_data, &rat->rat_tm);
171 TS_ADD(&rat->rat_tm, &now, &rat->rat_tm);
173 if (TS_CMP(&rat->rat_tm, &tm_max, <))
174 tm_max = rat->rat_tm;
176 if (TS_CMP(&tm_max, &tm_limit, ==)) {
177 /* no need to timeout */
179 } else if (TS_CMP(&tm_max, &now, <)) {
180 /* this may occur when the interval is too small */
181 returnval.tv_sec = returnval.tv_nsec = 0;
183 TS_SUB(&tm_max, &now, &returnval);
188 rtadvd_set_timer(struct timespec *tm, struct rtadvd_timer *rat)
192 /* reset the timer */
193 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
194 TS_ADD(&now, tm, &rat->rat_tm);
196 /* update the next expiration time */
197 if (TS_CMP(&rat->rat_tm, &tm_max, <))
198 tm_max = rat->rat_tm;