Add EnvLogger and start of trait fn for dumping routes
Some checks are pending
Rust / build (push) Waiting to run

This commit is contained in:
Rayhaan Jaufeerally
2024-08-17 14:16:46 +00:00
parent 4a7e346153
commit 9b44ab782d
8 changed files with 83 additions and 31 deletions

View File

@ -12,8 +12,6 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use bgp_server::bgp_server::Server;
use bgp_server::config::ServerConfig;
use clap::Parser; use clap::Parser;
use core::sync::atomic::AtomicBool; use core::sync::atomic::AtomicBool;
use libc::SIGUSR1; use libc::SIGUSR1;
@ -22,11 +20,17 @@ use signal_hook::consts::TERM_SIGNALS;
use signal_hook::flag; use signal_hook::flag;
use signal_hook::iterator::exfiltrator::WithOrigin; use signal_hook::iterator::exfiltrator::WithOrigin;
use signal_hook::iterator::SignalsInfo; 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::fs::File;
use std::io::BufReader; use std::io::BufReader;
use std::process::exit;
use std::sync::Arc; use std::sync::Arc;
use tracing::info;
use bgp_server::bgp_server::Server;
use bgp_server::config::ServerConfig;
#[derive(Parser)] #[derive(Parser)]
#[command(author = "Rayhaan Jaufeerally <rayhaan@rayhaan.ch>", version = "0.1")] #[command(author = "Rayhaan Jaufeerally <rayhaan@rayhaan.ch>", version = "0.1")]
@ -37,15 +41,10 @@ struct Cli {
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
let subscriber = tracing_subscriber::fmt(); tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer())
match subscriber.try_init() { .with(EnvFilter::from_default_env())
Ok(()) => {} .init();
Err(e) => {
eprintln!("Failed to initialize logger: {:?}", e);
exit(1);
}
}
let args = Cli::parse(); let args = Cli::parse();
@ -55,8 +54,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let reader = BufReader::new(config_file); let reader = BufReader::new(config_file);
let server_config: ServerConfig = serde_json::from_reader(reader).unwrap(); let server_config: ServerConfig = serde_json::from_reader(reader).unwrap();
info!("Parsed server config");
let mut bgp_server = Server::new(server_config); let mut bgp_server = Server::new(server_config);
bgp_server.start(true).await.unwrap(); bgp_server.start(true).await.unwrap();

View File

@ -5,6 +5,9 @@ use tracing::{info, warn};
use route_client::netlink::NetlinkConnector; use route_client::netlink::NetlinkConnector;
use route_client::{run_connector_v4, run_connector_v6}; 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)] #[derive(Parser)]
#[clap( #[clap(
@ -38,7 +41,10 @@ enum Commands {
async fn main() -> Result<()> { async fn main() -> Result<()> {
let args = Cli::parse(); 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"); info!("Starting route client");

View File

@ -15,7 +15,9 @@ workspace = true
byteorder = "1.4.3" byteorder = "1.4.3"
bytes.workspace = true bytes.workspace = true
eyre.workspace = true eyre.workspace = true
netlink-packet-route.workspace = true
nom = "7.1" nom = "7.1"
rtnetlink.workspace = true
serde.workspace = true serde.workspace = true
serde_json.workspace = true serde_json.workspace = true
tokio-util = { version = "0.7.10", features = ["codec"] } tokio-util = { version = "0.7.10", features = ["codec"] }

View File

@ -59,6 +59,25 @@ impl Into<Vec<u8>> for AddressFamilyIdentifier {
} }
} }
/// Convenience functions to convert AddressFamilyIdentifier into those used by netlink.
impl Into<netlink_packet_route::AddressFamily> 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<rtnetlink::IpVersion> 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. /// This parser for AFI makes it easier to write the other message parsers.
impl ReadablePacket for AddressFamilyIdentifier { impl ReadablePacket for AddressFamilyIdentifier {
fn from_wire<'a>( fn from_wire<'a>(

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // 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::address::Address;
use ip_network_table_deps_treebitmap::IpLookupTable; use ip_network_table_deps_treebitmap::IpLookupTable;
use std::convert::{TryFrom, TryInto}; use std::convert::{TryFrom, TryInto};
@ -20,6 +20,7 @@ use std::fmt::Formatter;
use std::net::Ipv6Addr; use std::net::Ipv6Addr;
use std::net::{IpAddr, Ipv4Addr}; use std::net::{IpAddr, Ipv4Addr};
use std::sync::Arc; use std::sync::Arc;
use tokio::sync::Mutex;
use tracing::{trace, warn}; use tracing::{trace, warn};
use bgp_packet::constants::AddressFamilyIdentifier; use bgp_packet::constants::AddressFamilyIdentifier;
@ -80,6 +81,10 @@ impl<
where where
String: From<<A as TryFrom<NLRI>>::Error>, String: From<<A as TryFrom<NLRI>>::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 /// route_add requests updating the nexthop to a particular path if it is not already
/// the best path. /// the best path.
pub async fn route_add(&mut self, nlri: &NLRI, nexthop: IpAddr) -> Result<(), String> { 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()?; let addr: A = nlri.clone().try_into()?;
self.fib self.fib
.insert(addr, nlri.prefixlen.into(), Arc::new(Mutex::new(entry))); .insert(addr, nlri.prefixlen.into(), Arc::new(Mutex::new(entry)));
trace!(nlri = %nlri, "Added to kernel");
} }
}; };
Ok(()) Ok(())

View File

@ -19,19 +19,18 @@ pub mod southbound_interface;
use std::convert::TryInto; use std::convert::TryInto;
use std::net::IpAddr; use std::net::IpAddr;
use std::net::Ipv4Addr; use std::net::Ipv4Addr;
use std::net::Ipv6Addr; use std::net::Ipv6Addr;
use std::str::FromStr; use std::str::FromStr;
use std::time::Duration; use std::time::Duration;
use bgp_packet::constants::AddressFamilyIdentifier; use eyre::Result;
use bgp_packet::nlri::NLRI;
use eyre::{anyhow, Result};
use ip_network_table_deps_treebitmap::IpLookupTable; use ip_network_table_deps_treebitmap::IpLookupTable;
use tonic::transport::Endpoint; use tonic::transport::Endpoint;
use tonic::transport::Uri; 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::fib_state::FibState;
use crate::proto::route_service_client::RouteServiceClient; use crate::proto::route_service_client::RouteServiceClient;
@ -147,7 +146,6 @@ pub async fn run_connector_v6<S: SouthboundInterface>(
let request = proto::StreamPathsRequest { let request = proto::StreamPathsRequest {
address_family: proto::AddressFamily::IPv6.into(), address_family: proto::AddressFamily::IPv6.into(),
}; };
info!("Request: {:?}", request);
let mut stream = client.stream_paths(request).await?.into_inner(); let mut stream = client.stream_paths(request).await?.into_inner();
let mut msg_ctr: u64 = 0; let mut msg_ctr: u64 = 0;

View File

@ -25,6 +25,18 @@ pub struct NetlinkConnector {
#[async_trait] #[async_trait]
impl SouthboundInterface for NetlinkConnector { impl SouthboundInterface for NetlinkConnector {
async fn get_all_routes(
&mut self,
address_family: AddressFamilyIdentifier,
) -> Result<Vec<(NLRI, IpAddr)>> {
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( async fn route_add(
&mut self, &mut self,
address_family: AddressFamilyIdentifier, address_family: AddressFamilyIdentifier,

View File

@ -34,6 +34,11 @@ pub trait SouthboundInterface {
) -> Result<()>; ) -> Result<()>;
/// route_del removes the route towards a particular prefix via a given nexthop. /// route_del removes the route towards a particular prefix via a given nexthop.
async fn route_del(&mut self, prefix: NLRI, nexthop: IpAddr) -> Result<()>; 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<Vec<(NLRI, IpAddr)>>;
} }
/// DummyVerifier is a SouthboundInterface that checks that routes are not added more than /// 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] #[async_trait]
impl SouthboundInterface for DummyVerifier { impl SouthboundInterface for DummyVerifier {
async fn get_all_routes(
&mut self,
address_family: AddressFamilyIdentifier,
) -> Result<Vec<(NLRI, IpAddr)>> {
todo!();
}
async fn route_add( async fn route_add(
&mut self, &mut self,
_: AddressFamilyIdentifier, _: AddressFamilyIdentifier,