linux下查看tcp状态的源头

linux中,各种tcp连接的状态存储在 /proc/net/tcp 文件中

其中st列就是tcp的各种状态,那么这个值代表什么意思呢? 有网友从内核源码里找到了这个

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! */
};
那么各种状态对应的数字就清楚了.

然后我们以dstat为例看看他是怎么显示的tcp状态

class dstat_tcp(dstat):
    def __init__(self):
        self.name = 'tcp sockets'
        self.nick = ('lis', 'act', 'syn', 'tim', 'clo')
        self.vars = ('listen', 'established', 'syn', 'wait', 'close')
        self.type = 'd'
        self.width = 4
        self.scale = 100
        self.open('/proc/net/tcp', '/proc/net/tcp6')

    def extract(self):
        for name in self.vars: self.val[name] = 0
        for l in self.splitlines():
            if len(l) < 12: continue
            ### 01: established, 02: syn_sent,  03: syn_recv, 04: fin_wait1,
            ### 05: fin_wait2,   06: time_wait, 07: close,    08: close_wait,
            ### 09: last_ack,    0A: listen,    0B: closing
            if l[3] in ('0A',): self.val['listen'] += 1 
            elif l[3] in ('01',): self.val['established'] += 1 
            elif l[3] in ('02', '03', '09',): self.val['syn'] += 1 
            elif l[3] in ('06',): self.val['wait'] += 1 
            elif l[3] in ('04', '05', '07', '08', '0B',): self.val['close'] += 1



从程序中看到,dstat将11种tcp状态分了5类做了下合并显示出来.


你可能感兴趣的:(linux下查看tcp状态的源头)