Linux ping 实际上每秒不发送 1 个数据包

Linux ping 实际上每秒不发送 1 个数据包

我遇到了一些奇怪的行为。在拥塞的无线网络中,我运行

$ ping google.com

我预计每秒会发送一个数据包,并且只有其中一些数据包会以不同的往返时间返回,大多数情况下都是这样的。但在这个非常拥挤的无线网络上,我看到了类似这样的情况:

PING google.com (74.125.224.228) 56(84) bytes of data.
64 bytes from lax04s08-in-f4.1e100.net (74.125.224.228): icmp_req=1 ttl=53 time=193 ms
64 bytes from lax04s08-in-f4.1e100.net (74.125.224.228): icmp_req=2 ttl=53 time=238 ms
64 bytes from lax04s08-in-f4.1e100.net (74.125.224.228): icmp_req=3 ttl=53 time=96.8 ms
64 bytes from lax04s08-in-f4.1e100.net (74.125.224.228): icmp_req=4 ttl=53 time=12.9 ms
64 bytes from lax04s08-in-f4.1e100.net (74.125.224.228): icmp_req=5 ttl=53 time=219 ms
64 bytes from lax04s08-in-f4.1e100.net (74.125.224.228): icmp_req=9 ttl=53 time=1105 ms
64 bytes from lax04s08-in-f4.1e100.net (74.125.224.228): icmp_req=8 ttl=53 time=2339 ms
64 bytes from lax04s08-in-f4.1e100.net (74.125.224.228): icmp_req=17 ttl=53 time=881 ms
64 bytes from lax04s08-in-f4.1e100.net (74.125.224.228): icmp_req=18 ttl=53 time=1200 ms
...

--- google.com ping statistics ---
57 packets transmitted, 41 received, 28% packet loss, time 113307ms
rtt min/avg/max/mdev = 5.773/447.217/2339.271/496.011 ms, pipe 3

关键的是:

57 packets transmitted, 41 received, 28% packet loss, time 113307ms

如您所见,ping 运行了大约 113 秒,但只发送了 57 个数据包。我已经多次看到这种情况:

$ ping google.com
PING google.com (74.125.224.232) 56(84) bytes of data.
64 bytes from lax04s08-in-f8.1e100.net (74.125.224.232): icmp_req=1 ttl=53 time=6.98 ms
64 bytes from lax04s08-in-f8.1e100.net (74.125.224.232): icmp_req=2 ttl=53 time=5.71 ms
64 bytes from lax04s08-in-f8.1e100.net (74.125.224.232): icmp_req=3 ttl=53 time=4.47 ms
64 bytes from lax04s08-in-f8.1e100.net (74.125.224.232): icmp_req=4 ttl=53 time=5.75 ms
64 bytes from lax04s08-in-f8.1e100.net (74.125.224.232): icmp_req=5 ttl=53 time=6.94 ms
64 bytes from lax04s08-in-f8.1e100.net (74.125.224.232): icmp_req=6 ttl=53 time=14.2 ms
64 bytes from lax04s08-in-f8.1e100.net (74.125.224.232): icmp_req=7 ttl=53 time=6.22 ms
64 bytes from lax04s08-in-f8.1e100.net (74.125.224.232): icmp_req=8 ttl=53 time=11.8 ms
64 bytes from lax04s08-in-f8.1e100.net (74.125.224.232): icmp_req=9 ttl=53 time=4.29 ms
64 bytes from lax04s08-in-f8.1e100.net (74.125.224.232): icmp_req=10 ttl=53 time=5.43 ms
64 bytes from lax04s08-in-f8.1e100.net (74.125.224.232): icmp_req=11 ttl=53 time=5.02 ms
64 bytes from lax04s08-in-f8.1e100.net (74.125.224.232): icmp_req=12 ttl=53 time=4.89 ms
^C64 bytes from lax04s08-in-f8.1e100.net (74.125.224.232): icmp_req=13 ttl=53 time=7.36 ms

--- google.com ping statistics ---
13 packets transmitted, 13 received, 0% packet loss, time 60262ms
rtt min/avg/max/mdev = 4.299/6.865/14.258/2.838 ms

这个更奇怪,因为所有的 RTT 都是合理的,只是数据包没有快速发送出去。有人能解释一下吗?我在 Debian 上测试(wheezy),还有一些统计数据:

$ ping -V
ping utility, iputils-sss20101006

Linux 3.0.0-1-amd64 #1 SMP Sat Aug 27 16:21:11 UTC 2011 x86_64 GNU/Linux

03:00.0 Network controller: Intel Corporation Centrino Ultimate-N 6300 (rev 35)

答案1

看起来您有一个版本,ping它会对每个收到的数据包进行 DNS 查找。由于 DNS UDP 数据包必须像 ping 数据包一样穿过相同的拥塞网络,因此数据包可能会被丢弃。由于超时和重试,DNS 请求可能需要很长时间才能返回数据。等待 DNS 响应所花费的时间会延迟下一个 ping 数据包的发送,因为您的ping是单线程的,并且不使用异步计时器来驱动每个 ping。

如果我的诊断正确,那么添加-n应该ping可以消除延误。

相关内容