From 50863008150caa3a71fd2d8f5f9c18292ed13c24 Mon Sep 17 00:00:00 2001 From: Roger Dingledine Date: Sat, 10 Jan 2004 23:40:38 +0000 Subject: [PATCH] split the token bucket into 'rate' and 'burst' params we're not entirely migrated to burst yet, for backward compatibility note some win32 probable-bugs clean up routerlist.c svn:r982 --- src/or/config.c | 10 ++++-- src/or/connection_edge.c | 4 ++- src/or/cpuworker.c | 1 + src/or/dns.c | 1 + src/or/main.c | 6 ++-- src/or/or.h | 8 ++--- src/or/router.c | 6 ++-- src/or/routerlist.c | 71 +++++++++++++++++++--------------------- 8 files changed, 57 insertions(+), 50 deletions(-) diff --git a/src/or/config.c b/src/or/config.c index 16efde10e9..a42ea6ec5a 100644 --- a/src/or/config.c +++ b/src/or/config.c @@ -153,6 +153,9 @@ static void config_assign(or_options_t *options, struct config_line *list) { /* string options */ config_compare(list, "Address", CONFIG_TYPE_STRING, &options->Address) || + config_compare(list, "BandwidthRate", CONFIG_TYPE_INT, &options->BandwidthRate) || + config_compare(list, "BandwidthBurst", CONFIG_TYPE_INT, &options->BandwidthBurst) || + config_compare(list, "DebugLogFile", CONFIG_TYPE_STRING, &options->DebugLogFile) || config_compare(list, "DataDirectory", CONFIG_TYPE_STRING, &options->DataDirectory) || config_compare(list, "DirPort", CONFIG_TYPE_INT, &options->DirPort) || @@ -194,7 +197,6 @@ static void config_assign(or_options_t *options, struct config_line *list) { config_compare(list, "SocksPort", CONFIG_TYPE_INT, &options->SocksPort) || config_compare(list, "SocksBindAddress",CONFIG_TYPE_STRING,&options->SocksBindAddress) || - config_compare(list, "TotalBandwidth", CONFIG_TYPE_INT, &options->TotalBandwidth) || config_compare(list, "TrafficShaping", CONFIG_TYPE_BOOL, &options->TrafficShaping) || config_compare(list, "User", CONFIG_TYPE_STRING, &options->User) @@ -211,10 +213,11 @@ static void config_assign(or_options_t *options, struct config_line *list) { /* prints the usage of tor. */ void print_usage(void) { printf("tor -f [args]\n" + "See man page for more options.\n\n" + "-b \t\tbytes/second rate limiting\n" "-d \t\tDebug file\n" "-m \t\tMax number of connections\n" "-l \t\tLog level\n" - "-t \t\tTotal bandwidth\n" "-r \t\tList of known routers\n"); printf("\nClient options:\n" "-e \"nick1 nick2 ...\"\t\tExit nodes\n" @@ -269,7 +272,8 @@ void init_options(or_options_t *options) { options->KeepalivePeriod = 300; options->MaxOnionsPending = 100; options->NewCircuitPeriod = 60; /* once a minute */ - options->TotalBandwidth = 800000; /* at most 800kB/s total sustained incoming */ + options->BandwidthRate = 800000; /* at most 800kB/s total sustained incoming */ + options->BandwidthBurst = 10000000; /* max burst on the token bucket */ options->NumCpus = 1; } diff --git a/src/or/connection_edge.c b/src/or/connection_edge.c index d8c23d90c3..133257579d 100644 --- a/src/or/connection_edge.c +++ b/src/or/connection_edge.c @@ -290,6 +290,7 @@ int connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ, connection return 0; } } +/* XXX add to this log_fn the exit node's nickname? */ log_fn(LOG_INFO,"end cell (%s) for stream %d. Removing stream.", connection_edge_end_reason(cell->payload+RELAY_HEADER_SIZE, rh.length), conn->stream_id); @@ -880,7 +881,8 @@ int connection_ap_can_use_exit(connection_t *conn, routerinfo_t *exit) exit->nickname, conn->socks_request->address, conn->socks_request->port); addr = client_dns_lookup_entry(conn->socks_request->address); - return router_supports_exit_address(addr, conn->socks_request->port, exit); + return router_compare_addr_to_exit_policy(addr, + conn->socks_request->port, exit->exit_policy); } /* ***** Client DNS code ***** */ diff --git a/src/or/cpuworker.c b/src/or/cpuworker.c index 51c582c1c0..0ff097f131 100644 --- a/src/or/cpuworker.c +++ b/src/or/cpuworker.c @@ -127,6 +127,7 @@ int cpuworker_main(void *data) { close(fdarray[0]); /* this is the side of the socketpair the parent uses */ fd = fdarray[1]; /* this side is ours */ connection_free_all(); /* so the child doesn't hold the parent's fd's open */ +/* XXX probably don't close all the fd's on MS_WINDOWS? */ for(;;) { diff --git a/src/or/dns.c b/src/or/dns.c index 88607172de..1aeecd2b13 100644 --- a/src/or/dns.c +++ b/src/or/dns.c @@ -396,6 +396,7 @@ int dnsworker_main(void *data) { close(fdarray[0]); /* this is the side of the socketpair the parent uses */ fd = fdarray[1]; /* this side is ours */ connection_free_all(); /* so the child doesn't hold the parent's fd's open */ +/* XXX probably don't close all the fd's on MS_WINDOWS? */ for(;;) { diff --git a/src/or/main.c b/src/or/main.c index cf2800a853..42031c5a31 100644 --- a/src/or/main.c +++ b/src/or/main.c @@ -347,8 +347,8 @@ static void run_scheduled_events(time_t now) { * increment global_read_bucket. */ stats_n_bytes_read += stats_prev_global_read_bucket-global_read_bucket; - if(global_read_bucket < 9*options.TotalBandwidth) { - global_read_bucket += options.TotalBandwidth; + if(global_read_bucket < options.BandwidthBurst) { + global_read_bucket += options.BandwidthRate; log_fn(LOG_DEBUG,"global_read_bucket now %d.", global_read_bucket); } stats_prev_global_read_bucket = global_read_bucket; @@ -418,7 +418,7 @@ static int init_from_config(int argc, char **argv) { log_fn(LOG_DEBUG, "Successfully opened DebugLogFile '%s'.", options.DebugLogFile); } - global_read_bucket = options.TotalBandwidth; /* start it at 1 second of traffic */ + global_read_bucket = options.BandwidthBurst; /* start it at max traffic */ stats_prev_global_read_bucket = global_read_bucket; if(options.User || options.Group) { diff --git a/src/or/or.h b/src/or/or.h index b7e323b3d0..fff833bcd2 100644 --- a/src/or/or.h +++ b/src/or/or.h @@ -389,7 +389,8 @@ typedef struct { int is_running; /* link info */ - uint32_t bandwidth; + uint32_t bandwidthrate; + uint32_t bandwidthburst; struct exit_policy_t *exit_policy; } routerinfo_t; @@ -505,7 +506,8 @@ typedef struct { int KeepalivePeriod; int MaxOnionsPending; int NewCircuitPeriod; - int TotalBandwidth; + int BandwidthRate; + int BandwidthBurst; int NumCpus; int loglevel; } or_options_t; @@ -800,8 +802,6 @@ int router_get_router_hash(const char *s, char *digest); int router_set_routerlist_from_directory(const char *s, crypto_pk_env_t *pkey); routerinfo_t *router_get_entry_from_string(const char **s); int router_add_exit_policy_from_string(routerinfo_t *router, const char *s); -int router_supports_exit_address(uint32_t addr, uint16_t port, - routerinfo_t *router); int router_compare_addr_to_exit_policy(uint32_t addr, uint16_t port, struct exit_policy_t *policy); int router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port); diff --git a/src/or/router.c b/src/or/router.c index af1309c9d9..9297e2bb90 100644 --- a/src/or/router.c +++ b/src/or/router.c @@ -338,7 +338,8 @@ int router_rebuild_descriptor(void) { ri->onion_pkey = crypto_pk_dup_key(get_onion_key()); ri->link_pkey = crypto_pk_dup_key(get_link_key()); ri->identity_pkey = crypto_pk_dup_key(get_identity_key()); - ri->bandwidth = options.TotalBandwidth; + ri->bandwidthrate = options.BandwidthRate; + ri->bandwidthburst = options.BandwidthBurst; ri->exit_policy = NULL; /* zero it out first */ router_add_exit_policy_from_config(ri); if (desc_routerinfo) @@ -421,7 +422,8 @@ int router_dump_router_to_string(char *s, int maxlen, routerinfo_t *router, router->or_port, router->socks_port, router->dir_port, - (int) router->bandwidth, + (int) router->bandwidthrate, +/* XXXBC also write bandwidthburst */ platform, published, onion_pkey, link_pkey, identity_pkey); diff --git a/src/or/routerlist.c b/src/or/routerlist.c index 01e863dca2..81de16c406 100644 --- a/src/or/routerlist.c +++ b/src/or/routerlist.c @@ -22,7 +22,7 @@ extern or_options_t options; /* command-line and config-file options */ /****************************************************************************/ /* Enumeration of possible token types. The ones starting with K_ correspond - * to directory 'keywords'. _SIGNATURE and _PUBLIC_KEY are self-explanitory. + * to directory 'keywords'. _SIGNATURE and _PUBLIC_KEY are self-explanatory. * _ERR is an error in the tokenizing process, _EOF is an end-of-file marker, * and _NIL is used to encode not-a-token. */ @@ -64,10 +64,9 @@ typedef struct directory_token_t { /****************************************************************************/ - - /* static function prototypes */ -static int router_set_routerlist_from_string(const char *s); +static int +router_set_routerlist_from_string(const char *s); static int router_get_list_from_string_impl(const char **s, routerlist_t **dest, int n_good_nicknames, @@ -75,23 +74,24 @@ router_get_list_from_string_impl(const char **s, routerlist_t **dest, static int router_get_routerlist_from_directory_impl(const char *s, routerlist_t **dest, crypto_pk_env_t *pkey); -static int router_add_exit_policy(routerinfo_t *router, - directory_token_t *tok); -static int router_resolve_routerlist(routerlist_t *dir); +static int +router_add_exit_policy(routerinfo_t *router, directory_token_t *tok); +static int +router_resolve_routerlist(routerlist_t *dir); - - -static int _router_get_next_token(const char **s, directory_token_t *tok); +static int +_router_get_next_token(const char **s, directory_token_t *tok); #ifdef DEBUG_ROUTER_TOKENS -static int router_get_next_token(const char **s, directory_token_t *tok); +static int +router_get_next_token(const char **s, directory_token_t *tok); #else #define router_get_next_token _router_get_next_token #endif -static int router_get_hash_impl(const char *s, char *digest, - const char *start_str, - const char *end_str); -static void router_release_token(directory_token_t *tok); - +static int +router_get_hash_impl(const char *s, char *digest, + const char *start_str, const char *end_str); +static void +router_release_token(directory_token_t *tok); /****************************************************************************/ @@ -262,7 +262,6 @@ int router_set_routerlist_from_file(char *routerfile) return 0; } - /* Helper function: read routerinfo elements from s, and throw out the * ones that don't parse and resolve. Replace the current * routerlist. */ @@ -296,8 +295,8 @@ int router_get_router_hash(const char *s, char *digest) "router ","router-signature"); } -/* return 0 if myversion is in versionlist. Else return -1. (versionlist - * contains a comma-separated list of versions.) */ +/* return 0 if myversion is in versionlist. Else return -1. + * (versionlist contains a comma-separated list of versions.) */ int compare_recommended_versions(const char *myversion, const char *versionlist) { int len_myversion = strlen(myversion); @@ -319,8 +318,7 @@ int compare_recommended_versions(const char *myversion, } /* Replace the current routerlist with the routers stored in the directory - * 's'. If pkey is provided, make sure that 's' is signed with pkey. - */ + * 's'. If pkey is provided, make sure that 's' is signed with pkey. */ int router_set_routerlist_from_directory(const char *s, crypto_pk_env_t *pkey) { if (router_get_routerlist_from_directory_impl(s, &routerlist, pkey)) { @@ -344,7 +342,6 @@ int router_set_routerlist_from_directory(const char *s, crypto_pk_env_t *pkey) exit(0); } } - return 0; } @@ -396,17 +393,6 @@ router_resolve_routerlist(routerlist_t *rl) return 0; } -/* Addr is 0 for "IP unknown". - * - * Returns -1 for 'rejected', 0 for accepted, 1 for 'maybe' (since IP is - * unknown. - */ -int router_supports_exit_address(uint32_t addr, uint16_t port, - routerinfo_t *router) -{ - return router_compare_addr_to_exit_policy(addr, port, router->exit_policy); -} - /* Addr is 0 for "IP unknown". * * Returns -1 for 'rejected', 0 for accepted, 1 for 'maybe' (since IP is @@ -716,7 +702,8 @@ routerinfo_t *router_get_entry_from_string(const char**s) { router = tor_malloc_zero(sizeof(routerinfo_t)); router->onion_pkey = router->identity_pkey = router->link_pkey = NULL; - if (N_ARGS != 6) { +/* XXXBC move to <7 once we require bandwidthburst */ + if (N_ARGS < 6) { log_fn(LOG_WARN,"Wrong # of arguments to \"router\""); goto err; } @@ -749,12 +736,22 @@ routerinfo_t *router_get_entry_from_string(const char**s) { router->dir_port = atoi(ARGS[4]); /* Router->bandwidth */ - router->bandwidth = atoi(ARGS[5]); - if (!router->bandwidth) { - log_fn(LOG_WARN,"bandwidth unreadable or 0. Failing."); + router->bandwidthrate = atoi(ARGS[5]); + if (!router->bandwidthrate) { + log_fn(LOG_WARN,"bandwidthrate unreadable or 0. Failing."); goto err; } +#if XXXBC + router->bandwidthburst = atoi(ARGS[6]); + if (!router->bandwidthburst) { + log_fn(LOG_WARN,"bandwidthburst unreadable or 0. Failing."); + goto err; + } +#else + router->bandwidthburst = 10*router->bandwidthrate; +#endif + log_fn(LOG_DEBUG,"or_port %d, socks_port %d, dir_port %d, bandwidth %u.", router->or_port, router->socks_port, router->dir_port, (unsigned) router->bandwidth);