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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7865069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:49:32  来源:igfitidea点击:

How to find the socket buffer size of linux

linuxsocketsbuffer

提问by Freewind

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

linux的默认套接字缓冲区大小是多少?有什么命令可以查看吗?

采纳答案by saeedn

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)
  • /proc/sys/net/ipv4/tcp_rmem(供阅读)
  • /proc/sys/net/ipv4/tcp_wmem(用于写)

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

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

回答by Dinesh P.R.

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

为了在 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

回答by Dawid Szymański

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).

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

回答by Pierz

Whilst, as has been pointed out, it is possible to see the current defaultsocket 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):

虽然,正如已经指出的,可以在 中查看当前默认套接字缓冲区大小/proc,也可以使用sysctl(注意:虽然名称包括 ipv4 这些大小也适用于 ipv6 套接字 - ipv6 tcp_v6_init_sock()代码只是调用了 ipv4 tcp_init_sock() 函数):

 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 sscommand (part of the iproutepackage), which can also provide a bunch more info on sockets like congestion control parameter etc. E.g. To list the currently open TCP (toption) sockets and associated memory (m) information:

但是,默认套接字缓冲区只是在 sock 初始化时设置,但内核随后动态调整它们的大小(除非使用 setsockopt() 和 SO_SNDBUF 设置)。可以使用ss命令(iproute包的一部分)检查当前打开的套接字缓冲区的实际大小,该命令还可以提供有关套接字的更多信息,如拥塞控制参数等。例如,列出当前打开的 TCP(t选项)套接字以及相关的内存 ( m) 信息:

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 (e.g. sock.h):

这是 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
w:sk_wmem_queued      # persistent transmit queue size
o:sk_omem_alloc
bl:sk_backlog
d:sk_drops
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