Commit 117cbeea authored by Roger Dingledine's avatar Roger Dingledine
Browse files

Implemented link padding and receiver token buckets

Each socket reads at most 'bandwidth' bytes per second sustained, but
can handle bursts of up to 10*bandwidth bytes.

Cells are now sent out at evenly-spaced intervals, with padding sent
out otherwise. Set Linkpadding=0 in the rc file to send cells as soon
as they're available (and to never send padding cells).

Added license/copyrights statements at the top of most files.

router->min and router->max have been merged into a single 'bandwidth'
value. We should make the routerinfo_t reflect this (want to do that,
Mat?)

As the bandwidth increases, and we want to stop sleeping more and more
frequently to send a single cell, cpu usage goes up. At 128kB/s we're
pretty much calling poll with a timeout of 1ms or even 0ms. The current
code takes a timeout of 0-9ms and makes it 10ms. prepare_for_poll()
handles everything that should have happened in the past, so as long as
our buffers don't get too full in that 10ms, we're ok.

Speaking of too full, if you run three servers at 100kB/s with -l debug,
it spends too much time printing debugging messages to be able to keep
up with the cells. The outbuf ultimately fills up and it kills that
connection. If you run with -l err, it works fine up through 500kB/s and
probably beyond. Down the road we'll want to teach it to recognize when
an outbuf is getting full, and back off.


svn:r50
parent ffc54531
Loading
Loading
Loading
Loading
+30 −12
Original line number Original line Diff line number Diff line
/* Copyright 2001,2002 Roger Dingledine, Matej Pfajfar. */
/* See LICENSE for licensing information */
/* $Id$ */


/* buffers.c */
/* buffers.c */


@@ -21,17 +24,29 @@ void buf_free(char *buf) {
  free(buf);
  free(buf);
}
}


int read_to_buf(int s, char **buf, size_t *buflen, size_t *buf_datalen, int *reached_eof) {
int read_to_buf(int s, int at_most, char **buf, size_t *buflen, size_t *buf_datalen, int *reached_eof) {


  /* grab from s, put onto buf, return how many bytes read */
  /* read from socket s, writing onto buf+buf_datalen. Read at most
   * 'at_most' bytes, and also don't read more than will fit based on buflen.
   * If read() returns 0, set *reached_eof to 1 and return 0. If you want to tear
   * down the connection return -1, else return the number of bytes read.
   */


  int read_result;
  int read_result;


  assert(buf && *buf && buflen && buf_datalen && reached_eof && (s>=0));
  assert(buf && *buf && buflen && buf_datalen && reached_eof && (s>=0) && (at_most >= 0));


  /* this is the point where you would grow the buffer, if you want to */
  /* this is the point where you would grow the buffer, if you want to */


  read_result = read(s, *buf+*buf_datalen, *buflen - *buf_datalen);
  if(*buflen - *buf_datalen < at_most)
    at_most = *buflen - *buf_datalen; /* take the min of the two */
    /* (note that this only modifies at_most inside this function) */

  if(at_most == 0)
    return 0; /* we shouldn't read anything */

  log(LOG_DEBUG,"read_to_buf(): reading at most %d bytes.",at_most);
  read_result = read(s, *buf+*buf_datalen, at_most);
  if (read_result < 0) {
  if (read_result < 0) {
    if(errno!=EAGAIN) { /* it's a real error */
    if(errno!=EAGAIN) { /* it's a real error */
      return -1;
      return -1;
@@ -49,22 +64,24 @@ int read_to_buf(int s, char **buf, size_t *buflen, size_t *buf_datalen, int *rea


}
}


int flush_buf(int s, char **buf, size_t *buflen, size_t *buf_datalen) {
int flush_buf(int s, char **buf, size_t *buflen, size_t *buf_flushlen, size_t *buf_datalen) {


  /* push from buf onto s
  /* push from buf onto s
   * then memmove to front of buf
   * then memmove to front of buf
   * return -1 or how many bytes remain on the buf */
   * return -1 or how many bytes remain to be flushed */


  int write_result;
  int write_result;


  assert(buf && *buf && buflen && buf_datalen && (s>=0));
  assert(buf && *buf && buflen && buf_flushlen && buf_datalen && (s>=0) && (*buf_flushlen <= *buf_datalen));


  if(*buf_datalen == 0) /* nothing to flush */
  if(*buf_flushlen == 0) /* nothing to flush */
    return 0;
    return 0;


  /* this is the point where you would grow the buffer, if you want to */
  /* this is the point where you would grow the buffer, if you want to */


  write_result = write(s, *buf, *buf_datalen);
  write_result = write(s, *buf, *buf_flushlen > 10240 ? 10240 : *buf_flushlen);
    /* try to flush at most 10240 bytes at a time. otherwise write() can hang for
     * quite a while trying to get it all out. that's bad. */
  if (write_result < 0) {
  if (write_result < 0) {
    if(errno!=EAGAIN) { /* it's a real error */
    if(errno!=EAGAIN) { /* it's a real error */
      return -1;
      return -1;
@@ -73,11 +90,12 @@ int flush_buf(int s, char **buf, size_t *buflen, size_t *buf_datalen) {
    return 0;
    return 0;
  } else {
  } else {
    *buf_datalen -= write_result;
    *buf_datalen -= write_result;
    *buf_flushlen -= write_result;
    memmove(*buf, *buf+write_result, *buf_datalen);
    memmove(*buf, *buf+write_result, *buf_datalen);
    log(LOG_DEBUG,"flush_buf(): flushed %d bytes, %d remain.",write_result,*buf_datalen);
    log(LOG_DEBUG,"flush_buf(): flushed %d bytes, %d ready to flush, %d remain.",
    return *buf_datalen;
        write_result,*buf_flushlen,*buf_datalen);
    return *buf_flushlen;
  }
  }

}
}


int write_to_buf(char *string, size_t string_len,
int write_to_buf(char *string, size_t string_len,
+5 −0
Original line number Original line Diff line number Diff line
/* Copyright 2001,2002 Roger Dingledine, Matej Pfajfar. */
/* See LICENSE for licensing information */
/* $Id$ */


#include "or.h"
#include "or.h"


@@ -5,10 +8,12 @@ int check_sane_cell(cell_t *cell) {


  assert(cell);
  assert(cell);


#if 0 /* actually, the aci is 0 for padding cells */
  if(cell->aci == 0) {
  if(cell->aci == 0) {
    log(LOG_DEBUG,"check_sane_cell(): Cell has aci=0. Dropping.");
    log(LOG_DEBUG,"check_sane_cell(): Cell has aci=0. Dropping.");
    return -1;
    return -1;
  }
  }
#endif


#if 0 /* actually, the length is sometimes encrypted. so it's ok. */
#if 0 /* actually, the length is sometimes encrypted. so it's ok. */
  if(cell->length > 120) {
  if(cell->length > 120) {
+3 −0
Original line number Original line Diff line number Diff line
/* Copyright 2001,2002 Roger Dingledine, Matej Pfajfar. */
/* See LICENSE for licensing information */
/* $Id$ */


#include "or.h"
#include "or.h"


+4 −2
Original line number Original line Diff line number Diff line
/* Copyright 2001,2002 Roger Dingledine, Matej Pfajfar. */
/* See LICENSE for licensing information */
/* $Id$ */


#include "or.h"
#include "or.h"


@@ -40,8 +43,7 @@ void command_process_create_cell(cell_t *cell, connection_t *conn) {
  if(!circ) { /* if it's not there, create it */
  if(!circ) { /* if it's not there, create it */
    circ = circuit_new(cell->aci, conn);
    circ = circuit_new(cell->aci, conn);
    circ->state = CIRCUIT_STATE_OPEN_WAIT;
    circ->state = CIRCUIT_STATE_OPEN_WAIT;
    memcpy((void *)&circ->onionlen,(void *)cell->payload, 4);
    circ->onionlen = ntohl(*(int*)cell->payload);
    circ->onionlen = ntohl(circ->onionlen);
    log(LOG_DEBUG,"command_process_create_cell():  Onion length is %u.",circ->onionlen);
    log(LOG_DEBUG,"command_process_create_cell():  Onion length is %u.",circ->onionlen);
    if(circ->onionlen > 50000 || circ->onionlen < 1) { /* too big or too small */
    if(circ->onionlen > 50000 || circ->onionlen < 1) { /* too big or too small */
      log(LOG_DEBUG,"That's ludicrous. Closing.");
      log(LOG_DEBUG,"That's ludicrous. Closing.");
+22 −52
Original line number Original line Diff line number Diff line
/* Copyright 2001,2002 Roger Dingledine, Matej Pfajfar. */
/* See LICENSE for licensing information */
/* $Id$ */

/**
/**
 * config.c 
 * config.c 
 * Routines for loading the configuration file.
 * Routines for loading the configuration file.
@@ -5,50 +9,6 @@
 * Matej Pfajfar <mp292@cam.ac.uk>
 * Matej Pfajfar <mp292@cam.ac.uk>
 */
 */


/*
 * Changes :
 * $Log$
 * Revision 1.10  2002/07/15 16:42:27  montrose
 * corrected some string literals
 *
 * Revision 1.9  2002/07/11 19:03:44  montrose
 * finishing touches. think its ready for integration now.
 *
 * Revision 1.8  2002/07/11 18:38:15  montrose
 * added new option GlobalRole to getoptions()
 *
 * Revision 1.7  2002/07/11 14:50:26  montrose
 * cleaned up some, added validation to getoptions()
 *
 * Revision 1.6  2002/07/10 12:37:49  montrose
 * Added usage display on error.
 *
 * Revision 1.5  2002/07/09 19:51:41  montrose
 * Miscellaneous bug fixes / activated "make check" for src/or
 *
 * Revision 1.4  2002/07/03 19:58:18  montrose
 * minor bug fix in error checking
 *
 * Revision 1.3  2002/07/03 16:53:34  montrose
 * added error checking into getoptions()
 *
 * Revision 1.2  2002/07/03 16:31:22  montrose
 * Added getoptions() and made minor adjustment to poptReadDefaultOptions()
 *
 * Revision 1.1.1.1  2002/06/26 22:45:50  arma
 * initial commit: current code
 *
 * Revision 1.3  2002/04/02 14:28:24  badbytes
 * Final finishes.
 *
 * Revision 1.2  2002/01/27 00:42:50  mp292
 * Reviewed according to Secure-Programs-HOWTO.
 *
 * Revision 1.1  2002/01/03 10:24:05  badbytes
 * COde based on that in op. Needs to be modified.
 *
 */

#include "or.h"
#include "or.h"
#include <libgen.h>
#include <libgen.h>


@@ -119,7 +79,9 @@ RETURN VALUE: 0 on success, non-zero on error
         0, "local port on which the onion proxy is running",  "<file>" },
         0, "local port on which the onion proxy is running",  "<file>" },
      { "TrafficShaping",  't',  POPT_ARG_INT,     &options->TrafficShaping,
      { "TrafficShaping",  't',  POPT_ARG_INT,     &options->TrafficShaping,
         0, "which traffic shaping policy to use",             "<policy>" },
         0, "which traffic shaping policy to use",             "<policy>" },
      { "GlobalRole",      'g',  POPT_ARG_INT,     &options->GlobalRole,
      { "LinkPadding",     'P',  POPT_ARG_INT,     &options->LinkPadding,
	 0, "whether to use link padding",                     "<padding>" },
      { "Role",            'g',  POPT_ARG_INT,     &options->Role,
         0, "4-bit global role id",                            "<role>" },
         0, "4-bit global role id",                            "<role>" },
      { "Verbose",         'v',  POPT_ARG_NONE,    &Verbose,
      { "Verbose",         'v',  POPT_ARG_NONE,    &Verbose,
         0, "display options selected before execution",       NULL },
         0, "display options selected before execution",       NULL },
@@ -137,7 +99,8 @@ RETURN VALUE: 0 on success, non-zero on error
   options->LogLevel = "debug";
   options->LogLevel = "debug";
   options->loglevel = LOG_DEBUG;
   options->loglevel = LOG_DEBUG;
   options->CoinWeight = 0.8;
   options->CoinWeight = 0.8;
   options->GlobalRole = ROLE_OR_LISTEN | ROLE_OR_CONNECT_ALL | ROLE_OP_LISTEN | ROLE_AP_LISTEN;
   options->LinkPadding = 1;
   options->Role = ROLE_OR_LISTEN | ROLE_OR_CONNECT_ALL | ROLE_OP_LISTEN | ROLE_AP_LISTEN;


   code = poptGetNextOpt(optCon);         /* first we handle command-line args */
   code = poptGetNextOpt(optCon);         /* first we handle command-line args */
   if ( code == -1 )
   if ( code == -1 )
@@ -170,19 +133,20 @@ RETURN VALUE: 0 on success, non-zero on error


   if ( Verbose )                      
   if ( Verbose )                      
   {
   {
      printf("LogLevel=%s, GlobalRole=%d\n",
      printf("LogLevel=%s, Role=%d\n",
             options->LogLevel,
             options->LogLevel,
             options->GlobalRole);
             options->Role);
      printf("RouterFile=%s, PrivateKeyFile=%s\n",
      printf("RouterFile=%s, PrivateKeyFile=%s\n",
             options->RouterFile,
             options->RouterFile,
             options->PrivateKeyFile);
             options->PrivateKeyFile);
      printf("ORPort=%d, OPPort=%d, APPort=%d\n",
      printf("ORPort=%d, OPPort=%d, APPort=%d\n",
             options->ORPort,options->OPPort,
             options->ORPort,options->OPPort,
             options->APPort);
             options->APPort);
      printf("CoinWeight=%6.4f, MaxConn=%d, TrafficShaping=%d\n",
      printf("CoinWeight=%6.4f, MaxConn=%d, TrafficShaping=%d, LinkPadding=%d\n",
             options->CoinWeight,
             options->CoinWeight,
             options->MaxConn,
             options->MaxConn,
             options->TrafficShaping);
             options->TrafficShaping,
             options->LinkPadding);
   }
   }


   /* Validate options */
   /* Validate options */
@@ -260,9 +224,15 @@ RETURN VALUE: 0 on success, non-zero on error
      code = -1;
      code = -1;
   }
   }


   if ( options->GlobalRole < 0 || options->GlobalRole > 15 )
   if ( options->LinkPadding != 0 && options->LinkPadding != 1 )
   {
      log(LOG_ERR,"LinkPadding option must be either 0 or 1.");
      code = -1;
   }

   if ( options->Role < 0 || options->Role > 15 )
   {
   {
      log(LOG_ERR,"GlobalRole option must be an integer between 0 and 15 (inclusive).");
      log(LOG_ERR,"Role option must be an integer between 0 and 15 (inclusive).");
      code = -1;
      code = -1;
   }
   }


Loading