繁体   English   中英

tcp缓冲区中的总包长度是否超过分配的缓冲区大小?

[英]Does total packets length in tcp buffer can exceed allocated buffer size?

众所周知, getsockopt(sock, SOL_SOCKET, SO_SNDBUF, &opt_val, &opt_len); 返回之前在setsockopt()分配的tcp缓冲区大小的两倍。

(正如man 7 tcp所写:

Note that TCP actually allocates twice the size of the buffer requested in the setsockopt(2) call, and so a succeeding getsockopt(2) call will not return the same size of buffer as requested in the setsockopt(2) call. TCP uses the extra space for administrative purposes and internal kernel structures, Note that TCP actually allocates twice the size of the buffer requested in the setsockopt(2) call, and so a succeeding getsockopt(2) call will not return the same size of buffer as requested in the setsockopt(2) call. TCP uses the extra space for administrative purposes and internal kernel structures,

因此,如果我为buf_size = 256K执行setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (int *)&buf_size, sizeof(buf_size)) ,将在getsockopt()分配和返回512K

我想计算tcp buffer的当前字节数。 为此,我计算队列中每个数据包的长度( sk->sk_write_queue->len ),而skstruct sock *sk

碰巧有时返回的长度大于256K (例如,我得到294879个字节,比256K32735个字节)。

为什么会这样? 它是否包含"extra space for administrative purposes and internal kernel structures"作为getsockopt(.., SOL_SOCKET, SO_SNDBUF, ..)

谢谢。

由于512K实际上已经分配,​​缓冲294879字节并不令人惊讶。 在linux上,当您设置SO_SNDBUF时,内核只会将该数量加倍。

如果您希望套接字缓冲区为256k,请使用128k调用setsockopt()。

现在,使用的缓冲区不仅仅是用于您的数据,内核需要的skb和其他数据结构在这些缓冲区中分配,这是“管理开销”,这相当于多少开销取决于内核如何分割数据你给它 - 所以只为你从用户空间发送的数据保留256k的缓冲空间是一个相当困难的,如果不是几乎不可能完成的任务。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM