From 6e5b6c91625fd431ac3d1339f55a4aa278ff2604 Mon Sep 17 00:00:00 2001 From: "/C=EU/ST=EU/CN=Pablo Neira Ayuso/emailAddress=pablo@netfilter.org" Date: Mon, 14 Jan 2008 15:22:24 +0000 Subject: improve alarm framework based on suggestions from Max Duempel --- ChangeLog | 1 + include/alarm.h | 7 +++++++ src/alarm.c | 33 ++++++++++++++++++--------------- src/cache_timer.c | 2 +- src/run.c | 15 ++++++++------- src/sync-alarm.c | 10 ++++++---- src/sync-ftfw.c | 4 ++-- 7 files changed, 43 insertions(+), 29 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6d4759a..c090831 100644 --- a/ChangeLog +++ b/ChangeLog @@ -38,6 +38,7 @@ o rename class `buffer' to `queue' which is what it really implements o fix logfiles permissions, do not default to umask o wake up the daemon iff there are real events to handle instead of polling o add support for tagged vlan interfaces in the config file, e.g. eth0.1 +o improve alarm framework based on suggestions from Max Duempel version 0.9.5 (2007/07/29) ------------------------------ diff --git a/include/alarm.h b/include/alarm.h index 82a1612..fbe34f6 100644 --- a/include/alarm.h +++ b/include/alarm.h @@ -10,4 +10,11 @@ struct alarm_list { void (*function)(struct alarm_list *a, void *data); }; +static inline void +set_alarm_expiration(struct alarm_list *t, long tv_sec, long tv_usec) +{ + t->tv.tv_sec = tv_sec; + t->tv.tv_usec = tv_usec; +} + #endif diff --git a/src/alarm.c b/src/alarm.c index e1d2e24..eb2226b 100644 --- a/src/alarm.c +++ b/src/alarm.c @@ -28,16 +28,6 @@ static LIST_HEAD(alarm_list); -void set_alarm_expiration_secs(struct alarm_list *t, unsigned long expires) -{ - t->tv.tv_sec = expires; -} - -void set_alarm_expiration_usecs(struct alarm_list *t, unsigned long expires) -{ - t->tv.tv_usec = expires; -} - void set_alarm_function(struct alarm_list *t, void (*fcn)(struct alarm_list *a, void *data)) { @@ -51,8 +41,6 @@ void set_alarm_data(struct alarm_list *t, void *data) void init_alarm(struct alarm_list *t) { - INIT_LIST_HEAD(&t->head); - timerclear(&t->tv); t->data = 0; t->function = NULL; @@ -77,14 +65,26 @@ void mod_alarm(struct alarm_list *alarm, unsigned long sc, unsigned long usc) struct timeval tv; list_del(&alarm->head); - INIT_LIST_HEAD(&alarm->head); gettimeofday(&tv, NULL); alarm->tv.tv_sec = tv.tv_sec + sc; alarm->tv.tv_usec = tv.tv_usec + usc; list_add_tail(&alarm->head, &alarm_list); } -void do_alarm_run(struct timeval *next_alarm) +int get_next_alarm(struct timeval *tv, struct timeval *next_alarm) +{ + struct list_head *i; + struct alarm_list *t; + + list_for_each(i, &alarm_list) { + t = (struct alarm_list *) i; + timersub(&t->tv, tv, next_alarm); + return 1; + } + return 0; +} + +int do_alarm_run(struct timeval *next_alarm) { struct list_head *i, *tmp; struct alarm_list *t; @@ -97,10 +97,13 @@ void do_alarm_run(struct timeval *next_alarm) if (timercmp(&t->tv, &tv, >)) { timersub(&t->tv, &tv, next_alarm); - break; + return 1; } del_alarm(t); t->function(t, t->data); } + + /* check for refreshed alarms to get the next one */ + return get_next_alarm(&tv, next_alarm); } diff --git a/src/cache_timer.c b/src/cache_timer.c index c0075f5..02f3ae4 100644 --- a/src/cache_timer.c +++ b/src/cache_timer.c @@ -36,7 +36,7 @@ static void timer_add(struct us_conntrack *u, void *data) struct alarm_list *alarm = data; init_alarm(alarm); - set_alarm_expiration_secs(alarm, CONFIG(cache_timeout)); + set_alarm_expiration(alarm, CONFIG(cache_timeout), 0); set_alarm_data(alarm, u); set_alarm_function(alarm, timeout); add_alarm(alarm); diff --git a/src/run.c b/src/run.c index eab3ad2..3481193 100644 --- a/src/run.c +++ b/src/run.c @@ -224,19 +224,20 @@ static int __run(struct timeval *next_alarm) void run(void) { - struct timeval next_alarm = { - .tv_sec = 1, - .tv_usec = 0 - }; + struct timeval next_alarm; struct timeval *next = &next_alarm; + struct timeval tv; - if (CONFIG(flags) & CTD_STATS_MODE) - next = NULL; + /* initialization: get the first alarm available */ + gettimeofday(&tv, NULL); + get_next_alarm(&tv, next); while(1) { if (__run(next)) { sigprocmask(SIG_BLOCK, &STATE(block), NULL); - do_alarm_run(next); + next = &next_alarm; + if (!do_alarm_run(next)) + next = NULL; /* no next alarms */ sigprocmask(SIG_UNBLOCK, &STATE(block), NULL); } } diff --git a/src/sync-alarm.c b/src/sync-alarm.c index 632eff2..3d20867 100644 --- a/src/sync-alarm.c +++ b/src/sync-alarm.c @@ -31,8 +31,9 @@ static void refresher(struct alarm_list *a, void *data) debug_ct(u->ct, "persistence update"); init_alarm(a); - set_alarm_expiration_secs(a, random() % CONFIG(refresh) + 1); - set_alarm_expiration_usecs(a, random() % 999999 + 1); + set_alarm_expiration(a, + random() % CONFIG(refresh) + 1, + random() % 999999 + 1); set_alarm_data(a, u); set_alarm_function(a, refresher); @@ -48,8 +49,9 @@ static void cache_alarm_add(struct us_conntrack *u, void *data) struct alarm_list *alarm = data; init_alarm(alarm); - set_alarm_expiration_secs(alarm, random() % CONFIG(refresh) + 1); - set_alarm_expiration_usecs(alarm, random() % 999999 + 1); + set_alarm_expiration(alarm, + random() % CONFIG(refresh) + 1, + random() % 999999 + 1); set_alarm_data(alarm, u); set_alarm_function(alarm, refresher); add_alarm(alarm); diff --git a/src/sync-ftfw.c b/src/sync-ftfw.c index ac1b8b6..125e82e 100644 --- a/src/sync-ftfw.c +++ b/src/sync-ftfw.c @@ -88,7 +88,7 @@ static void do_alive_alarm(struct alarm_list *a, void *data) tx_queue_add_ctlmsg(NET_F_ALIVE, 0, 0); init_alarm(&alive_alarm); - set_alarm_expiration_secs(&alive_alarm, 1); + set_alarm_expiration(&alive_alarm, 1, 0); set_alarm_function(&alive_alarm, do_alive_alarm); add_alarm(&alive_alarm); } @@ -112,7 +112,7 @@ static int ftfw_init() /* XXX: alive message expiration configurable */ init_alarm(&alive_alarm); - set_alarm_expiration_secs(&alive_alarm, 1); + set_alarm_expiration(&alive_alarm, 1, 0); set_alarm_function(&alive_alarm, do_alive_alarm); add_alarm(&alive_alarm); -- cgit v1.2.3