use boxlite_shared::errors::BoxliteResult;
use std::path::PathBuf;
pub(crate) mod ca;
pub mod constants;
pub mod socket_path;
#[cfg(feature = "libslirp")]
mod libslirp;
#[cfg(feature = "gvproxy")]
pub mod gvproxy;
#[cfg(feature = "libslirp")]
pub use libslirp::LibslirpBackend;
#[cfg(feature = "gvproxy")]
pub use gvproxy::GvisorTapBackend;
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum NetworkBackendEndpoint {
UnixSocket {
path: PathBuf,
connection_type: ConnectionType,
mac_address: [u8; 6],
},
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct NetworkBackendConfig {
pub port_mappings: Vec<(u16, u16)>,
pub socket_path: PathBuf,
#[serde(default)]
pub allow_net: Vec<String>,
#[serde(default)]
pub secrets: Vec<crate::runtime::options::Secret>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub ca_cert_pem: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub ca_key_pem: Option<String>,
}
impl NetworkBackendConfig {
pub fn new(port_mappings: Vec<(u16, u16)>, socket_path: PathBuf) -> Self {
Self {
port_mappings,
socket_path,
allow_net: Vec::new(),
secrets: Vec::new(),
ca_cert_pem: None,
ca_key_pem: None,
}
}
}
#[derive(Debug, Clone, Default)]
pub struct NetworkMetrics {
pub bytes_sent: u64,
pub bytes_received: u64,
pub tcp_connections: Option<u64>,
pub tcp_connection_errors: Option<u64>,
}
pub trait NetworkBackend: Send + Sync + std::fmt::Debug {
fn endpoint(&self) -> BoxliteResult<NetworkBackendEndpoint>;
fn name(&self) -> &'static str;
fn metrics(&self) -> BoxliteResult<Option<NetworkMetrics>> {
Ok(None)
}
}
#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize)]
pub enum ConnectionType {
UnixStream,
UnixDgram,
}
pub struct NetworkBackendFactory;
impl NetworkBackendFactory {
pub fn create(config: NetworkBackendConfig) -> BoxliteResult<Option<Box<dyn NetworkBackend>>> {
#[cfg(feature = "gvproxy")]
{
tracing::info!("Using gvisor-tap-vsock backend");
let backend = GvisorTapBackend::new(config)?;
Ok(Some(Box::new(backend)))
}
#[cfg(all(feature = "libslirp", not(feature = "gvproxy")))]
{
tracing::info!("Using libslirp backend");
let backend = LibslirpBackend::new(config)?;
Ok(Some(Box::new(backend)))
}
#[cfg(all(not(feature = "libslirp"), not(feature = "gvproxy")))]
{
let _ = config; tracing::info!("No network backend - engine will use default net");
Ok(None)
}
}
}