网络编程-SOCKET选项-缓冲区

15 篇文章 1 订阅
6 篇文章 0 订阅

1.TCP UDP 发送 和接收的缓冲区大小

 创建socket时 socket(af_inet,sock_stream/sock_dgram,0)->inet_create->sock_init_data 不设置的情况下 不调用setsockopt时候

 TCP UDP 的缓冲区都市: rmem_default ,wmem_default

sock.c sock_init_data()

 sk->sk_rcvbuf  = sysctl_rmem_default; //rmem_default /proc/sys/net/core/rmem_default
 sk->sk_sndbuf  = sysctl_wmem_default; //wmem_default /proc/sys/net/core/wmem_default

sysctl_net_core.c

 {
  .procname = "wmem_default",
  .data  = &sysctl_wmem_default,
  .maxlen  = sizeof(int),
  .mode  = 0644,
  .proc_handler = proc_dointvec_minmax,
  .extra1  = &min_sndbuf,
 },
 {
  .procname = "rmem_default",
  .data  = &sysctl_rmem_default,
  .maxlen  = sizeof(int),
  .mode  = 0644,
  .proc_handler = proc_dointvec_minmax,
  .extra1  = &min_rcvbuf,
 },


但TCP 随后的 tcp_init_sock 覆盖了 开始的rmem_default ,wmem_default

tcp.c:tcp_init_sock

 sk->sk_sndbuf = sysctl_tcp_wmem[1]; ///proc/sys/net/ipv4/tcp_wmem
 sk->sk_rcvbuf = sysctl_tcp_rmem[1];  ///proc/sys/net/ipv4/tcp_rmem

tcp_init:

 sysctl_tcp_wmem[0] = SK_MEM_QUANTUM;
 sysctl_tcp_wmem[1] = 16*1024;                      //默认发送大小
 sysctl_tcp_wmem[2] = max(64*1024, max_wshare);

 sysctl_tcp_rmem[0] = SK_MEM_QUANTUM;
 sysctl_tcp_rmem[1] = 87380;                            //默认接收大小
 sysctl_tcp_rmem[2] = max(87380, max_rshare);


但TCP UDP 的最大发送和接收值 由/proc/sys/net/core/wmem_max  /proc/sys/net/core/rmem_max 限制,如要setsockopt 够大,首先得修改wmem_max rmem_max


TCP缓冲区 具体参考 man tcp

To make use of them, the send and receive  buffer
       sizes   must   be   increased.    They   can  be  set  globally  with  the  /proc/sys/net/ipv4/tcp_wmem  and
       /proc/sys/net/ipv4/tcp_rmem files, or on individual sockets by using  the  SO_SNDBUF  and  SO_RCVBUF  socket
       options with the setsockopt(2) call.

       The  maximum sizes for socket buffers declared via the SO_SNDBUF and SO_RCVBUF mechanisms are limited by the
       values in the /proc/sys/net/core/rmem_max and /proc/sys/net/core/wmem_max files.   Note  that  TCP  actually
       allocates  twice  the  size  of the buffer requested in the setsockopt(2) call, and so a succeeding getsock-
       opt(2) call will not return the same size of buffer as requested in the setsockopt(2) call.



       tcp_rmem (since Linux 2.4)
              This  is  a  vector of 3 integers: [min, default, max].  These parameters are used by TCP to regulate
              receive buffer sizes.  TCP dynamically adjusts the size of  the  receive  buffer  from  the  defaults
              listed below, in the range of these values, depending on memory available in the system.

              min       minimum  size of the receive buffer used by each TCP socket.  The default value is the sys-
                        tem page size.  (On Linux 2.4, the default value is 4K, lowered to PAGE_SIZE bytes in  low-
                        memory  systems.)   This  value is used to ensure that in memory pressure mode, allocations
                        below this size will still succeed.  This is not used to bound  the  size  of  the  receive
                        buffer declared using SO_RCVBUF on a socket.

              default   the default size of the receive buffer for a TCP socket.  This value overwrites the initial
                        default buffer size from the generic global net.core.rmem_default defined  for  all  proto-
                        cols.   The  default value is 87380 bytes.  (On Linux 2.4, this will be lowered to 43689 in
                        low-memory systems.)  If larger receive buffer sizes are  desired,  this  value  should  be
                        increased  (to  affect  all  sockets).   To employ large TCP windows, the net.ipv4.tcp_win-
                        dow_scaling must be enabled (default).

              max       the maximum size of the receive buffer used by each TCP socket.  This value does not  over-
                        ride  the  global  net.core.rmem_max.   This  is  not used to limit the size of the receive
                        buffer declared using SO_RCVBUF on a socket.  The default value  is  calculated  using  the
                        formula

                            max(87380, min(4MB, tcp_mem[1]*PAGE_SIZE/128))

                        (On Linux 2.4, the default is 87380*2 bytes, lowered to 87380 in low-memory systems).


UDP 发送:

   超过MTU IP层分片,最好在UDP 层就拆包,避免丢包导致整个包丢弃,

UDP 接收:

   原子性接收,IP层可能会组包,队列满则丢弃,接收的缓冲区只能存放一个UDP报文,






评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值