【问题标题】:In /proc/sockstat which TCP states count as 'inuse' and which count as 'alloc'?在 /proc/sockstat 中,哪些 TCP 状态算作“正在使用”,哪些算作“分配”?
【发布时间】:2019-03-28 19:44:31
【问题描述】:

例如,在 /proc/net/sockstat 中,CLOSE_WAIT 中的 TCP 套接字是否算作“inuse”或“alloc”?

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

但是我看不出套接字与被分配(分配)与“使用”相反的区别

[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”?

【问题讨论】:

    标签: linux networking tcp kernel


    【解决方案1】:

    以上哪些算“inuse”,哪些算“alloc”?

    通过定位sockstat_seq_show,您已经接近答案了——我们可以看到'inuse' 是sock_prot_inuse_get(net, &tcp_prot) 的值,'alloc' 是proto_sockets_allocated_sum_positive(&tcp_prot) 的值。现在,沿着调用链往下走并不总是那么容易,但如果没记错的话,我会得出以下结论。

    1. 'alloc' - 底部是percpu_counter tcp_sockets_allocated 的总和,在tcp_init_sock() 中递增;那里的套接字状态被初始化为TCP_CLOSE。无论套接字在其存在期间经历什么状态变化,“alloc”都不依赖于 - 所有 TCP 状态都算作“alloc”。
    2. 'inuse' - 这是(每个 CPU)计数器 net->core.inuse 或 prot_inuse(在这种情况下为 TCP)的总和,它们基本上通过调用 sock_prot_inuse_add(…, 1) 来递增和递减。 (…, -1) 在inet_hash() 分别。 inet_unhash()。 inet_hash() 中的条件是if (sk->sk_state != TCP_CLOSE),所以除了TCP_CLOSE 之外的所有TCP 状态都算作'inuse'。

    我认为这在理论上意味着任何处于 >= TCP_CLOSE 状态的套接字都不算作“正在使用”

    在我看来不可能是这样,因为 TCP_LISTEN > TCP_CLOSE,并且处于 TCP_LISTEN 状态的套接字肯定会算作“正在使用”,这可以从 e 中看出。 g.

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

    【讨论】:

    • 我认为这只是意味着 sk->sk_state != TCP_CLOSE 可以增加计数器的套接字。 inet_unhash() 仍然可以被调用并减少该计数器。当我们调用 inet_hash() 和 inet_unhash() 函数时,我们正在使用内核的网络连接哈希表,它定义了以下不变量(回忆可能状态的枚举) TCP_ESTABLISHED sk_state sk_state > TCP_CLOSE TCP_CLOSE_WAIT 时调用。我认为这意味着理论上任何处于 >= TCP_CLOSE 状态的套接字都不算作“使用中”
    • 你从哪里得到不变量? - 我扩展了我的答案。
    • 你是对的,我在服务器上测试过,CLOSE_WAIT 中的套接字在 sockstat 中肯定算作“正在使用”。不变量来自 inet_hashtables.h 中的 cmets elixir.bootlin.com/linux/v4.8/source/include/net/…
    • 解释得很清楚,但是从那里我不明白为什么我们可以在这里看到 tw > alloc ?
    • @Armali 好的,谢谢你的关注!
    猜你喜欢
    • 1970-01-01
    • 2018-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-02
    • 1970-01-01
    • 2022-07-29
    相关资源
    最近更新 更多