diff --git a/statime-linux/src/main.rs b/statime-linux/src/main.rs index 12c0f3411..2e90d709e 100644 --- a/statime-linux/src/main.rs +++ b/statime-linux/src/main.rs @@ -987,7 +987,7 @@ async fn handle_actions( link_local, } => { // send timestamp of the send - let time = event_socket + let time = match event_socket .send_to( data, if link_local { @@ -997,7 +997,14 @@ async fn handle_actions( }, ) .await - .expect("Failed to send event message"); + { + Ok(time) => time, + // e.g. the interface is down: drop the message, the announce receipt timeout copes + Err(e) => { + log::warn!("Failed to send event message: {e}"); + continue; + } + }; // anything we send later will have a later pending (send) timestamp if let Some(time) = time { @@ -1008,7 +1015,7 @@ async fn handle_actions( } } PortAction::SendGeneral { data, link_local } => { - general_socket + if let Err(e) = general_socket .send_to( data, if link_local { @@ -1018,7 +1025,9 @@ async fn handle_actions( }, ) .await - .expect("Failed to send general message"); + { + log::warn!("Failed to send general message: {e}"); + } } PortAction::ResetAnnounceTimer { duration } => { timers.port_announce_timer.as_mut().reset(duration); @@ -1062,7 +1071,7 @@ async fn handle_actions_ethernet( link_local, } => { // send timestamp of the send - let time = socket + let time = match socket .send_to( data, EthernetAddress::new( @@ -1080,7 +1089,14 @@ async fn handle_actions_ethernet( ), ) .await - .expect("Failed to send event message"); + { + Ok(time) => time, + // e.g. the interface is down: drop the message, the announce receipt timeout copes + Err(e) => { + log::warn!("Failed to send event message: {e}"); + continue; + } + }; // anything we send later will have a later pending (send) timestamp if let Some(time) = time { @@ -1091,7 +1107,7 @@ async fn handle_actions_ethernet( } } PortAction::SendGeneral { data, link_local } => { - socket + if let Err(e) = socket .send_to( data, EthernetAddress::new( @@ -1109,7 +1125,9 @@ async fn handle_actions_ethernet( ), ) .await - .expect("Failed to send general message"); + { + log::warn!("Failed to send general message: {e}"); + } } PortAction::ResetAnnounceTimer { duration } => { timers.port_announce_timer.as_mut().reset(duration); diff --git a/statime-linux/tests/link_down.py b/statime-linux/tests/link_down.py new file mode 100644 index 000000000..289755195 --- /dev/null +++ b/statime-linux/tests/link_down.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python3 +"""Linux/root integration check: build statime, then run this with its binary path. + +All interface changes occur inside a fresh network namespace. The daemon uses +only a virtual monotonic clock and never exports a clock socket. +""" +import os +from pathlib import Path +import socket +import subprocess +import sys +import tempfile +import time + + +def ip(*args): + subprocess.run(["ip", *args], check=True) + + +def check(binary, mode): + ip("link", "add", "ptptest", "type", "dummy") + try: + ip("link", "set", "ptptest", "address", "00:11:22:33:44:55") + ip("addr", "add", "192.0.2.1/24", "dev", "ptptest") + ip("link", "set", "ptptest", "up") + with tempfile.TemporaryDirectory(prefix="statime-link-down-") as tmp: + config = Path(tmp) / "config.toml" + config.write_text(f'''loglevel = "debug" +virtual-system-clock = true +virtual-system-clock-base = "monotonic_raw" +usrvclock-export = false +[[port]] +interface = "ptptest" +network-mode = "{mode}" +hardware-clock = "none" +protocol-version = "PTPv2" +announce-interval = -2 +sync-interval = -2 +''') + with (Path(tmp) / "daemon.log").open("w+") as log: + daemon = subprocess.Popen([binary, "-c", str(config)], stdout=log, stderr=log) + try: + time.sleep(4) + ip("link", "set", "ptptest", "down") + time.sleep(3) + ip("link", "set", "ptptest", "up") + packets = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(3)) + packets.bind(("ptptest", 0)) + packets.settimeout(0.2) + time.sleep(3) + recovered = False + try: + deadline = time.monotonic() + 1 + while time.monotonic() < deadline: + frame = packets.recv(65536) + if frame[12:14] == b"\x88\xf7": + recovered = True + elif frame[12:14] == b"\x08\x00" and len(frame) > 38 and frame[23] == 17: + udp = 14 + (frame[14] & 15) * 4 + if int.from_bytes(frame[udp + 2:udp + 4], "big") in (319, 320): + recovered = True + except socket.timeout: + pass + finally: + packets.close() + log.flush() + log.seek(0) + output = log.read() + assert daemon.poll() is None, output + assert "panicked" not in output, output + assert "Failed to send event message" in output, output + assert "Failed to send general message" in output, output + assert recovered, "no PTP transmission after link recovery\n" + output + print(f"{mode}: event/general send failures observed; no panic; PTP packets resume after link up") + finally: + daemon.terminate() + daemon.wait(timeout=5) + finally: + ip("link", "delete", "ptptest") + + +if __name__ == "__main__": + if len(sys.argv) == 2: + subprocess.run(["unshare", "--net", sys.executable, str(Path(__file__).resolve()), + "--inside", str(Path(sys.argv[1]).resolve()), + str(os.stat("/proc/self/ns/net").st_ino)], check=True) + elif len(sys.argv) == 4 and sys.argv[1] == "--inside": + assert os.stat("/proc/self/ns/net").st_ino != int(sys.argv[3]), "network namespace was not isolated" + for mode in ("ipv4", "ethernet"): + check(sys.argv[2], mode) + else: + sys.exit(f"usage: {sys.argv[0]} /path/to/statime")