ping: Avoid reporting negative time statistics

Display a warning when the time goes back.

PR:		192417
Reviewed by:	markj
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D38480
This commit is contained in:
Jose Luis Duran 2023-02-09 20:38:54 -03:00 committed by Mark Johnston
parent 076b718d9e
commit ea6d169266
2 changed files with 36 additions and 2 deletions

View File

@ -1212,8 +1212,14 @@ pr_pack(char *buf, ssize_t cc, struct sockaddr_in *from, struct timespec *tv)
tv1.tv_sec = ntohl(tv32.tv32_sec);
tv1.tv_nsec = ntohl(tv32.tv32_nsec);
timespecsub(tv, &tv1, tv);
triptime = ((double)tv->tv_sec) * 1000.0 +
triptime = ((double)tv->tv_sec) * 1000.0 +
((double)tv->tv_nsec) / 1000000.0;
if (triptime < 0) {
warnx("time of day goes back (%.3f ms),"
" clamping time to 0",
triptime);
triptime = 0;
}
tsum += triptime;
tsumsq += triptime * triptime;
if (triptime < tmin)

View File

@ -268,6 +268,7 @@ def redact(output):
("hlim=[0-9]*", "hlim="),
("ttl=[0-9]*", "ttl="),
("time=[0-9.-]*", "time="),
("\(-[0-9\.]+[0-9]+ ms\)", "(- ms)"),
("[0-9\.]+/[0-9.]+", "/"),
]
for pattern, repl in pattern_replacements:
@ -1209,6 +1210,32 @@ def test_ping_46(self, expected):
},
id="_3_1_special_udp",
),
pytest.param(
{
"src": "192.0.2.1",
"dst": "192.0.2.2",
"icmp_type": 0,
"icmp_code": 0,
"special": "warp",
},
{
"returncode": 0,
"stdout": """\
PATTERN: 0x01
PING 192.0.2.2 (192.0.2.2): 56 data bytes
64 bytes from: icmp_seq=0 ttl= time= ms
--- 192.0.2.2 ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = /// ms
""",
"stderr": """\
ping: time of day goes back (- ms), clamping time to 0
""",
"redacted": True,
},
id="_0_0_special_warp",
),
]
@pytest.mark.parametrize("pinger_kargs, expected", pinger_testdata)
@ -1221,6 +1248,7 @@ def test_pinger(self, pinger_kargs, expected):
assert ping.returncode == expected["returncode"]
if expected["redacted"]:
assert redact(ping.stdout) == expected["stdout"]
assert redact(ping.stderr) == expected["stderr"]
else:
assert ping.stdout == expected["stdout"]
assert ping.stderr == expected["stderr"]
assert ping.stderr == expected["stderr"]