linux:netstat监听队列长度

Art*_*m G 10 networking linux freebsd netstat socket

有没有办法在 Linux 下查看侦听套接字上的队列长度,与netstat -LFreeBSD 的输出相同?即你可以在netstat -L输出中看到 X/Y/Z ,但 Linux 下的 netstat 不支持-L标志。

Ale*_*der 13

让我们看看源代码,因为它是开源世界中最好的文档。

net/ipv4/tcp_diag.c:

if (sk->sk_state == TCP_LISTEN) {
    r->idiag_rqueue = sk->sk_ack_backlog;
    r->idiag_wqueue = sk->sk_max_ack_backlog;
} else {
    r->idiag_rqueue = max_t(int, tp->rcv_nxt - tp->copied_seq, 0);
    r->idiag_wqueue = tp->write_seq - tp->snd_una;
}
Run Code Online (Sandbox Code Playgroud)

我们可以在 unix 域套接字 net/unix/diag.c 中看到同样的事情:

if (sk->sk_state == TCP_LISTEN) {
    rql.udiag_rqueue = sk->sk_receive_queue.qlen;
    rql.udiag_wqueue = sk->sk_max_ack_backlog;
} else {
    rql.udiag_rqueue = (u32) unix_inq_len(sk);
    rql.udiag_wqueue = (u32) unix_outq_len(sk);
}
Run Code Online (Sandbox Code Playgroud)

所以。

如果建立了套接字,则 Recv-Q 和 Send-Q 表示文档中描述的字节。
如果套接字正在侦听,则 Recv-Q 表示当前队列大小,Send-Q 表示已配置的 backlog。

深入了解 mans 让我们在sock_diag(7) 中找到以下内容

      UDIAG_SHOW_RQLEN
             The attribute reported in answer to this request is
             UNIX_DIAG_RQLEN.  The payload associated with this
             attribute is represented in the following structure:

                 struct unix_diag_rqlen {
                     __u32 udiag_rqueue;
                     __u32 udiag_wqueue;
                 };

             The fields of this structure are as follows:

             udiag_rqueue
                    For listening sockets: the number of pending
                    connections.  The length of the array associated
                    with the UNIX_DIAG_ICONS response attribute is
                    equal to this value.

                    For established sockets: the amount of data in
                    incoming queue.

             udiag_wqueue
                    For listening sockets: the backlog length which
                    equals to the value passed as the second argu?
                    ment to listen(2).

                    For established sockets: the amount of memory
                    available for sending.
Run Code Online (Sandbox Code Playgroud)

换句话说,ss -ln是你唯一需要的命令


Art*_*m G 10

ss -l 显示正确的 Recv-Q Send-Q。


qua*_*nta 1

awk可以帮助:

netstat -ntp | awk '{ if ($6 == "ESTABLISHED" && $7 == "-") arrQueue[$4] += 1; } END { for (service in arrQueue) print service" "arrQueue[service] }'

来源: http: //mysyslog.ru/posts/633