可能的内部套接字状态列表,来自/proc

28
我想了解/proc/net/tcpst列可能的取值。我认为st列对应于netstat(8)ss(8)中的STATE列。
我已经成功识别出三个代码:
sl  local_address rem_address   st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode
0: 0100007F:08A0 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 7321 1 ffff81002f449980 3000 0 0 2 -1                     
1: 00000000:006F 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 6656 1 ffff81003a30c080 3000 0 0 2 -1                     
2: 00000000:0272 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 6733 1 ffff81003a30c6c0 3000 0 0 2 -1                     
3: 0100007F:0277 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 7411 1 ffff81002f448d00 3000 0 0 2 -1                     
4: 0100007F:0019 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 7520 1 ffff81002f4486c0 3000 0 0 2 -1                     
5: 0100007F:089F 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 7339 1 ffff81002f449340 3000 0 0 2 -1           
6: 0100007F:E753 0100007F:0016 01 00000000:00000000 02:000AFA92 00000000   500        0 18198 2 ffff81002f448080 204 40 20 2 -1                   
7: 0100007F:E752 0100007F:0016 06 00000000:00000000 03:000005EC 00000000     0        0 0 2 ffff81000805dc00                                      

以上显示:

  • 在sl 0行:监听tcp/2208端口。 st = 0A = LISTEN
  • 在sl 6行:已建立tcp/22的会话。 st = 01 = ESTABLISHED
  • 在sl 7行:ssh注销后处于TIME_WAIT状态的套接字。没有inode。 st = 06 = TIME_WAIT

有人可以详细解释一下这个列表吗? proc(5)手册在此主题上非常简略,指出:

   /proc/net/tcp
          Holds a dump of the TCP socket table. Much of the information is not of use apart from debugging. The "sl" value is the kernel hash slot for the socket, the "local address" is  the  local  address  and
          port  number pair.  The "remote address" is the remote address and port number pair (if connected). ’St’ is the internal status of the socket.  The ’tx_queue’ and ’rx_queue’ are the outgoing and incom-
          ing data queue in terms of kernel memory usage.  The "tr", "tm->when", and "rexmits" fields hold internal information of the kernel socket state and are only useful  for  debugging.   The  "uid"  field
          holds the effective UID of the creator of the socket.

另外需要说明的是,上面的/proc/net/tcp输出显示了几个正在监听的进程(2208、62、111等),但是我无法看到在tcp/22上的监听TCP连接,尽管已经显示了已建立和TIME_WAIT状态。是的,在/proc/net/tcp6中可以看到它们,但是它们不应该也出现在/proc/net/tcp中吗?与仅绑定ipv4的应用程序显示的netstat输出不同。例如:

tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      4231/portmap        
tcp        0      0 :::22                       :::*                        LISTEN      4556/sshd           

非常感谢,-Andrew


那个链接已经失效了。我认为它可能链接到这里:http://serverfault.com/questions/21657/semantics-of-and-0-0-0-0-in-dual-stack-oses - user314104
1个回答

34

它们应该与Linux内核源代码中的./include/net/tcp_states.h中的枚举匹配:

enum {
    TCP_ESTABLISHED = 1,
    TCP_SYN_SENT,
    TCP_SYN_RECV,
    TCP_FIN_WAIT1,
    TCP_FIN_WAIT2,
    TCP_TIME_WAIT,
    TCP_CLOSE,
    TCP_CLOSE_WAIT,
    TCP_LAST_ACK,
    TCP_LISTEN,
    TCP_CLOSING,    /* Now a valid state */

    TCP_MAX_STATES  /* Leave at the end! */
};

关于你的第二个问题,你确定在0.0.0.0:22上没有sshd在监听吗?如果没有,我怀疑你看到的与v4-mapped-on-v6 sockets有关,可以参考man 7 ipv6


谢谢,不知道为什么在 grepping 源代码时我没有注意到这个。我想我当时试图匹配 EST。肯定没有 0016 上的服务,所以它必须是你提到的 v4 到 v6 映射。对我来说是新的。 - The_Viper
另外,我不确定如何从 tcp_states.h 中获取十六进制值。我只能看到 ESTABLISHED 有一个像你上面粘贴的值,但其他状态是如何工作和匹配的呢? - The_Viper
3
这是一个枚举,从1开始。例如,TCP_SYN_SENT是2,TCP_LISTEN是10。在十进制中,10是“A”,在十六进制中表示为“0A”,这也是您在/proc/net/tcp中看到的值。 - nos

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接