The work of the speaker-cleanup branch as one commit; the branch keeps the step-by-step history. Sessions - The state machine is a pure function from events to actions, driven by one task per connection. Idle, Connect and Active belong to a dialer that keeps configured peers up with backoff. - Capabilities are negotiated: address families, four-octet AS numbers with AS_TRANS and AS4_PATH, route refresh, ADD-PATH per family and direction. - Collision detection per RFC 4271 section 6.8, by address and identifier. - Connections are taken only from configured peers, a few per address. - A panic in a session is contained and answered with a Cease; a core task that stops shuts the server down with a Cease to every peer. - Writes that make no progress are given up (RFC 9687). Codec - Malformed UPDATEs are handled as RFC 7606 says: treat-as-withdraw, attribute discard, or session reset, with the NOTIFICATION to send. - Address families are open types; any can be named and ignored. - Prefixes are fixed-size values, normalised, with path identifiers. - Lengths are filled in after writing; one definition of each type code. - Property tests and fuzz targets for every decoder. Routing table - Adj-RIBs-In and Loc-RIB in one sharded table that sessions write to directly. Routes are sixteen bytes; attribute sets are interned in wire form. Best-path selection per RFC 4271 section 9.1.2.2 with deterministic MED. - A journal per shard lets followers read changes at their own pace; one that falls behind is resynchronised. - On a RIPE RIS dump of 148 peers and 39.3M routes: loaded in 2.0 s on 12 cores, about 52 bytes a route. Announcing - Originated routes come from the configuration. Each session has its own exporter with an Adj-RIB-Out, so a slow peer delays only itself. - Routes to a peer in our own AS have an empty path and a LOCAL_PREF. Operations - HTTP status page, JSON API, event stream and Prometheus metrics. - Benchmarks on synthetic and real tables. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
113 lines
3.7 KiB
Rust
113 lines
3.7 KiB
Rust
//! Who may connect: only the addresses of configured peers, and each of
|
|
//! them only so many times at once.
|
|
|
|
use std::net::{Ipv4Addr, SocketAddr};
|
|
use std::time::Duration;
|
|
|
|
use bgp_integration_tests::test_peer::TestPeer;
|
|
use bgp_integration_tests::test_server::TestServer;
|
|
use bgp_server::config::PeerConfig;
|
|
use bgp_server::family::FamilySet;
|
|
use eyre::Result;
|
|
use tokio::net::TcpListener;
|
|
|
|
const LOCAL_ASN: u32 = 65000;
|
|
const PEER_ASN: u32 = 65001;
|
|
const TIMEOUT: Duration = Duration::from_secs(5);
|
|
|
|
fn peer_at(endpoint: SocketAddr) -> PeerConfig {
|
|
PeerConfig {
|
|
name: "r1".into(),
|
|
endpoint,
|
|
remote_asn: PEER_ASN,
|
|
hold_time_secs: 90,
|
|
local_address: None,
|
|
families: FamilySet::supported(),
|
|
add_path: None,
|
|
}
|
|
}
|
|
|
|
/// Whether the server closed the connection without sending anything.
|
|
async fn closed_unanswered(peer: &mut TestPeer) -> bool {
|
|
matches!(
|
|
tokio::time::timeout(TIMEOUT, peer.recv()).await,
|
|
Ok(Err(_))
|
|
)
|
|
}
|
|
|
|
/// A host that is not a configured peer gets nothing: no OPEN to answer,
|
|
/// no session, no routes taken from it. Any host used to be able to open a
|
|
/// session as any AS it named.
|
|
#[tokio::test]
|
|
async fn test_a_host_that_is_not_a_configured_peer_is_refused() -> Result<()> {
|
|
// The only peer configured is somewhere else (documentation space).
|
|
let server = TestServer::start_with(LOCAL_ASN, |c| {
|
|
c.accept_unconfigured_peers = false;
|
|
c.peers = vec![peer_at("192.0.2.9:179".parse().unwrap())];
|
|
})
|
|
.await?;
|
|
|
|
let mut stranger = TestPeer::connect(server.addr).await?;
|
|
assert!(closed_unanswered(&mut stranger).await);
|
|
assert!(server.registry.list().is_empty());
|
|
Ok(())
|
|
}
|
|
|
|
/// The address of a configured peer is let in, and held to what is
|
|
/// configured for that peer.
|
|
#[tokio::test]
|
|
async fn test_a_configured_peer_is_let_in() -> Result<()> {
|
|
// Somewhere for the server's own dialling to go; never answered.
|
|
let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).await?;
|
|
let endpoint = listener.local_addr()?;
|
|
let server = TestServer::start_with(LOCAL_ASN, |c| {
|
|
c.accept_unconfigured_peers = false;
|
|
c.peers = vec![peer_at(endpoint)];
|
|
})
|
|
.await?;
|
|
|
|
let mut peer = TestPeer::connect(server.addr).await?;
|
|
peer.handshake(PEER_ASN, Ipv4Addr::new(10, 0, 0, 2), 90)
|
|
.await?;
|
|
tokio::time::timeout(TIMEOUT, async {
|
|
while server.registry.list().is_empty() {
|
|
tokio::time::sleep(Duration::from_millis(10)).await;
|
|
}
|
|
})
|
|
.await?;
|
|
assert_eq!(server.registry.list()[0].name.as_deref(), Some("r1"));
|
|
Ok(())
|
|
}
|
|
|
|
/// One address may hold only a few connections at a time, so that one host
|
|
/// cannot use up the server's sockets with connections that never get as
|
|
/// far as a session.
|
|
#[tokio::test]
|
|
async fn test_one_address_may_open_only_a_few_connections() -> Result<()> {
|
|
let server = TestServer::start(LOCAL_ASN).await?;
|
|
|
|
// Four that say nothing: each is sent the server's OPEN and waited on.
|
|
let mut held = Vec::new();
|
|
for _ in 0..4 {
|
|
let mut peer = TestPeer::connect(server.addr).await?;
|
|
tokio::time::timeout(TIMEOUT, peer.recv_open()).await??;
|
|
held.push(peer);
|
|
}
|
|
let mut one_too_many = TestPeer::connect(server.addr).await?;
|
|
assert!(closed_unanswered(&mut one_too_many).await);
|
|
|
|
// When one of them goes, there is room again.
|
|
held.pop();
|
|
tokio::time::timeout(TIMEOUT, async {
|
|
loop {
|
|
let mut peer = TestPeer::connect(server.addr).await?;
|
|
if peer.recv_open().await.is_ok() {
|
|
return Ok::<_, eyre::Report>(());
|
|
}
|
|
tokio::time::sleep(Duration::from_millis(20)).await;
|
|
}
|
|
})
|
|
.await??;
|
|
Ok(())
|
|
}
|