简体   繁体   English

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

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

It is known that getsockopt(sock, SOL_SOCKET, SO_SNDBUF, &opt_val, &opt_len); 众所周知, getsockopt(sock, SOL_SOCKET, SO_SNDBUF, &opt_val, &opt_len); returns the twice of the size of the tcp buffer that was allocated before in setsockopt() . 返回之前在setsockopt()分配的tcp缓冲区大小的两倍。

(As written in man 7 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, ) 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,

So if I do setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (int *)&buf_size, sizeof(buf_size)) for buf_size = 256K , 512K will be allocated and returned in getsockopt() . 因此,如果我为buf_size = 256K执行setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (int *)&buf_size, sizeof(buf_size)) ,将在getsockopt()分配和返回512K

I want to count the current amount of bytes in tcp buffer . 我想计算tcp buffer的当前字节数。 for that I'm counting the length of each packet in the queue ( sk->sk_write_queue->len ) while sk is struct sock *sk . 为此,我计算队列中每个数据包的长度( sk->sk_write_queue->len ),而skstruct sock *sk

It happens that there are times which the length returned is bigger than 256K . 碰巧有时返回的长度大于256K (for instance, I got 294879 bytes that is 32735 bytes more than 256K ). (例如,我得到294879个字节,比256K32735个字节)。

Why does it happens? 为什么会这样? Does it includes "extra space for administrative purposes and internal kernel structures" as the getsockopt(.., SOL_SOCKET, SO_SNDBUF, ..) ? 它是否包含"extra space for administrative purposes and internal kernel structures"作为getsockopt(.., SOL_SOCKET, SO_SNDBUF, ..)

Thanks. 谢谢。

Since 512K actually gets allocated, buffering 294879 bytes is not that surprising. 由于512K实际上已经分配,​​缓冲294879字节并不令人惊讶。 On linux, when you set the SO_SNDBUF, the kernel simply doubles that amount. 在linux上,当您设置SO_SNDBUF时,内核只会将该数量加倍。

If you want the socket buffer to be 256k, call setsockopt() with 128k. 如果您希望套接字缓冲区为256k,请使用128k调用setsockopt()。

Now, the buffers used are not purely for your data, the skb's and other data structures the kernel needs are allocated in these buffers, which is the "administrative overhead", how much overhead this amounts to depends on how the kernel slices up the data you give it - so reserving exactly 256k of buffer space for only the data you send from userspace is a rather hard if not practically impossible task. 现在,使用的缓冲区不仅仅是用于您的数据,内核需要的skb和其他数据结构在这些缓冲区中分配,这是“管理开销”,这相当于多少开销取决于内核如何分割数据你给它 - 所以只为你从用户空间发送的数据保留256k的缓冲空间是一个相当困难的,如果不是几乎不可能完成的任务。

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

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