5
int n = 0;
if ( 0 != getsockopt(iSockFd,SOL_SOCKET,SO_RCVBUF, &n, sizeof(n)))
{
    printf("Get socket option failed, errno: %d\n",errno);
}
else
{
    printf("Current socket buff len = %d\n", n);
}
n = 225280;
if(0 != setsockopt(iSockFd, SOL_SOCKET, SO_RCVBUF, (const void *)&n, sizeof(n)))
{
    printf("setsock err errno %d\n", errno);
}
else
{
    printf("setsock opt success\n");
}
n = 0;
if ( 0 != getsockopt(iSockFd,SOL_SOCKET,SO_RCVBUF, &n, sizeof(n)))
{
    printf("Get socket option failed, errno: %d\n",errno);
}
else
{
    printf("After setting socket buff len = %d\n", n);
}

Output is -

Current socket buff len = 41600

setsock opt success

After setting socket buff len = 41600.

Looks like receive buffer size is not increasing, any idea why this happens?

Thanks in advance!

pa1
  • 778
  • 3
  • 11
  • 26
  • The platform is free to adjust the actual value up or down. There isn't much point in a socket buffer that big. – user207421 Nov 18 '15 at 11:05
  • @Coder : Can you share version of linux kernel and the configurations in the following files to give a better picture : /proc/sys/net/ipv4/tcp_moderate_rcvbuf /proc/sys/net/ipv4/tcp_rmem /proc/sys/net/core/rmem_default /proc/sys/net/core/rmem_max /proc/sys/net/ipv4/tcp_rmem – Karthik Balaguru Nov 18 '15 at 17:56

2 Answers2

4

If the kernel is of newer version (2.6.17 or higher), checkout whether autotuning is enabled by verifying the file /proc/sys/net/ipv4/tcp_moderate_rcvbuf . If the value of tcp_moderate_rcvbuf is 1, then autotuning is enabled. In such a scenario, the receive buffer will be dynamically updated by the kernel and is bound to the values in /proc/sys/net/ipv4/tcp_rmem. Check whether this limit is hit.

If the kernel is of older version, check whether the SO_RCVBUF is limited by the values in /proc/sys/net/core/rmem_default and /proc/sys/net/core/rmem_max. Incase of TCP, also check the value of /proc/sys/net/ipv4/tcp_rmem

Also note that 'Manually adjusting socket buffer sizes with setsockopt() disables autotuning' . Here is good link on tuning for linux http://www.psc.edu/index.php/networking/641-tcp-tune

Karthik Balaguru
  • 7,424
  • 7
  • 48
  • 65
  • You are contradicting yourself. Your first paragraph suggests to check for autotuning but your last paragraph explains that setting a buffer size disables autotuning. So how could autotuning prevent the socket buffer being set here if the code in the question would disable it? Autotuning is totally unrelated to the question. – Mecki Jan 30 '19 at 09:31
0

Always have a look what the man page says:

SO_RCVBUF
Sets or gets the maximum socket receive buffer in bytes. The kernel doubles this value (to allow space for bookkeeping overhead) when it is set using setsockopt(2), and this doubled value is returned by getsockopt(2). The default value is set by the /proc/sys/net/core/rmem_default file, and the maximum allowed value is set by the /proc/sys/net/core/rmem_max file. The minimum (doubled) value for this option is 256.

http://man7.org/linux/man-pages/man7/socket.7.html

So there is an upper limit and any attempt to set a larger value will silently fail, which means there will be no error, the size just isn't raised. Such a limit exists on pretty much all existing systems, not just Linux. Also note that even if your setsockopt() was successful, getsockopt() would return a larger value because this value is internally doubled (this is Linux exclusive, other systems don't do that).

Mecki
  • 125,244
  • 33
  • 244
  • 253