Commit 483c1e90 authored by Nick Mathewson's avatar Nick Mathewson 🤹
Browse files

r13774@catbus: nickm | 2007-07-16 12:23:28 -0400

 Tweaks on constrained socket buffers patch from coderman: Add a changelog; rename some variables; fix some long lines and whitespace; make ConstrainedSockSize a memunit; pass setsockopt a void.


svn:r10843
parent f4a66737
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -5,6 +5,8 @@ Changes in version 0.2.0.3-alpha - 2007-??-??
      root, and have Tor bind those ports before it changes to another UID.
    - tor-gencert creates all files as readable to the file creator only, and
      write-protects the authority identity key.
    - New ConstrainedSockets option to set SO_SNDBUF and SO_RCVBUF on TCP
      sockets.  (Patch from coderman.)

  o Minor features (directory authority):
    - Fail quickly and (relatively) harmlessly if we generate a network
+12 −10
Original line number Diff line number Diff line
@@ -102,30 +102,32 @@ Windows since that platform lacks getrlimit(). (Default: 1000)
.LP
.TP
\fBConstrainedSockets \fR\fB0\fR|\fB1\fR\fP
If set, Tor will attempt to shrink the recv and xmit buffers for all 
If set, Tor will tell the kernel to attempt to shrink the buffers for all 
sockets to the size specified in \fBConstrainedSockSize\fP.  This is useful 
for virtual servers and other environments where system level TCP
buffers may be limited.  If you encounter the "Error creating network
socket: No buffer space available" message you are likely experiencing
buffers may be limited.  If you're on a virtual server, and you
encounter the "Error creating network
socket: No buffer space available" message, you are likely experiencing
this problem.

The preferred solution is to have the admin increase the buffer pool for
the host itself via /proc/sys/net/ipv4/tcp_mem or equivalent facility. 
the host itself via /proc/sys/net/ipv4/tcp_mem or equivalent facility; this
configuration option is a second-resort.

The DirPort option should also not be used if TCP buffers are scarce.  The
cached directory requests consume additional sockets which exacerbates the
problem.

You should \fBnot\fP enable this feature unless you encounter the no buffer
space available issue.  Reducing the TCP buffers affects window size for
You should \fBnot\fP enable this feature unless you encounter the "no buffer
space available" issue.  Reducing the TCP buffers affects window size for
for the TCP stream and will reduce throughput in proportion to round trip
time on long paths.
time on long paths.  (Default: 0.)
.LP
.TP
\fBConstrainedSockSize \fR\fINUM\fP
When \fBConstrainedSockets\fP is enabled the recv and xmit buffers for
\fBConstrainedSockSize \fR\fIN\fR \fBbytes\fR|\fBKB\fP
When \fBConstrainedSockets\fP is enabled the receive and transmit buffers for
all sockets will be set to this limit.  Must be a value between 2048
and 262144 in 1024 byte increments.  Default of 8192 is recommended.
and 262144, in 1024 byte increments.  Default of 8192 is recommended.
.LP
.TP
\fBControlPort \fR\fIPort\fP
+5 −5
Original line number Diff line number Diff line
@@ -147,7 +147,7 @@ static config_var_t _option_vars[] = {
  VAR("ClientOnly",          BOOL,     ClientOnly,           "0"),
  VAR("ConnLimit",           UINT,     ConnLimit,            "1000"),
  VAR("ConstrainedSockets",  BOOL,     ConstrainedSockets,   "0"),
  VAR("ConstrainedSockSize", UINT,     ConstrainedSockSize,  "8192"),
  VAR("ConstrainedSockSize", MEMUNIT,  ConstrainedSockSize,  "8192"),
  VAR("ContactInfo",         STRING,   ContactInfo,          NULL),
  VAR("ControlListenAddress",LINELIST, ControlListenAddress, NULL),
  VAR("ControlPort",         UINT,     ControlPort,          "0"),
@@ -2935,13 +2935,13 @@ options_validate(or_options_t *old_options, or_options_t *options,
  if (options->ConstrainedSockets) {
    /* If the user wants to constrain socket buffer use, make sure the desired
     * limit is between MIN|MAX_TCPSOCK_BUFFER in k increments. */
    if (options->ConstrainedSockSize < MIN_TCPSOCK_BUFFER ||
        options->ConstrainedSockSize > MAX_TCPSOCK_BUFFER ||
    if (options->ConstrainedSockSize < MIN_CONSTRAINED_TCP_BUFFER ||
        options->ConstrainedSockSize > MAX_CONSTRAINED_TCP_BUFFER ||
        options->ConstrainedSockSize % 1024) {
      r = tor_snprintf(buf, sizeof(buf),
          "ConstrainedSockSize is invalid.  Must be a value between %d and %d "
          "in 1024 byte increments.",
          MIN_TCPSOCK_BUFFER, MAX_TCPSOCK_BUFFER);
          MIN_CONSTRAINED_TCP_BUFFER, MAX_CONSTRAINED_TCP_BUFFER);
      *msg = tor_strdup(r >= 0 ? buf : "internal error");
      return -1;
    }
+11 −13
Original line number Diff line number Diff line
@@ -926,9 +926,8 @@ connection_handle_listener_read(connection_t *conn, int new_type)

  set_socket_nonblocking(news);

  if (options->ConstrainedSockets) {
    set_constrained_socket_buffers (news, options->ConstrainedSockSize);
  }
  if (options->ConstrainedSockets)
    set_constrained_socket_buffers(news, (int)options->ConstrainedSockSize);

  tor_assert(((struct sockaddr*)addrbuf)->sa_family == conn->socket_family);

@@ -1103,9 +1102,8 @@ connection_connect(connection_t *conn, const char *address,

  set_socket_nonblocking(s);

  if (options->ConstrainedSockets) {
    set_constrained_socket_buffers (s, options->ConstrainedSockSize);
  }
  if (options->ConstrainedSockets)
    set_constrained_socket_buffers(s, (int)options->ConstrainedSockSize);

  memset(&dest_addr,0,sizeof(dest_addr));
  dest_addr.sin_family = AF_INET;
@@ -2561,17 +2559,17 @@ client_check_address_changed(int sock)
static void
set_constrained_socket_buffers(int sock, int size)
{
  if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (const char *)&size, sizeof(size)) < 0) {
  void *sz = (void*)&size;
  if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, sz, sizeof(size)) < 0) {
    int e = tor_socket_errno(sock);
    log_warn(LD_NET, "setsockopt() to constrain send buffer to %d bytes failed: %s",
             size, tor_socket_strerror(e));
    log_warn(LD_NET, "setsockopt() to constrain send "
             "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
  }
  if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (const char *)&size, sizeof(size)) < 0) {
  if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, sz, sizeof(size)) < 0) {
    int e = tor_socket_errno(sock);
    log_warn(LD_NET, "setsockopt() to constrain recv buffer to %d bytes failed: %s",
             size, tor_socket_strerror(e));
    log_warn(LD_NET, "setsockopt() to constrain recv "
             "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
  }
  return;
}

/** Process new bytes that have arrived on conn-\>inbuf.
+3 −3
Original line number Diff line number Diff line
@@ -1749,8 +1749,8 @@ typedef struct exit_redirect_t {
} exit_redirect_t;

/* limits for TCP send and recv buffer size used for constrained sockets */
#define MIN_TCPSOCK_BUFFER 2048
#define MAX_TCPSOCK_BUFFER 262144  /* 256k */
#define MIN_CONSTRAINED_TCP_BUFFER 2048
#define MAX_CONSTRAINED_TCP_BUFFER 262144  /* 256k */

/** A linked list of lines in a config file. */
typedef struct config_line_t {
@@ -1894,7 +1894,7 @@ typedef struct {
  config_line_t *ReachableDirAddresses; /**< IP:ports for Dir conns. */

  int ConstrainedSockets; /**< Shrink xmit and recv socket buffers. */
  int ConstrainedSockSize; /**< Size of constrained buffers. */
  uint64_t ConstrainedSockSize; /**< Size of constrained buffers. */

  /** Application ports that require all nodes in circ to have sufficient
   * uptime. */