在 /proc/sockstat 中,哪些 TCP 状态算作 'inuse',哪些算作 'alloc'?

In /proc/sockstat which TCP states count as 'inuse' and which count as 'alloc'?

例如,在 /proc/net/sockstat 中,CLOSE_WAIT 中的 TCP 套接字算作 'inuse' 还是 'alloc'?

在内核源代码 net/ipv4/proc.c 中,我看到 sockstat_seq_show 在从 /proc/net/sockstat 获取信息时被调用。

但是我看不出有什么区别套接字被分配 (alloc) 相对于 'inuse'

[me@myhostname ~]$ cat /proc/net/sockstat
sockets: used 481
TCP: inuse 52 orphan 1 tw 66 alloc 62 mem 12
UDP: inuse 11 mem 5
UDPLITE: inuse 0
RAW: inuse 0
FRAG: inuse 0 memory 0

在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_NEW_SYN_RECV,

    TCP_MAX_STATES  /* Leave at the end! */

};

以上哪个算作'inuse',哪个算作'alloc'?

Which of the above count as 'inuse' and which count as 'alloc' ?

您已经通过定位 sockstat_seq_show 接近答案 - 我们可以看到 'inuse' 是 sock_prot_inuse_get(net, &tcp_prot), and 'alloc' is the value of proto_sockets_allocated_sum_positive(&tcp_prot) 的值。现在要进一步了解调用链并不总是那么容易,但如果我没记错的话,我会得出以下结论。

  1. 'alloc' - 这在底部是 percpu_counter tcp_sockets_allocated, which gets incremented in tcp_init_sock() 的总和;套接字状态被初始化为 TCP_CLOSE。无论套接字在其存在期间经历什么状态更改,'alloc' 都不依赖于 - 所有 TCP 状态 算作 'alloc'.
  2. 'inuse' - 这是(每个 CPU)计数器 net->core.inuseprot_inuse(在这种情况下对于 TCP)的总和,它实际上是递增的并通过 sock_prot_inuse_add(…, 1) 的调用减少。 (…, -1)inet_hash() resp. inet_unhash() 中。 inet_hash()中的条件为if (sk->sk_state != TCP_CLOSE),所以除TCP_CLOSE外的所有TCP状态都算作'inuse'.

I think this means in theory any socket in a state >= TCP_CLOSE is not counted as 'inuse'

在我看来不可能如此,因为 TCP_LISTEN > TCP_CLOSE,并且处于 TCP_LISTEN 状态的套接字肯定会 算作 'inuse',用e可以看出。 g.

(cd /proc/net; cat sockstat; nc -l 8888& sleep 1; cat sockstat; kill $!; cat sockstat)|grep TCP