From 9ed507f740db41a6e566c759fb66aa57b0b65efa Mon Sep 17 00:00:00 2001 From: Jeremy Lakeman Date: Mon, 12 May 2014 13:41:51 +0930 Subject: [PATCH] Move rate limit code to separate file --- headerfiles.mk | 1 + limit.c | 75 +++++++++++++++++++++++++++++++++++++++++++++ limit.h | 38 +++++++++++++++++++++++ overlay_interface.h | 17 +--------- overlay_link.c | 53 -------------------------------- sourcefiles.mk | 1 + 6 files changed, 116 insertions(+), 69 deletions(-) create mode 100644 limit.c create mode 100644 limit.h diff --git a/headerfiles.mk b/headerfiles.mk index 1a5b3385..4914b22c 100644 --- a/headerfiles.mk +++ b/headerfiles.mk @@ -4,6 +4,7 @@ HDRS= fifo.h \ overlay_address.h \ overlay_packet.h \ overlay_interface.h \ + limit.h \ rhizome.h \ httpd.h \ instance.h \ diff --git a/limit.c b/limit.c new file mode 100644 index 00000000..254996df --- /dev/null +++ b/limit.c @@ -0,0 +1,75 @@ +/* +Copyright (C) 2014 Serval Project Inc. + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include +#include "os.h" +#include "limit.h" + +#define MIN_BURST_LENGTH 5000 + +static void update_limit_state(struct limit_state *state, time_ms_t now){ + if (state->next_interval > now || state->burst_size==0){ + return; + } + + if (state->next_interval + state->burst_length>now) + state->next_interval+=state->burst_length; + else + state->next_interval=now + state->burst_length; + + state->sent = 0; +} + +/* When should we next allow this thing to occur? */ +time_ms_t limit_next_allowed(struct limit_state *state){ + time_ms_t now = gettime_ms(); + if (!state->burst_size) + return now; + update_limit_state(state, now); + + if (state->sent < state->burst_size) + return now; + return state->next_interval; +} + +/* Can we do this now? if so, track it */ +int limit_is_allowed(struct limit_state *state){ + time_ms_t now = gettime_ms(); + if (!state->burst_size) + return 0; + update_limit_state(state, now); + if (state->sent >= state->burst_size){ + return -1; + } + state->sent ++; + return 0; +} + +/* Initialise burst size and length based on the number we can do in one MIN_BURST */ +int limit_init(struct limit_state *state, uint32_t rate_micro_seconds){ + state->rate_micro_seconds = rate_micro_seconds; + if (rate_micro_seconds==0){ + state->burst_size=0; + state->burst_length=0; + }else{ + state->burst_size = (MIN_BURST_LENGTH / rate_micro_seconds)+1; + state->burst_length = (state->burst_size * rate_micro_seconds) / 1000.0; + } + return 0; +} + diff --git a/limit.h b/limit.h new file mode 100644 index 00000000..f40dcfa4 --- /dev/null +++ b/limit.h @@ -0,0 +1,38 @@ +/* +Copyright (C) 2014 Serval Project Inc. + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#ifndef __SERVAL_DNA__LIMIT_H +#define __SERVAL_DNA__LIMIT_H + +struct limit_state{ + uint32_t rate_micro_seconds; + // length of time for a burst + time_ms_t burst_length; + // how many in a burst + int burst_size; + // how many have we sent in this burst so far + int sent; + // when can we allow another burst + time_ms_t next_interval; +}; + +time_ms_t limit_next_allowed(struct limit_state *state); +int limit_is_allowed(struct limit_state *state); +int limit_init(struct limit_state *state, uint32_t rate_micro_seconds); + +#endif \ No newline at end of file diff --git a/overlay_interface.h b/overlay_interface.h index aa5d9b7b..92229836 100644 --- a/overlay_interface.h +++ b/overlay_interface.h @@ -22,27 +22,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #define __SERVAL_DNA__OVERLAY_INTERFACE_H #include "socket.h" +#include "limit.h" #define INTERFACE_STATE_DOWN 0 #define INTERFACE_STATE_UP 1 #define INTERFACE_STATE_DETECTING 2 -struct limit_state{ - uint32_t rate_micro_seconds; - // length of time for a burst - time_ms_t burst_length; - // how many in a burst - int burst_size; - // how many have we sent in this burst so far - int sent; - // when can we allow another burst - time_ms_t next_interval; -}; - -time_ms_t limit_next_allowed(struct limit_state *state); -int limit_is_allowed(struct limit_state *state); -int limit_init(struct limit_state *state, uint32_t rate_micro_seconds); - struct overlay_interface; // where should packets be sent to? diff --git a/overlay_link.c b/overlay_link.c index 8c51de38..7edc431e 100644 --- a/overlay_link.c +++ b/overlay_link.c @@ -28,59 +28,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "keyring.h" #include "strbuf_helpers.h" -#define MIN_BURST_LENGTH 5000 - -static void update_limit_state(struct limit_state *state, time_ms_t now){ - if (state->next_interval > now || state->burst_size==0){ - return; - } - - if (state->next_interval + state->burst_length>now) - state->next_interval+=state->burst_length; - else - state->next_interval=now + state->burst_length; - - state->sent = 0; -} - -/* When should we next allow this thing to occur? */ -time_ms_t limit_next_allowed(struct limit_state *state){ - time_ms_t now = gettime_ms(); - if (!state->burst_length) - return now; - update_limit_state(state, now); - - if (state->sent < state->burst_size) - return now; - return state->next_interval; -} - -/* Can we do this now? if so, track it */ -int limit_is_allowed(struct limit_state *state){ - time_ms_t now = gettime_ms(); - if (!state->burst_length) - return 0; - update_limit_state(state, now); - if (state->sent >= state->burst_size){ - return -1; - } - state->sent ++; - return 0; -} - -/* Initialise burst size and length based on the number we can do in one MIN_BURST */ -int limit_init(struct limit_state *state, uint32_t rate_micro_seconds){ - state->rate_micro_seconds = rate_micro_seconds; - if (rate_micro_seconds==0){ - state->burst_size=0; - state->burst_length=1; - }else{ - state->burst_size = (MIN_BURST_LENGTH / rate_micro_seconds)+1; - state->burst_length = (state->burst_size * rate_micro_seconds) / 1000.0; - } - return 0; -} - int set_reachable(struct subscriber *subscriber, struct network_destination *destination, struct subscriber *next_hop){ diff --git a/sourcefiles.mk b/sourcefiles.mk index 37aba602..497ab3cc 100644 --- a/sourcefiles.mk +++ b/sourcefiles.mk @@ -46,6 +46,7 @@ SERVAL_DAEMON_SOURCES = \ http_server.c \ keyring.c \ lsif.c \ + limit.c \ main.c \ radio_link.c \ meshms.c \