From a47c1271aa165fdb8c08b1e11cb976f1aa29b0b5 Mon Sep 17 00:00:00 2001 From: Rayhaan Jaufeerally Date: Sun, 18 Aug 2024 08:53:43 +0000 Subject: [PATCH] Added additional debug prints --- bin/Cargo.toml | 1 + bin/src/client/main.rs | 3 ++ crates/bgp_packet/Cargo.toml | 1 + crates/bgp_packet/src/constants.rs | 2 +- crates/route_client/Cargo.toml | 1 + crates/route_client/src/fib_state.rs | 55 +++++++++++++++++++++++++++- 6 files changed, 60 insertions(+), 3 deletions(-) diff --git a/bin/Cargo.toml b/bin/Cargo.toml index 6ebe8ba..8ba558b 100644 --- a/bin/Cargo.toml +++ b/bin/Cargo.toml @@ -8,6 +8,7 @@ rust-version.workspace = true version.workspace = true [dependencies] +bgp_packet.workspace = true bgp_server.workspace = true clap.workspace = true eyre.workspace = true diff --git a/bin/src/client/main.rs b/bin/src/client/main.rs index 19a57ea..5527d82 100644 --- a/bin/src/client/main.rs +++ b/bin/src/client/main.rs @@ -1,3 +1,4 @@ +use bgp_packet::constants::AddressFamilyIdentifier; use clap::{Parser, Subcommand}; use eyre::{bail, Result}; use route_client::southbound_interface::{DummyVerifier, SouthboundInterface}; @@ -21,6 +22,8 @@ struct Cli { route_server: String, #[clap(subcommand)] command: Option, + #[clap(long = "af")] + address_family: Vec, } #[derive(Subcommand)] diff --git a/crates/bgp_packet/Cargo.toml b/crates/bgp_packet/Cargo.toml index a6e50f2..5163336 100644 --- a/crates/bgp_packet/Cargo.toml +++ b/crates/bgp_packet/Cargo.toml @@ -14,6 +14,7 @@ workspace = true [dependencies] byteorder = "1.4.3" bytes.workspace = true +clap.workspace = true eyre.workspace = true netlink-packet-route.workspace = true nom = "7.1" diff --git a/crates/bgp_packet/src/constants.rs b/crates/bgp_packet/src/constants.rs index 6d73d20..cbba71e 100644 --- a/crates/bgp_packet/src/constants.rs +++ b/crates/bgp_packet/src/constants.rs @@ -21,7 +21,7 @@ use super::traits::{BGPParserError, ParserContext, ReadablePacket}; // Address Family Identifiers as per // https://www.iana.org/assignments/address-family-numbers/address-family-numbers.xhtml -#[derive(Eq, PartialEq, Debug, Copy, Clone, Serialize, Deserialize, Hash)] +#[derive(Eq, PartialEq, Debug, Copy, Clone, Serialize, Deserialize, Hash, clap::ValueEnum)] pub enum AddressFamilyIdentifier { Ipv4, Ipv6, diff --git a/crates/route_client/Cargo.toml b/crates/route_client/Cargo.toml index 80d315d..b17aa43 100644 --- a/crates/route_client/Cargo.toml +++ b/crates/route_client/Cargo.toml @@ -30,6 +30,7 @@ tokio-stream = "0.1.14" tokio-util = { version = "0.7.10", features = ["codec"] } tokio.workspace = true tonic.workspace = true +tracing-subscriber.workspace = true tracing.workspace = true warp.workspace = true diff --git a/crates/route_client/src/fib_state.rs b/crates/route_client/src/fib_state.rs index b6bf6bc..e7902c5 100644 --- a/crates/route_client/src/fib_state.rs +++ b/crates/route_client/src/fib_state.rs @@ -21,7 +21,7 @@ use std::net::Ipv6Addr; use std::net::{IpAddr, Ipv4Addr}; use std::sync::Arc; use tokio::sync::Mutex; -use tracing::{trace, warn}; +use tracing::{info, trace, warn}; use bgp_packet::constants::AddressFamilyIdentifier; use bgp_packet::nlri::NLRI; @@ -88,6 +88,7 @@ where /// route_add requests updating the nexthop to a particular path if it is not already /// the best path. pub async fn route_add(&mut self, nlri: &NLRI, nexthop: IpAddr) -> Result<(), String> { + info!(af = ?self.af, %nlri, %nexthop); // Lookup the path in the Fib, there are three possible outcomes: // 1. The route is not yet known, we add it to the FibState and inject it into the kernel, // 2. The route is known and has a prior nexthop that needs to be updated @@ -156,7 +157,7 @@ where let addr: A = nlri.clone().try_into()?; self.fib .insert(addr, nlri.prefixlen.into(), Arc::new(Mutex::new(entry))); - trace!(nlri = %nlri, "Added to kernel"); + trace!(af = ?self.af, nlri = %nlri, nexthop = %nexthop, "Added to kernel"); } }; Ok(()) @@ -168,6 +169,7 @@ where if let Some(entry_wrapped) = self.fib.exact_match(prefix_addr, nlri.prefixlen.into()) { { let entry = entry_wrapped.lock().await; + info!(%nlri, %prefix_addr, %entry.nexthop, "route_del"); if let Err(e) = self.southbound.route_del(nlri.clone(), entry.nexthop).await { warn!( "Failed to apply route mutation to remove NLRI: {}, error: {}", @@ -183,3 +185,52 @@ where Ok(()) } } + +#[cfg(test)] +mod tests { + use std::{ + net::{IpAddr, Ipv6Addr}, + str::FromStr, + }; + + use bgp_packet::nlri::NLRI; + use eyre::{eyre, Result}; + use ip_network_table_deps_treebitmap::IpLookupTable; + use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter}; + + use crate::southbound_interface::DummyVerifier; + + use super::FibState; + + #[tokio::test] + async fn test_double_add() -> Result<()> { + tracing_subscriber::registry() + .with(tracing_subscriber::fmt::layer()) + .with(EnvFilter::from_default_env()) + .init(); + + let southbound = DummyVerifier::default(); + let mut fib_state: FibState = FibState { + fib: IpLookupTable::default(), + southbound, + af: bgp_packet::constants::AddressFamilyIdentifier::Ipv6, + }; + + let nlri = NLRI::try_from("2602:feda:b8d::/48").map_err(|e| eyre!(e))?; + let nexthop = IpAddr::V6(Ipv6Addr::from_str("2001:db8:cafe::1")?); + + fib_state + .route_add(&nlri, nexthop) + .await + .map_err(|e| eyre!(e))?; + + let nexthop_2 = IpAddr::V6(Ipv6Addr::from_str("2001:db8:babe::1")?); + + fib_state + .route_add(&nlri, nexthop_2) + .await + .map_err(|e| eyre!(e))?; + + Ok(()) + } +}