如何查看linux的socket缓冲区大小

发布于 2024-12-11 04:55:20 字数 39 浏览 0 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

与他有关 2024-12-18 04:55:20

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

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

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

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)

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

街角迷惘 2024-12-18 04:55:20

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

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

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
深海里的那抹蓝 2024-12-18 04:55:20

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

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

返回最小、默认、最大缓冲区大小。但是,默认套接字缓冲区只是在初始化 sock 时设置,但内核随后会在这些限制内动态调整它们的大小,除非应用程序更改了最大发送缓冲区大小(尽管 内核使用双倍值),使用带有 SO_SNDBUF 套接字选项的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)

接收缓冲区带有前缀rb 和发送/传输缓冲区是tbss 手册页 现在显示了返回各种 skmem (套接字内存)信息。

skmem:(r,rb,t,tb,
   f、w、o、bl、d
         为接收数据包分配的内存

  ;
         可以分配用于接收的总内存
         包

  ;
         用于发送数据包的内存(已被
         发送到第3层)

  ;
         可以分配用于发送的总内存
         包

  ;
         套接字分配的内存作为缓存,但是
         尚未用于接收/发送数据包。如果需要的话
         发送/接收数据包的内存,这个内存
         缓存将在分配额外的之前使用
         记忆。

  
         分配用于发送数据包的内存(其中有
         未发送至第 3 层)

  
         用于存储套接字选项的内存,例如,
         TCP MD5 签名的密钥

  <返回日志>
         用于 sk backlog 队列的内存。上一个
         进程上下文,如果进程正在接收
         数据包,并且收到一个新数据包,它将是
         放入sk backlog队列中,这样就可以
         进程立即收到

  ;
         数据包被删除之前丢弃的数量
         多路复用到套接字中

此信息来自内核 - 这是相关 skmem 变量的简要说明 - 有关更多详细信息,请查看内核源代码(即 sock.h):

<前><代码>r:sk_rmem_alloc
rb:sk_rcvbuf # 当前接收缓冲区大小
t:sk_wmem_alloc
tb:sk_sndbuf # 当前传输缓冲区大小
f:sk_forward_alloc # 向前分配的空间
w:sk_wmem_queued # 持久传输队列大小
o:sk_omem_alloc
bl:sk_backlog
d:sk_drops

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

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

This returns the min, default, max buffer sizes. However, the default socket buffers are just set when the sock is initialised but the kernel then dynamically sizes them within those limits, unless an app changes the maximum send buffer size (though the kernel uses double the value) using setsockopt() with SO_SNDBUF socket option.

The actual size of the buffers for currently open sockets may be inspected using the ss command (part of the iproute/iproute2 package), which can also provide a bunch more info on sockets like congestion control parameters etc. E.g. To list the currently open TCP (t option) sockets and associated memory (m) information:

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)

The receive buffer is prefixed rb and send/transmit buffer is tb. The ss man page now shows the definitions of the various skmem (socket memory) info returned.

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

This info comes from the kernel - here's a brief explanation of the relevant skmem variables - for more details take a look at the kernel sources (i.e. 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
迷爱 2024-12-18 04:55:20

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

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 套接字
    人 7 udp
    人 7 TCP
    男子党卫军
    
  2. https://www.linux.org /threads/how-to-calculate-tcp-socket-memory-usage.32059/

I'm still trying to piece together the details, but to add to the answers already given, these are some of the important commands:

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`

References & help pages:

  1. Man pages
    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/
怎会甘心 2024-12-18 04:55:20

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文