From 9b44ab782d42fb87765bb91f393e37d9e031029b Mon Sep 17 00:00:00 2001 From: Rayhaan Jaufeerally Date: Sat, 17 Aug 2024 14:16:46 +0000 Subject: [PATCH] Add EnvLogger and start of trait fn for dumping routes --- bin/src/bgp_server/main.rs | 27 +++++++++---------- bin/src/client/main.rs | 8 +++++- crates/bgp_packet/Cargo.toml | 16 ++++++----- crates/bgp_packet/src/constants.rs | 19 +++++++++++++ crates/route_client/src/fib_state.rs | 8 +++++- crates/route_client/src/lib.rs | 12 ++++----- crates/route_client/src/netlink.rs | 12 +++++++++ .../route_client/src/southbound_interface.rs | 12 +++++++++ 8 files changed, 83 insertions(+), 31 deletions(-) diff --git a/bin/src/bgp_server/main.rs b/bin/src/bgp_server/main.rs index 8953686..000f967 100644 --- a/bin/src/bgp_server/main.rs +++ b/bin/src/bgp_server/main.rs @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -use bgp_server::bgp_server::Server; -use bgp_server::config::ServerConfig; use clap::Parser; use core::sync::atomic::AtomicBool; use libc::SIGUSR1; @@ -22,11 +20,17 @@ use signal_hook::consts::TERM_SIGNALS; use signal_hook::flag; use signal_hook::iterator::exfiltrator::WithOrigin; use signal_hook::iterator::SignalsInfo; +use tracing::info; +use tracing_subscriber::layer::SubscriberExt; +use tracing_subscriber::util::SubscriberInitExt; +use tracing_subscriber::EnvFilter; + use std::fs::File; use std::io::BufReader; -use std::process::exit; use std::sync::Arc; -use tracing::info; + +use bgp_server::bgp_server::Server; +use bgp_server::config::ServerConfig; #[derive(Parser)] #[command(author = "Rayhaan Jaufeerally ", version = "0.1")] @@ -37,15 +41,10 @@ struct Cli { #[tokio::main] async fn main() -> Result<(), Box> { - let subscriber = tracing_subscriber::fmt(); - - match subscriber.try_init() { - Ok(()) => {} - Err(e) => { - eprintln!("Failed to initialize logger: {:?}", e); - exit(1); - } - } + tracing_subscriber::registry() + .with(tracing_subscriber::fmt::layer()) + .with(EnvFilter::from_default_env()) + .init(); let args = Cli::parse(); @@ -55,8 +54,6 @@ async fn main() -> Result<(), Box> { let reader = BufReader::new(config_file); let server_config: ServerConfig = serde_json::from_reader(reader).unwrap(); - info!("Parsed server config"); - let mut bgp_server = Server::new(server_config); bgp_server.start(true).await.unwrap(); diff --git a/bin/src/client/main.rs b/bin/src/client/main.rs index 20397df..19a57ea 100644 --- a/bin/src/client/main.rs +++ b/bin/src/client/main.rs @@ -5,6 +5,9 @@ use tracing::{info, warn}; use route_client::netlink::NetlinkConnector; use route_client::{run_connector_v4, run_connector_v6}; +use tracing_subscriber::layer::SubscriberExt; +use tracing_subscriber::util::SubscriberInitExt; +use tracing_subscriber::EnvFilter; #[derive(Parser)] #[clap( @@ -38,7 +41,10 @@ enum Commands { async fn main() -> Result<()> { let args = Cli::parse(); - tracing_subscriber::fmt().pretty().init(); + tracing_subscriber::registry() + .with(tracing_subscriber::fmt::layer()) + .with(EnvFilter::from_default_env()) + .init(); info!("Starting route client"); diff --git a/crates/bgp_packet/Cargo.toml b/crates/bgp_packet/Cargo.toml index 4238bbb..a6e50f2 100644 --- a/crates/bgp_packet/Cargo.toml +++ b/crates/bgp_packet/Cargo.toml @@ -12,10 +12,12 @@ version.workspace = true workspace = true [dependencies] -byteorder = "1.4.3" -bytes.workspace = true -eyre.workspace = true -nom = "7.1" -serde.workspace = true -serde_json.workspace = true -tokio-util = { version = "0.7.10", features = ["codec"] } +byteorder = "1.4.3" +bytes.workspace = true +eyre.workspace = true +netlink-packet-route.workspace = true +nom = "7.1" +rtnetlink.workspace = true +serde.workspace = true +serde_json.workspace = true +tokio-util = { version = "0.7.10", features = ["codec"] } diff --git a/crates/bgp_packet/src/constants.rs b/crates/bgp_packet/src/constants.rs index dc1379f..6d73d20 100644 --- a/crates/bgp_packet/src/constants.rs +++ b/crates/bgp_packet/src/constants.rs @@ -59,6 +59,25 @@ impl Into> for AddressFamilyIdentifier { } } +/// Convenience functions to convert AddressFamilyIdentifier into those used by netlink. +impl Into for AddressFamilyIdentifier { + fn into(self) -> netlink_packet_route::AddressFamily { + match self { + AddressFamilyIdentifier::Ipv4 => netlink_packet_route::AddressFamily::Inet, + AddressFamilyIdentifier::Ipv6 => netlink_packet_route::AddressFamily::Inet6, + } + } +} + +impl Into for AddressFamilyIdentifier { + fn into(self) -> rtnetlink::IpVersion { + match self { + AddressFamilyIdentifier::Ipv4 => rtnetlink::IpVersion::V4, + AddressFamilyIdentifier::Ipv6 => rtnetlink::IpVersion::V6, + } + } +} + /// This parser for AFI makes it easier to write the other message parsers. impl ReadablePacket for AddressFamilyIdentifier { fn from_wire<'a>( diff --git a/crates/route_client/src/fib_state.rs b/crates/route_client/src/fib_state.rs index 22dcb97..b6bf6bc 100644 --- a/crates/route_client/src/fib_state.rs +++ b/crates/route_client/src/fib_state.rs @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use futures::lock::Mutex; +use eyre::Result; use ip_network_table_deps_treebitmap::address::Address; use ip_network_table_deps_treebitmap::IpLookupTable; use std::convert::{TryFrom, TryInto}; @@ -20,6 +20,7 @@ use std::fmt::Formatter; use std::net::Ipv6Addr; use std::net::{IpAddr, Ipv4Addr}; use std::sync::Arc; +use tokio::sync::Mutex; use tracing::{trace, warn}; use bgp_packet::constants::AddressFamilyIdentifier; @@ -80,6 +81,10 @@ impl< where String: From<>::Error>, { + pub async fn get_routing_table(&mut self) -> Result<()> { + todo!(); + } + /// 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> { @@ -151,6 +156,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"); } }; Ok(()) diff --git a/crates/route_client/src/lib.rs b/crates/route_client/src/lib.rs index be3fe3e..afee01d 100644 --- a/crates/route_client/src/lib.rs +++ b/crates/route_client/src/lib.rs @@ -19,19 +19,18 @@ pub mod southbound_interface; use std::convert::TryInto; use std::net::IpAddr; use std::net::Ipv4Addr; - use std::net::Ipv6Addr; use std::str::FromStr; use std::time::Duration; -use bgp_packet::constants::AddressFamilyIdentifier; -use bgp_packet::nlri::NLRI; - -use eyre::{anyhow, Result}; +use eyre::Result; use ip_network_table_deps_treebitmap::IpLookupTable; use tonic::transport::Endpoint; use tonic::transport::Uri; -use tracing::{info, trace, warn}; +use tracing::{trace, warn}; + +use bgp_packet::constants::AddressFamilyIdentifier; +use bgp_packet::nlri::NLRI; use crate::fib_state::FibState; use crate::proto::route_service_client::RouteServiceClient; @@ -147,7 +146,6 @@ pub async fn run_connector_v6( let request = proto::StreamPathsRequest { address_family: proto::AddressFamily::IPv6.into(), }; - info!("Request: {:?}", request); let mut stream = client.stream_paths(request).await?.into_inner(); let mut msg_ctr: u64 = 0; diff --git a/crates/route_client/src/netlink.rs b/crates/route_client/src/netlink.rs index 0789c4a..b2b575c 100644 --- a/crates/route_client/src/netlink.rs +++ b/crates/route_client/src/netlink.rs @@ -25,6 +25,18 @@ pub struct NetlinkConnector { #[async_trait] impl SouthboundInterface for NetlinkConnector { + async fn get_all_routes( + &mut self, + address_family: AddressFamilyIdentifier, + ) -> Result> { + let route = self.handle.route(); + + let mut get_request = route.get(address_family.into()); + get_request.message_mut().header.table = 201; + + todo!(); + } + async fn route_add( &mut self, address_family: AddressFamilyIdentifier, diff --git a/crates/route_client/src/southbound_interface.rs b/crates/route_client/src/southbound_interface.rs index 4c642fe..93373ba 100644 --- a/crates/route_client/src/southbound_interface.rs +++ b/crates/route_client/src/southbound_interface.rs @@ -34,6 +34,11 @@ pub trait SouthboundInterface { ) -> Result<()>; /// route_del removes the route towards a particular prefix via a given nexthop. async fn route_del(&mut self, prefix: NLRI, nexthop: IpAddr) -> Result<()>; + /// get_all_routes returns all the routes for the given address family. + async fn get_all_routes( + &mut self, + address_family: AddressFamilyIdentifier, + ) -> Result>; } /// DummyVerifier is a SouthboundInterface that checks that routes are not added more than @@ -53,6 +58,13 @@ impl std::default::Default for DummyVerifier { #[async_trait] impl SouthboundInterface for DummyVerifier { + async fn get_all_routes( + &mut self, + address_family: AddressFamilyIdentifier, + ) -> Result> { + todo!(); + } + async fn route_add( &mut self, _: AddressFamilyIdentifier,