From 86888f3e1b6ccc0691bbd881f6a8e68b252c367e Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sat, 19 Apr 2014 02:09:04 +0200 Subject: Optimize maintenance routine (and don't run it more often than necessary) --- src/config.c | 2 +- src/fastd.c | 74 +++++++++++++++++++++++++++++------------------------------- src/fastd.h | 4 ++-- 3 files changed, 39 insertions(+), 41 deletions(-) (limited to 'src') diff --git a/src/config.c b/src/config.c index 58327a5..4453942 100644 --- a/src/config.c +++ b/src/config.c @@ -51,7 +51,7 @@ static void default_config(fastd_config_t *conf) { conf->log_syslog_ident = strdup("fastd"); - conf->keepalive_interval = 10; + conf->maintenance_interval = 10; conf->keepalive_timeout = 15; conf->peer_stale_time = 90; conf->eth_addr_stale_time = 300; diff --git a/src/fastd.c b/src/fastd.c index 2a31da2..d9e78b5 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -550,14 +550,14 @@ static void handle_input(fastd_context_t *ctx) { if (i != n_fds) exit_bug(ctx, "fd count mismatch"); - int keepalive_timeout = timespec_diff(&ctx->next_keepalives, &ctx->now); + int maintenance_timeout = timespec_diff(&ctx->next_maintenance, &ctx->now); - if (keepalive_timeout < 0) - keepalive_timeout = 0; + if (maintenance_timeout < 0) + maintenance_timeout = 0; int timeout = handshake_timeout(ctx); - if (timeout < 0 || timeout > keepalive_timeout) - timeout = keepalive_timeout; + if (timeout < 0 || timeout > maintenance_timeout) + timeout = maintenance_timeout; int ret = poll(fds, n_fds, timeout); if (ret < 0) { @@ -594,53 +594,50 @@ static void handle_input(fastd_context_t *ctx) { exit_bug(ctx, "fd count mismatch"); } -static void cleanup_peers(fastd_context_t *ctx) { - fastd_peer_t *peer, *next; - - for (peer = ctx->peers; peer; peer = next) { - next = peer->next; +static void maintain_peer(fastd_context_t *ctx, fastd_peer_t *peer) { + if (fastd_peer_is_temporary(peer) || fastd_peer_is_established(peer)) { + /* check for peer timeout */ + if (fastd_timed_out(ctx, &peer->timeout)) { + if (fastd_peer_is_temporary(peer)) + fastd_peer_delete(ctx, peer); + else + fastd_peer_reset(ctx, peer); - if (fastd_peer_is_temporary(peer) || fastd_peer_is_established(peer)) { - if (fastd_timed_out(ctx, &peer->timeout)) { - if (fastd_peer_is_temporary(peer)) { - fastd_peer_delete(ctx, peer); - } - else { - fastd_peer_reset(ctx, peer); - } - } + return; } + + /* check for keepalive timeout */ + if (!fastd_peer_is_established(peer)) + return; + + if (!fastd_timed_out(ctx, &peer->keepalive_timeout)) + return; + + pr_debug2(ctx, "sending keepalive to %P", peer); + ctx->conf->protocol->send(ctx, peer, fastd_buffer_alloc(ctx, 0, ctx->conf->min_encrypt_head_space, ctx->conf->min_encrypt_tail_space)); } } static void maintenance(fastd_context_t *ctx) { + fastd_peer_t *peer, *next; + while (ctx->peers_temp) { - fastd_peer_t *peer = ctx->peers_temp; + peer = ctx->peers_temp; ctx->peers_temp = ctx->peers_temp->next; fastd_peer_enable_temporary(ctx, peer); } - cleanup_peers(ctx); - fastd_peer_eth_addr_cleanup(ctx); - fastd_socket_handle_binds(ctx); - if (fastd_timed_out(ctx, &ctx->next_keepalives)) { - fastd_peer_t *peer; - for (peer = ctx->peers; peer; peer = peer->next) { - if (!fastd_peer_is_established(peer)) - continue; - - if (!fastd_timed_out(ctx, &peer->keepalive_timeout)) - continue; + for (peer = ctx->peers; peer; peer = next) { + next = peer->next; + maintain_peer(ctx, peer); + } - pr_debug2(ctx, "sending keepalive to %P", peer); - ctx->conf->protocol->send(ctx, peer, fastd_buffer_alloc(ctx, 0, ctx->conf->min_encrypt_head_space, ctx->conf->min_encrypt_tail_space)); - } + fastd_peer_eth_addr_cleanup(ctx); - ctx->next_keepalives.tv_sec += ctx->conf->keepalive_interval; - } + ctx->next_maintenance.tv_sec += ctx->conf->maintenance_interval; } @@ -916,7 +913,7 @@ int main(int argc, char *argv[]) { update_time(&ctx); - ctx.next_keepalives = fastd_in_seconds(&ctx, conf.keepalive_interval); + ctx.next_maintenance = fastd_in_seconds(&ctx, conf.maintenance_interval); ctx.unknown_handshakes[0].timeout = ctx.now; @@ -974,7 +971,8 @@ int main(int argc, char *argv[]) { handle_input(&ctx); - maintenance(&ctx); + if (fastd_timed_out(&ctx, &ctx.next_maintenance)) + maintenance(&ctx); sigset_t set, oldset; sigemptyset(&set); diff --git a/src/fastd.h b/src/fastd.h index 1ce1055..c2b85de 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -143,8 +143,8 @@ struct fastd_config { char *log_syslog_ident; fastd_log_file_t *log_files; + unsigned maintenance_interval; unsigned keepalive_timeout; - unsigned keepalive_interval; unsigned peer_stale_time; unsigned eth_addr_stale_time; @@ -242,7 +242,7 @@ struct fastd_context { fastd_peer_t *peers_temp; fastd_dlist_head_t handshake_queue; - struct timespec next_keepalives; + struct timespec next_maintenance; int async_rfd; int async_wfd; -- cgit v1.2.3