“ ss -s”的输出是什么意思

netstat实用程序已由ss实用程序代替并且通常将汇总(摘要)信息“ ss -s ”(或“ ss --summary ”)的输出用于监视需求。但是,每个显示的字段是什么意思?

# ss -s
Total: 15046 (kernel 16739)
TCP:   39306 (estab 11458, closed 25092, orphaned 110, synrecv 0, timewait 24929/0), ports 0

Transport Total     IP        IPv6
*	  16739     -         -        
RAW	  0         0         0        
UDP	  15        5         10       
TCP	  14214     1214      13000    
INET	  14229     1219      13010    
FRAG	  0         0         0        

事实证明,这里有一些微妙之处。

真理的源头就是源代码。iproute2 / misc / ss.c中找到了实用程序«ss»的源代码“ ss -s”的输出在print_summary函数中完成

在4.17.0之前的版本中,“ ss -s”的输出显示在上方。提交90ee99d,它开始是这样的:

# ss -s
Total: 15046
TCP:   39306 (estab 11458, closed 25092, orphaned 110, timewait 24929)

Transport Total     IP        IPv6
RAW	  0         0         0        
UDP	  15        5         10       
TCP	  14214     1214      13000    
INET	  14229     1219      13010    
FRAG	  0         0         0        

如您所见,“内核”字段已从“总计”行中删除。在“ TCP”行中,删除了“ synrecv”字段,保留了一个(第一个)数字“ timewait”,删除了“端口”字段。在“运输”表中,“ *”行被删除。提交的一般含义可以翻译为:“ 我们已从输出中删除了长期以来已中断的内容。”

什么坏了?


print_summary版本4.16.0中,使用结构slabstat,并通过调用get_slabstat进行填充如果尝试将get_slabstat调用转换为Shell命令,则会得到以下信息:

# egrep '^(sock|tcp_bind_bucket|tcp_tw_bucket|tcp_open_request|skbuff_head_cache)' /proc/slabinfo | awk '{ print $1, $2; }'

来自“ / proc / slabinfo”的键对应于“ slabstat”结构的字段:

struct slabstat {
	int socks;       // sock, net/core/sock.c:sk_init,   2.6.12
	int tcp_ports;   // tcp_bind_bucket, net/ipv4/tcp.c:tcp_init
	int tcp_tws;     // tcp_tw_bucket, net/ipv4/tcp.c:tcp_init,   2.6.14
	int tcp_syns;    // tcp_open_request, net/ipv4/tcp.c:tcp_init,   2.6.13
	int skbs;        // skbuff_head_cache, net/core/skbuff:skb_init,  
};

换句话说,在今天从“ / proc / slabinfo”获得的所有值中,只能获取值“ tcp_bind_bucket”,该值显示在“端口”字段中。我将从“ Linux中的TCP / IP体系结构,设计和实现”一书中对其进行描述。由S. Seth和MA Venkatesulu在2008年的IEEE计算机学会中:
This structure keeps information about the port number usage by sockets and the way the port number is being used. The information is useful enough to tell the new binding socket whether it can bind itself to a particular port number that is already in use. The data structure also keeps track of all the socket’s that are associated with this port number.
现在还是一样,只是一张照片(我的画家很一般):



您注意到什么奇怪的事吗?平板“袜子”(在2.6.12中删除)的值非零。解释非常简单-由于字符串比较特殊性,任何键以“ sock”开头的数字都将进入字段值。在我的情况下,这是与“ sockfs”相关的“ net / socket.c:init_inodecache”中的“ sock_inode_cache”-包含“ socket struct”的“ inodes”列表(也许不是该实用程序的作者想要的):

# egrep '^sock' /proc/slabinfo | awk '{ print $1, $2; }'
sock_inode_cache 16739

好吧,仅由于内核构建选项而缺少“ tcp_bind_bucket”(因此,“ ports”字段始终具有值“ 0”)。

输出描述“ ss -s”


在深入进行复杂的字段计算之前,有必要刷新内存中套接字的状态(已建立,CLOSE-WAIT,TIME-WAIT等)。对于那些已经忘记的人,请帮助Wiki:RUEN

该实用程序的输出基于从文件中获得的值:

# cat /proc/net/sockstat
sockets: used 15046
TCP: inuse 1205 orphan 111 tw 24952 alloc 14368 mem 5890
UDP: inuse 5 mem 86
UDPLITE: inuse 0
RAW: inuse 0
FRAG: inuse 0 memory 0

# cat /proc/net/sockstat6
TCP6: inuse 13000
UDP6: inuse 10
UDPLITE6: inuse 0
RAW6: inuse 0
FRAG6: inuse 0 memory 0

# egrep '^Tcp:' /proc/net/snmp
Tcp: RtoAlgorithm RtoMin RtoMax ... AttemptFails EstabResets CurrEstab ...
Tcp: 1 200 120000 ... 1348218 4095008 11458 ...

从``net / sockstat''的内容计算出的值(以简化图片中的感知):



  • 总计 -在除TIME_WAIT之外的任何状态下系统中的套接字总数(包括unix套接字);
  • TCP: -任何状态下TCP套接字(包括IPv6)的总数,由于所谓的tw套接字与alloc套接字分开死行 ”-在过渡到CLOSED状态之前不能使用它们。
    • orphaned — «» TCP (, );
    • timewait — TCP TIME_WAIT;
    • inuse TCP, UDP, RAWv4 CLOSED TIME_WAIT;
    • inuse FRAG (bool) — , memory — ( mem, ).

通过“网/ sockstat6”的内容计算值:



似乎一切都在这里很简单- “ 交通运输/总 ”是“的值加总求和一个IP ”和“ IPv6的 ”。

从``net / snmp''的内容计算出的值。根据RFC-4022,“ tcpCurrEstab ”是“当前状态为ESTABLISHED或CLOSE-WAIT的TCP连接数”。



最后一个“ closed字段是分配的套接字和处于TIME_WAIT状态的套接字的总和减去处于任何未闭合状态的TCPv4和TCPv6套接字的总和:



PS并且不要忘记在图形上签名。

All Articles