Default value for socket TCP buffer size, maximum __unix network programming

Source: Internet
Author: User

Author: Brother Adon

Created:2013-4-17

blog:http://blog.csdn.net/c359719435/

Copyright 2013 Arab http://blog.csdn.net/c359719435/

Use and reprint please indicate the source

1 set the size of the socket TCP buffer to doubt 1: Set SO_SNDBUF, so_rcvbuf the value of this default buffer by setsockopt, and then use getsockopt to get the set value, and find that the return value is twice times the set value.         Why. Looking through the Internet, see the Linux kernel code/USR/SRC/LINUX-2.6.13.2/NET/CORE/SOCK.C, find this code sock_setsockopt this function:
      CASE&NBSP;SO_SNDBUF:                       &N Bsp /* Don ' t error on "This BSD doesn ' t and if"                            about it right. Otherwise apps have to                           &NBSP;PL Ay ' guess the biggest size ' games. Rcvbuf/sndbuf                            are treated In BSD as hints *                           if (Val) ; Sysctl_wmem_max)//val is the value of the buffer size we want to set                         &N Bsp       val = sysctl_wmem_max;//greater than maximum, Val value is set to maximum                 &NB Sp         Sk->sk_userloCks |= Sock_sndbuf_lock;                         if ((Val * 2) < SOCK_MIN_SNDBUF)//v Twice times less than minimum for AL, set to minimum                             &NBSP ;   SK-&GT;SK_SNDBUF = sock_min_sndbuf;                         else         &NBSP ;                       SK-&GT;SK_SNDBUF = val * 2;//val twice times greater than minimum, set to Val value twice times                          /                         *      wake up sending tasks if we &N Bsp                        *      upped the value.                          */                        Sk->sk_writ E_space (SK);                         break;                   CASE&NBSP;SO_RCVBUF:           &NB Sp            /* Don ' t error on this BSD doesn ' t and if ' I-i-the                            about it it right. Otherwise apps have to                           &NBSP;PL Ay ' guess the biggest size ' games. Rcvbuf/sndbuf                            are treated In BSD as hints *                           if (Val) ; Sysctl_rmem_max)                                 val = Sysctl_rmem_max;                           Sk->sk_userlocks |= SOCK_RCV Buf_lock;                         * Fixme:is This lower bound the righ T one? *                         if ((Val * 2) < SOCK_MIN_RCVBUF)                                 SK-&GT;SK _rcvbuf = Sock_min_rcvbuf;                         else         &NBSP ;                       SK-&GT;SK_RCVBUF = val * 2;                         break;       &NBSP; As can be seen from the above code: (1) When the value val > Max Sysctl_wmem_max is set, it is set to twice times the maximum value: 2*sysctl_wmem_max                                       (2) When setting the value of twice times val*2 > Minimum value, then set to the minimum value: sock_min_sndbuf;                                      (3) When setting the value Val < maximum value Sysctl_wmem_max, and Val*2 > sock_min_sndbuf, set to 2*val.
        View Linux manual:             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                 & nbsp   Is set using setsockopt (2),                    &N Bsp And this doubled value is returned by getsockopt (2).                     the default value is set  by the/proc/sys/net/core/rmem_default file,      &nbsp               And the maximum allowed value Is set by  the /proc/sys/net/core/rmem_max file.                       The minimum (doubled)  value fo r this option is 256.       View my host Linux 2.6.6:/proc/sys/net/core/rmem_max:            4194304//4m       View/proc/sys/net/core/wmem_max:            8388608  //8m   & nbsp  Therefore, the maximum number of receive buffers that can be set is 8M, and the maximum value of the send buffer is 16M.
Puzzle 2: Why should there be twice times such a kernel setting? My understanding is that when setting this value, the user may only take into account the size of the data, without considering the byte overhead of the packet. So set this value to twice times.
Note: Overhead, in the frame structure of computer network, in addition to useful data, there are many control information, these control information to ensure the completion of communication.          These control information is referred to as system overhead. 2 default value of TCP buffer size to create a socket, the value of the buffer is obtained by getsockopt the following: Send buffer size: sndbufsize = 16384 Receive buffer size: RC Vbufsize = 87380
Puzzle 3:linux Manual, the default value of the receive buffer is saved in/proc/sys/net/core/rmem_default, and the send buffer is saved in/proc/sys/net/core/wmem_default. [Root@cfs_netstorage core]# cat/proc/sys/net/core/rmem_default 1048576 [Root@cfs_netstorage core]# Core/wmem_default 512488
The default value for the receive buffer is: 1048576,1m. The default value for the send buffer is: 512488,512k. Why the default value for creating a socket is 87380, 16384 ...
Further information found that the default value of the Linux socket buffer size is configured in the/proc virtual file system.  In two files respectively:/proc/sys/net/ipv4/tcp_wmem [root@cfs_netstorage core]# cat/proc/sys/net/ipv4/tcp_wmem 4096 16384 131072 The first represents the minimum value, the second represents the default value, and the third represents the maximum value. /proc/sys/net/ipv4/tcp_rmem [root@cfs_netstorage core]# cat/proc/sys/net/ipv4/tcp_rmem 4096 87380 174760
Thus, a new socket is selected, and the default values are read from these two files. You can tune by changing the values in these two files, but the surest way to do this is to call setsockopt in your program. With the setsockopt setting, the maximum number of receive buffers that can be set is 8M, and the maximum send buffer is 16M (in Linux 2.6.6).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.