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>
185 lines
6.2 KiB
Rust
185 lines
6.2 KiB
Rust
//! A minimal stand-in for a neighbouring router: connects to the daemon,
|
|
//! completes the handshake, optionally announces some prefixes, and prints
|
|
//! every UPDATE it receives. Useful for trying things out when no real
|
|
//! router is around.
|
|
//!
|
|
//! cargo run -p bgp-integration-tests --example fake_peer -- \
|
|
//! --server 127.0.0.1:1790 --asn 65001 --announce 203.0.113.0/24
|
|
//!
|
|
//! With `--echo`, every prefix received is re-announced back to the server
|
|
//! with this peer's ASN prepended, which lets a single daemon show its own
|
|
//! routes "coming back" in the received table.
|
|
//!
|
|
//! With `--listen ADDR` it instead waits for the server to dial it (one
|
|
//! connection at a time), which exercises the server's active side.
|
|
|
|
use std::net::Ipv4Addr;
|
|
use std::time::Duration;
|
|
|
|
use bgp_integration_tests::test_peer::TestPeer;
|
|
use bgp_packet::message::{
|
|
AsPathAttribute, CommunitiesPathAttribute, Message, NextHopPathAttribute, OriginPathAttribute,
|
|
PathAttribute, UpdateMessage,
|
|
};
|
|
use bgp_primitives::ip_prefix::IpPrefix;
|
|
use clap::Parser;
|
|
use eyre::Result;
|
|
use tokio::net::TcpListener;
|
|
|
|
#[derive(Parser, Debug)]
|
|
struct Args {
|
|
/// Address of the daemon's BGP listener to connect to.
|
|
#[arg(long, default_value = "127.0.0.1:1790", conflicts_with = "listen")]
|
|
server: String,
|
|
/// Instead of connecting, listen here and accept connections from the
|
|
/// server, one at a time, forever.
|
|
#[arg(long)]
|
|
listen: Option<String>,
|
|
/// ASN this peer speaks as.
|
|
#[arg(long, default_value_t = 65001)]
|
|
asn: u32,
|
|
/// Router ID to send in OPEN.
|
|
#[arg(long, default_value = "10.0.0.2")]
|
|
router_id: Ipv4Addr,
|
|
/// Hold time to negotiate.
|
|
#[arg(long, default_value_t = 30)]
|
|
hold_time: u16,
|
|
/// Prefixes to announce after the session is established.
|
|
#[arg(long)]
|
|
announce: Vec<String>,
|
|
/// Re-announce every received prefix back to the server, with our ASN
|
|
/// prepended and NO_EXPORT-tagged routes dropped.
|
|
#[arg(long)]
|
|
echo: bool,
|
|
}
|
|
|
|
const NO_EXPORT: (u16, u16) = (0xFFFF, 0xFF01);
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<()> {
|
|
let args = Args::parse();
|
|
match &args.listen {
|
|
Some(listen) => {
|
|
let listener = TcpListener::bind(listen).await?;
|
|
println!("listening on {}", listener.local_addr()?);
|
|
loop {
|
|
let (stream, from) = listener.accept().await?;
|
|
println!("accepted connection from {from}");
|
|
if let Err(e) = run(&args, TestPeer::from_stream(stream), from).await {
|
|
println!("session ended: {e}");
|
|
}
|
|
}
|
|
}
|
|
None => {
|
|
let addr = args.server.parse()?;
|
|
run(&args, TestPeer::connect(addr).await?, addr).await
|
|
}
|
|
}
|
|
}
|
|
|
|
async fn run(args: &Args, mut peer: TestPeer, addr: std::net::SocketAddr) -> Result<()> {
|
|
let open = peer
|
|
.handshake(args.asn, args.router_id, args.hold_time)
|
|
.await?;
|
|
println!(
|
|
"established with {} (AS{} router-id {})",
|
|
addr, open.asn, open.identifier
|
|
);
|
|
|
|
for p in &args.announce {
|
|
let prefix = IpPrefix::try_from(p.as_str())?;
|
|
let update = announce(prefix, vec![args.asn], vec![(args.asn as u16, 1)]);
|
|
peer.send(&Message::Update(update)).await?;
|
|
println!("announced {prefix}");
|
|
}
|
|
|
|
let keepalive = Duration::from_secs((args.hold_time / 3).max(1) as u64);
|
|
let mut ticker = tokio::time::interval(keepalive);
|
|
loop {
|
|
tokio::select! {
|
|
_ = ticker.tick() => peer.send(&Message::KeepAlive).await?,
|
|
msg = peer.recv() => match msg? {
|
|
Message::KeepAlive => {}
|
|
Message::Update(u) => {
|
|
for w in &u.withdrawn_routes {
|
|
println!("WITHDRAW {w}");
|
|
}
|
|
for a in &u.announced_routes {
|
|
println!("UPDATE {a} attrs={:?}", u.path_attributes);
|
|
}
|
|
if args.echo {
|
|
echo_back(&mut peer, &u, args.asn).await?;
|
|
}
|
|
}
|
|
Message::Notification(n) => {
|
|
println!("NOTIFICATION {}; session over", n.error);
|
|
return Ok(());
|
|
}
|
|
other => println!("unexpected {other:?}"),
|
|
},
|
|
}
|
|
}
|
|
}
|
|
|
|
fn announce(prefix: IpPrefix, as_path: Vec<u32>, communities: Vec<(u16, u16)>) -> UpdateMessage {
|
|
let mut path_attributes = vec![
|
|
PathAttribute::Origin(OriginPathAttribute::IGP),
|
|
AsPathAttribute::from_asns(as_path),
|
|
PathAttribute::NextHop(NextHopPathAttribute(Ipv4Addr::new(10, 0, 0, 2))),
|
|
];
|
|
if !communities.is_empty() {
|
|
path_attributes.push(PathAttribute::Communities(CommunitiesPathAttribute(
|
|
communities,
|
|
)));
|
|
}
|
|
UpdateMessage {
|
|
withdrawn_routes: vec![],
|
|
path_attributes,
|
|
announced_routes: vec![prefix.into()],
|
|
}
|
|
}
|
|
|
|
async fn echo_back(peer: &mut TestPeer, u: &UpdateMessage, asn: u32) -> Result<()> {
|
|
let mut as_path = vec![asn];
|
|
let mut communities = vec![];
|
|
for attr in &u.path_attributes {
|
|
match attr {
|
|
PathAttribute::AsPath(p) => {
|
|
as_path.extend(p.segments.iter().flat_map(|s| s.path.iter().copied()))
|
|
}
|
|
PathAttribute::Communities(c) => communities = c.0.clone(),
|
|
_ => {}
|
|
}
|
|
}
|
|
if communities.contains(&NO_EXPORT) {
|
|
println!(" not echoing: NO_EXPORT");
|
|
return Ok(());
|
|
}
|
|
for nlri in &u.announced_routes {
|
|
let prefix = nlri.prefix;
|
|
peer.send(&Message::Update(announce(
|
|
prefix,
|
|
as_path.clone(),
|
|
communities.clone(),
|
|
)))
|
|
.await?;
|
|
println!(" echoed {prefix} back with path {as_path:?}");
|
|
}
|
|
if !u.withdrawn_routes.is_empty() {
|
|
peer.send(&Message::Update(UpdateMessage {
|
|
withdrawn_routes: u.withdrawn_routes.clone(),
|
|
path_attributes: vec![],
|
|
announced_routes: vec![],
|
|
}))
|
|
.await?;
|
|
println!(
|
|
" echoed withdraw of {:?}",
|
|
u.withdrawn_routes
|
|
.iter()
|
|
.map(|p| p.to_string())
|
|
.collect::<Vec<_>>()
|
|
);
|
|
}
|
|
Ok(())
|
|
}
|