84

What's the default socket buffer size of linux? Is there any command to see it?

Freewind
  • 193,756
  • 157
  • 432
  • 708

5 Answers5

133

If you want see your buffer size in terminal, you can take a look at:

  • /proc/sys/net/ipv4/tcp_rmem (for read)
  • /proc/sys/net/ipv4/tcp_wmem (for write)

They contain three numbers, which are minimum, default and maximum memory size values (in byte), respectively.

Blue
  • 820
  • 4
  • 17
saeedn
  • 3,288
  • 1
  • 21
  • 20
43

For getting the buffer size in c/c++ program the following is the flow

int n;
unsigned int m = sizeof(n);
int fdsocket;
fdsocket = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); // example
getsockopt(fdsocket,SOL_SOCKET,SO_RCVBUF,(void *)&n, &m);
// now the variable n will have the socket size
Pang
  • 9,564
  • 146
  • 81
  • 122
Dinesh P.R.
  • 6,936
  • 6
  • 35
  • 44
  • 1
    Is it safe to call `socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)` instead? I'm using this C call in my Swift TCP framework to get the buffer size to reduce `recv` calls. – DevAndArtist Jun 18 '16 at 09:25
  • 2
    Note: this is also only the read buffer size, use SO_SNDBUF to get the size of the write buffer. Under at least linux, you can use ioctl SIOCINQ and SIOCOUTQ, to respectfully get the current used state of the buffer. – Rahly Jun 07 '17 at 22:01
26

Whilst, as has been pointed out, it is possible to see the current default socket buffer sizes in /proc, it is also possible to check them using sysctl (Note: Whilst the name includes ipv4 these sizes also apply to ipv6 sockets - the ipv6 tcp_v6_init_sock() code just calls the ipv4 tcp_init_sock() function):

 sysctl net.ipv4.tcp_rmem
 sysctl net.ipv4.tcp_wmem

However, the default socket buffers are just set when the sock is initialised but the kernel then dynamically sizes them (unless set using setsockopt() with SO_SNDBUF). The actual size of the buffers for currently open sockets may be inspected using the ss command (part of the iproute/iproute2 package), which can also provide a bunch more info on sockets like congestion control parameter etc. E.g. To list the currently open TCP (t option) sockets and associated memory (m) information:

ss -tm

Here's some example output:

State       Recv-Q Send-Q        Local Address:Port        Peer Address:Port
ESTAB       0      0             192.168.56.102:ssh        192.168.56.1:56328
skmem:(r0,rb369280,t0,tb87040,f0,w0,o0,bl0,d0)

Here's a brief explanation of skmem (socket memory) - for more info you'll need to look at the kernel sources (i.e. sock.h):

r:sk_rmem_alloc
rb:sk_rcvbuf          # current receive buffer size
t:sk_wmem_alloc
tb:sk_sndbuf          # current transmit buffer size
f:sk_forward_alloc
w:sk_wmem_queued      # persistent transmit queue size
o:sk_omem_alloc
bl:sk_backlog
d:sk_drops
Pierz
  • 7,064
  • 52
  • 59
2

I'm still trying to piece together the details, but to add to the answers already given, these are some of the important commands:

cat /proc/sys/net/ipv4/udp_mem
cat /proc/sys/net/core/rmem_max
cat /proc/sys/net/ipv4/tcp_rmem
cat /proc/sys/net/ipv4/tcp_wmem
ss -m  # see `man ss`

References & help pages:

  1. Man pages
    man 7 socket
    man 7 udp
    man 7 tcp
    man ss
    
  2. https://www.linux.org/threads/how-to-calculate-tcp-socket-memory-usage.32059/
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
-6

Atomic size is 4096 bytes, max size is 65536 bytes. Sendfile uses 16 pipes each of 4096 bytes size. cmd : ioctl(fd, FIONREAD, &buff_size).

Dawid Szymański
  • 775
  • 6
  • 15
  • 6
    What does 'atomic size' mean; what does `sendfile()` have to do with it; and where have you answered the question about the default socket buffer size? – user207421 Oct 02 '15 at 07:37
  • @ EJP Atomic size - Linux internals assert for 4096b socket transfer - MT-safe, mutex, 1/16 internal pipes. sandfile has a lot to do with it. I think i have. Default buffer size is 16*4096b. – Dawid Szymański Oct 02 '15 at 10:47
  • 1
    None of this answers the question. It is about socket send and receive buffer sizes. Not about `sendfile()`, which therefore has *nothing* to do with it, nor atomic transfer sizes either, ditto. – user207421 Apr 25 '17 at 22:36