When both ends dial each other, exactly one TCP connection must survive
and both ends must pick the same one. Previously ferrix relied on the
other router to sort this out and could briefly hold two sessions.
- collision: a shared map keyed by the peer's BGP Identifier holds the one
connection currently preferred for that peer plus a kill signal. Each
connection calls admit() on receiving the peer's OPEN. The connection
initiated by the higher BGP Identifier wins; the loser gets Cease /
Connection Collision Resolution, whether it is the new connection or an
existing one in OpenConfirm running in another task.
- A collision with an Established session closes the new connection (RFC
default). The optional CollisionDetectEstablishedState is available as
collision_detect_established_state: a restarted peer's new connection
then replaces the stale session at once instead of being refused until
the hold timer expires. Enabled in the lab config.
- Two connections opened by the same side: the newer wins.
- OPEN validation rejects a peer using our own BGP Identifier or 0.0.0.0
with Bad BGP Identifier; collision resolution needs distinct identifiers.
- OpenConfirm no longer times out instantly when the negotiated hold time
is zero.
- Sessions record which side opened them; shown as "opened by" on the
status page and as `direction` in /api/peers. New counter
bgp_connection_collisions_total{closed}.
- Tests: unit tests for the rule (including that both ends agree), and
integration tests for simultaneous open in both identifier orders and
both arrival orders, Established default, restart replacement with route
flush, distinct identifiers, slot release, and Bad BGP Identifier.
Existing fan-out test now gives its two peers distinct identifiers.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
88 lines
3.3 KiB
Rust
88 lines
3.3 KiB
Rust
//! Active side of the FSM: dial each configured peer and keep the session up.
|
|
//!
|
|
//! One task per configured peer loops forever: connect, run the session,
|
|
//! back off, repeat. While a session with the same remote IP and ASN is
|
|
//! already established (for example because the peer dialled us first) the
|
|
//! dialer stays idle rather than opening a second one. When both sides dial
|
|
//! at once the FSM resolves the collision (see [`crate::collision`]); if our
|
|
//! connection is the one closed, the next pass finds the surviving session
|
|
//! and goes idle.
|
|
|
|
use std::sync::Arc;
|
|
use std::time::{Duration, SystemTime};
|
|
|
|
use tokio::net::TcpStream;
|
|
use tokio::task::JoinHandle;
|
|
use tracing::{info, warn};
|
|
|
|
use crate::collision::Direction;
|
|
use crate::config::PeerConfig;
|
|
use crate::fsm::{SessionConfig, run_session};
|
|
use crate::session::{DialState, SessionRegistry};
|
|
|
|
/// Upper bound for exponential backoff between connection attempts.
|
|
const MAX_BACKOFF: Duration = Duration::from_secs(120);
|
|
const CONNECT_TIMEOUT: Duration = Duration::from_secs(10);
|
|
|
|
pub fn spawn_dialers(
|
|
peers: &[PeerConfig],
|
|
base: &SessionConfig,
|
|
connect_retry: Duration,
|
|
) -> Vec<JoinHandle<()>> {
|
|
peers
|
|
.iter()
|
|
.map(|peer| {
|
|
let config = SessionConfig {
|
|
hold_time_secs: peer.hold_time_secs,
|
|
direction: Direction::Outbound,
|
|
peer_name: Some(peer.name.clone()),
|
|
expected_asn: Some(peer.remote_asn),
|
|
..base.clone()
|
|
};
|
|
tokio::spawn(dial_loop(Arc::new(peer.clone()), config, connect_retry))
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
async fn dial_loop(peer: Arc<PeerConfig>, config: SessionConfig, connect_retry: Duration) {
|
|
let registry: SessionRegistry = config.registry.clone();
|
|
let mut backoff = connect_retry;
|
|
|
|
loop {
|
|
if registry.find(peer.endpoint.ip(), peer.remote_asn).is_some() {
|
|
// Peer dialled us; nothing to do until that session goes away.
|
|
registry.set_dial_state(&peer.name, DialState::Idle);
|
|
tokio::time::sleep(connect_retry).await;
|
|
continue;
|
|
}
|
|
|
|
registry.set_dial_state(&peer.name, DialState::Connecting);
|
|
info!(peer = %peer.name, endpoint = %peer.endpoint, "Connecting to peer");
|
|
|
|
let outcome = match tokio::time::timeout(CONNECT_TIMEOUT, TcpStream::connect(peer.endpoint)).await {
|
|
Ok(Ok(stream)) => {
|
|
info!(peer = %peer.name, endpoint = %peer.endpoint, "TCP connected, starting session");
|
|
backoff = connect_retry;
|
|
run_session(stream, peer.endpoint, config.clone()).await.map(|()| "session closed by peer".to_string())
|
|
}
|
|
Ok(Err(e)) => Err(eyre::eyre!("connect failed: {e}")),
|
|
Err(_) => Err(eyre::eyre!("connect timed out after {CONNECT_TIMEOUT:?}")),
|
|
};
|
|
|
|
let reason = match outcome {
|
|
Ok(msg) => msg,
|
|
Err(e) => {
|
|
warn!(peer = %peer.name, endpoint = %peer.endpoint, err = %e, "Session attempt ended");
|
|
e.to_string()
|
|
}
|
|
};
|
|
|
|
registry.set_dial_state(
|
|
&peer.name,
|
|
DialState::Backoff { until: SystemTime::now() + backoff, last_error: reason },
|
|
);
|
|
tokio::time::sleep(backoff).await;
|
|
backoff = (backoff * 2).min(MAX_BACKOFF);
|
|
}
|
|
}
|