hypha: multiple cleanups to fix shared module

This commit is contained in:
Alex Zenla
2024-02-06 10:28:39 +00:00
parent 5edbff02a7
commit 44d3799dd3
6 changed files with 28 additions and 33 deletions

View File

@ -1,7 +1,7 @@
use crate::image::ImageInfo; use crate::image::ImageInfo;
use crate::shared::LaunchInfo;
use anyhow::Result; use anyhow::Result;
use backhand::{FilesystemWriter, NodeHeader}; use backhand::{FilesystemWriter, NodeHeader};
use hypha::LaunchInfo;
use log::trace; use log::trace;
use std::fs; use std::fs;
use std::fs::File; use std::fs::File;

View File

@ -5,9 +5,9 @@ use crate::ctl::cfgblk::ConfigBlock;
use crate::image::cache::ImageCache; use crate::image::cache::ImageCache;
use crate::image::name::ImageName; use crate::image::name::ImageName;
use crate::image::{ImageCompiler, ImageInfo}; use crate::image::{ImageCompiler, ImageInfo};
use crate::shared::{LaunchInfo, LaunchNetwork};
use advmac::MacAddr6; use advmac::MacAddr6;
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use hypha::{LaunchInfo, LaunchNetwork};
use ipnetwork::Ipv4Network; use ipnetwork::Ipv4Network;
use loopdev::LoopControl; use loopdev::LoopControl;
use std::io::{Read, Write}; use std::io::{Read, Write};
@ -114,10 +114,9 @@ impl Controller {
let cmdline_options = [if debug { "debug" } else { "quiet" }, "elevator=noop"]; let cmdline_options = [if debug { "debug" } else { "quiet" }, "elevator=noop"];
let cmdline = cmdline_options.join(" "); let cmdline = cmdline_options.join(" ");
let mac = MacAddr6::random() let mut mac = MacAddr6::random();
.to_string() mac.set_local(true);
.replace('-', ":") let mac = mac.to_string().replace('-', ":");
.to_lowercase();
let config = DomainConfig { let config = DomainConfig {
backend_domid: 0, backend_domid: 0,
name: &name, name: &name,

View File

@ -1,4 +1,3 @@
pub mod autoloop; pub mod autoloop;
pub mod ctl; pub mod ctl;
pub mod image; pub mod image;
pub mod shared;

View File

@ -1,14 +0,0 @@
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
pub struct LaunchNetwork {
pub link: String,
pub ipv4: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct LaunchInfo {
pub network: Option<LaunchNetwork>,
pub env: Option<Vec<String>>,
pub run: Option<Vec<String>>,
}

View File

@ -64,7 +64,7 @@ impl XenCall {
pub fn hypercall(&self, op: c_ulong, arg: [c_ulong; 5]) -> Result<c_long> { pub fn hypercall(&self, op: c_ulong, arg: [c_ulong; 5]) -> Result<c_long> {
trace!( trace!(
"call fd={} hypercall op={:#x}, arg={:?}", "call fd={} hypercall op={:#x} arg={:?}",
self.handle.as_raw_fd(), self.handle.as_raw_fd(),
op, op,
arg arg

View File

@ -1,6 +1,7 @@
use std::os::fd::AsRawFd; use std::os::fd::AsRawFd;
use std::panic::UnwindSafe; use std::panic::UnwindSafe;
use std::str::FromStr; use std::str::FromStr;
use std::sync::{Arc, Mutex};
use std::time::Duration; use std::time::Duration;
use std::{panic, thread}; use std::{panic, thread};
@ -73,14 +74,10 @@ impl NetworkBackend {
} }
pub fn run(mut self) -> Result<()> { pub fn run(mut self) -> Result<()> {
let interface = self.interface.clone();
let result = panic::catch_unwind(move || self.run_maybe_panic()); let result = panic::catch_unwind(move || self.run_maybe_panic());
if result.is_err() { if result.is_err() {
return Err(anyhow!( return Err(anyhow!("network backend has terminated"));
"network backend for interface {} encountered an error and is now shutdown",
interface
));
} }
result.unwrap() result.unwrap()
@ -109,7 +106,7 @@ impl NetworkBackend {
impl NetworkService { impl NetworkService {
pub async fn watch(&mut self) -> Result<()> { pub async fn watch(&mut self) -> Result<()> {
let mut spawned: Vec<String> = Vec::new(); let spawned: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));
let (connection, handle, _) = rtnetlink::new_connection()?; let (connection, handle, _) = rtnetlink::new_connection()?;
tokio::spawn(connection); tokio::spawn(connection);
loop { loop {
@ -131,25 +128,33 @@ impl NetworkService {
continue; continue;
} }
if spawned.contains(&name) { if let Ok(spawns) = spawned.lock() {
continue; if spawns.contains(&name) {
continue;
}
} }
if let Err(error) = self.add_network_backend(&name).await { if let Err(error) = self.add_network_backend(&name, spawned.clone()).await {
warn!( warn!(
"failed to initialize network backend for interface {}: {}", "failed to initialize network backend for interface {}: {}",
name, error name, error
); );
} }
spawned.push(name); if let Ok(mut spawns) = spawned.lock() {
spawns.push(name.clone());
}
} }
sleep(Duration::from_secs(2)).await; sleep(Duration::from_secs(2)).await;
} }
} }
async fn add_network_backend(&mut self, interface: &str) -> Result<()> { async fn add_network_backend(
&mut self,
interface: &str,
spawned: Arc<Mutex<Vec<String>>>,
) -> Result<()> {
let interface = interface.to_string(); let interface = interface.to_string();
let mut network = NetworkBackend::new(&interface, &[&self.network])?; let mut network = NetworkBackend::new(&interface, &[&self.network])?;
info!("initializing network backend for interface {}", interface); info!("initializing network backend for interface {}", interface);
@ -163,6 +168,12 @@ impl NetworkService {
interface, error interface, error
); );
} }
if let Ok(mut spawns) = spawned.lock() {
if let Some(position) = spawns.iter().position(|x| *x == interface) {
spawns.remove(position);
}
}
}); });
Ok(()) Ok(())
} }