use crate::audit::AuditLog;
use crate::bulkhead::BulkheadManager;
use crate::error::{Error, Result};
use crate::jail::JailInstance;
use crate::manifest::BlackshipConfig;
use crate::network::{
Bridge as NetworkBridge, IpAllocator, NetworkLeaseStore, ResolvedNetwork, VnetSetup,
VnetStateStore, build_runtime_allocator,
};
use crate::scope::ScopeStore;
use crate::sys::OsVersion;
use crate::warden::WardenHandle;
use crate::zfs::ZfsManager;
use petgraph::graph::DiGraph;
use std::collections::HashMap;
use std::net::IpAddr;
use std::sync::Mutex;
use std::time::Instant;
use throttle_machines::TokenBucket;
pub struct Bridge {
pub(super) config: BlackshipConfig,
pub(super) graph: DiGraph<String, ()>,
pub(super) zfs: Option<ZfsManager>,
pub(super) bulkhead: BulkheadManager,
pub(super) runtime_networks: HashMap<String, ResolvedNetwork>,
pub(super) ip_allocator: IpAllocator,
pub(super) lease_store: NetworkLeaseStore,
pub(super) vnet_state_store: VnetStateStore,
pub(super) scope_store: ScopeStore,
pub(super) audit: AuditLog,
pub(super) allocated_ips: HashMap<String, (String, IpAddr)>,
pub(super) instances: HashMap<String, JailInstance>,
pub(super) verbose: bool,
pub(super) rate_limiter: Mutex<TokenBucket>,
pub(super) rate_limiter_epoch: Instant,
pub(super) warden_handle: Option<WardenHandle>,
pub(super) vnet_setups: HashMap<String, VnetSetup>,
pub(super) os_version: OsVersion,
pub(super) runtime_prepared: bool,
}
impl Bridge {
pub fn new(config: BlackshipConfig) -> Result<Self> {
let os_version = OsVersion::detect_kernel()?;
if let Some(ref bridge_config) = config.config.bridge
&& bridge_config.vlan_filtering
&& !os_version.supports_vlan_filtering()
{
return Err(Error::UnsupportedOsVersion {
feature: "VLAN filtering".to_string(),
minimum: "15.0".to_string(),
current: os_version.to_string(),
});
}
let mut graph = DiGraph::new();
let mut node_map = HashMap::new();
for jail in &config.jails {
let idx = graph.add_node(jail.name.clone());
node_map.insert(jail.name.clone(), idx);
}
for jail in &config.jails {
let to = node_map[&jail.name];
for dep in &jail.depends_on {
let from = node_map
.get(dep)
.ok_or_else(|| Error::UnknownDependency(dep.clone()))?;
graph.add_edge(*from, to, ());
}
}
let zfs = if config.config.zfs_enabled {
let pool = config.config.zpool.as_ref().ok_or(Error::ZfsNotEnabled)?;
Some(ZfsManager::new(
pool,
&config.config.dataset,
config.config.jails_dir(),
))
} else {
None
};
let bulkhead = BulkheadManager::from_data_dir(&config.config.data_dir)?;
let (runtime_networks, ip_allocator, lease_store) = build_runtime_allocator(Some(&config))?;
let vnet_state_store = VnetStateStore::from_config(Some(&config));
let scope_store = ScopeStore::from_config(Some(&config));
let audit = AuditLog::from_config(Some(&config));
let rate_limiter = TokenBucket::new(config.config.rate_limit.jail_start_policy()?);
let now = Instant::now();
Ok(Self {
config,
graph,
zfs,
bulkhead,
runtime_networks,
ip_allocator,
lease_store,
vnet_state_store,
scope_store,
audit,
allocated_ips: HashMap::new(),
instances: HashMap::new(),
verbose: false,
rate_limiter: Mutex::new(rate_limiter),
rate_limiter_epoch: now,
warden_handle: None,
vnet_setups: HashMap::new(),
os_version,
runtime_prepared: false,
})
}
pub const fn verbose(mut self, verbose: bool) -> Self {
self.verbose = verbose;
self
}
pub fn open(config: BlackshipConfig, verbose: bool) -> Result<Self> {
Ok(Self::new(config)?.verbose(verbose))
}
pub fn set_warden_handle(&mut self, handle: WardenHandle) {
self.warden_handle = Some(handle);
}
pub fn prepare_host(&mut self) -> Result<()> {
self.prepare_runtime()
}
pub(super) fn resolve_jail_names(&self, name: &str) -> Result<(String, String)> {
self.config.require_jail_names(name)
}
pub(super) fn prepare_runtime(&mut self) -> Result<()> {
if self.runtime_prepared {
return Ok(());
}
self.prepare_networking()?;
self.prepare_storage()?;
crate::gc::auto_reconcile(&self.config, self.verbose);
self.runtime_prepared = true;
Ok(())
}
fn prepare_networking(&self) -> Result<()> {
let Some(bridge_config) = self.config.config.bridge.as_ref() else {
return Ok(());
};
if !bridge_config.vlan_filtering {
return Ok(());
}
let bridge = NetworkBridge::create_or_open(&bridge_config.name)?;
bridge.enable_vlan_filtering()?;
if let Some(trunk) = bridge_config.trunk.as_ref() {
if trunk.disable_hwfilter {
NetworkBridge::disable_hwfilter(&trunk.interface)?;
}
if !trunk.tagged.is_empty() {
bridge.add_trunk_member(&trunk.interface, &trunk.tagged)?;
}
}
Ok(())
}
fn prepare_storage(&self) -> Result<()> {
if let Some(zfs) = &self.zfs {
zfs.init()?;
}
Ok(())
}
}