From fcb6ad6d0cc7acd36341f6503f99f31ea7a53abc Mon Sep 17 00:00:00 2001 From: dominic <510002+dmah42@users.noreply.github.com> Date: Sun, 18 Aug 2024 06:23:24 +0100 Subject: [PATCH] Introduce network config struct (#529) Only used by pid1 for now, but other things that need to initialize networking may want to use it too. --- auraed/src/init/network/mod.rs | 50 ++++++++++--------- .../system_runtimes/pid1_system_runtime.rs | 15 +++++- auraed/tests/common/mod.rs | 2 + 3 files changed, 43 insertions(+), 24 deletions(-) diff --git a/auraed/src/init/network/mod.rs b/auraed/src/init/network/mod.rs index 494f3d83f..4a5a2ef06 100644 --- a/auraed/src/init/network/mod.rs +++ b/auraed/src/init/network/mod.rs @@ -53,6 +53,13 @@ pub(crate) enum NetworkError { Other(#[from] rtnetlink::Error), } +pub(crate) struct Config { + pub device: String, + pub address: String, + pub gateway: String, + pub subnet: String, +} + pub(crate) struct Network(Handle); impl Network { @@ -62,9 +69,12 @@ impl Network { Ok(Self(handle)) } - pub(crate) async fn init(&self) -> Result<(), NetworkError> { + pub(crate) async fn init( + &self, + config: &Config, + ) -> Result<(), NetworkError> { configure_loopback(&self.0).await?; - configure_nic(&self.0).await?; + configure_nic(&self.0, config).await?; Ok(()) } @@ -125,38 +135,32 @@ async fn configure_loopback(handle: &Handle) -> Result<(), NetworkError> { Ok(()) } -// TODO: design network config struct -async fn configure_nic(handle: &Handle) -> Result<(), NetworkError> { - const DEFAULT_NET_DEV: &str = "eth0"; - const DEFAULT_NET_DEV_IPV6: &str = "fe80::2"; - const DEFAULT_NET_DEV_IPV6_GATEWAY: &str = "fe80::1"; - const DEFAULT_NET_DEV_IPV6_SUBNET: &str = "/64"; - - trace!("configure {DEFAULT_NET_DEV}"); - - let ipv6_addr = - format!("{DEFAULT_NET_DEV_IPV6}{DEFAULT_NET_DEV_IPV6_SUBNET}") - .parse::() - .expect("valid ipv6 address"); +async fn configure_nic( + handle: &Handle, + config: &Config, +) -> Result<(), NetworkError> { + trace!("configure {0}", config.device); - let gateway = DEFAULT_NET_DEV_IPV6_GATEWAY - .to_string() + let ipv6_addr = format!("{0}{1}", config.address, config.subnet) .parse::() - .expect("gateway"); + .expect("valid ipv6 address"); - add_address(handle, DEFAULT_NET_DEV.to_owned(), ipv6_addr).await?; + let gateway = + config.gateway.to_string().parse::().expect("gateway"); - set_link_up(handle, DEFAULT_NET_DEV.to_owned()).await?; + add_address(handle, config.device.clone(), ipv6_addr).await?; + + set_link_up(handle, config.device.clone()).await?; add_route_v6( handle, - DEFAULT_NET_DEV.to_owned(), + config.device.clone(), "::/0".parse::().expect("valid ipv6 address"), gateway, ) .await?; - info!("Successfully configured {DEFAULT_NET_DEV}"); + info!("Successfully configured {0}", config.device); Ok(()) } @@ -371,4 +375,4 @@ async fn dump_addresses( } else { Err(NetworkError::DeviceNotFound { iface: iface.to_string() }) } -} \ No newline at end of file +} diff --git a/auraed/src/init/system_runtimes/pid1_system_runtime.rs b/auraed/src/init/system_runtimes/pid1_system_runtime.rs index 12ae0b459..5221b055c 100644 --- a/auraed/src/init/system_runtimes/pid1_system_runtime.rs +++ b/auraed/src/init/system_runtimes/pid1_system_runtime.rs @@ -94,9 +94,22 @@ impl SystemRuntime for Pid1SystemRuntime { .mount()?; trace!("Configure network"); + + const DEFAULT_NET_DEV: &str = "eth0"; + const DEFAULT_NET_DEV_IPV6: &str = "fe80::2"; + const DEFAULT_NET_DEV_IPV6_GATEWAY: &str = "fe80::1"; + const DEFAULT_NET_DEV_IPV6_SUBNET: &str = "/64"; + // show_dir("/sys/class/net/", false); // Show available network interfaces let network = network::Network::connect()?; - network.init().await?; + network + .init(&network::Config { + device: DEFAULT_NET_DEV.to_owned(), + address: DEFAULT_NET_DEV_IPV6.to_owned(), + gateway: DEFAULT_NET_DEV_IPV6_GATEWAY.to_owned(), + subnet: DEFAULT_NET_DEV_IPV6_SUBNET.to_owned(), + }) + .await?; network.show_network_info().await; // TODO: do we need to create an interface and address for socket_address? diff --git a/auraed/tests/common/mod.rs b/auraed/tests/common/mod.rs index 2fc989043..90fed3550 100644 --- a/auraed/tests/common/mod.rs +++ b/auraed/tests/common/mod.rs @@ -118,6 +118,8 @@ pub async fn auraed_client() -> Client { CLIENT.get_or_init(inner).await.clone() } +// This is only used in ignored tests (for now). +#[allow(dead_code)] pub async fn remote_auraed_client( ip: String, ) -> Result {