Skip to content

Commit

Permalink
Introduce network config struct (#529)
Browse files Browse the repository at this point in the history
Only used by pid1 for now, but other things that need to initialize
networking may want to use it too.
  • Loading branch information
dmah42 authored Aug 18, 2024
1 parent 02e32f0 commit fcb6ad6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 24 deletions.
50 changes: 27 additions & 23 deletions auraed/src/init/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(())
}

Expand Down Expand Up @@ -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::<Ipv6Network>()
.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::<Ipv6Network>()
.expect("gateway");
.expect("valid ipv6 address");

add_address(handle, DEFAULT_NET_DEV.to_owned(), ipv6_addr).await?;
let gateway =
config.gateway.to_string().parse::<Ipv6Network>().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::<Ipv6Network>().expect("valid ipv6 address"),
gateway,
)
.await?;

info!("Successfully configured {DEFAULT_NET_DEV}");
info!("Successfully configured {0}", config.device);
Ok(())
}

Expand Down Expand Up @@ -371,4 +375,4 @@ async fn dump_addresses(
} else {
Err(NetworkError::DeviceNotFound { iface: iface.to_string() })
}
}
}
15 changes: 14 additions & 1 deletion auraed/src/init/system_runtimes/pid1_system_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
2 changes: 2 additions & 0 deletions auraed/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Client, client::ClientError> {
Expand Down

0 comments on commit fcb6ad6

Please sign in to comment.