Prioritize live traffic recovery over a stalled carrier comparison
When the incumbent went down during a background dial, an unrelated healthy third carrier could wait up to 30 seconds. Restore the configured fallback order immediately if another carrier is eligible. Cancel the pending dial and invalidate its result so a late link cannot overwrite recovery. Keep the comparison alive if it is the only available fallback. Reproduced the timeout with two loopback agents, a real WG carrier, a stalled TLS handshake and QUIC. All four carrier integration tests passed after the change, including bidirectional delivery over the recovered path. Workspace formatting and Clippy passed on macOS.
This commit is contained in:
1 parent
972b98363d
commit
15a4e45710
3 files changed
+196
-81
No files matched your search
@@ -2063,11 +2063,19 @@ impl Runtime {
|
|||||||
self.review_comparisons();
|
self.review_comparisons();
|
||||||
let now = std::time::Instant::now();
|
let now = std::time::Instant::now();
|
||||||
let mut wanted: Vec<(EndpointId, String)> = Vec::new();
|
let mut wanted: Vec<(EndpointId, String)> = Vec::new();
|
||||||
|
let mut interrupted = Vec::new();
|
||||||
for session in self.sessions.values() {
|
for session in self.sessions.values() {
|
||||||
let peer = session.peer;
|
let peer = session.peer;
|
||||||
if self.links.keys().any(|(p, _)| *p == peer)
|
if self.links.keys().any(|(p, _)| *p == peer) {
|
||||||
|| self.opening.iter().any(|(p, _)| *p == peer)
|
continue;
|
||||||
{
|
}
|
||||||
|
let opening = self.opening.iter().find(|(id, _)| *id == peer);
|
||||||
|
if opening.is_some_and(|(_, protocol)| {
|
||||||
|
!self
|
||||||
|
.comparisons
|
||||||
|
.get(&peer)
|
||||||
|
.is_some_and(|s| s.is_trial(protocol))
|
||||||
|
}) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if self.no_direct_data(peer) {
|
if self.no_direct_data(peer) {
|
||||||
@@ -2094,27 +2102,39 @@ impl Runtime {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let start = next_protocol_index(
|
let last_failed = self.last_failed_protocol.get(&peer);
|
||||||
&supported,
|
let start = next_protocol_index(&supported, last_failed.map(String::as_str));
|
||||||
self.last_failed_protocol.get(&peer).map(String::as_str),
|
|
||||||
);
|
|
||||||
let chosen = supported
|
let chosen = supported
|
||||||
.iter()
|
.iter()
|
||||||
.cycle()
|
.cycle()
|
||||||
.skip(start)
|
.skip(start)
|
||||||
.take(supported.len())
|
.take(supported.len())
|
||||||
.find(
|
.find(|proto| {
|
||||||
|proto| match self.protocol_dial_states.get(&(peer, (*proto).clone())) {
|
opening.is_none_or(|(_, kind)| kind != *proto && last_failed != Some(*proto))
|
||||||
Some(state) => state.cooldown_until <= now,
|
&& self
|
||||||
None => true,
|
.protocol_dial_states
|
||||||
},
|
.get(&(peer, (*proto).clone()))
|
||||||
);
|
.is_none_or(|state| state.cooldown_until <= now)
|
||||||
|
});
|
||||||
|
|
||||||
if let Some(proto) = chosen {
|
if let Some(proto) = chosen {
|
||||||
wanted.push((peer, proto.clone()));
|
wanted.push((peer, proto.clone()));
|
||||||
|
if let Some(key) = opening {
|
||||||
|
interrupted.push(key.clone());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A trial can still recover a two-carrier peer. Cancel it only for a
|
||||||
|
// third eligible carrier, preserving the failure order and cooldowns.
|
||||||
|
for key in interrupted {
|
||||||
|
self.opening.remove(&key);
|
||||||
|
self.link_attempts.remove(&key);
|
||||||
|
if let Some(task) = self.link_tasks.remove(&key) {
|
||||||
|
task.abort();
|
||||||
|
}
|
||||||
|
self.finish_comparison(key.0);
|
||||||
|
}
|
||||||
let trial = self.next_comparison(&served, now);
|
let trial = self.next_comparison(&served, now);
|
||||||
wanted.extend(trial.iter().cloned());
|
wanted.extend(trial.iter().cloned());
|
||||||
for (peer, protocol) in wanted {
|
for (peer, protocol) in wanted {
|
||||||
|
|||||||
@@ -55,6 +55,12 @@ pub(super) struct Schedule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Schedule {
|
impl Schedule {
|
||||||
|
pub(super) fn is_trial(&self, protocol: &str) -> bool {
|
||||||
|
self.trial
|
||||||
|
.as_ref()
|
||||||
|
.is_some_and(|trial| trial.protocol == protocol)
|
||||||
|
}
|
||||||
|
|
||||||
/// The responder can close the losing link before its acknowledgement
|
/// The responder can close the losing link before its acknowledgement
|
||||||
/// arrives. Only an exact generation match makes this a planned closure.
|
/// arrives. Only an exact generation match makes this a planned closure.
|
||||||
pub(super) fn retiring(&self, profile: Option<[u8; 16]>) -> bool {
|
pub(super) fn retiring(&self, profile: Option<[u8; 16]>) -> bool {
|
||||||
@@ -152,7 +158,7 @@ impl Runtime {
|
|||||||
Some((peer, protocol))
|
Some((peer, protocol))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn finish_comparison(&mut self, peer: EndpointId) {
|
pub(super) fn finish_comparison(&mut self, peer: EndpointId) {
|
||||||
if let Some(schedule) = self.comparisons.get_mut(&peer) {
|
if let Some(schedule) = self.comparisons.get_mut(&peer) {
|
||||||
schedule.trial = None;
|
schedule.trial = None;
|
||||||
schedule.next = Instant::now() + NEXT_TRIAL;
|
schedule.next = Instant::now() + NEXT_TRIAL;
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ use std::{
|
|||||||
use tempfile::TempDir;
|
use tempfile::TempDir;
|
||||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||||
use tokio::net::{TcpListener, TcpStream};
|
use tokio::net::{TcpListener, TcpStream};
|
||||||
use tsunagi::dataplane::transport::{DataCarrier, PacketTransport, SharedLink, TransportError};
|
use tsunagi::dataplane::transport::{PacketTransport, SharedLink, TransportError};
|
||||||
use tsunagi::discovery::SharedMemoryDiscovery;
|
use tsunagi::discovery::SharedMemoryDiscovery;
|
||||||
use tsunagi::overlay::{MemoryTun, MemoryTunFactory};
|
use tsunagi::overlay::{MemoryTun, MemoryTunFactory};
|
||||||
use tsunagi::testing::{config_with, network, wait_for_peers, wait_until};
|
use tsunagi::testing::{config_with, network, wait_for_peers, wait_until};
|
||||||
@@ -58,7 +58,8 @@ struct Node {
|
|||||||
agent: Agent,
|
agent: Agent,
|
||||||
tunnel: Arc<WireguardPlugin>,
|
tunnel: Arc<WireguardPlugin>,
|
||||||
tuns: MemoryTunFactory,
|
tuns: MemoryTunFactory,
|
||||||
accept: tokio::task::JoinHandle<()>,
|
wireguard: Option<Arc<UdpTransport>>,
|
||||||
|
_listeners: tokio::task::JoinSet<()>,
|
||||||
}
|
}
|
||||||
impl Node {
|
impl Node {
|
||||||
async fn new(
|
async fn new(
|
||||||
@@ -88,52 +89,64 @@ impl Node {
|
|||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let tuns = MemoryTunFactory::new();
|
let tuns = MemoryTunFactory::new();
|
||||||
let config = config_with(root.path(), discovery)
|
let mut config = config_with(root.path(), discovery)
|
||||||
.with_interface(Arc::new(tuns.clone()), "carrier-test", 1280)
|
.with_interface(Arc::new(tuns.clone()), "carrier-test", 1280)
|
||||||
.with_tunnel(tunnel.clone());
|
.with_tunnel(tunnel.clone());
|
||||||
let protocol = protocols[0];
|
let mut wireguard = None;
|
||||||
let (agent, accept) = if protocol == "wg" {
|
let mut tls = None;
|
||||||
let transport =
|
for &protocol in protocols {
|
||||||
UdpTransport::wireguard(identity.endpoint_id(), Ipv4Addr::LOCALHOST, None);
|
match protocol {
|
||||||
let carrier: Arc<dyn DataCarrier> = UdpCarrier::new(transport.clone());
|
"wg" => {
|
||||||
let mut config = config.with_carrier(carrier);
|
let transport =
|
||||||
if register_transport {
|
UdpTransport::wireguard(identity.endpoint_id(), Ipv4Addr::LOCALHOST, None);
|
||||||
config = config.with_custom_transport(protocol, transport.clone());
|
config = config.with_carrier(UdpCarrier::new(transport.clone()));
|
||||||
|
if register_transport {
|
||||||
|
config = config.with_custom_transport(protocol, transport.clone());
|
||||||
|
}
|
||||||
|
wireguard = Some(transport);
|
||||||
|
}
|
||||||
|
"tcp-tls" => {
|
||||||
|
let transport = Arc::new(
|
||||||
|
TcpTlsTransport::bind(&identity, Some(0), None)
|
||||||
|
.await
|
||||||
|
.unwrap(),
|
||||||
|
);
|
||||||
|
ports
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.push((identity.endpoint_id(), transport.bound_port()));
|
||||||
|
config = config
|
||||||
|
.with_carrier(TcpTlsCarrier::new(transport.clone()))
|
||||||
|
.with_custom_transport(
|
||||||
|
protocol,
|
||||||
|
Arc::new(LoopbackTls {
|
||||||
|
inner: transport.clone(),
|
||||||
|
ports: ports.clone(),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
tls = Some(transport);
|
||||||
|
}
|
||||||
|
"wg-quic" => {
|
||||||
|
config = config.with_carrier(Arc::new(
|
||||||
|
tsunagi::dataplane::transport::iroh_link::IrohCarrier,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
_ => panic!("unknown carrier {protocol}"),
|
||||||
}
|
}
|
||||||
let agent = Agent::spawn(config).await.unwrap();
|
}
|
||||||
|
let agent = Agent::spawn(config).await.unwrap();
|
||||||
|
let mut listeners = tokio::task::JoinSet::new();
|
||||||
|
if let Some(transport) = wireguard.clone() {
|
||||||
let incoming = agent.clone();
|
let incoming = agent.clone();
|
||||||
let accept = tokio::spawn(async move {
|
listeners.spawn(async move {
|
||||||
while let Some(link) = transport.next_inbound().await {
|
while let Some(link) = transport.next_inbound().await {
|
||||||
let _ = incoming.install_inbound_link(link).await;
|
let _ = incoming.install_inbound_link(link).await;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
(agent, accept)
|
}
|
||||||
} else {
|
if let Some(transport) = tls {
|
||||||
let transport = Arc::new(
|
|
||||||
TcpTlsTransport::bind(&identity, Some(0), None)
|
|
||||||
.await
|
|
||||||
.unwrap(),
|
|
||||||
);
|
|
||||||
ports
|
|
||||||
.lock()
|
|
||||||
.unwrap()
|
|
||||||
.push((identity.endpoint_id(), transport.bound_port()));
|
|
||||||
let carrier: Arc<dyn DataCarrier> = TcpTlsCarrier::new(transport.clone());
|
|
||||||
let custom = Arc::new(LoopbackTls {
|
|
||||||
inner: transport.clone(),
|
|
||||||
ports,
|
|
||||||
});
|
|
||||||
let mut config = config
|
|
||||||
.with_carrier(carrier)
|
|
||||||
.with_custom_transport(protocol, custom);
|
|
||||||
if protocols.contains(&"wg-quic") {
|
|
||||||
config = config.with_carrier(Arc::new(
|
|
||||||
tsunagi::dataplane::transport::iroh_link::IrohCarrier,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
let agent = Agent::spawn(config).await.unwrap();
|
|
||||||
let incoming = agent.clone();
|
let incoming = agent.clone();
|
||||||
let accept = tokio::spawn(async move {
|
listeners.spawn(async move {
|
||||||
transport
|
transport
|
||||||
.accept_loop(move |link| {
|
.accept_loop(move |link| {
|
||||||
let incoming = incoming.clone();
|
let incoming = incoming.clone();
|
||||||
@@ -143,14 +156,14 @@ impl Node {
|
|||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
});
|
});
|
||||||
(agent, accept)
|
}
|
||||||
};
|
|
||||||
Self {
|
Self {
|
||||||
_root: root,
|
_root: root,
|
||||||
agent,
|
agent,
|
||||||
tunnel,
|
tunnel,
|
||||||
tuns,
|
tuns,
|
||||||
accept,
|
wireguard,
|
||||||
|
_listeners: listeners,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn tun(&self) -> Arc<MemoryTun> {
|
fn tun(&self) -> Arc<MemoryTun> {
|
||||||
@@ -200,6 +213,26 @@ async fn delayed_tcp_listener(port: u16) -> (u16, tokio::task::JoinHandle<()>) {
|
|||||||
(local_port, task)
|
(local_port, task)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn deliver(source: &Node, destination: &Node, id: NetworkId, tag: u8) {
|
||||||
|
let mut packet = vec![0; 32];
|
||||||
|
packet[0] = 0x45;
|
||||||
|
packet[2..4].copy_from_slice(&32_u16.to_be_bytes());
|
||||||
|
packet[8] = 64;
|
||||||
|
packet[9] = 253;
|
||||||
|
packet[12..16].copy_from_slice(&source.addr(id).octets());
|
||||||
|
packet[16..20].copy_from_slice(&destination.addr(id).octets());
|
||||||
|
packet[20] = tag;
|
||||||
|
let packet = Bytes::from(packet);
|
||||||
|
source.tun().push_from_os(packet.clone());
|
||||||
|
assert_eq!(
|
||||||
|
tokio::time::timeout(Duration::from_secs(5), destination.tun().pop_to_os())
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.unwrap(),
|
||||||
|
packet
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn data_flows_over_the_incumbent_while_a_faster_carrier_is_measured_and_promoted() {
|
async fn data_flows_over_the_incumbent_while_a_faster_carrier_is_measured_and_promoted() {
|
||||||
use tsunagi::agent::{DataPath, Event};
|
use tsunagi::agent::{DataPath, Event};
|
||||||
@@ -239,25 +272,6 @@ async fn data_flows_over_the_incumbent_while_a_faster_carrier_is_measured_and_pr
|
|||||||
matches!(status.peers.first().map(|p| &p.data_path),
|
matches!(status.peers.first().map(|p| &p.data_path),
|
||||||
Some(DataPath::Direct { transport }) if transport == protocol)
|
Some(DataPath::Direct { transport }) if transport == protocol)
|
||||||
};
|
};
|
||||||
async fn deliver(source: &Node, destination: &Node, id: NetworkId, tag: u8) {
|
|
||||||
let mut packet = vec![0; 32];
|
|
||||||
packet[0] = 0x45;
|
|
||||||
packet[2..4].copy_from_slice(&32_u16.to_be_bytes());
|
|
||||||
packet[8] = 64;
|
|
||||||
packet[9] = 253;
|
|
||||||
packet[12..16].copy_from_slice(&source.addr(id).octets());
|
|
||||||
packet[16..20].copy_from_slice(&destination.addr(id).octets());
|
|
||||||
packet[20] = tag;
|
|
||||||
let packet = Bytes::from(packet);
|
|
||||||
source.tun().push_from_os(packet.clone());
|
|
||||||
assert_eq!(
|
|
||||||
tokio::time::timeout(Duration::from_secs(5), destination.tun().pop_to_os())
|
|
||||||
.await
|
|
||||||
.unwrap()
|
|
||||||
.unwrap(),
|
|
||||||
packet
|
|
||||||
);
|
|
||||||
}
|
|
||||||
tokio::time::timeout(Duration::from_secs(60), async {
|
tokio::time::timeout(Duration::from_secs(60), async {
|
||||||
wait_until("slow TLS data-link probes are visible", || async {
|
wait_until("slow TLS data-link probes are visible", || async {
|
||||||
let status = a.agent.network_status(id).await.ok()?;
|
let status = a.agent.network_status(id).await.ok()?;
|
||||||
@@ -283,13 +297,92 @@ async fn data_flows_over_the_incumbent_while_a_faster_carrier_is_measured_and_pr
|
|||||||
}).await.expect("comparison must finish with traffic on the existing tunnel");
|
}).await.expect("comparison must finish with traffic on the existing tunnel");
|
||||||
a.agent.shutdown().await;
|
a.agent.shutdown().await;
|
||||||
b.agent.shutdown().await;
|
b.agent.shutdown().await;
|
||||||
a.accept.abort();
|
|
||||||
b.accept.abort();
|
|
||||||
for proxy in proxies {
|
for proxy in proxies {
|
||||||
proxy.abort();
|
proxy.abort();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_stalled_comparison_does_not_delay_recovery_on_a_third_carrier() {
|
||||||
|
use tsunagi::agent::DataPath;
|
||||||
|
let discovery = SharedMemoryDiscovery::new();
|
||||||
|
let ports = Arc::default();
|
||||||
|
let protocols = ["wg", "tcp-tls", "wg-quic"];
|
||||||
|
let a = Node::configured(&discovery, &protocols, Arc::clone(&ports), true).await;
|
||||||
|
let b = Node::configured(&discovery, &protocols, Arc::clone(&ports), true).await;
|
||||||
|
let stalled = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).await.unwrap();
|
||||||
|
for (_, port) in &mut *ports.lock().unwrap() {
|
||||||
|
*port = stalled.local_addr().unwrap().port();
|
||||||
|
}
|
||||||
|
let (name, secret) = network("recover-during-comparison");
|
||||||
|
let id = a.agent.join_network(&name, &secret).await.unwrap();
|
||||||
|
b.agent.join_network(&name, &secret).await.unwrap();
|
||||||
|
let nodes = [&a, &b];
|
||||||
|
let both_use = |carrier: &'static str| async move {
|
||||||
|
for node in nodes {
|
||||||
|
let status = node.agent.network_status(id).await.ok()?;
|
||||||
|
if !matches!(&status.peers.first()?.data_path, DataPath::Direct { transport } if transport == carrier)
|
||||||
|
|| !node
|
||||||
|
.tunnel
|
||||||
|
.overview(id)?
|
||||||
|
.peers
|
||||||
|
.first()?
|
||||||
|
.tunnel
|
||||||
|
.as_ref()?
|
||||||
|
.health
|
||||||
|
.is_up()
|
||||||
|
{
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(())
|
||||||
|
};
|
||||||
|
wait_until("both endpoints start on native WireGuard", || {
|
||||||
|
both_use("wg")
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
deliver(&a, &b, id, 1).await;
|
||||||
|
// Accept the real TLS ClientHello but never answer it. The TCP handshake
|
||||||
|
// establishes that the background attempt has started before WG is lost.
|
||||||
|
let mut handshake = tokio::time::timeout(Duration::from_secs(30), async {
|
||||||
|
let (mut handshake, _) = stalled.accept().await.unwrap();
|
||||||
|
assert_eq!(handshake.read_u8().await.unwrap(), 22);
|
||||||
|
handshake
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.expect("background TLS ClientHello");
|
||||||
|
for (source, destination) in [(&a, &b), (&b, &a)] {
|
||||||
|
source
|
||||||
|
.wireguard
|
||||||
|
.as_ref()
|
||||||
|
.unwrap()
|
||||||
|
.forget_peer(id, destination.agent.endpoint_id());
|
||||||
|
}
|
||||||
|
tokio::time::timeout(Duration::from_secs(8), async {
|
||||||
|
wait_until(
|
||||||
|
"QUIC recovers traffic before the background deadline",
|
||||||
|
|| both_use("wg-quic"),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
deliver(&a, &b, id, 2).await;
|
||||||
|
deliver(&b, &a, id, 3).await;
|
||||||
|
let mut tail = Vec::new();
|
||||||
|
if let Err(error) = handshake.read_to_end(&mut tail).await {
|
||||||
|
assert!(
|
||||||
|
matches!(
|
||||||
|
error.kind(),
|
||||||
|
std::io::ErrorKind::ConnectionReset | std::io::ErrorKind::ConnectionAborted
|
||||||
|
),
|
||||||
|
"{error}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.expect("the stalled TLS attempt must be cancelled when QUIC can recover");
|
||||||
|
a.agent.shutdown().await;
|
||||||
|
b.agent.shutdown().await;
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn missing_native_transport_cannot_silently_turn_wireguard_into_quic() {
|
async fn missing_native_transport_cannot_silently_turn_wireguard_into_quic() {
|
||||||
let discovery = SharedMemoryDiscovery::new();
|
let discovery = SharedMemoryDiscovery::new();
|
||||||
@@ -321,8 +414,6 @@ async fn missing_native_transport_cannot_silently_turn_wireguard_into_quic() {
|
|||||||
.await;
|
.await;
|
||||||
a.agent.shutdown().await;
|
a.agent.shutdown().await;
|
||||||
b.agent.shutdown().await;
|
b.agent.shutdown().await;
|
||||||
a.accept.abort();
|
|
||||||
b.accept.abort();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
@@ -407,7 +498,5 @@ async fn wireguard_and_tls_carriers_both_carry_the_same_internal_cipher_and_exac
|
|||||||
}
|
}
|
||||||
a.agent.shutdown().await;
|
a.agent.shutdown().await;
|
||||||
b.agent.shutdown().await;
|
b.agent.shutdown().await;
|
||||||
a.accept.abort();
|
|
||||||
b.accept.abort();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user