How to query tcp buffer sizes for a certain socket?

https://access.redhat.com/discussions/3624151

 

The -m switch of ss gives socket memory info.

# ss -ntmp
State      Recv-Q Send-Q Local Address:Port  Peer Address:Port
ESTAB      0      0      10.xx.xx.xxx:22     10.yy.yy.yyy:12345  users:(("sshd",pid=1442,fd=3))
         skmem:(r0,rb369280,t0,tb87040,f4096,w0,o0,bl0,d92)

 

Here we can see this socket has Receive Buffer 369280 bytes, and Transmit Buffer 87040 bytes.

Keep in mind the kernel will double any socket buffer allocation for overhead. So a process asks for 256 KiB buffer with setsockopt(SO_RCVBUF) then it will get 512 KiB buffer space. This is described on man 7 tcp.

The entire print format of ss -m is given in the source:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
printf(" skmem:(r%u,rb%u,t%u,tb%u,f%u,w%u,o%u",
       skmeminfo[SK_MEMINFO_RMEM_ALLOC],
       skmeminfo[SK_MEMINFO_RCVBUF],
       skmeminfo[SK_MEMINFO_WMEM_ALLOC],
       skmeminfo[SK_MEMINFO_SNDBUF],
       skmeminfo[SK_MEMINFO_FWD_ALLOC],
       skmeminfo[SK_MEMINFO_WMEM_QUEUED],
       skmeminfo[SK_MEMINFO_OPTMEM]);
 
if (RTA_PAYLOAD(tb[attrtype]) >=
        (SK_MEMINFO_BACKLOG + 1) * sizeof(__u32))
        printf(",bl%u", skmeminfo[SK_MEMINFO_BACKLOG]);
 
if (RTA_PAYLOAD(tb[attrtype]) >=
        (SK_MEMINFO_DROPS + 1) * sizeof(__u32))
        printf(",d%u", skmeminfo[SK_MEMINFO_DROPS]);
 
printf(")");

 I expect that the ALLOC values are only used when there is outstanding data - either data in a receive buffer waiting for an application to recv(), or un-ACKed sent data. You could test these with Ctrl+z to put a receiver to sleep, and firewalls to block ACKs. It's easier to use nc than iperf for that sort of testing. Socket option memory is rarely used.


 

posted @   张同光  阅读(160)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
历史上的今天:
2020-12-28 05---二叉树---20195106023---王亚威.c
2020-12-28 05---二叉树---20195106043---方传祥.c
2020-12-28 05---二叉树---20195106006---陈辉.c
2020-12-28 05---二叉树---20195106064---陈昕.c
2020-12-28 05---二叉树---20195106100---李遂勋.c
点击右上角即可分享
微信分享提示