如何找到linux的socket缓冲区大小

问题描述 投票:0回答:5

linux默认的socket缓冲区大小是多少?有什么命令可以看到吗

linux sockets buffer
5个回答
133
投票

如果您想在终端中查看缓冲区大小,您可以查看:

  • /proc/sys/net/ipv4/tcp_rmem
    (供阅读)
  • /proc/sys/net/ipv4/tcp_wmem
    (用于写入)

它们包含三个数字,分别是最小、默认和最大内存大小值(以字节为单位)。


43
投票

在c/c++程序中获取缓冲区大小的流程如下

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

28
投票

虽然正如已经指出的那样,可以在 /proc 中查看当前

default
套接字缓冲区大小,也可以使用
sysctl
检查它们(注意:虽然名称包括 ipv4,但这些大小也适用到 ipv6 套接字 - ipv6 tcp_v6_init_sock() 代码仅调用 ipv4 tcp_init_sock() 函数):

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

但是,默认套接字缓冲区只是在初始化 sock 时设置,但内核随后动态调整它们的大小(除非使用带有

SO_SNDBUF
scoekt 选项的 setsockopt() 进行设置)。可以使用
ss
命令(
iproute
/
iproute2
包的一部分)检查当前打开的套接字缓冲区的实际大小,该命令还可以提供有关套接字的更多信息,例如拥塞控制参数等。要列出当前打开的 TCP(
t
选项)套接字和关联的内存 (
m
) 信息:

ss -tm

这是一些示例输出:

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)

ss 手册页现在显示返回的各种

skmem
(套接字内存)信息的定义。

skmem:(r<rmem_alloc>,rb<rcv_buf>,t<wmem_alloc>,tb<snd_buf>,
   f<fwd_alloc>,w<wmem_queued>,o<opt_mem>,bl<back_log>,d<sock_drop>)

  <rmem_alloc>
         the memory allocated for receiving packet

  <rcv_buf>
         the total memory can be allocated for receiving
         packet

  <wmem_alloc>
         the memory used for sending packet (which has been
         sent to layer 3)

  <snd_buf>
         the total memory can be allocated for sending
         packet

  <fwd_alloc>
         the memory allocated by the socket as cache, but
         not used for receiving/sending packet yet. If need
         memory to send/receive packet, the memory in this
         cache will be used before allocate additional
         memory.

  <wmem_queued>
         The memory allocated for sending packet (which has
         not been sent to layer 3)

  <opt_mem>
         The memory used for storing socket option, e.g.,
         the key for TCP MD5 signature

  <back_log>
         The memory used for the sk backlog queue. On a
         process context, if the process is receiving
         packet, and a new packet is received, it will be
         put into the sk backlog queue, so it can be
         received by the process immediately

  <sock_drop>
         the number of packets dropped before they are de-
         multiplexed into the socket

此信息来自内核 - 这是相关 skmem 变量的简要说明 - 有关更多详细信息,请查看内核源代码(即 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    # space allocated forward
w:sk_wmem_queued      # persistent transmit queue size
o:sk_omem_alloc
bl:sk_backlog
d:sk_drops

2
投票

我仍在尝试拼凑细节,但要添加到已经给出的答案中,这些是一些重要的命令:

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`

参考和帮助页面:

  1. 手册页
    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/

-7
投票

原子大小为 4096 字节,最大大小为 65536 字节。 Sendfile 使用 16 个管道,每个管道大小为 4096 字节。 cmd : ioctl(fd, FIONREAD, &buff_size).

© www.soinside.com 2019 - 2024. All rights reserved.