Changeset 11956


Ignore:
Timestamp:
Feb 18, 2011, 12:43:47 AM (8 years ago)
Author:
jch
Message:

Use large kernel buffers for the UDP socket when uTP is enabled.

Since we're using a single UDP socket to implement multiple uTP sockets,
and since we're not always timely in servicing an incoming UDP packet,
it's important to use a large receive buffer. The send buffer is probably
less critical, we increase it nonetheless.

Location:
trunk/libtransmission
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/session.c

    r11955 r11956  
    19911991    session->isUTPEnabled = !session->isUTPEnabled;
    19921992
     1993    tr_udpSetSocketBuffers( session );
     1994
    19931995    /* But don't call tr_utpClose -- see reset_timer in tr-utp.c for an
    19941996       explanation. */
  • trunk/libtransmission/tr-udp.c

    r11926 r11956  
    3333#include "tr-utp.h"
    3434#include "tr-udp.h"
     35
     36/* Since we use a single UDP socket in order to implement multiple
     37   uTP sockets, try to set up huge buffers. */
     38
     39#define RECV_BUFFER_SIZE (4 * 1024 * 1024)
     40#define SEND_BUFFER_SIZE (1 * 1024 * 1024)
     41#define SMALL_BUFFER_SIZE (32 * 1024)
     42
     43static void
     44set_socket_buffers(int fd, int large)
     45{
     46    int size, rbuf, sbuf, rc;
     47    socklen_t rbuf_len = sizeof(rbuf), sbuf_len = sizeof(sbuf);
     48
     49    size = large ? RECV_BUFFER_SIZE : SMALL_BUFFER_SIZE;
     50    rc = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
     51    if(rc < 0)
     52        tr_nerr("UDP", "Failed to set receive buffer: %s",
     53                tr_strerror(errno));
     54
     55    size = large ? SEND_BUFFER_SIZE : SMALL_BUFFER_SIZE;
     56    rc = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));
     57    if(rc < 0)
     58        tr_nerr("UDP", "Failed to set send buffer: %s",
     59                tr_strerror(errno));
     60
     61    if(large) {
     62        rc = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rbuf, &rbuf_len);
     63        if(rc < 0)
     64            rbuf = 0;
     65
     66        rc = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sbuf, &sbuf_len);
     67        if(rc < 0)
     68            sbuf = 0;
     69
     70        if(rbuf < RECV_BUFFER_SIZE) {
     71            tr_nerr("UDP", "Failed to set receive buffer: requested %d, got %d",
     72                    RECV_BUFFER_SIZE, rbuf);
     73#ifdef __linux__
     74            tr_ninf("UDP",
     75                    "Please add the line "
     76                    "\"net.core.rmem_max = %d\" to /etc/sysctl.conf",
     77                    RECV_BUFFER_SIZE);
     78#endif
     79        }
     80
     81        if(sbuf < SEND_BUFFER_SIZE) {
     82            tr_nerr("UDP", "Failed to set send buffer: requested %d, got %d",
     83                    SEND_BUFFER_SIZE, sbuf);
     84#ifdef __linux__
     85            tr_ninf("UDP",
     86                    "Please add the line "
     87                    "\"net.core.wmem_max = %d\" to /etc/sysctl.conf",
     88                    SEND_BUFFER_SIZE);
     89#endif
     90        }
     91    }
     92}
     93
     94void
     95tr_udpSetSocketBuffers(tr_session *session)
     96{
     97    tr_bool utp = tr_sessionIsUTPEnabled(session);
     98    if(session->udp_socket >= 0)
     99        set_socket_buffers(session->udp_socket, utp);
     100    if(session->udp6_socket >= 0)
     101        set_socket_buffers(session->udp6_socket, utp);
     102}
     103
     104
     105
    35106
    36107/* BEP-32 has a rather nice explanation of why we need to bind to one
     
    200271    }
    201272
     273    tr_udpSetSocketBuffers(ss);
     274
    202275    if(ss->isDHTEnabled)
    203276        tr_dhtInit(ss);
  • trunk/libtransmission/tr-udp.h

    r11752 r11956  
    2828void tr_udpInit( tr_session * );
    2929void tr_udpUninit( tr_session * );
    30 
     30void tr_udpSetSocketBuffers(tr_session *);
Note: See TracChangeset for help on using the changeset viewer.