偶然将UDP的接收端关闭,测试性能的时候,查看数据口和lo的bps情况,发现ifconfig里lo的bps比平常大了一倍左右,应该与其机制有关,干脆简单测试一把
1:直接ping本地
[lihui@master script]$ ping 127.0.0.1
2:脚本打印ifconfig的输出结果,ping 3次结束掉,此时RX的包数每次多两个包
[lihui@master script]$ ./nic_traffic.pl lo #################################################################################### used memory: 3.523G cached: 2.100G swap: 0G Nic lo bps: 1.312 bits/s packets: 5879 dropped: 0 pkts/s #################################################################################### used memory: 3.523G cached: 2.100G swap: 0G Nic lo bps: 1.312 bits/s packets: 5881 dropped: 0 pkts/s #################################################################################### used memory: 3.523G cached: 2.100G swap: 0G Nic lo bps: 1.312 bits/s packets: 5883 dropped: 0 pkts/s ####################################################################################
3:同时监听lo口,将所有包抓下来
[lihui@master script]$ sudo tcpdump -i lo -w ping.pcap
4:查看一下包信息
[lihui@master script]$ tshark -r ping.pcap 1 0.000000 127.0.0.1 -> 127.0.0.1 ICMP 98 Echo (ping) request id=0x1870, seq=1/256, ttl=64 2 0.000026 127.0.0.1 -> 127.0.0.1 ICMP 98 Echo (ping) reply id=0x1870, seq=1/256, ttl=64 (request in 1) 3 0.999233 127.0.0.1 -> 127.0.0.1 ICMP 98 Echo (ping) request id=0x1870, seq=2/512, ttl=64 4 0.999255 127.0.0.1 -> 127.0.0.1 ICMP 98 Echo (ping) reply id=0x1870, seq=2/512, ttl=64 (request in 3) 5 1.999209 127.0.0.1 -> 127.0.0.1 ICMP 98 Echo (ping) request id=0x1870, seq=3/768, ttl=64 6 1.999232 127.0.0.1 -> 127.0.0.1 ICMP 98 Echo (ping) reply id=0x1870, seq=3/768, ttl=64 (request in 5)
可见,我一共ping了3次,从上面脚本来看,每次多捕获了两个数据包,从这里分析数据包的结果,每次ping都有一个request和一个reply
5:再看UDP的情况
UDP接收程序关闭的时候,监听端口进程关闭,包信息如下
6178 1.139939 127.0.0.1 -> 127.0.0.1 CFLOW 286 total: 1 (v9) record 6179 1.139943 127.0.0.1 -> 127.0.0.1 ICMP 314 Destination unreachable (Port unreachable) 6180 1.139957 127.0.0.1 -> 127.0.0.1 CFLOW 486 total: 1 (v9) record 6181 1.139961 127.0.0.1 -> 127.0.0.1 ICMP 514 Destination unreachable (Port unreachable) 6182 1.139986 127.0.0.1 -> 127.0.0.1 CFLOW 598 total: 1 (v9) record 6183 1.139991 127.0.0.1 -> 127.0.0.1 ICMP 590 Destination unreachable (Port unreachable)
从这里可以看到,由于监听端口的进程被我关闭了,因此虽然UDP发出去了,但是没有被接收,因此local就产生了一个相当于报错信息的ICMP包,这也就是为什么bps会增大一倍的原因
将监听端口程序打开,继续抓包分析,UDP全都成功发出和接收,少了一半的ICMP,所以lo的流量自然少了一半左右~!
4125 1.072155 127.0.0.1 -> 127.0.0.1 CFLOW 246 total: 1 (v9) record 4126 1.072169 127.0.0.1 -> 127.0.0.1 CFLOW 410 total: 1 (v9) record 4127 1.072184 127.0.0.1 -> 127.0.0.1 CFLOW 502 total: 1 (v9) record 4128 1.072200 127.0.0.1 -> 127.0.0.1 CFLOW 694 total: 1 (v9) record 4129 1.072216 127.0.0.1 -> 127.0.0.1 CFLOW 622 total: 1 (v9) record